Linux and Disk Defrag and Trim

DEFRAG

If you’re a Linux user you probably heard the claims that Linux Machines don’t need to Defraging because they use a Journalized Filesystem such as EXT2, EXT2, EXT4, JFS and ZFS to name a few. The File Allocation isn’t as perfect as they claim however it’s still 95% better then FAT16, FAT32 and NTFS.

With SSD systems you shouldn’t have to worry about Defraging since they randomly select to store files. With SSD you use more of a Trim function then Defrag. Now if you use a Mechanical Drive then Defraging is still needed. Continue to the TRIM section for SSD.

There are several tools for Linux that can Defrag a drive. The most common one that is installed is e4defrag.

Really and truly on a daily driver of a Linux System you should check out for Defrag is your User Home Directory. If there are multiple Users on a single system then I would do the whole /home directory itself.

Single User
sudo e4defrag /home/[username]

Multiple User
sudo e4defrag /home

Reason being is the User Home Directories has the most Read/Write cycles on the whole system. Especially for a Directory you Download and Remove huge files from.

TRIM

SSD Systems store Data in Chunks called Pages and stored into larger groups called Blocks. SSD Systems can only Read/Write to the Pages and can’t do much with the whole Block. In other words it can’t Delete Blocks and just skips over them to a new Block and leaves holes basically. so If you deleted enough Pages to a Block it will still skip that Block and in turn degrade the performance of the SSD. However there are programs that can quickly scan and delete the Blocks so new Data and be written there. On most Linux Systems there is a little scan tool called fstrim. You can setup a CRON script to scan weekly to wipe out the unused Blocks.

Run this to see if it is installed.
sudo fstrim -a -v

Now create a script
sudo nano /etc/cron.weekly/fstrim

Code for script
#!/bin/bash
/sbin/fstrim -a || true

Make it executable
sudo chmod +x /etc/cron.weekly/fstrim

So once a week it will run and Clean up the unused Blocks. When I scanned mine it removed 505.5MB from the /boot/efi and 48.2GB from / and has made a total difference.

Advertisement

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 )

Facebook photo

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

Connecting to %s