Expanding the hard drive (LVM) of a Linux virtual machine
I was doing some testing of using a Samba share for OSX's Time Machine backup, and needed to verify that it would actually work.
My initial test simply made sure that the Samba share showed up as an option in the Time Machine preferences, but I needed to run a Time Machine backup.
However, the Ubuntu VM I've been using for NAS migration testing only had a 25Gb disk defined, which wasn't going to be enough to run through an entire Time Machine backup.
I needed to increase the size of the disk of the Ubuntu VM, so it could accept a Time Machine backup, which is currently just south of 500Gb.
Make sure the VM is off
virsh list --all
Id Name State
--------------------------------------------
- ubuntu20.04-nas-testing shut off
If it is still in the running state, shut it off:
virsh shutdown ubuntu20.04-nas-testing
Domain ubuntu20.04-nas-testing is being shutdown
Dump the current disks configuration
virsh domblklist ubuntu20.04-nas-testing
Target Source
-------------------------------------------------------
vda /kvm/images/ubuntu20.04-clone-1.qcow2
Delete any snapshots
The qemu-img
command can't resize an image if that image has snapshots, so we'll need to delete any snapshots before resizing.
virsh snapshot-list ubuntu20.04-nas-testing
Name Creation Time State
--------------------------------------------------
snapshot1 2021-09-01 11:31:51 -0600 shutoff
virsh snapshot-delete --domain ubuntu20.04-nas-testing --snapshotname snapshot1
Domain snapshot snapshot1 deleted
Resize the image
As, at least in my KVM system, the /kvm/images/ are all owned by root, need to use sudo
to resize the image.
sudo qemu-img resize /kvm/images/ubuntu20.04-clone-1.qcow2 +500G
Image resized.
It doesn't actually perform the resize, it only configures it to use the additional space, so the Image resized. comes back almost immediately.
Verify new disk size
sudo qemu-img info /kvm/images/ubuntu20.04-clone-1.qcow2
image: /kvm/images/ubuntu20.04-clone-1.qcow2
file format: qcow2
virtual size: 525 GiB (563714457600 bytes)
disk size: 12.9 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: true
refcount bits: 16
corrupt: false
Boot the VM and extend the disk
From the KVM perspective, the disk has been resized, but we need to tell the OS (Ubuntu in this case) to resize the disk too.
-
Verify Ubuntu sees the increased disk size:
lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 70.3M 1 loop /snap/lxd/21029 loop1 7:1 0 55.5M 1 loop /snap/core18/2074 loop2 7:2 0 55.4M 1 loop /snap/core18/2128 loop3 7:3 0 32.3M 1 loop /snap/snapd/12883 loop4 7:4 0 67.6M 1 loop /snap/lxd/20326 loop5 7:5 0 32.3M 1 loop /snap/snapd/12704 vda 252:0 0 525G 0 disk ├─vda1 252:1 0 1M 0 part ├─vda2 252:2 0 1G 0 part /boot └─vda3 252:3 0 24G 0 part └─ubuntu--vg-ubuntu--lv 253:0 0 20G 0 lvm /
-
Install, then use
growpart
to extend the partitionsudo apt-get install cloud-guest-utils
sudo growpart /dev/vda 3 CHANGED: partition=3 start=2101248 old: size=50325504 end=52426752 new: size=1098903519 end=1101004767
-
Verify the partition size change
lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop0 7:0 0 70.3M 1 loop /snap/lxd/21029 loop1 7:1 0 55.5M 1 loop /snap/core18/2074 loop2 7:2 0 55.4M 1 loop /snap/core18/2128 loop3 7:3 0 32.3M 1 loop /snap/snapd/12883 loop4 7:4 0 67.6M 1 loop /snap/lxd/20326 loop5 7:5 0 32.3M 1 loop /snap/snapd/12704 vda 252:0 0 525G 0 disk ├─vda1 252:1 0 1M 0 part ├─vda2 252:2 0 1G 0 part /boot └─vda3 252:3 0 524G 0 part └─ubuntu--vg-ubuntu--lv 253:0 0 20G 0 lvm /
-
Use
pvresize
to resize the PV (LVM Physical Volume) to use all available spacesudo pvresize /dev/vda3 Physical volume "/dev/vda3" changed 1 physical volume(s) resized or updated / 0 physical volume(s) not resized
-
Determine the LVM Logical Volume (LV) to extend
df -hT | grep mapper /dev/mapper/ubuntu--vg-ubuntu--lv ext4 20G 12G 6.6G 65% /
-
Use the
lvextend
to extend (resize) the file system on the LV (LVM Logical Volume)sudo lvextend -r -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv Size of logical volume ubuntu-vg/ubuntu-lv changed from 20.00 GiB (5120 extents) to <524.00 GiB (134143 extents). Logical volume ubuntu-vg/ubuntu-lv successfully resized. resize2fs 1.45.5 (07-Jan-2020) Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required old_desc_blocks = 3, new_desc_blocks = 66 The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 137362432 (4k) blocks long.
-
Verify resize
df -hT | grep mapper /dev/mapper/ubuntu--vg-ubuntu--lv ext4 516G 12G 483G 3% /
References
How To extend/increase KVM Virtual Machine (VM) disk size https://computingforgeeks.com/how-to-extend-increase-kvm-virtual-machine-disk-size/