Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
I'm a little confused.. Do you want to destroy the existing garbage data? Can you not just format the partition? You could use dd to write that partition with zeros or random data (from /dev/zero or /dev/random). I'm just confused as I'm not sure if "TRIM/DISCARD" means something really particular that isn't covered by my suggestions..? –  Doc Jul 8 '11 at 19:31
3  
I think the point is that he doesn't want to write any data, he wants the SSD to ERASE the flash block but leave it empty so it's available to be written to, known as freshing an SSD drive. –  Chris Thompson Jul 8 '11 at 20:44
    
ATA Secure Erase wiki ata.wiki.kernel.org/index.php/ATA_Secure_Erase –  joe Jul 7 '12 at 22:46
    
@joe: Your ATA Secure Erase link doesn't even mention parititions. I don't want to erase the entire drive, just a partition. And I don't want to risk accidentally erasing the whole drive. –  pts Jul 10 '12 at 15:03
4  
@Doc: Filling the partition with garbage data is quite the opposite of pts' intention: It will tell the SSD that the partition is completely filled an in use. This will decrease performance and life-time of the SSD. –  Black Jul 28 '12 at 6:51
add comment

6 Answers

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.

share|improve this answer
1  
wiper.sh is obsoleted by fstrim which is much safer, but for this specific usage blkdiscard beats both. In 2013, there is no need to risk computing ranges manually. –  Gabriel Sep 19 '13 at 18:54
add comment

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.

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.

share|improve this answer
    
Thanks, didn't know about blkdiscard. Dangerous command... :) –  frostschutz Sep 28 '13 at 20:43
add comment

You could have a look at the script wiper.sh included with the hdparm package. It uses the hdparm command --trim-sector-ranges to TRIM all empty sectors on a partition, at least if it contains an ext3/4 filesystem. Not sure if it works with unformatted partitions, but you could always temporarily format it with ext4.

share|improve this answer
    
Formatting it with ext4 and then running wiper.sh would trim almost all of the partition except for the ext4 metadata. But I'd like to trim the whole partition. –  pts Jul 13 '11 at 22:24
2  
@pts: I suggest you look at the code of wiper.sh and try to figure out how it deduces the trim ranges. Then you could manually send a trim range to hdparm to TRIM the whole partition. –  eldering Jul 14 '11 at 8:40
add comment

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.)

share|improve this answer
    
Thank you for the tip! FYI After TRIMming, writes some blocks with metadata for an empty ext4 filesystem, so the a few percent of the partition remains unTRIMmed. –  pts Jan 11 '13 at 19:47
add comment

Here's an interesting article that talks about using HDDErase to invoke the SSD's built-in secure erase feature, which should get you want you want:

http://www.windowsitpro.com/article/file-systems/q-how-can-i-reset-a-solid-state-disk-ssd-to-a-fully-erased-clean-state-

share|improve this answer
    
Thank you for your answer, but it doesn't work, since 1) it erases the whole drive 2) it requires Microsoft Windows, and I was looking for a Linux solution. –  pts Jul 13 '11 at 22:23
add comment

blkdiscard can work on partitions, not just on whole drives. I tested it successfully on Fedora 20. I checked the sources of blkdiscard - it's a very simple program that knows nothing about partitions. I assume the kernel does the translation.

Be very careful, blkdiscard asks no questions and shows no progress information.

If blkdiscard is not available, use this script and feed its output to hdparm. First argument is the number of sectors to trim, the second is the first sector to trim.

#! /bin/sh
# List ranges for hdparm --trim-sector-ranges-stdin
: ${MAXSECT=65535}
test $# = 2 || { echo "Usage: trimlist sectors offset" >&2; exit 1; }
sectors=$1
pos=$2
while test $sectors -gt 0; do
    if test $sectors -gt $MAXSECT; then
            size=$MAXSECT
    else
            size=$sectors
    fi
    echo $pos:$size
    sectors=$(($sectors-$size))
    pos=$(($pos+$size))
done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.