live-resizing KVM VM disk backed by qcow2 file

let’s say you have KVM VM with Linux and you’d like to resize its qcow2-backed disk without shutting down VM.

VM in this example is called mlt0. first – on the KVM server level I’ll find out the location and current size of the qcow2 file:

root@gkvm0:/mnt/big/kvm/mlt0# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # domblklist mlt0
 Target   Source
---------------------------------------
 vda      /mnt/big/kvm/mlt0/vda.qcow2
 hda      -

virsh # domblkinfo mlt0 /mnt/big/kvm/mlt0/vda.qcow2
Capacity:       34359738368
Allocation:     22932295680
Physical:       34365243392

then i’ll increase size of it [ be careful, take backup in advance ]:

root@gkvm0:/mnt/big/kvm/mlt0# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # blockresize mlt0 /mnt/big/kvm/mlt0/vda.qcow2 64G
Block device '/mnt/big/kvm/mlt0/vda.qcow2' is resized
virsh # domblkinfo mlt0 /mnt/big/kvm/mlt0/vda.qcow2
Capacity:       68719476736
Allocation:     22932299776
Physical:       34365244416

given new-enough kernel the VM should already ‘notice’ increased disk size, if not – partprobe should help:

root@mlt0:/# dmesg
[5988323.071764] virtio_blk virtio1: [vda] new size: 134217728 512-byte logical blocks (68.7 GB/64.0 GiB)

now we just need to increase partition and file system:

root@mlt0:/# apt-get install cloud-guest-utils
root@mlt0:/# echo 1 > /sys/class/block/vda/device/rescan 
root@mlt0:/# growpart /dev/vda 1 # grow the 1st partition to use all of available disk space
root@mlt0:/# resize2fs /dev/vda1

Leave a Reply

Your email address will not be published. Required fields are marked *

 

(Spamcheck Enabled)