SSDs are really fast (especially for reads) compared with spinning platter drives, but are relatively expensive and so generally have smaller capacity. This makes an SSD a good choice for the system partition of a Linux installation where all the frequently read system files are located – programs will boot up faster.
SSD drives also have a potentially shorter lifespan, especially if used in a write-delete-write fashion. At the time of writing, it’s estimated that system drives used this way will start to show degradation after about 1-2 years. To improve the performance and longevity of the SSD here are some of the best practices I’ve discovered for configuring Linux with an SSD.
- Check the SSD’s page size and erase block size (4K and 512K respectively in my case)
- Install the latest firmware available for the SSD
- Create the partition so as to align its start to the SSD erase block size (or multiple of it). This will ensure your SSD won’t have to do extra write operations.
fdisk -S<sectors> -H<heads> -cu /dev/sda
You do this by specifying a number of heads and sectors. Ultimately, you need to look this up because it depends on the erase block size of your drive. There’s some good advice here.
- Only format the partition with the ext4 filesystem – it has TRIM support
- Align the filesystem with the block size on the SSD when formatting the partition. If you aligned the partition (as above) then this further ensures minimum writes by choosing an ext4 block size that matches the page size on the drive.
mkfs.ext4 -b <block size in bytes> /dev/sda2
For me, I didn’t need to specify the block size because the default ext4 block size is 4K which already matches my drive’s 4K page size.
- Once Linux is install, edit fstab to enable TRIM on the SSD filesystem. This is done by adding the discard flag to the partition.
- A further tweak that can be done in fstab to minimize unnecessary writes on the SSD is to switch off updates to the file access times – remove atime and relatime flags on ssd partitions and replace with noatime and nodiratime
UUID=XXXX / ext4 defaults,noatime,nodiratime,discard 1 1
- If you have a second, platter drive installed, move the swap partition to that device.
- You can minimize swap disk accesses by disabling swap which will keep swap space in memory (provided you have enough memory and it is not full). In /etc/rc.local file add the line:
echo 0 > /proc/sys/vm/swappiness
- Use the deadline scheduler for writes. In /etc/rc.local file add the line:
echo deadline > /sys/block/sda/queue/scheduler
- Move small temporary file directories away from the SSD storage and into ramdisk, by adding the following to fstab. (Note that this would could mean data loss if server crashes, but it’s an acceptable tradeoff for me).
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 tmpfs /var/spool tmpfs defaults,noatime,mode=1777 0 0 tmpfs /var/tmp tmpfs defaults,noatime,mode=1777 0 0
- On a per application basis, repoint any temporary file write directories to platter partitions. The best example of such an application is a web browser, which maintains writes a large cache of temporary files. (In the end, to handle all these temporary application file scenarios, I decided to redirect the entire /var directory onto a second platter drive – this also covered Linux system /var/log files.)