Sometimes I need to extend or add disk space to my running VM (guest) to satisfy growing software requirements. KVM uses QEMU which supports several image types, among them raw, cow, qcow, qcow2, vmdk, vdi among others available.

The “native” and most flexible type is qcow2, which supports copy on write, encryption, compression, and VM snapshots.
Run all the commands below as root or with sudo.

Step 1: Shut down the Virtual Machine on KVM

Before you can extend your guest machine Virtual disk, you need to first shut it down.

# virsh list
 Id   Name    State
-----------------------
 4    webserver   running

If your guest machine is in running state, power it off using its ID or Name.

# virsh shutdown webserver
Domain webserver is being shutdown

Confirm that it is truly down before proceeding to manage its disks.

# virsh list          
 Id   Name   State
--------------------

Step 2: Extend your KVM guest OS disk

Locate your guest OS disk path.

# virsh domblklist webserver
 Target   Source
-----------------------------------------------
 vda      /var/lib/libvirt/images/webserver-2.qcow2
 sda      -

You can obtain the same information from the Virtual Machine Manager GUI. My VM disk is located in ‘/var/lib/libvirt/images/rhel8.qcow2‘.

# qemu-img info /var/lib/libvirt/images/webserver-2.qcow2
image: /var/lib/libvirt/images/webserver-2.qcow2
file format: qcow2
virtual size: 30G (32212254720 bytes)
disk size: 30.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

Step 3: Extend guest VM disk

Since we know the location of our Virtual Machine disk, let’s extend it to our desired capacity.

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

Please note that qemu-img can’t resize an image which has snapshots. You will need to first remove all VM snapshots. See this example:

# virsh snapshot-list webserver
 Name        Creation Time               State
--------------------------------------------------
 snapshot1   2019-04-16 08:54:24 +0300   shutoff

# virsh snapshot-delete --domain webserver --snapshotname snapshot1
Domain snapshot snapshot1 deleted

# virsh snapshot-list webserver
 Name   Creation Time   State
-------------------------------

Then extend the disk by using the `+‘ before disk capacity.

$ qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G
Image resized.

Check the new virtual disk size 40G. Note: The disk size inside the virtual machine still has to be resized, now still 30G

# qemu-img info /var/lib/libvirt/images/webserver-2.qcow2
 image: /var/lib/libvirt/images/webserver-2.qcow2
 file format: qcow2
 virtual size: 40G (42949672960 bytes)
 disk size: 30G
 cluster_size: 65536
 Format specific information:
     compat: 1.1
     lazy refcounts: true
     refcount bits: 16
     corrupt: false

Confirm disk size with fdisk command. Also still 30G and has to be resized.

# fdisk -l /var/lib/libvirt/images/webserver-2.qcow2      
Disk /var/lib/libvirt/images/webserver.qcow2: 30.2 GiB, 32399818752 bytes, 63280896 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Step 4: Grow VM partition

Now power up the VM

# virsh start webserver
Domain webserver started

SSH to your VM and become the root or using an user account that has sudo rights.

# ssh webserver           
Last login: Fri Apr 19 06:11:19 2019 from 192.168.122.1
user@webserver:~# 

Check your new 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]

My VM total disk capacity is now 40GB, previously it was 30GB. To extend your OS partition, refer to below guides.

How To extend/increase KVM Virtual Machine (VM) disk size

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *