Webmasters at times need to increase the size of a running VM. For KVM VM, we can use QEMU to increase the virtual machine disk size.
As a part of our Server Management Services, we help our Customers with disk related requests regularly.
Let us today discuss the steps to increase the size of a VM in KVM.
How to increase VM disk size in KVM?
At times, we may need to increase the disk space of the VM. The process of increasing the disk size of the VM typically includes three steps.
- Shutdown the VM
- Locate OS disk
- Extend VM disk
Let us now look at each of these steps in detail.
Shut down the VM
Before we extend the Virtual disk we first need to shut it down:
$ sudo virsh list
Id Name State
-----------------------
4 rhel8 running
If the machine is in running state, power it off using its ID or Name. Then confirm the status is truly down.
$ sudo virsh shutdown rhel8
Domain rhel8 is being shutdown
$ sudo virsh list
Id Name State
--------------------
Locate OS disk
Once the VM is in shut down state, we need to locate the OS disk path.
$ sudo virsh domblklist rhel8
Target Source
-----------------------------------------------
vda /var/lib/libvirt/images/rhel8.qcow2
sda -
Here the VM is located at /var/lib/libvirt/images/rhel8.qcow2. To view the virtual disk setting of a KVM virtual machine, use this command:
$ sudo qemu-img info /var/lib/libvirt/images/rhel8.qcow2
Here, there are two fields that represent size:
- virtual_size – is the size of a virtual disk set when creating or expanding the disk (in this example, the maximum disk size is 30GB)
- disk_size — is the current size of the disk file, ie, how much space the disk occupies on a physical server. In our example, the virtual disk takes up only 2GB of storage.
Extend VM disk
We can use the qemu-img resize command to extend the disk to our desired capacity. However, this command cannot resize an image that has snapshots.
Thus we need to locate and remove all the VM snapshots with the commands below:
$ sudo virsh snapshot-list rhel8
Name Creation Time State
--------------------------------------------------
snapshot1 2019-04-16 08:54:24 +0300 shutoff
$ sudo virsh snapshot-delete --domain rhel8 --snapshotname snapshot1
Domain snapshot snapshot1 deleted
Now we can extend the disk by using a ‘+’ before the disk capacity.
$ sudo qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G
We can also resize with virsh command. This requires the domain to be running.
$ sudo virsh start rhel8
$ sudo virsh blockresize rhel8 /var/lib/libvirt/images/rhel8.qcow2 40G
Block device '/var/lib/libvirt/images/rhel8.qcow2' is resized
Now power up the VM and check the disk layout:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
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]
VM’s total disk capacity is now changed from 30GB to 40GB. Users will then require to extend the OS partition.
[Need any further assistance to increase KVM VM size? – We’re available 24*7]
Conclusion
In short, the process of increasing the disk size of the VM typically includes three steps – Shutdown of the VM, Locating OS disk, and Extending the VM disk. Today, we saw how our Support Engineers perform this task.
0 Comments