uniq_

Increase size of a libvirt kvm image

I have this huge VM and it tends to grow, luckily I could figure out how to increase the VM image. (Following this guide the old KVM image will stay in tact without any modifications.)

On the host system:

# install required tools
sudo apt-get install libguestfs-tools

# shutdown running libvirt container
virsh shutdown my_vm

# move old image
sudo mv /var/lib/libvirt/images/my_vm.img /var/lib/libvirt/images/my_vm.old.img

# create new empty file for our new kvm image
truncate -s 128G /var/lib/libvirt/images/my_vm.img
# or if you want qcow2:
# qemu-img create -f qcow2 /var/lib/libvirt/images/my_vm.qcow2 128G

# (optional) you can list the partitions of an existing KVM image like this
sudo virt-filesystems --long --parts --blkdevs -h -a /var/lib/libvirt/images/my_vm.old.img
# in case of lvm you can list lvm partitions like this:
# virt-filesystems --logical-volumes --long -a /var/lib/libvirt/images/my_vm.old.img

# make copy of old image and expand the new image to all available space in the designeted new image file.
sudo virt-resize --expand /dev/sda1 /var/lib/libvirt/images/mv_vm.old.img /var/lib/libvirt/images/my_vm.img

# if you are using LLVM you might need to run the command like this:
# virt-resize --expand /dev/sda2 --LV-expand /dev/vg_guest/lv_root olddisk newdisk

Once this is complete you might want to edit the qemu VM definition in /etc/libvirt/qemu/my_vm.xml. Alternatively you may also simply rename the old image and use the new resized image to the path of the old one.

Now you can start the VM again:

virsh start my_vm

Next you'll need to connect your VM and resize the partition to fill the newly create empty (virtual) disk space:

sudo resize2fs /dev/sda1

resources:

written by uniq on 2017-02-17