Reputation: 95
My question is about to resize the qcow2 VM image. I have a image with packages and applications built in. For example when creating the image, the size is 40G, but for now it in reality use about 5G disk space. So i would like to shrink it.
My method is like below:
qemu-img convert -O raw VM1.qcow2 VM1.raw qemu-img resize VM1.raw -20G qemu-img convert -c -O qcow2 VM1.raw VM1.qcow2
But after i did that, although the size is shrunk a lot, the VM could not be able to boot correctly. I am using linux with KVM/libvirt.
Upvotes: 6
Views: 26874
Reputation: 31
1) the simplest method: install libguestfs-tools 2) run: virt-sparsify --in-place virtualDisk.qcow2
it does it on file so you don't need extra space.
Upvotes: 1
Reputation: 11
Manipulating only from the host side, may leave the guest file system corrupted.
Thus, before any changes from the host you need to change the VM's storage partitions sizes with partitioning tools like gparted
from inside of the guest. So that the total size of the virtual disk becomes less than desired size of the virtual storage. After that, you can do converting and resizing and converting back the virtual storage file from the host.
Upvotes: 1
Reputation: 41
If your guest OS is windows, you can expand the partition from within the guest, while it is booted. First copy your original image (for safety) and use qemu-img resize to add space to the disk image:
cp small_image.qcow2 large_image.qcow2
qemu-img resize large_image.qcow2 +100G
Then boot the windows VM in large_image.qcow2. Open the "Disk Management" utility. Right click on C:, and select either "Extend Volume" or "Shrink Volume"
Your VM guest will now have access to the space added by qemu-img resize.
Upvotes: 3
Reputation: 1139
To shrink a disk, you must do some work on the Guest VM.
An excellent description: http://www.jamescoyle.net/how-to/323-reclaim-disk-space-from-a-sparse-image-file-qcow2-vmdk. I've added defrag, and expanding a disk.
From a windows guest:
De-fragment your disk so that all the files are moved toward the beginning of the disk.
Download: http://technet.microsoft.com/en-gb/sysinternals/bb897443.aspx
Fill all free space with 0's.
c:\sdelete.exe -z c:
From a Linux guest, fill all free space with 0's:
dd if=/dev/zero of=~/mytempfile
rm -f ~/mytempfile
On the libvirt host:
mv original_image.qcow2 original_image.qcow2_backup
qemu-img convert -O qcow2 original_image.qcow2_backup original_image.qcow2
To expand a disk, it can all be done on the libvirt host.
mv original_image.qcow2 original_image.qcow2_backup
truncate -s <desired number of Gigabytes>G original_image.qcow2
qemu-img convert -O qcow2 original_image.qcow2_backup original_image.qcow2
In BOTH cases, check your work.
qemu-img info original_image.qcow2
Upvotes: 2