Source : https://computingforgeeks.com/how-to-extend-increase-kvm-virtual-machine-disk-size/
Note : Worked well on Centos Stream 8.4 Host Machine with Ubuntu 22.04 VM
How can I extend / increase / grow a virtual Disk in KVM?. I personally use KVM for all Linux virtualization projects. 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.
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.
$ sudo virsh list
Id Name State
-----------------------
4 rhel8 running
If your guest machine is in running state, power it off using its ID or Name.
$ sudo virsh shutdown rhel8
Domain rhel8 is being shutdown
Confirm that it is truly down before proceeding to manage its disks.
$ sudo virsh list
Id Name State
--------------------
Step 2: Extend your KVM guest OS disk
Locate your guest OS disk path.
$ sudo virsh domblklist rhel8
Target Source
-----------------------------------------------
vda /var/lib/libvirt/images/rhel8.qcow2
sda -
OR use:
$ sudo virsh dumpxml rhel8 | egrep 'disk type' -A 5
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/rhel8.qcow2'/>
<backingStore/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
--
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='sda' bus='sata'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
You can obtain the same information from the Virtual Machine Manager GUI. My VM disk is located in ‘/var/lib/libvirt/images/rhel8.qcow2‘.
$ sudo qemu-img info /var/lib/libvirt/images/rhel8.qcow2
image: /var/lib/libvirt/images/rhel8.qcow2
file format: qcow2
virtual size: 30G (42949672960 bytes)
disk size: 2.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.
sudo qemu-img resize /var/lib/libvirt/images/rhel8.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:
$ 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
$ sudo virsh snapshot-list rhel8
Name Creation Time State
-------------------------------
Then extend the disk by using the `+‘ before disk capacity.
$ sudo qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G
Image resized.
You can also resize with virsh command. This requires domain to be running.
$ sudo qemu-img info /var/lib/libvirt/images/rhel8.qcow2
image: /var/lib/libvirt/images/rhel8.qcow2
file format: qcow2
virtual size: 30G (42949672960 bytes)
disk size: 2.0G
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
refcount bits: 16
corrupt: false
$ 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
Confirm disk size with fdisk command.
$ sudo fdisk -l /var/lib/libvirt/images/rhel8.qcow2
Disk /var/lib/libvirt/images/rhel8.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
$ sudo virsh start rhel8
Domain rhel8 started
SSH to your VM as root user or using user account that has sudo.
$ ssh rhel8
Last login: Fri Apr 19 06:11:19 2019 from 192.168.122.1
[jmutai@rhel8 ~]$
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 root filesystem using LVM on Linux
Welcome to our guide on how to extend root filesystem using LVM on Linux. This will cover both ext4 and XFS filesystem root partition extending. To demonstrate a complete LVM lifecycle, we will perform the following actions:
- Create an LVM physical volume, volume group, and logical volume.
- Create an XFS and ext4 file systems on the logical volumes
- Extend LVM logical volumes ( root and non-root filesystem)
LVM allows you to create, resize or delete partitions on a running system without requiring any reboot. So check the steps below to extend root filesystem using LVM in Linux. You can skip some steps which don’t apply to use.
If you’re not using LVM, check our guide below which covers extending Ext2/3/4 and XFS file systems.
Step 1: Confirm Disk Partitions in Distribution.
Before we can do any extension, let’s just confirm our disk layout / partitioning scheme.
$ 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
├─rhel-root 253:0 0 26.9G 0 lvm /
└─rhel-swap 253:1 0 2.1G 0 lvm [SWAP]
As noted, we have a root filesystem on /dev/vda2 physical volume.
$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 rhel lvm2 a-- <29.00g 0
Step 2: Extend your OS root disk
As shown in step 1, my root filesystem is on a 30GB disk. I’ll grow it to 40GB by extending the virtual disk (VM disk device).
I use KVM virtualization technology, so this guide works for me: How to extend/increase KVM Virtual Machine (VM) disk size
$ 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]
If you’re on a different Virtualization platform, refer to its documentation for how to extend OS disk.
Once the OS block device is resized, ssh to your Linux machine and extend LVM to use newly added disk capacity. The command below will expand the last partition (Partition 2), as shown by 252:2,on the disk (/dev/vda) to the maximum size the disk provides.
Install cloud utils package on the system
For those new to growpart, it is a Linux command line tool used to extend a partition in a partition table to fill available space. This command is provided by cloud utils package.
On Ubuntu / Debian system, run the commands below to install growpart tool.
sudo apt install cloud-guest-utils
For CentOS server, run
sudo yum -y install cloud-utils-growpart
Help page can be viewed by passing -h argument
$ growpart -h
growpart disk partition
rewrite partition table so that partition takes up all the space it can
options:
-h | --help print Usage and exit
--fudge F if part could be resized, but change would be
less than 'F' bytes, do not resize (default: 1048576)
-N | --dry-run only report what would be done, show new 'sfdisk -d'
-v | --verbose increase verbosity / debug
-u | --update R update the the kernel partition table info after growing
this requires kernel support and 'partx --update'
R is one of:
- 'auto' : [default] update partition if possible
- 'force' : try despite sanity checks (fail on failure)
- 'off' : do not attempt
- 'on' : fail if sanity checks indicate no support
Example:
- growpart /dev/sda 1
Resize partition 1 on /dev/sd
Now use growpart to extend your partition. In this example we’re extending partition 2 in disk /dev/vda. Replace 2 and /dev/vda with your correct values.
$ sudo growpart /dev/vda 2
CHANGED: partition=2 start=2099200 old: size=18872320 end=20971520 new: size=60815327,end=62914527
Confirm if the change was successful.
$ 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 39G 0 part
├─rhel-root 253:0 0 26.9G 0 lvm /
└─rhel-swap 253:1 0 2.1G 0 lvm [SWAP]
Step 3: Resize root logical volume to occupy all space
Resize physical volume.
$ sudo pvresize /dev/vda2
Physical volume "/dev/vda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
$ sudo pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 rhel lvm2 a-- <39.00g 10.00g
Check the size of the volume group configured.
$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- <39.00g 10.00g
Then resize logical volume used by the root file system using the extended volume group:
sudo lvextend -r -l +100%FREE /dev/name-of-volume-group/root
This extends the logical volume to use all available capacity in the volume group. With the + sign the value is added to the actual size of the logical volume.
Command options used:
-l – extend or set the logical volume size in units of logical extents
-r – Resize underlying filesystem together with the logical volume
Here’s an example of my setup file system extension:
$ df -hT | grep mapper
/dev/mapper/rhel-root xfs 27G 1.9G 26G 8% /
$ sudo lvextend -r -l +100%FREE /dev/mapper/rhel-root
Size of logical volume rhel/root changed from <26.93 GiB (6893 extents) to <36.93 GiB (9453 extents).
Logical volume rhel/root successfully resized.
If you prefer setting the size to be extended manually, use command option:
-L, --size [+]LogicalVolumeSize[bBsSkKmMgGtTpPeE]
Where size suffix are:
M for megabytes
G for gigabytes
T for terabytes
P for petabytes
E for exabytes
Without the + sign the value is taken as an absolute one.
# Add 20 gigabytes to the current logical volume size
$ sudo lvextend -r -L +20G /dev/name-of-volume-group/root
Step 4: Update changes on the filesystem (If you didn’t use -r option in step 3)
Your root filesystem will still show the old size.
$ df -hT | grep mapper
/dev/mapper/rhel-root xfs 27G 1.9G 26G 8% /
Let’s make the filesystem report the actual size, including extended.
For ext4 filesystem
sudo resize2fs /dev/name-of-volume-group/root
For xfs filesystem
$ sudo xfs_growfs /
meta-data=/dev/mapper/rhel-root isize=512 agcount=4, agsize=1764608 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=7058432, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=3446, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 7058432 to 9679872
