dd on entire disk, but do not want empty portion

Back in the day I ran into a similar problem with embedded Linux distributions – get rid of all the junk before compressing the image.

dd if=/dev/zero of=asdf.txt. Wait until it dies. Delete asdf.txt.

You’ve just written zeros to all free space on the device.

Now take a disk image and run it through gzip. Voila, sparse image.

Probably doesn’t scale very well and could cause problems if you actually need to write to the disk, but hey.

You could take an rsync snapshot of the disk to another volume, zero that, and then take that disk image.

Note: Could be hazardous for SSD, user should consider this operation befor committing.

Leave a Comment