Friday, October 19, 2012

Erase (Discard) all data on an SSD in Linux

A little background:
I had a 60 GB OCZ SSD that had Windows on it and some apps. I wanted to erase it to start fresh to test out Fedora 18 Alpha on it in a laptop. I didn't need to securely erase anything, I just wanted the SSD to know it can erase everything.


Here's what I did.

First I had a Fedora LiveUSB drive. That's not important but what is important is hdparm installed. I had to yum install hdparm from a terminal. Single user runlevel is a good idea for the rest of this...

First I needed to see how large the SSD is in sectors.
gdisk told me what what I wanted to know.

(The following is retyped, not copy and pasted and parts are omitted)

gdisk /dev/sda
Command (? for help): p
Disk /dev/sda: 117231408 sectors

So now know the size, 117231408 sectors which is important because hdparm will need input in sectors.

I found out that the --trim-sector-ranges option of hdparm will not do more than 65535 sectors at a time so I needed to use --trim-sector-ranges-stdin so I could specify all at once.

First I needed to generate a sector list.

for ((i = 0; $i < 117231408; i = $i + 65535)) do echo $i:65535 >> sectors.txt; done

Warning about that, I typed that from memory. The important part is you're looping from Sector 0 up untill the end before 11723140 (for my drive). Then it's stepping by 65535. I feel like I should have stepped by 65536 but I didn't so each line might have overlapped by 1 sector. In practice it wasn't an issue. Also, the very last entry in sectors.txt will be wrong. I didn't care enough to special case it or manually calculate the correct length.

So now sectors.txt looks like
0:65535
65535:65535
131070:65535
...

Then I did the actual (well nearly the actual) trim command.

hdparm --trim-sector-ranges-stdin /dev/sda < sectors.txt

Actually there's another --option thing but it will tell you and warn you what you're about to do is dangerous to your data. I'll omit it for the people copy and pasting to make the stop and think.

I ended up getting an error about a few of the sectors at the end (the error on the last entry basically) but nothing bad. Rebooting is now a good idea. And you're done!

If you follow these instructions be warned I might be doing it wrong and I only looked at about the first 1K of data on the drive to see it was blanked.

Good luck.

No comments: