Shi Yan
Shi Yan

Reputation: 95

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.

Upvotes: 6

Views: 26874

Answers (4)

Juan Matias Granda
Juan Matias Granda

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

Datis
Datis

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

user2051965
user2051965

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

rickfoosusa
rickfoosusa

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:

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

Related Questions