I have a drive (SD card) with a few ext4 partitions but also some unallocated space. The fstrim utility can only work within a filesystem. Before I reinvent the wheel and write one, is there another utility that can TRIM the unallocated space (or that can TRIM an explicitly specified range)?I can verify that the majority of the unallocated space on the device is not currently known to be free by the controller, as I've observed that, on this particular card, reads to trimmed space return 0's, but a scan of the device shows plenty of garbage data left over.Edit: I am having an issue using hdparm. The example below discards the first sector, but I am seeing the same results regardless of the range I specify. fstrim has no issues on the device:root@ubuntu:~# hdparm --please-destroy-my-drive --trim-sector-ranges 0:1 --verbose /dev/mmcblk0 /dev/mmcblk0:trimming 1 sectors from 1 rangesoutgoing cdb:  85 0d 06 00 01 00 01 00 00 00 00 00 00 40 06 00outgoing_data:  00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 0000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00ioctl(fd,SG_IO): Invalid argumentFAILED: Invalid argumentI am investigating further but does anybody have any insight?
Utility to TRIM unallocated space on drive
partition;sd card;trim
If you have a recent enough version of util-linux, it contains the tool blkdiscard which is able to TRIM entire devices, or ranges within a device using --offset and --length options.Please note: blkdiscard is dangerous, if you let it TRIM the wrong regions, your data is gone!So you can figure out the unpartitioned (free) regions of your partition table and then TRIM them using this tool. For msdos and gpt partitions, parted provides the free regions like so:# parted -m /dev/sda unit b print free | grep ':free;'1:17408B:1048575B:1031168B:free;1:64022904832B:64023240191B:335360B:free;Add a loop to it...while IFS=: read -ra FREEdo    echo blkdiscard --offset ${FREE[1]%%B} --length ${FREE[3]%%B} /dev/sdadone < <(parted -m /dev/sda unit b print free | grep ':free;')which printsblkdiscard --offset 17408 --length 1031168 /dev/sdablkdiscard --offset 64022904832 --length 335360 /dev/sdaVerify that this output is correct for you, add additional options if you like (verbose?), and finally remove the echo so it will be actually executed, and you should be set.The second command of that example actually fails because the length is too small - it may be worth checking inside the loop, ignore regions smaller than 1MB as they're unlikely to be successfully trimmed.If you are using LVM instead of partitions, you can create a LV for the unoccupied space and trim that:lvcreate -l100%FREE -n blkdiscard SSD-VGblkdiscard -v /dev/SSD-VG/blkdiscardlvremove SSD-VG/blkdiscardIf you set issue_discards = 1 in your lvm.conf, you can skip the blkdiscard call as LVM will issue the TRIM on lvremove by itself.