How to increase disk size for KVM virtual machine

This article explains the detailed steps to increase disk size for KVM virtual machine through command line.

Step1: Login to the node server via ssh as root.

Step2: Get the name of the virtual machine.

#virsh list

 Id   Name    State
1    test   running

Step3: Stop the virtual machine.

#virsh shutdown test

It will take some time to complete the shutdown and we can check the state after few mins.

Step4: Identify the disk path for the virtual machine.

#virsh domblklist test

 Target   Source
vda      /var/lib/libvirt/images/test.qcow2
sda      -

Step5: Check for snapshots. Because qemu-img can’t resize an image which has snapshots.

#virsh snapshot-list test

 Name        Creation Time               State
snapshot1   2020-12-22 04:02:25 +0300   shutoff

Step6: Remove the snapshots. If there is no snapshots, we can ignore this step.

#virsh snapshot-delete --domain test --snapshotname snapshot1

Domain snapshot snapshot1 deleted

Step7: Extend the virtual machine disk.

#qemu-img resize /var/lib/libvirt/images/test.qcow2 +10G

Step8: Start the virtual machine.

#virsh start test

Domain test started

Step9: Login the virtual machine as root.

Step10: Check your new disk layout.

$ lsblk 

sr0            11:0    1 1024M  0 rom  
vda           252:0    0   40G  0 disk 
├─vda1        252:1    0    1G  0 part /boot
└─vda2        252:2    0   29G  0 part 
    ├─rhel-root 253:0    0 26.9G  0 lvm  /
    └─rhel-swap 253:1    0  2.1G  0 lvm  [SWAP]

Step11: Install cloud utils for getting growpart command. If the command is already installed, we can skip this step.

For Debian systems,

#apt install cloud-guest-utils

For Centos/Redhat systems,

#yum -y install cloud-utils-growpart

Step12: Extend the partition.

#growpart /dev/vda 2

CHANGED: partition=2 start=2099200 old: size=18872320 end=20971520 new: size=60815327,end=62914527

Please note that here all the command aruguments are given as per our current system. This need to be replaced with the corresponding values of real environment.

That's all…

Leave a Reply

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