Linux Workstation Optimizations for SSDs

Kasun Chathuranga
5 min readDec 25, 2019

My Asus laptop completed the 3 years without almost no troubles. My operating system is Fedora Linux and I usually do a fresh install when the new Fedora version is released which is every 6 months. With the fine-tuned partition table I was able to do every 6 months fresh installation with very low backing ups.

Why I switched to SSD —The Virtualization IO Problem

Before a couple of years, I used Windows XP virtual machines to run the software which not available for Linux on Oracle Virtual box. After Windows XP reaching the End of Life (EoL), I switched to Windows 7 guest VMs. Unlike XP, Win 7 was bulky. Therefore VMs were not fast as XP. The solution is Linux Native Kernel-based Virtual Machine (KVM). Unlike Virtualbox, in KVM it is possible to pass hardware to VMs as Virt* devices that are not emulated. For example, if you use VirtIO disk in your VM, host OS is directly passing hard disk blocks to the guest OS instead of emulated disk. If the guest OS is Linux, it recognizes VirtIO disks without any hassle. But for Windows guests, Emulated SATA disk to VirtIO disk switch can be later.

For Win 7 guest, VirtIO disks solve the performance issues. But the day came, I have to use Windows 10 and install Office 365 on it. Even though VirtIO disk did a good job for performance, There were freezes in my both host machine and VMs. My laptop performance monitors indicated that CPU and RAM are not the issues but disk IOs. My 5400 rpm 1GB hard disk is the bottleneck.

The solution was nothing other than switching to Solid State Disk Drives. Unfortunately my laptop does not have M2 ports which support 2GBps writing speeds. So answer was switching to SATA SSDs which supports 500MBps writing speeds which is 10 times faster than my HDD.

Gnome Disks Benchmark Results for my Kingston 480GB A400 SSD 2.5"

Problems in SSDs over HDDs & TRIM Command

n contrast to magnetic hard disk drives, SSD blocks have a relatively short lifespan of write cycles. To reduce writing on the same block, again and again, SSD firmware often rotates previously written block with a less used block in storage. These averages write counts to blocks. However, we need to tell SSD that we do not use this anymore which is called a TRIM command which levels wear. If the laptop operating system were Windows, it handles the all most all workarounds silently. But for Linux, you need to season the system with your recipe.

First of all, make sure your SSD supports the TRIM command by simply using the following command. If not supported, TRIM may cause data losses.

# hdparm -I /dev/sda | grep TRIM
* Data Set Management TRIM supported (limit 8 blocks)

Optimization 1: Swapping Control

Linux operating system uses swap space when the OS is running out of RAM. OS tries to write some sections from RAM to swap for getting free memory. Even this method is OK with magnetic HDDs, frequent writes reduce the lifespan of SSD blocks. However, turning off swap will disable to hibernation feature.

Linux has a kernel variable called vm.swappiness which is set at 60 by default. That means after reaching 60% of RAM, the system starts swapping the memory. As the first step, I set this value to 1. But I noticed, the swap is still going on due caches and buffers uses RAM. Then the option was set it to 0 which no swap at all unless you are totally ran out of the RAM. Since I know the memory consumption of my VMs, the swapping is not needed.

To set vm.swappiness to 0 permanently just run following command as root user.

# echo “vm.swappiness=0” >> /etc/sysctl.d/ssd-no-swap.conf

After the next reboot, kernel will use this value. For nonpersistence, setup use the following command which will apply now but reset after the next reboot.

# echo 0 > /proc/sys/vm/swappiness

Optimization 2: LVM Discard Requests

I use Logical Volume & Manager (LVMs) control my storage. I only give Logical Volumes (LVs) rather than direct partitions to operating system where I can easily resize the LV sizes later¹.

My disk layout

In lvm.conf we can mention to issue TRIM commands upon LV size reductions and LV deletion as follows.

Edit /etc/lvm/lvm.conf and set

issue_discards = 1

Optimization 3: Enable with Periodic TRIM fstrim.timer but not with every Single File Deletion

Ubuntu and Red Hat discourage user File System TRIM for every file deletion. They have introduced a service which is called ‘fstrim’ for this. The fstrim service weekly issues trim command which is recommended².

In Ubuntu 18.04 this service is enabled by default³, but not in Fedora 31.

Check the status of the service.

# systemctl status fstrim.timer
● fstrim.timer — Discard unused blocks once a week
Loaded: loaded (/usr/lib/systemd/system/fstrim.timer; disabled; vendor preset: disabled)
Active: inactive (dead)
Trigger: n/a
Docs: man:fstrim

To enable the service

# systemctl enable fstrim.timer
Created symlink /etc/systemd/system/timers.target.wants/fstrim.timer → /usr/lib/systemd/system/fstrim.timer.

Now check the service status. It should be enabled now.

# systemctl status fstrim.timer
● fstrim.timer — Discard unused blocks once a week
Loaded: loaded (/usr/lib/systemd/system/fstrim.timer; enabled; vendor preset: disabled)
Active: inactive (dead)
Trigger: n/a
Docs: man:fstrim

Use the journalctl to as follows to check service logs.

# journalctl -u fstrim.timer

Things I did not Do Purposefully

Even other recommends to do, I did not do the following optimizations for SSD longer life. These are acceptable compromises for my self.

  1. Use a RAM file system to store logs — The directory /var/log is very dynamic and consumes write cycles. But I decided, I need logs to be kept across boots and I did not use RAMFS for /var/log. Logs are required for both debugging and forensic purposes.
  2. Disable Firefox Cache — Firefox writes files to the cache folder which consumes several life cycles. But in Sri Lanka, internet connections are not so great where I can turn off Firefox caching.

TO-DO: Switch VM Hard Disk to LUN Pass through instead of VirtIO

Now the workstations fine. But for the Windows 10 guest, much more to go. For now, I turned off disk defragmenter and tuned on discard command in VM configurations.

<disk type="file" device="disk">
<driver name="qemu" type="qcow2" discard="unmap"/>

However, there is no still way to pass backing up storage as SSD to guest OS in KVM other than LUN/Thin Provision in KVM. But the feature is already there in VirtualBox⁴.

More Information and References

  1. Solid state drive — ArchWiki — https://wiki.archlinux.org/index.php/Solid_state_drive#LVM
  2. Red Hat Linux: Storage Administration Guide — Discard unused blocks — https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Storage_Administration_Guide/ch02s04.html
  3. Is TRIM enabled on my Ubuntu 18.04 installation? — https://askubuntu.com/questions/1034169/is-trim-enabled-on-my-ubuntu-18-04-installation
  4. marking a virtual disk as SSD: what it do? — https://forums.virtualbox.org/viewtopic.php?f=1&t=92851
  5. How to add virtual storage as SSD in KVM — https://serverfault.com/questions/876467/how-to-add-virtual-storage-as-ssd-in-kvm

--

--

Kasun Chathuranga

⚔️ InfoSec Engineer 🛡️ | 🔥 AR Game Player 🔥 | 🚀🛰️ Rocketry & Space Enthusiast 🔭🌌