RT Cunningham

Blogging For As Long As I'm Able

The Raspberry Pi Trim Function Revisited

Tagged with computers, linux, raspberry pi, trim on May 7, 2024

Raspberry Pi 400 When I initially wrote about my Raspberry Pi 400 (“Pi” for short), I included the steps I took to enable the trim function on an SSD connected by USB. At the time, those steps were correct. I got that information from an article by Jeff Geerling, which he wrote in 2020. That information is no longer correct.

A Kernel Upgrade

I don’t know which kernel upgrade first included it, but the trim function is supported in the latest kernel for Raspberry Pi OS. Well, sort of.

When I initially tested things, the command “lsblk -D”, would show zeros under “DISC-MAX” and the “sudo fstrim -v /” command would tell me “fstrim: /: the discard operation is not supported”. Once I followed all the steps, it would show “4G” under “DISC-MAX” and the command would perform the trim function. If I ran it manually more than once, it would return the “not supported” message if there wasn’t anything left to trim.

I ran the fstrim command earlier today and then ran “lsblk -D” and noticed that each 4G had reverted to zeroes. A subsequent fstrim command returned the “not supported” message. After doing some research, I discovered it was an issue with the provisioning mode. The old kernels would start with “full”, and had to be changed to “unmap” at startup. The latest kernel starts with “unmap” and changes to “disabled” after one fstrim command.

The way it works now is fine for people who use a Pi as a desktop computer. It isn’t fine for someone running a Pi constantly, acting as a server. That’s the way I’m using mine, and I don’t want to reboot just to get the trim function working again. I created a solution.

My BASH Script

This is the script:

#!/bin/bash
if [ "cat /sys/block/sda/device/scsi_disk/*/provisioning_mode" != "unmap" ] ; then
    echo unmap | tee /sys/block/sda/device/scsi_disk/*/provisioning_mode
fi

The script reads the provisioning mode and if it isn’t “unmap”, it changes it to “unmap”. Short and sweet. It has to be run as root, so I have it set up as an hourly cronjob with “sudo crontab -e”. It doesn’t actually do anything until after the weekly fstrim function is invoked by the systemd timer and the provisioning mode is changed to “disabled”.

Image by Raspberry Pi

← Previous ArticleNext Article →