25

Is there an easy command that I can use to zero out the last 1MB of a hard drive?

For the start of the drive I would dd if=/dev/zero of=/dev/sdx bs=1M count=1. The seek option for dd looks promising, but does someone have an easy way to determine exactly how far I should seek?

I have a hardware RAID appliance, that stores some of the RAID configuration at the end of the drive. I need the RAID appliance to see the drives as un-configured, so I want to remove the RAID configuration without having to spend the time to do a full wipe of the drives. I have a dozen 2TB drives, and a full erase of all of those drives would take a long time.

22

The simplest way on Linux to get the size of the disk is with blockdev --getsz:

sudo -s
dd bs=512 if=/dev/zero of=/dev/sdx count=2048 seek=$((`blockdev --getsz /dev/sdx` - 2048))
  • Excellent, I knew there must be a tool that could get the size easily. – Zoredache May 30 '11 at 19:33
  • 1
    While I like the solution, using sda as example is horribly dangerous if you copy & paste and manage to get the carriage return copied. sdx would be much better suited. I take precautions, but only because of them I didn't destroy my disk right now. – emk2203 Sep 17 '17 at 13:02
  • @emk2203 Good point. I'm curious, what precautions do you have in place for such scenarios? – Hashim Feb 12 '18 at 18:35
  • 1
    I use a single quote before pasting. If the line contains a carriage return, it gets caught by the shell prompt because of the missing end for the quotation. If everything is OK, I go to the start of the line with Ctrl-A, then delete the quote, then return. Three more keystrokes for insurance, worth it. – emk2203 Feb 13 '18 at 1:39
  • Instead of /dev/sdx, look at the links in /dev/disk/by-id/ such as nvme-SAMSUNG_MZVLW1TXXXX-000XX_XXXXXXXXX. ls -l /dev/disk/by-id/ will show the link name and the /dev/sdX it links to. – rcpa0 Dec 13 '18 at 19:18
6

The size of every partition is available in /proc/partitions. The following command shows the size of sdx (in kB units):

awk '$4 == "sdx" {print $3}' </proc/partitions

Thus:

dd if=/dev/zero of=/dev/sdx bs=1k count=1024 \
   seek=$(($(awk '$4 == "sdx" {print $3}' </proc/partitions) - 1024))
  • What's the reason for the -1024 at the end? – Hashim Feb 12 '18 at 18:43
  • 1
    @Hashim The question asks how to wipe the last 1MB. The last 1MB starts 1024kB before the end. – Gilles Feb 12 '18 at 22:02
3

Using the seek to get to the end of the drive works very well, i.e.:

seek=$((blockdev --getsz /dev/sda - 2048))

However, when you use this, I recommend that you either know that your count value is correct, or not use it at all. The reason I say this is that drives can have either 512 byte sectors or 4k sectors, and if you use this solution with a drive that has 4k sectors on it, you won't go to the end of the drive with that count value, and may miss the RAID information at the end (I have run into this).

  • Could you share somehow what exactly you ran into? I am intrigued, wondering if I misunderstood how dd adresses sectors when used with custom bs values smaller/bigger then the actual blocksize reported by the device's firmware or the linux kernel. – sjas Apr 27 '17 at 6:09
2

You can get the number of sectors on the disk with hdparm -v /dev/sdx. Try seeking to number_of_sectors - 2048 (assuming 512 byte sectors).

  • hdparm seems handy, but how would I check the sector size? – Peter.O May 27 '11 at 14:16
  • 1
    /sys/block/sdx/queue/physical_block_size may give you the information but some newer disks play fast and loose with the sector size they report. It's probably better to read the label on the disk or look it up in the manufacturers data sheet. – user591 May 27 '11 at 14:41
  • 1
    Thanks.. I've just noticed that hdparm -I /dev/sda lists 2 "versions" of sector size: Logical and Physical. Is this what you mean by "fast-and-loose"? .... I jsut get that "feeling" that if anything is going to behave in such a way it could well be a RAID system.. – Peter.O May 27 '11 at 14:58
  • 1
    @red: I can't fid the reference now but I read somewhere that some drives with 4096 byte sectors will report to the system that they are 512byte and then deal wit the conversion internally. – user591 May 27 '11 at 15:53
1

You can use dd and get the device size via fdisk:

dd if=/dev/zero of=/dev/sda bs=1k \
   seek=$(( (`fdisk -l /dev/sda | grep 'Disk /' | cut -d' ' -f 5` -1024*1024)/1024  )) count=1024

I am not using a blocksize of 1m to avoid rounding errors.

Alternatively, you could write a small C-program (or script) which invokes the lseek function with SEEK_END and a negative offset of 1mb and then call write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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