# Introduction
**What does the term Solid State Drive (SSD) commonly refer to?**
The term SSD commonly refers to *flash-based block devices.*
> **What does flash-based technology offer compared to Hard Disk Drives (HDDs)?**
> Compared to HDDs, flash-based technology offers:
> * Faster access time.
> * Lower latency.
> * Silent operation.
> * Power savings.
>
> **What does flash-based technology have despite its benefits?**
> Despite its benefits, flash-based technology has *issues which require special system attention and care.*
# Dealing with empty blocks
**What do traditional filesystems do when deleting data?**
When deleting data, traditional filesystems *flag the data blocks as deleted but don't change the actual data.*
> **What type of flash memory cell can write operations be perform on?**
> Write operations can be performed on *empty flash memory cells.*
> > **What must be done before a write operation can be done on memory cells which are flagged as deleted but aren't empty, and what effect does it have on performance?**
> > Before a write operation can be done on memory cells which are flagged as deleted but aren't empty, *the data must be erased which causes the performance to be slower.*
>
> **What other effect is there by keeping data that's flagged as deleted?**
> The other effect there is by keeping data that's flagged as deleted is *less available empty cells.*
**What is possible with modern kernels in dealing with empty blocks?**
In dealing with empty blocks, it is possible to *hint the deleted, not-used data blocks to the SSD.*
> **What is the mechanism called which hints the deleted, not-used data blocks to the SSD?**
> The mechanism that hints the deleted, not-used data blocks to the SSD is called *discard.*
>
> **What are the names for the different implementations of the discard mechanism?**
> The names for the different implementations of the discard mechanism are:
> * TRIM for ATAPI.
> * UNMAP for SCSI.
> * Deallocate for NVMe.
>
> **What is required in order to use discard?**
> In order to use discard, *filesystem support* is required.
> > **What modern filesystems support discard?**
> > The modern filesystems that support discard include:
> > * ext4.
> > * XFS.
> > * btrfs.
> > * bcachefs.
> >
> > **What traditional filesystems have had discard support added to them?**
> > The traditional filesystems which have had discard support added to them include:
> > * FAT.
> > * NTFS.
**What are the two basic approaches to issue the discard command to the SSD?**
The two basic approaches to issue the discard command to the SSD are:
1. Using `mount -o discard` for continuous discard.
2. Making periodic calls with the `fstream` utility.
> **What should you keep in mind with the two approaches of issuing the discard command?**
> With the two approaches of issuing the discard command, you should keep in mind that *not all filesystems support both.*
# Slowing wear out
**What causes wear to a NAND flash cell?**
Wear to a NAND flash cell is caused by *each write operation.*
> **What doesn't cause wear to a NAND flash cell?**
> Wear to a NAND flash cell isn't caused by *read operations.*
**What's a basic method for increasing the lifespan of a Solid State Drive (SSD)?**
A basic method for increasing the lifespan of an SSD is *to uniformly distribute write operations across all blocks.*
> **What is the method of distributing write operations across all blocks in a Solid State Drive (SSD) called?**
> The method of distributing write operations across all blocks in an SSD is called *wear leveling.*
> > **How is wear leveling deployed?**
> > Wear leveling is deployed *with SSD firmware.*
# Discard (trim) support
**What should be verified before performing any form of discarding on a drive?**
Before performing any form of discarding on a drive, *support for discarding* should be verified.
**What command can you use to verify if a drive supports discard?**
The command you can use to verify if a drive supports discard is:
```sh
lsblk --discard
```
> **What do you check for in the output of the `lsblk --discard` command to verify that a drive supports discard?**
> To verify that a drive supports discard, check for *non-zero values in the `DISC-GRAN` and `DISC-MAX` columns* in the output of the `lsblk --discard`.
> [!example] Example of output from the `lsblk --discard` command
> ```sh
NAME DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
sda 0 512B 2G 0
├─sda1 0 512B 2G 0
├─sda2 0 512B 2G 0
└─sda3 0 512B 2G 0
sdb 0 0B 0B 0
└─sdb1 0 0B 0B 0
> ```
# Partitioning
**What varies across different Solid State Drives (SSDs)?**
*The sizes of internal data structures, like blocks and pages*, vary across different SSDs.
> **What should filesystem data structures not do for optimal performance?**
> For optimal performance, filesystem data structures shouldn't *cross the boundaries of underlying SSD internal data structures.*
>
> **How do you ensure that filesystem data structures don't cross the boundaries of underlying Solid State Drive (SSD) internal data structures?**
> To ensure that filesystem data structures don't cross the boundaries of underlying SSD internal data structures, *align the start of each partition, commonly to 1 MiB.*
**What two partitioning utilities support partition alignment?**
The two partitioning utilities that support partition alignment include:
1. `parted`.
2. `fdisk`.
> **How do you use partition alignment with `parted`?**
> To use partition alignment with `parted`, *use the `-a optimal` flag*.
>
> **How do you use partition alignment with `fdisk`?**
> To use partition alignment with `fdisk`, *simply use it as it's turned on by default.*
**How can you check the alignment for a given partition with `parted`?**
To check the alignment for a given partition with `parted`, *run `parted /dev/<drive>`.*
# blkdiscard
**What program can you use to discard all data blocks on an entire device?**
To discard all data blocks on an entire device, you can use `blkdiscard`.
# LVM
...
# dm-crypt/LUKS
**How do you allow discards to pass through full encrypted devices?**
To allow discards to pass through full encrypted devices, *they have to be opened by `cryptsetup` with the `--allow-discards` flag.*
> [!example] Opening a full encrypted drive at `/dev/thing` while allowing discards
> ```sh
> cryptsetup luksOpen --allow-discards /dev/thing luks
> ```
**What does enabling discards depend on if a root-device exists in the Linux Unified Key Setup (LUKS)?**
If a root-device exists in the LUKS, enabling discards depends on *the initramfs implementation.*
> **What kernel option do you need to pass in order to enable discards in the generated initramfs depending on the initramfs implementation?**
> The kernel option that you need to pass in order to enable discards in the generated initramfs depending on the initramfs implementation is:
> * genkernel - `root_trim=yes`.
> * dracut - `rd.luks.allow-discards`.
>
> **How do you check if discard is enabled on a Linux Unified Key Setup (LUKS) device?**
> To check if discard is enabled on a LUKS device, *run `dmsetup table /dev/mapper/crypt_dev -- showkeys` and check the output for the string `allow_discards`.*
# Formatting
**How can you configure the filesystem to improve performance with a drive?**
You can configure the filesystem to improve performance with a drive *by aligning its data structures with the size of the drive's internal structures.*
# Configuring for erase block size
**What can you do if you know the erase block size of a Solid State Drive (SSD)?**
If you know the erase block size of an SSD, you can *use it when creating the filesystem.*
**What does `mkfs.ext4` apply to a drive when it is used on an average-sized partition?**
On an average sized partition, `mkfs.ext4` will apply *4KiB blocks* to a drive.
> **What are the two options you use with `mkfs.ext4` to set the alignment to the erase block size?**
> To set the alignment to the erase block size, the two options that you use with `mkfs.ext4` are:
> 1. `-E stride=<stride in KiB>`.
> 2. `-E stripe-width=<stripe-width in KiB>`.
>
> **Since `mkfs.ext4` applies 4KiB blocks to a drive, what value do you set `stride` and `stripe-width` in order for the filesystem to be aligned with the erase block size?**
> In order for the filesystem to be aligned with the erase block size, the value you set `stride` and `stripe-width` to is *the erase block size of the drive divided by 4KiB.*
> [!example] Example of formatting a drive with ext4 that has a block erase size of 512KiB
> ```
> mkfs.ext4 -E stride=128,stripe-width=128 /dev/sda3
> ```
...
# Mounting
**What is the recommended way for using discard on the rootfs?**
The recommended way for using discard on the rootfs is *to periodically use the `fstrim` utility.*
> **What can potentially happen if you use the `discard` mount option with older, poorer-quality Solid State Drives (SSDs)?**
> If you use the `discard` mount option with older, poorer-quality SSDs, it can potentially cause *degradation because of discard being done continuously.*
**What command do you run to perform a discard operation on the rootfs?**
The command you run to perform a discard operation on the rootfs is:
```sh
fstrim -v /
```
> **What are the two ways you can use the `fstrim` command?**
> The two ways you can use the `fstrim` command are:
> 1. Manually.
> 2. As a periodic job to run once a week.
>
> **What are some filesystem drivers that don't support `fstrim`?**
> Some filesystem drivers that don't support `fstrim` include:
> * ntfs3.
> * bcachefs.
**When should it be safe to mount a Solid State Drive (SSD) with the `discard` mount option?**
It should be safe to mount an SSD with the `discard` mount option *if the mount point is going to have a low amount of disk writes occurring on it.*
> **When is it recommended to mount a Solid State Drive (SSD) with the `discard` mount option?**
> It is recommended to mount an SSD with the `discard` mount option *if maintaining performance is required.*
>
> **Where do you set the `discard` mount option for a drive?**
> You set the `discard` mount option for a drive *in `/etc/fstab`.*
> > **What command do you run to remount all drives once `/etc/fstab` has been modified?**
> > Once `/etc/fstab` has been modified, the command you run to remount all drives is:
> > ```sh
> > mount -a
> > ```
> [!example] Example of using the `discard` mount option on a drive.
> ```sh
> /dev/sda3 /mnt/archive ext4 defaults,relatime,discard 0 1
> ```
# cron
> [!example] Example of configuring `cron` to run `fstrim` once a week on all mounted drives
> ```cron
> # Mins Hours Days Months Day of the week Command
> 15 13 * * 1 /sbin/fstrim --all
> ```
> [!example] Example of configuring `cron` to run `fstrim` once a week on rootfs
> ```cron
> # Mins Hours Days Months Day of the week Command
> 15 13 * * 1 /sbin/fstrim -v /
> ```
# SSDcronTRIM
...
# SSDcronTRIM-LUKS
...
# Without cron, on system shutdown (with OpenRC)
...
# Reducing amount of writes
**What limited lifetime do flash-based Solid State Drives (SSDs) have?**
The limited lifetime that flash-based SSDs have is *the number of writes performed.*
# Portage TMPDIR on tmpfs
**What can theoretically reduce writes to a Solid State Drive (SSD) when building packages with Portage?**
When building packages with Portage, you can theoretically reduce writes to an SSD *by performing the operations in RAM by using a tmpfs or zram mount.*
# Temporal files on tmpfs
**What can you mount desired mount points as to reduce the amount of writes and improve performance?**
To reduce the amount of writes and improve performance, you can mount desired mount points as *tmpfs.*
> [!example] Example of mounting `/tmp` and `/var/tmp` as tmpfs in `/etc/fstab`
> ```fstab
> # temporal mountpoints on tmpfs
> tmpfs /tmp tmpfs size=16G,noatime 0 0
> tmpfs /var/tmp tmpfs size=1G,noatime 0 0
> ```
**What does systemd do automatically with `/tmp`?**
With `/tmp`, systemd automatically *mounts it as tmpfs by default.*
**What should you make sure of when mounting `/var/tmp` as tmpfs and why?**
When mounting `/var/tmp` as tmpfs, you should make sure that *`/var/tmp` is appropriately sized because Portage uses it and there will be build errors if the size is too small.*
**What is the alternative to tmpfs on modern, fast systems?**
On modern, fast systems, the alternative to tmpfs is *zram.*
# XDG cache on tmpfs
**What programs often make frequent writes to cache?**
Programs that often make frequent writes to cache include *many programs that run with the X Window System.*
**What specification does the location of the cache directory usually adhere to?**
The specification that the location of the cache directory usually adheres to is *the XDG Base Directory Specification.*
> **What is the name of the environment variable that determines the location of the cache directory under the XDG Base Directory Specification?**
> The name of the environment variable that determines the location of the cache directory under the XDG Base Directory Specification is *`XDG_CACHE_HOME`.*
>
> **What is the default cache location under the XDG Base Directory Specification?**
> The default cache location under the XDG Base Directory Specification is *`~/.cache`.*
**How do you remap the cache directory location so that it's mounted under a tmpfs?**
To remap the cache directory location so that it's mounted under a tmpfs, *create a script at `/etc/profile.d/xdg_cache_home.sh` which exports the directory under run like so:*
```bash
if [ ${LOGNAME} ]; then
export XDG_CACHE_HOME="/run/user/${UID}/cache"
fi
```
# Web browser profile(s) and cache on tmpfs
**What utility can you use to relocate web browser profiles and cache to a tmpfs?**
The utility you can use to relocate web browser profiles and cache to a tmpfs is *`www-misc/profile-sync-daemon`.*
...
# OpenRC
**How do you set up `profile-sync-daemon` for systems with OpenRC?**
To setup `profile-sync-daemon` for systems with OpenRC:
1. Create or modify `/etc/psd.conf` by adding or changing the variable `USERS` to be `="usernames..."`. This variable determines which users on the system are going to have their browser profiles symlinked to a tmpfs.
2. Close all web browsers.
3. Run `rc-update add psd default`.
4. Run `rc-service psd start`.
> **How do you view all symlinks created by `profile-sync-daemon`?**
> To view all symlinks created by `profile-sync-daemon`, run:
> ```sh
> psd p
> ```
...