SSD optimize your Linux

SSD drives became a common storage-device for most computer enthusiasts and while they have a lot of advantages in comparison with the “traditional” hard disk, it’s main disadvantage is that the write-operations are theoretically limited. This process is called wearing. In order to prevent your SSD from wearing out, to maximize it’s lifespan and to  improve it’s performance, you can performthe following steps:

1. Prevent writing a time stamp every time a file is accessed.
By default, Linux keeps the time when a file has been last accessed. This is also known as the access-time. In order to prevent Linux from doing this, we need to mount all file-systems that are physically on the SSD with the noatime option. To do so, edit your /etc/fstab file as follows:

/dev/mapper/vg_ssd-lv_root /         ext4 defaults 1 1
/dev/mapper/vg_ssd-lv_home /home     ext4 defaults 1 2

becomes:

/dev/mapper/vg_ssd-lv_root /         ext4 defaults,noatime 1 1
/dev/mapper/vg_ssd-lv_home /home     ext4 defaults,noatime 1 2

 

2. Regularly trim the SSD in order to keep the device performing optimal.

Trimming can be done by executing the fstrim-command. Either execute the command on boot, for systems that are rebooted from time to time or let the necessary commands be executed automatically on a regular basis by using cron, for systems that are always on.

To execute the trimming-commands on a regular basis by cron, do the following:

Create a script which contains the necessary trimming commands in the cron.daily directory so it will be executed on a daily basis:

# sudo vi /etc/cron.daily/trim-ssd.sh
#!/bin/bash
fstrim -v /
fstrim -v /home
exit 0

Set the permissions of the script correctly so it is allowed to be executed:

# sudo chmod +x /etc/cron.daily/trim-ssd.sh

To execute trim when starting your system (computers that reboot from time to time), do the following:

Edit the file /etc/rc.local and add the correct trim-command for every mount-point that uses the SSD:

fstrim -v /
fstrim -v /home

 

3. Prevent the system from swapping when it’s not really needed.
By default, a linux-system will try to swap in a rather conservative way. It will try to swap out unused memory-pages to disk in order to free more space for actively used processes and to prevent the system from the need to write a lot of memory-pages to swap when it runs out of memory. This behavior is not a bad thing but it causes regular writes to the swap-space. When using an SSD, excessive writes is what we want to avoid so it’s better to tune this behavior. For a recent desktop system, the chance that you run out of memory is rather small but in case it happens, swap should still be available.

Swapping behavior is determined by the kernel-parameter vm.swappiness. In order to check the current value on your system, do the following:

# cat /proc/sys/vm/swappiness
60

The default value is 60 (or 30 for some more recent distributions). We would like to limit the use of swap space but not disable it. To do so, we need to change the value of this parameter. For kernel versions lower than 3.5-rc1, a value of 0 means that swapping will be avoided but not disabled, for newer versions of the linux kernel, a value of 0 completely disables swapping! In order to let the kernel be more relaxing about cached files in memory, it could also be interesting to lower the cache pressure.

To change the values, edit the file /etc/sysctl.conf and add or edit the following lines:

vm.swappiness=1
vm.vfs_cache_pressure=50

 

4.  Change the I/O scheduler
The I/O-scheduler determines the behavior of disk access and caching in Linux. Several different I/O schedulers are normally available. Unfortunately, there is no optimal scheduler that can be used for every case since it depends a lot on how the system is used and how it is configured. In a lot of distribution, the default scheduler, cfq, is less optimal for SSD drives so it is interesting to change the scheduler to deadline, which is more SSD-friendly. Newer distributions tend to already use deadline.

In order to check the currently active scheduler on your system, do the following:

# cat /sys/block/sda/queue/scheduler
noop deadline [cfq]

The scheduler-name that has square brackets around it, is the active scheduler.
To change the active scheduler without reboot, do the following:

# echo "deadline" > /sys/block/sda/queue/scheduler

Changes become effective immediately but after a reboot, the scheduler will be back to it’s default value. In order to permanently change the I/O-scheduler, we need to adjust the kernel parameters in GRUB. How to do so depends on which version of GRUB is installed.

To check which version of GRUB is installed depends on your distribution since tools and package names can differ.

For Red Hat/CentOS/… you can check it as follows:

When GRUB is installed, you should get something similar to:

# rpm -qa|grep grub
grubby-7.0.15-5.el6.x86_64
grub-0.97-83.el6.x86_64

When GRUB2 is installed, it should look as follows:

# rpm -qa|grep grub
grub2-tools-2.02-0.2.10.el7.x86_64
grub2-2.02-0.2.10.el7.x86_64

For Debian/Ubuntu/… you should get similar results by using the following command:

dpkg --get-selections | grep grub

To change the scheduler when GRUB is installed edit /etc/grub.conf and add elevator=deadline to every line that starts with kernel.

title CentOS (2.6.32-431.17.1.el6.x86_64)
 root (hd0,0)
 kernel /vmlinuz-2.6.32-431.17.1.el6.x86_64 ro root=/dev/mapper/vg_sys-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_sys/lv_root KEYBOARDTYPE=pc KEYTABLE=be-latin1 rd_NO_DM rhgb quiet
 initrd /initramfs-2.6.32-431.17.1.el6.x86_64.img

becomes

title CentOS (2.6.32-431.17.1.el6.x86_64)
 root (hd0,0)
 kernel /vmlinuz-2.6.32-431.17.1.el6.x86_64 ro root=/dev/mapper/vg_sys-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_sys/lv_root KEYBOARDTYPE=pc KEYTABLE=be-latin1 rd_NO_DM rhgb quiet elevator=deadline
 initrd /initramfs-2.6.32-431.17.1.el6.x86_64.img

for GRUB 2, edit /etc/default/grub and add elevator=deadline to GRUB_CMDLINE_LINUX_DEFAULT.

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

becomes

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=deadline"

for GRUB2, after editing /etc/default/grub, GRUB needs to be updated by executing the following command:

for Debian/Ubuntu/…

sudo update-grub

for Red Hat/CentOS/…

grub2-mkconfig -o /boot/grub2/grub.cfg

 

5. Follow some other best-practices

  • Keep around 15-20% free space on every partition in order to prevent fragmentation. This doesn’t have an influence on lifetime for an SSD but improves performance since it prevents fetching different parts of a file on different places on the SSD. The performance impact is much less than with traditional hard disks but is still there.
  • Never defragment your SSD, not in Linux (which is never really needed), not in Windows. Defragmenting causes a lot of write actions which seriously shorten the lifetime of your SSD while fragmentation doesn’t have a significant high impact on performance
  • Do not use hibernate-features. Hibernate causes a lot of write actions to the SSD so it could shorten it’s lifetime.
  • If you have a second, traditional, hard disk, use it for small write actions such as keeping logfiles or small temporary files

 

With the above actions, you should have significantly increased the lifetime of your SSD and have an improved performance as a bonus. There is no need to be scared to use your SSD-drive but a little tuning could be welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *