Trim your SSD down to size

Solid state drives (SSD) work a little differently than regular hard disks. Obviously, they are a lot faster when reading or writing data, but file deletion is another story.

On hard drives, deleting a file marks the disk sectors that were used showing the file to have been deleted. A subsequent write operation is then allowed to reuse these sectors. However, this is not the case for an SSD where the disk sectors have to first be erased before new data can be written to them. The TRIM utility is available to perform the erase function – but this feature is not necessarily enabled by default.

A lot of information on the use of trim is available on the web. It is worth noting that some postings, that suggest modifying the file system table (fstab) and adding the “discard” option are no longer in favour. Instead, using the fstrim command is preferred.

Perhaps the first thing to do is to make sure that trim is supported on your SSD. This can be achieved (assuming that the SSD is /dev/sda) by running the following command to obtain information (the -I option) and to identify “TRIM supported” in the output:

sudo hdparm -I /dev/sda | grep “TRIM supported”

On my system, this produces the following:

* Data Set Management TRIM supported (limit 1 block)

The fstrim command can be initiated using the command:

sudo fstrim -v /

The command is being run on the root directory (/) with the verbose (-v) option. The resulting output is:

/: 9912041472 bytes were trimmed

The fstrim command can be run manually as shown above or set up as a scheduled task using cron or anacron (daily task).

An interesting set of commands checks that fstrim is actually doing its job by erasing sectors used by a file that has been deleted. The commands are as follows:

sudo dd if=/dev/urandom of=tempfile bs=1M count=3

This creates an output file named tempfile, using a random number generator to fill the file’s disk sectors with data. We now find the starting block of the temporary file using the command:

sudo hdparm –fibmap tempfile

The fibmap option produces a a list of the block extents (sector ranges) occupied by tempfile. For example, on my system, the command’s output is:

tempfile:
 filesystem blocksize 4096, begins at LBA 80193536; 
   assuming 512 byte sectors.
 byte_offset  begin_LBA  end_LBA sectors
 0            99416992   99423135 6144

The number of interest is the value of begin_LBA (i.e. 99416992 as shown above, or the first entry if there is a list of disk sectors). This is now used to view the contents of tempfile using the command:

sudo hdparm –read-sector 99416992 /dev/sda

The read-sector option is used to read tempfile, starting at sector number 99416992, and displaying the file’s contents in hexadecimal. The result is a series of hex numbers similar to the following:

{snip}
6d37 c796 99b2 8690 e877 950f 30e8 2309
28b3 135c f802 319b 37a5 53c0 0991 eeb1
23e1 3237 20c9 b237 efd4 cab7 6396 75cd

This shows that tempfile contains actual (random number) data.

Now, we delete (remove) tempfile using the command:

sudo rm tempfile

An ls command shows that tempfile no longer exists; however re-running sudo hdparm –read-sector 99416992 /dev/sda produces the same set of random numbers as before. Thus, even though the file has been deleted, the disk sectors still contain the associated data.

We now run the fstrim command and use sync to flush the disk buffers:

sudo fstrim -v /
sync

Now, when we rerun sudo hdparm –read-sector 99416992 /dev/sda, the resulting output is:

{snip}
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000

The relevant disk sectors are now empty so we can be assured that fstrim has done its job by erasing them and clearing the SSD’s decks for future action.

References:

Enable TRIM On SSD (Solid-State Drives) In Ubuntu For Better Performance
http://www.webupd8.org/2013/01/enable-trim-on-ssd-solid-state-drives.html

Ubuntu Doesn’t TRIM SSDs By Default: Why Not and How To Enable It Yourself
http://www.howtogeek.com/176978/ubuntu-doesnt-trim-ssds-by-default-why-not-and-how-to-enable-it-yourself/

How to check TRIM is working on your SSD running Linux
http://andyduffell.com/techblog/?p=852

Advertisements
This entry was posted in Commands and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s