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!
man shred