2

Two cases:

1.If making hdd image using dd and copy it trough nfs to another server:

mount -o ro,remount /
dd if=/dev/sda bs=64K | gzip -c > /share/test.img.gz

then after gunzip I get 20Gb (for ex.) file, filled w/zeroes at the end:

du -h /md/share/test.img
21G /md/share/test.img

2. If I create empty image using dd:

dd if=/dev/zero of=/md/share/test2.img bs=1024k seek=20480 count=0

I got:

du -h /md/share/test2.img
0 /md/share/test2.img
ls -lah /md/share/test2.img
-rw-r--r-- 1 root root 20G Aug 2 14:46 /md/share/test2.img

After mounting this to guest, I can use all 20G in any way I want, and IMG size is always equal it's actual contents, without any "zeroes" eating my array space.

Question: how I can reduce disk image size for case 1?

Answer: if dd supports conv=sparse attribute, it must be used in addition to tar --sparse. If not, update coreutils and try it again. Else install package libguestfs-tools and run virt-sparcify /md/share/test.img /md/share/test_sparse.img

Dmitry
  • 31

2 Answers2

1

This feature is called sparse files. GNU tar --sparse handles them well, other than gzip alone.

It doesn't also hurt to use zerofree on the resulting image.

0

As @sam_pan_mariusz said, the reduced-size image you're creating your "empty" image as a "sparse" file. In order to create a sparse file from an existing block device, add the conv=sparse option to the dd if=/dev/sda [...] command that you used to make the initial image. You will need to put the image file into tar --sparse, because just compressing the file image will cause the sparse chunks to be written out, making them non-sparse.

womble
  • 98,245