How to TRIM/DISCARD a whole SSD partition on Linux?

My partition /dev/sda3 on an SSD drive doesn't contain any filesystem, but it contains garbage. How do I do a TRIM/DISCARD operation on the whole partition?


Solution 1:

If your version of util-linux is new enough (September 2012), there is actually a purpose-built tool, blkdiscard, that is the best way to do this:

sudo blkdiscard /dev/sda3

But if you need compatibility to older Linux distro versions, read on... There are cases where hdparm/wiper.sh refuse to touch a volume because it's not a partition, so we need something beyond that.

The most supported way I've found is to take advantage of the fact that Linux swap volumes support DISCARD when they are enabled. The wipefs on the end is there so the volume isn't recognized as swap later.

D=/dev/sda3 ; mkswap $D && swapon -d $D && swapoff $D && wipefs -o 0xff6 $D

This issues the DISCARD on the majority of the device.

Solution 2:

I know this question is pretty old, but...

The simplest way to do this is to simply create an ext4 filesystem on the partition with a reasonably recent version of mkfs.ext4. The first thing this tool does is TRIM the entire partition. Once you've done that, you can overwrite the data it's created with whatever filesystem you desire.

(I've just done this to create a new vfat partition on an SD card, for example.)

Solution 3:

hdparm --trim-sector-ranges is low level, it is supposed to talk directly to the SSD, so no dependency on the filesystem. What wiper.sh does is use filesystem specific programs to map free (filesystem) regions to (hardware) SSD sectors, them use hdparm to trim those.

Answering the question, you can use hdparm to trim that partition, but you should be very careful. The way to do it is obtaining the sector range used by the partition, then use hdparm on that sector range. Probably you'll need to pass multiple sector ranges, as each range for --trim-sector-ranges can have at maximum 65535 sectors.

fdisk -l can tell you the beginning and size of the partitions. Pay attention to the units in use by fdisk, and to the sector size of your SSD.

Example (my SSD):

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    39070079    19534016   83  Linux

To discard the space before this partition, I could trim 2047 (512 byte sized, on my SSD) sectors starting from sector 1: hdparm --trim-sector-ranges 1:2047. I can't trim starting from sector 0 because that is the MBR (dunno how it goes for GPT).

To discard the space used by the partition, I'd do hdparm --trim-sector-ranges 2048:65535 67583:65535 133118:65535 .... It can be scripted but I'm not going to write one now.

Also just to be sure: I'm not guaranteeing anything here, I may have miscalculated sectors, do a backup of everything before effectively using --trim-sector-ranges.

Note: if you trim the "empty" space before the first partition, like I showed, be sure to reinstall GRUB (or whatever boot loader you use) before rebooting, as GRUB stores part of it on "unused" sectors before the first partition. Don't know other bootloaders but I guess it may be the case, so take care.