Easy Way to Extend KVM Virtual Machine Disk Size

|
Last Updated:
|
|

This is a simple tutorial on an easy way to extend KVM virtual machine disk size. Disk usage in a VM is among the limited resources which sometimes require to be increased/reduced. It is possible to resize the disk in KVM using QEMU.

Easy Way to Extend KVM Virtual Machine Disk Size

In this guide, we will use the qcow2 disk format, which is the default in KVM. Other formarts such as vmdk,qcow,cow, raw e.t.c can be extended in a similar way.

Get the Correct Name of the Domain/Virtual Machine

Before you proceed, you need to check the properties of the VM you need to extend.

Use virsh command to check the list of all running VMs so as to find the correct name of the VM/domain you need to extend the disk.

virsh list --all

You will get a list similar to the following:

 Id   Name         State
----------------------------
 15   almalinux8   running
 17   opensuse     running

You can also use the GUI tool Virtual Machine Manager (VMM) to view the details/state of VMs.

Now select a VM you want to resize from list provided by the command above.

In our case, we will be extending the almalinux8 VM.

Locate Domain/Virtual Machine Disk Path

Let’s locate the disk path of the VM whose disk is to be extended using the following command:

virsh domblklist --domain almalinux8

Change the last part (almalinux8) to the name of your VM.

Sample output;

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

Backup the Virtual Machine

I recommended that you make a backup of the virtual machine you want to the extend the disk before you can proceed to avoid loses in case of any mistake.

You can make a copy of the qcow2;

cp /var/lib/libvirt/images/almalinux8.qcow2 /home/vms/almalinux8-COPY.qcow2

Or clone the current machine state and extend the disk on the cloned VM.

Check our guide on how to clone KVM Virtual Machines.

You can then power on the clone and extend the disk as follows.

Extend KVM Virtual Machine Disk Size

There are different ways in which you can extend KVM virtual machine disk size:

  • Shutting down the VM, extending using qemu-img, then start the VM to complete disk expansion.
  • Extending a live VM Disk
  • Extending a powered-off VM Disk entirely from a host.

In this guide, we will see how to extend KVM virtual machine disk size using the first method.

  1. The first step is to power off the VM before moving to the next steps.
virsh shutdown --domain <vm_name>

You can also use GUI to shutdown.

  1. Next, you need to delete the VM snapshots if there are any. Note that you have to have backed up the VM before deleting snapshots.

You can delete the snapshots from GUI or delete using the command snapshot-delete.

virsh snapshot-list --domain <vm_name>
virsh snapshot-delete --domain <vm_name> --snapshotname <snapshot_name>
  1. Check the size of the disk using fdisk
fdisk -l /var/lib/libvirt/images/almalinux8.qcow2
Disk /var/lib/libvirt/images/almalinux8.qcow2: 20 GiB, 21478375424 bytes, 41949952 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
  1. Extend the VM using qemu-img.

You can specify the ultimate size or the size you want to add preceeded by + for adding and  for reducing. If we want the ultimate size to be 30 GB, we use 30G. We can also specify +30G to mean 30GB will be added to the existing size. We are adding 10 GB in our case.

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

Again, check the size from fdisk

fdisk -l /var/lib/libvirt/images/almalinux8.qcow2
Disk /var/lib/libvirt/images/almalinux8.qcow2: 20 GiB, 21478375424 bytes, 41949952 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

You can see that the disk has not increased. However, when checked with qemu-img command we note an increase in size.

qemu-img info /var/lib/libvirt/images/almalinux8.qcow2
image: /var/lib/libvirt/images/almalinux8.qcow2
file format: qcow2
virtual size: 30 GiB (32212254720 bytes)
disk size: 2.91 GiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: true
    refcount bits: 16
    corrupt: false
    extended l2: false
  1. Power on the VM
virsh start --domain <vm_name>

Login to your VM and check the disk.

