How to shrink raw qemu / kvm images
If you allocated to much hard disk space to your virtual machines and you’re running out of free space on your storage device, here’s how to reclaim space from your existing virtual machines.
First, shrink and realign the partitions on the image file as you see fit. In Linux, you may use a liveCD and the command resize2fs to shrink the root partition. Starting with Windows 2008, this is easily possible using the Management Console.
When you’re done, shutdown the machine.
On your virtualization platform, setup the image as loopback device:
# losetup -fv /path/to/image.raw Loop device is /dev/loop0
Then, find out the sector size and last used sector of the last partition(marked in red):
# fdisk -cul /dev/loop0 Disk /dev/loop0: 32.2 GB, 32212254720 bytes 255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x1449bcb0 Device Boot Start End Blocks Id System /dev/loop0p1 * 2048 11452502 5725227 7 HPFS/NTFS /dev/loop0p2 11452503 42432503 15490000 7 HPFS/NTFS
You’ve got all the information you need, so you can detach the loopback device.
# losetup -d /dev/loop0
dump the needed part of the old image into a new one, adding 2 to the last sector:
# dd if=/path/to/image.raw of=/path/to/shrinked_image.raw bs=512 count=42432505
Additionally, you may compress the new image with the native qcow2 compression:
# qemu-img convert -c -O qcow2 /path/to/shrinked_image.raw /path/to/shrinked_image.qcow2
Edit the configuration of your virtual machine to reflect the new path and image type and boot.