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:

becomes:

 

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:

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

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:

 

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:

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:

 

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:

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

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:

When GRUB2 is installed, it should look as follows:

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

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

becomes

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

becomes

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

for Debian/Ubuntu/…

for Red Hat/CentOS/…

 

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 *