onsdag 3. august 2011

HOWTO: Resize kvm disk image

I Recently needed a way of re-sizing a kvm disk image. I found quite a few guides and tutorials on this. But i found that several steps were unnecessary and time consuming.


The following is my own recipe on re-sizing a kvm disk image. (some of which is plain copy & paste)


Make backup of kvm disk image
sudo mv disk-image.img /backup-path/disk-image.img.backup


Create a new blank qemu image file
sudo qemu-img create -f raw addon.raw 30G


Append addon.raw to the end of disk-image.img and store as the original disk-image.img file
sudo -s
cat /backup/disk-image.img.backup addon.raw >> disk-image.img


Mount the new disk-image to loop device.
losetup -f (we will assume it returns /dev/loop0)
losetup /dev/loop0 disk-image.img


Then use your favorite partitioning tool (fdisk/parted) to recreate your partitions. take care to ensure the partition numbers and partiton types stay the same, also remember bootable flag.
parted /dev/loop0


Add partition mappings
kpartx -av /dev/loop0


Check filesystem and resize partition
e2fsck -f /dev/mapper/loop0p1 (assuming this is the partition we are extending)
resize2fs /dev/mapper/loop0p1 (assuming this is the partition we are extending)


Recreate filesystem on swap partition
mkswap /dev/mapper/loop0p2 (assuming this is your swap partition)


Remove kpartx mappings
kpartx -dv /dev/loop0


Detach loopback device
losetup -d /dev/loop0

1 kommentar: