Changing the Disk Size of a KVM Guest
This article is based on the author's own research and testing and does not guarantee the accuracy or completeness of the information provided.
If you use the information in this article, you do so at your own risk. The author assumes no responsibility for any issues or damages resulting from its use.
Depending on the purpose of a KVM guest, the disk created initially may sometimes become insufficient in size.
CPU and memory resources require a reboot, but can be changed relatively easily (subject to the resources available on the KVM host).
Disk settings can also be changed flexibly when external disks are mounted using NFS or similar methods. However, changing the disk capacity of the image itself requires some work. Since this is the kind of procedure that is easy to forget if you do not write it down somewhere, I am documenting the steps here.
Environment
The environment is as follows.
- KVM host OS: Rocky 9
- KVM guest OS: Linux-based (using LVM)
The image file format I use is qcow2, so the following procedure is for qcow2.
Procedure
* Before starting, shut down the KVM guest whose disk capacity you want to change, and back up the image if necessary.
* Since a mistake when operating the commands could, in the worst case, make the system unable to boot, I recommend making a backup.
First, perform the following operations on the KVM host.
Check the KVM image
Check the format and size of the KVM image.
qemu-img info /var/lib/libvirt/images/<image-file-name>
Expand the file size
qemu-img resize -f qcow2 <image-file-name> +10G
* Change the +10G part as appropriate for the amount by which you want to resize the disk.
After running the command, run qemu-img info again and make sure that the size has been changed correctly.
From this point onward, the operations are performed on the KVM guest.
Using fdisk
Start the KVM guest with the image whose capacity has been increased.
Run fdisk and recreate the partition.
* Change the device name as appropriate for your environment.
* Although there are explanations in the middle, everything up to entering q is an operation performed with the fdisk command.
fdisk /dev/vda
Check the current state.
Command (m for help): p
(omitted)
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
Resize the LVM on /dev/vda2.
First, delete it:
Command (m for help): d
Partition number (1,2, default 2): 2
Then create it again.
Since the basic partition 2 was deleted, recreate basic partition 2.
When entering the number of sectors, the default value (just press Enter) is fine.
(It will be created using all available free space.)
Command (m for help): n
Partition type
p primary (1-4)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-41943039, default 2099200):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2099200-41943039, default 41943039):
Created a new partition 2 of type 'Linux' and of size 19 GiB.
Partition #2 contains a LVM2_member signature.
Do you want to remove the signature? [Y]es/[N]o: N
Change the partition type to Linux LVM
The recreated partition type is Linux, so change it to Linux LVM.
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code or alias (type L to list all): 8e
Change type of partition 'Linux' to 'Linux LVM'.
Check the state after the change:
Command (m for help): p
(omitted)
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
If it has been changed to the expected state (size and partition type), write the changes and exit fdisk.
Command (m for help): w
Command (m for help): q
On current operating systems (Red Hat 7 and later), partprobe should allow the changes to take effect without rebooting.
* A reboot may be required depending on the OS.
* partprobe itself may not even be necessary.
partprobe
Resize the physical volume
When using LVM, you need to resize the PV (physical volume) and LV (logical volume).
First, resize the PV (Physical Volume).
Check the current state:
pvdisplay
Then resize it.
This changes it so that all of the space corresponding to the size changed with fdisk is used.
pvresize /dev/vda2
After resizing, run pvdisplay again to confirm the change.
Resize the logical volume
Resize the logical volume.
Check the current state:
lvs
lvdisplay
Then resize it.
lvextend -l +100%FREE /dev/cl/root
Finally, expand the file system.
xfs_growfs /dev/cl/root
※ On Ubuntu, you may need to use something like resize2fs /dev/ubuntu-vg/ubuntu-lv instead.
Check the size.
df
evtmpfs 912992 0 912992 0% /dev
tmpfs 930236 0 930236 0% /dev/shm
tmpfs 930236 8796 921440 1% /run
tmpfs 930236 0 930236 0% /sys/fs/cgroup
/dev/mapper/cl-root 17811456 8021136 9790320 46% /
/dev/vda1 999320 262420 668088 29% /boot
tmpfs 186044 0 186044 0% /run/user/1000
If the capacity has been expanded, the operation was successful.