I have a USB flash drive usb 3, the reading speed is much more than the writing speed.
Let's say that 99% of the flash memory is already full with zeros, and I would like to fill it with zeros until 100%, by overwriting all the memory flash with zeros dd if=/dev/zero of=/dev/FLASH
.
This process is going to be long, and it will minimize the life expectancy of the flash drive.
I thought, maybe it would be much quicker to check which areas are non-zero, and overwrite only those non-zertos areas with zeros?
Are there anyways of doing this? If it is interesting, I would need all this for security reasons.
Best Answer
Security reasons aside, let's do it. We can (ab)use GNU
ddrescue
.To detect sectors of zeros
--generate-mode
is useful.(source)
Let's pretend your device is
outfile
from previousddrescue
run. We cannot use it asinfile
(becauseddrescue
refuses to work wheninfile
andoutfile
are the same file), we need a dummy one,/dev/zero
will do. We should know the physical sector size of your device and use it with-b
option. This command may help:Here I assume it's
512
.Now
flash.map
describes every sector either as non-tried (?
) or as finished (+
), depending on whether it was full of zeros or not. The next step is to fill non-zero sectors with zeros;--fill-mode
is perfect for this job:(source)
We must use the same
-b
value as with--generate-mode
, additionally--force
to overwrite the output device. This is the command:This time
/dev/zero
is not just a dummy argument, it's the actual source of data (zeros) written to the device.After
ddrescue
finishes, invokesync
. Now/dev/FLASH
is filled with zeros.