I have an ext4 disk partition. I want to "dd" an image of it. To improve compression ratio of the image, I want to write 0 to all bits not recognized by the filesystem or GPT/MBR. Is this possible and how?
It is, and if you don't insist on a 100% perfect solution, it is also easy:
Step 1: Unused space on the disk image
fdisk -l
on the image to find out about space on the image, that is not assigned to a partitiondd
to copy from /dev/zero
to these partsStep 2: Unused space in side the file systems
fdisk -l
from before gives you enough information to find offset an size to losetup
a loopdevice to every partition carrying a file systemdd if=/dev/zero of=/your/mountpoint/zero.fill bs=1M oflag=sync
until it stops with "no space left on device". You can drop the oflag=sync
for file systems, that do no sparse files.rm -f /your/mountpoint/zero.fill
to remove this fileThis will leave you with an image, that has zeroed