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

2 Answers 2

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.

3
  • Briliant, womble! But there is no "sparse" option in my centos's dd. It looks like deprecated version (in Centos 5.11), but I found other questions with similar answers dated 2009 (serverfault.com/questions/93667/…). It's different centos coreutils trunk or something like that? Available "conv" values are: ascii ebcdic ibm block unblock lcase nocreat excl notrunc ucase swab noerror sync fdatasync fsync
    – Dmitry
    Aug 3, 2015 at 11:40
  • Great. bugzilla.redhat.com/show_bug.cgi?id=908980
    – Dmitry
    Aug 3, 2015 at 12:03
  • Solution is: yum install libguestfs-tools, then virt-sparsify /md/share/test.img /md/share/test_sparse.img
    – Dmitry
    Aug 3, 2015 at 15:43
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .