Informational Website publish

Blog

News about our company, tutorials about IT and much more you will find in this page.

Optimize your Linux for an SSD

Janeiro 31st, 2018

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 perform the 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:

  • vi /etc/fstab
/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. For systems that are always on, let the necessary commands be executed automatically on a regular basis by using cron.

To execute the trimming-commands on a regular basis by cron, create a script which contains the necessary trimming commands in the cron.daily directory so it will be executed on a daily basis and add the correct trim-command for every mount-point that uses the SSD:

  • vi /etc/cron.daily/trim-ssd.sh
#!/bin/sh
LOG=/var/log/trim-ssd.log
echo "*** $(date -R) ***" >> $LOG
fstrim -v / >> $LOG
fstrim -v /home >> $LOG
exit 0

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

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

 

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.

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. 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:

  • vi /etc/sysctl.conf
vm.swappiness=1
vm.vfs_cache_pressure=50

And make the changes active:

  • sysctl -p

 

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.

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.

  • vi /etc/grub.conf
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.

  • vi /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

becomes:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=deadline"

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

For Red Hat/CentOS/…

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

For Debian/Ubuntu/…

  • sudo update-grub

Now that we are at the end of this tutorial and you made all those alterations to your system, it is recommended to reboot your server to make sure that all the settings are applied and active.

  • shutdown -r now