Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to recreate a dynamically allocated qcow2 image in order to shrink it. Is it sufficient that all unnecessary files have been deleted, or do I also need to fill the space formerly occupied by those files with zeros? In other words, is qemu-img filesystem-aware?

share|improve this question

Yes, you do need to zero-fill the filesystem if you want to recover the space used by deleted files. And no, qemu-img isn't fs-aware.

I forgot to do this for one VM image I created today (a minimal Debian Sid image for my openstack cloud at work) and it ended up being almost 900MB, even with "-c" for qcow2 compression.

I recreated it after running "dd if=/dev/zero of=/root/zero ; rm -f /root/zero ; shutdown -h now", and the image size shrunk down to about 335MB. That's a lot less (worthless) data to copy around whenever I start up a new instance.

there were a lot of deleted files, because the VM started out as debian squeeze and was apt-get upgraded to sid.

share|improve this answer

Personally, I think it works better to clone the disk using Clonezilla or Symantec Ghost. It's a lot quicker than filling up the drive with zeros. Also it avoids the growing the image even more.

I have done this with Ghost and Win guests countless times. It's actually quicker if "used space" is smaller than those to be zeroed. Also you can use qemu-nbd to mount the images and run Clonezilla from host, avoiding the hassle of Clonezilla-within-guest. Either way it's always much quicker than sdelete/dd in my experience. (Also I often end up with no space available on host for a full zero-out-guest-disk operation, so filling up available space in guest in seldom feasible to me.)

share|improve this answer
    
Running Clonezilla or Ghost inside the VM ? Seems like a lot of unnecessary work. – user186340 Apr 3 '15 at 15:09
    
@AndréDaniel I have done this with Ghost and Win guests countless times. It's actually quicker if "used space" is smaller than those to be zeroed. Also you can use qemu-nbd to mount the images and run Clonezilla from host. Either way it's always much quicker than sdelete/dd to me. (Also I often end up with no space available on host for a full zero-out-guest-disk operation. – phoeagon Apr 3 '15 at 16:12
    
Please add your comment to the answer to make it more substantive. – Deer Hunter Apr 3 '15 at 18:10

See also: virt-sparsify, an utility which can zero-fill filesystems inside disk images (supporting various formats):

http://libguestfs.org/virt-sparsify.1.html

share|improve this answer

I´m using zerofree ( apt-get install zerofree ) for this task:

Zerofree finds the unallocated blocks with non-zero value content in an ext2, ext3 or ext4 file-system and fills them with zeroes

after that you can shrink your image:
kvm-img convert -O qcow2 original_image.qcow2 deduplicated_image.qcow2

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.