Everything you need to know about SSD Support in Ubuntu 14.04

Solid State Drives (SSD) have started dominating the chromebook kind of new generation laptop and Premium laptops. It replaces conventional Hard Disk Drives (HDD) due to advantages of high throughput (fast read/write speed), light weight and small form factor. The cost of SSD is going down gradually so that it is affordable for Desktop system too.

In this background, recently I bought a SSD for my ageing home desktop machine replacing the faulty hard disk. It is Transcend SATA 128GB drive of model SSD340 (TS128GSSD340). I have installed Ubuntu 14.04 on this SSD. Here is the quick look on how far this kind of SSDs are supported in Ubuntu 14.04 kind of Linux distributions. The reason for “kind of” in above sentence is most of this discussion is applicable/useful on any modern Linux distributions and SSD.

Installation

Ensure that your BIOS settings for SSD SATA device is set as AHCI mode to get full benefit out of SSD. The installation is straight forward, did not find any issue in detecting and installing the OS on top of it. Good start! Now we have Ubuntu 14.04 installed on SSD.

Collecting information about SSD

Issue following command to get in depth information about your SSD. It is highly useful data comes handy while you configure and fine tune your SSD. Replace the “/dev/sda” with your drive in the following command.

Health of SSD

S.M.A.R.T is the standard to report health condition of a storage device. You need to install a package called “smartmontools” to get health report of your SSD. Issue the following commands to install and run this tool and know the how healthy your device is.

Trim Support

Trim is the way of housekeeping in SSD, which ensures that free blocks are readily available in advance for further write. All modern SSDs support this feature, you can verify with hdparm command we used to collect information of SSD.

“fstrim” is the command which does this job. This can be run periodically through a cron job. Here is a way to see this in action. Let’s say you have created a new partition and file system on SSD. In this case /home file system is on SSD. Now I am asking the SSD to trim the deleted file’s blocks.

$ sudo fstrim -v /home
/home: 0 bytes were trimmed

Since we have not deleted any file, it does show 0 bytes trimmed. Now let’s delete a file of size 1.9GB. Then issue fstrim command as shown below, which shows the no. of bytes trimmed around 1.9GB. Till you issue this command, SSD in no way to know that you don’t need this data again. Now this command explicitly instructed SSD to delete the data and keep it ready for further writing (In fact flash memory chips in SSD needs to erase the blocks before every write – which I simply call “keep it ready”).

$ sudo fstrim -v /home
/home: 2113138688 bytes were trimmed

Ubuntu has automated this for you, but with a catch. Yes, it is enabled by default for Intel and Samsung SSDs, which have been well tested and supported. In case your drive supports Trim, you can also try enabling this (Disclaimer: It is your responsibility to keep the data backed up properly, I am not responsible for any lost you claim on this). I have enabled it for my Transcend SSD by appending “–no-model-check” to the fstrim command in the file /etc/cron.weekly/fstrim. Here is how it is in my file.

exec fstrim-all --no-model-check

In case of new SSD, you may need not to run this cron job weekly as per Ubuntu default. You can move this to /etc/cron.monthly/fstrim till you accumulate a lot of files.

There is a way to do this trim operation on the fly. You can do this by adding “discard” flag while mounting the file system. It means that file system can issue this trim instruction to SSD when ever it deletes a file. But it may affect the performance of device. So it is not preferred for a new SSD at least.

File System mount

The default file system mount file shown in my case.

$ sudo vi /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#
# / was on /dev/sda5 during installation
UUID=3fca70df-d717-4664-8268-3c2048b00ac3 / ext4 errors=remount-ro 0 1
# /home was on /dev/sda6 during installation
UUID=f7d9be87-6a1c-4b7e-9179-7a3056db9716 /home ext4 defaults 0 2
# swap was on /dev/sda7 during installation
UUID=4c48ce5f-1204-4bf8-a4e1-6776a17fd8d8 none swap sw 0 0

“discard” flag is not set by default during installation. This flag will instruct file system to issue trim command when ever you delete a file, which may lead to slow down of system. Periodical trim should be fine for a desktop system.

Add “noatime” flag to all SSD file systems in /etc/fstab except swap. This flag helps preventing updating file system meta data of “file access time” (which in turn writing to SSD) when ever you access a file in SSD.

Here is the updated /etc/fstab.

$ sudo vi /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#
# / was on /dev/sda5 during installation
UUID=3fca70df-d717-4664-8268-3c2048b00ac3 / ext4 noatime,errors=remount-ro 0 1
# /home was on /dev/sda6 during installation
UUID=f7d9be87-6a1c-4b7e-9179-7a3056db9716 /home ext4 defaults,noatime 0 2
# swap was on /dev/sda7 during installation
UUID=4c48ce5f-1204-4bf8-a4e1-6776a17fd8d8 none swap sw 0 0

Browser configuration

Some people prefer to fine tune SSD to optimize the number of writes. Here is how to do it. Firefox caches the pages you have visited which causes a lot of IO to SSD. You can disable it by setting cache limit as 0MB as shown in below. This setting can be reached by Firefox Menu -> Preferences -> Advanced -> Cached Web Content

Swap configurations

Setting the swappiness is highly debated topic in Linux kernel world. In case you plan to disable the swap partition, in stead of disabling the swap partition (or omitting), we can set swappiness as 0 so that swap partition can be used for suspend to disk (Hibernate). By default Hibernation is disabled in Ubuntu. You need to enable it through other hacks.

Turning the Kernel parameters

The following commands shows the default kernel parameter which may need tuning.

$ sysctl vm.swappiness
vm.swappiness = 60

$ sysctl vm.vfs_cache_pressure
vm.vfs_cache_pressure = 100

The lower (from 100 to 50) value of vfs_cache_pressure configures kerenl to retain filesystem meta data (dentry and inode cache), which means even during memory pressure this cache won’t be released. In case you have enough RAM (in my case it is 8GB), it is fine to reduce it further. This will lead to less frequent SSD access.

Append the following three lines in /etc/sysctl.conf to fine tune the above parameters. You need to reboot the system to make it effective.

IO Scheduler algorithm

Before reading further, here is a quick overview of IO scheduler algorithms.

the Anticipatory scheduler introduces a controlled delay before dispatching the I/O to attempt to aggregate and/or re-order requests improving locality and reducing disk seek operations. This algorithm is intended to optimize systems with small or slow disk subsystems;
the Completely Fair Queuing (CFQ) scheduler maintains a scalable per-process I/O queue and attempts to distribute the available I/O bandwidth equally among all I/O requests;
the Deadline scheduler uses a deadline algorithm to minimize I/O latency for a given I/O request;
the Noop scheduler is a simple FIFO queue, and it assumes performance of the I/O has been or will be optimized at the block device.

By default “deadline” is selected which is good for SSD, leaving it as it is.

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

For a 1GB file, copying from HDD to SSD, it took 10.347 seconds as per below test with the above default setting.

Follow me

For Firmware Engineers

Digital Design FundamentalsThis is a series of post on Digital Design Fundamentals required for a Firmware Engineer. It includes many basic building blocks used in Hardware (FPGA/ASIC) design. This should help a Firmware Engineer to effectively understand Hardware Micro-architecture documents from Design team.

Featured Sections

DE0-nano FPGAThis section features posts on DE0-nano FPGA Kit based projects. This kit has Cyclone IV E series FPGA with 32MB SDRAM, 40 Pin GPIOs, Accelerometer, AD Converters, built-in LEDs and Switches, etc. necessary to explore the world of FPGA.

GNU/LinuxLinux was originally developed as a free operating system for Intel x86–based personal computers, but has since been ported to more computer hardware platforms. It runs from supercomputers to embedded devices like phone. This section features all hacks.

MSP430 MCUMSP430 is a power efficient Microcontroller from TI, suitable for wearable devices. This section includes posts on eZ430 Chronos and MSP430 Launchpad kit.

Raspberry PiRaspberry Pi is an revolutionary board hackable for any purpose you imagine. Its support for standard linux stack and GPIOs makes it easy to implement any embedded system protocols. It deserves its place right from learning hello world LED programming to Home theatre system building.