lsblk
[root@localhost ~]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                 11:0    1 1024M  0 rom  
vda                252:0    0   30G  0 disk 
├─vda1             252:1    0    1G  0 part /boot
└─vda2             252:2    0   19G  0 part 
  ├─almalinux-root 253:0    0   17G  0 lvm  /
  └─almalinux-swap 253:1    0    2G  0 lvm  [SWAP]

You can note that the total disk (vda) is 30 GB while the usable (vda1+vda2) is 20 GB.

You need to expand the disk now.

The fdisk utility will recognize the partition mismatch and will correct. Run fdisk against disk:

fdisk /dev/vda
[root@localhost ~]# fdisk /dev/vda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

write p and press enter to show the partition table.

Command (m for help): p
Disk /dev/vda: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: dos
Disk identifier: 0x73223988

Device     Boot   Start      End  Sectors Size Id Type
/dev/vda1  *       2048  2099199  2097152   1G 83 Linux
/dev/vda2       2099200 41943039 39843840  19G 8e Linux LVM

Note that vda2 is the partition we need to resize in this example setup.

So we need to delete it and allocate a new partition at the start of the same segment. So press d and enter.

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Then create a new partition.

Press n and enter to accept defaults for partition number, first sector and last sector.

When prompted to remove signature, enter NO.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (2099200-62914559, default 2099200): 
Last sector, +sectors or +size{K,M,G,T,P} (2099200-62914559, default 62914559): 

Created a new partition 2 of type 'Linux' and of size 29 GiB.
Partition #2 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: N

Commit the changes using w

Command (m for help): w

The partition table has been altered.
Syncing disks.

At this point, there are two options;

  • If the disk is partitioned using standard partitioning system, the disk extension is now done and you should just reboot the system.
reboot
  • If the disk is partitioned dynamically using Logical Volume Management system, then proceed as follows;
lsblk

NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                 11:0    1 1024M  0 rom  
vda                252:0    0   30G  0 disk 
├─vda1             252:1    0    1G  0 part /boot
└─vda2             252:2    0   29G  0 part 
  ├─almalinux-root 253:0    0   17G  0 lvm  /
  └─almalinux-swap 253:1    0    2G  0 lvm  [SWAP]

We can now see that vda2 is ~30GB BUT the logical volume has not been extended.

To finalize the disk expansion;

  • Extend the Physical Volume
  • Extend the Volume Group
  • Extend the logical volume
  • Extend the filesystem.

Extend the physical volume;

pvs

Output;

  PV         VG        Fmt  Attr PSize   PFree
  /dev/vda2  almalinux lvm2 a--  <19.00g    0

Next, resize the physical volume,

pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized

Extend volume group;

vgextend almalinux /dev/vda2

Extend the logical volume to all Free space;

lvextend -l +100%FREE /dev/mapper/almalinux-root

Resize the Filesystem;

If using EXT4, then;

resize2fs /dev/mapper/almalinux-root

If using XFS, then;

xfs_growfs /dev/almalinux/root

meta-data=/dev/mapper/almalinux-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=0 inobtcount=0
data     =                       bsize=4096   blocks=4455424, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4455424 to 7076864

Your disk should now be extended.

Confirm with the df command;

df -hT

Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  636M     0  636M   0% /dev
tmpfs                      tmpfs     655M     0  655M   0% /dev/shm
tmpfs                      tmpfs     655M  8.6M  646M   2% /run
tmpfs                      tmpfs     655M     0  655M   0% /sys/fs/cgroup
/dev/mapper/almalinux-root xfs        27G  2.1G   25G   8% /
/dev/vda1                  xfs      1014M  258M  757M  26% /boot
tmpfs                      tmpfs     131M     0  131M   0% /run/user/0

And that is how you can easily extend KVM virtual machine disk size.

Other Tutorials

Start and Stop KVM Virtual Machines from Command Line

List Running and Stopped VMS on KVM

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
kaptotonindet

Leave a Comment