Resize KVM images

At the start of running KVM, I have created all the images to be used for VM’s at the same size. Just to have the additional space, without the hassle of having to add space to it. However, having images at the same size, while usage is below 10%, backup takes too much time.

So, having found this link in the Net, I decided to try this.

Be aware – use at your own risk!

  • Make a backup of the image file
  • Install the required tools
  • root@host:# apt-get install libguestfs-tools

  • Check the current size
  • root@host:# virt-df vm.img
    Filesystem 1K-blocks Used Available Use%
    vm.img:/dev/sda1 233191 70989 149761 31%
    vm.img:/dev/vg0/root 19223252 2804440 15442328 15%

  • Resize
  • root@host:# guestfish -a vm.img

    Welcome to guestfish, the libguestfs filesystem interactive shell for
    editing virtual machine filesystems.

    Type: ‘help’ for help on commands
    ‘man’ to read the manual
    ‘quit’ to quit the shell

    > run
    > e2fsck-f /dev/vg0/root
    > resize2fs-size /dev/vg0/root 5G
    > lvresize /dev/vg0/root/lv_root 5120
    > pvresize-size /dev/sda5 6G
    libguestfs: error: pvresize_size: /dev/sda5: /dev/vda5: cannot resize to 191 extents as later ones are allocated.

    Hmm. Errors. Would that be since swap is the last lvm created? So we remove swap and try again.

    > lvremove /dev/vg0/swaplv/lv_swap
    > pvresize-size /dev/sda5 6G
    > lvcreate swaplv vg0 512
    > mkswap /dev/vg0/swaplv
    > exit

    So far, so good. The link above states virt-resize can be used, however virt-resize does not recognise the extended partition. I’ll do it differently – as I did make my backup, I can try…

  • Run qemu-img
  • root@host:# qemu-img resize vm.img 6G

    root@host:# virsh start vm

And? It worked. Although the backup ran faster, there is still the problem of parted not recognizing the extended partition. That will bring mayhem, in the future and I don’t like to run systems of which I do not trust the built.

So, different approach, which works for resizing a VM into any direction.

  • Step 1- create a small VM
  • So I created a tiny VM (2G) which contains a basic OS of the systems I intend to check/rebuild.

  • Stop original VM and virt-copy-out original data
  • virt-copy-out -a disk.img / original.tar

  • Place copy of small VM in place and resize, if required
  • The resize of the VM, most of the time bigger:

    qemu-img resize disk.img +5Gb

    Delete the storage – I used local disk storage – from the pool, and re-import the new image into libvirt. Stop/restart Storage Pool – else virt-manager still recognises the old size, refreshing volumes does not show new size

    Boot VM and resize partition via the running VM’s disk manager tools (LVM or windows)

  • virt-copy-in
  • virt-copy-in -a disk.img original.tar /

  • start vm
  • Comments are closed.