5

Some days ago I decided to wipe my 750 GB hard disk in order to sell it, so I booted Ubuntu off a live DVD and started the process with sudo dd if=/dev/zero of=/dev/sda. I left dd running during the night. When I came back in the morning I discovered that at some point of the process Ubuntu freezed. I'm unable to determine at which point of the process the freeze happened, because whenever it happened it happened while the screen saver was on, so i wasn't able see bash's output.

Question: How do I avoid starting dd over again?

2
  • You can shred it. run man shred Feb 27, 2015 at 16:50
  • @Mudit I need to start from the the point where the freeze occured, not to wipe the whole drive again
    – kos
    Feb 27, 2015 at 17:44

1 Answer 1

4

I worked out a solution myself:

Quick answer

Assuming that your drive is /dev/sdX:

  • Run dd if=/dev/zero | cmp - /dev/sdX to spot the first non-zero byte of the device: in my case it was byte 742300476649
  • Calculate to which block does the first non-zero byte belong to: <device_first_non_zero_block>=floor(<device_first_non_zero_byte>/<device_block_size>)+1: you can check your <device_block_size> running fdisk -l /dev/sdX in a terminal: in my case it was block 1449805619
  • Start dd again from there: dd if=/dev/zero bs=<device_block_size> skip=<device_first_non_zero_block>-1: in my case the command was dd if=/dev/zero of=/dev/sda bs=512 skip=1449805618

Long answer

Giving cmp - as FILE1 will force it to read FILE1 from stdin, so a constant stream of zeros will make cmp compare each byte of FILE2 against zero until EOF, reporting (if any) the first non-zero byte: assuming that the drive is /dev/sdX:

dd if=/dev/zero | cmp - /dev/sdX

The first non-zero block of the device is the block containing the first non-zero byte, i.e.:

<device_first_non_zero_block>=floor(<device_first_non_zero_byte>/<device_block_size>)+1

So, to start dd again from there, just skip the first <device_first_non_zero_block>-1 blocks:

dd if=/dev/zero of=/dev/sdX bs=<device_block_size> skip=<device_first_non_zero_block>-1

Testing

Creating a 512KB file containing only zeros to simulate a wiped hard drive:

$ dd if=/dev/zero of=hdd1 bs=512 count=1000

Creating a 512KB file containing only random bytes to simulate a hard drive containing data:

$ dd if=/dev/urandom of=hdd2 bs=512 count=1000

Merging the two files to simulate a partially wiped hard drive:

$ cat hdd1 hdd2 > hdd3

Output of the command on the test drives:

$ dd if=/dev/zero | cmp - hdd1
cmp: EOF on hdd1
# cmp reached EOF on hdd1, hdd1 contains only zeros
$ dd if=/dev/zero | cmp - hdd2
- hdd2 differ: byte 1, line 1
# cmp reported byte 1 to be not zero, hdd2 doesn't contain any leading zero
$ dd if=/dev/zero | cmp - hdd3
- hdd3 differ: byte 512001, line 1
# cmp reported byte 512001 to be not zero, hdd3 contains leading zeros up to byte 512000

In this case:

<device_first_non_zero_block>=floor(512001/512)+1=floor(1000,001953125)+1=1000+1=1001

So, to start dd again from there:

notice that in this case, being the test drive a file, count is needed and has been set to <device_total_number_of_blocks>-(<device_first_non_zero_block>-1) in order to not exceed the test drive size, but this doesn't apply to regular drives

dd if=/dev/zero of=hdd3 bs=512 seek=1000 count=1000

Output of the command on hdd3:

$ dd if=/dev/zero of=test2 bs=512 seek=1000 count=1000
1000+0 records in
1000+0 records out
512000 bytes (512 kB) copied, 0,00190399 s, 269 MB/s

Checking if the procedure succeded:

$ dd if=/dev/zero | cmp - hdd3
cmp: EOF on hdd3

Bingo!

1
  • 1
    Good one! Upvoted! ;-)
    – Fabby
    Mar 4, 2015 at 9:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .