Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize the qcow2 image without impact the application

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.

like image 649
Shi Yan Avatar asked Mar 18 '15 14:03

Shi Yan


People also ask

How do I reduce the size of an image in qcow2?

To reduce the virtual size of a qcow2 disk image, we must first reduce the size of the partitions and filesystem on it. We need to proceed this way since all data in the space that will be removed by the shrinking operation will be lost.

What is the difference between qcow2 and ISO?

qcow2 or . vhd formats using the qemu-img tool that comes with Qemu. On the other hand, "pure" ISO images made for CDs/DVDs use a boot process somewhat incompatible with fixed disk (HDD/USB) boot process – their boot sectors are different, their partition tables are different.


2 Answers

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.

like image 111
user2051965 Avatar answered Nov 01 '22 13:11

user2051965


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
like image 45
rickfoosusa Avatar answered Nov 01 '22 14:11

rickfoosusa