Search code examples
filesystemscompressiondisk

write 0 to disk except existing files


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?


Solution

  • 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

    • run fdisk -l on the image to find out about space on the image, that is not assigned to a partition
    • use dd to copy from /dev/zero to these parts
    • On a typical image, this might be so little, as to make skipping this step quite reasonable

    Step 2: Unused space in side the file systems

    • The output of fdisk -l from before gives you enough information to find offset an size to losetup a loopdevice to every partition carrying a file system
    • For each file system in the image, mount this loop device to a temporary mountpoint
    • Now run dd 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.
    • Do rm -f /your/mountpoint/zero.fill to remove this file

    This will leave you with an image, that has zeroed

    • most of the space not used by a partition
    • most of the space allocated to a file system, but not used