I love to run virtual machines locally when testing new tech. One of the wonderful things about virtual hardware is that it's quite easy to scale up and down. In most cases, no additional work is required because the VM (Virtual Machine) OS will adapt to the new CPU, RAM, or other virtual hardware you scale up or down. Unfortunately, it's not so easy with storage.
In this article, I will explain how you can use added storage capacity to an HDD attached to a VM running Ubuntu 22.04 or 24.04
Partition resize
In this example, I expanded the virtual disk drive from 30 to 50 GB before booting the VM. The new size of the storage device can be verified by checking the size of the "sda" drive.
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 55.7M 1 loop /snap/core18/2751
loop1 7:1 0 63.5M 1 loop /snap/core20/1891
loop2 7:2 0 55.4M 1 loop /snap/core18/2846
loop3 7:3 0 63.7M 1 loop /snap/core20/2434
loop4 7:4 0 91.9M 1 loop /snap/lxd/24061
loop5 7:5 0 91.9M 1 loop /snap/lxd/29619
loop6 7:6 0 44.3M 1 loop /snap/snapd/23258
loop7 7:7 0 44.4M 1 loop /snap/snapd/23545
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 29G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 29G 0 lvm /
sr0 11:0 1 1024M 0 rom
As seen from the output, there is 20GB of unallocated space on the drive.
The interactive CLI application cfdisk is an excellent tool for allocating the available space on the sda drive to the sda3 partition.
sudo cfdisk
Disk: /dev/sda
Size: 50 GiB, 53687091200 bytes, 104857600 sectors
Label: gpt, identifier: 128B8A04-CB1F-43AB-B65C-DBB034EDE22E
Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 2101247 2097152 1G Linux filesystem
>> /dev/sda3 2101248 62912511 60811264 29G Linux filesystem
Free space 62912512 104857566 41945055 20G
┌───────────────────────────────────────────────────────────────────────────────────┐
│ Partition UUID: 92D4C0D6-9F56-4F51-9DB9-582E58F61D4E │
│ Partition type: Linux filesystem (0FC63DAF-8483-4772-8E79-3D69D8477DE4) │
│Filesystem UUID: ij0H5i-nJrb-32cQ-ecAx-OsEI-FSyX-pGhyyf │
│ Filesystem: LVM2_member │
└───────────────────────────────────────────────────────────────────────────────────┘
[ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]
Use the up/down arrows on your keyboard to select /dev/sda3, then the right/left arrows to select resize. Press enter to continue. Once the partition is resized, choose to write and quit the cfdisk application.
You can now verify the size of the partition
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 55.7M 1 loop /snap/core18/2751
loop1 7:1 0 63.5M 1 loop /snap/core20/1891
loop2 7:2 0 55.4M 1 loop /snap/core18/2846
loop3 7:3 0 63.7M 1 loop /snap/core20/2434
loop4 7:4 0 91.9M 1 loop /snap/lxd/24061
loop5 7:5 0 91.9M 1 loop /snap/lxd/29619
loop6 7:6 0 44.3M 1 loop /snap/snapd/23258
loop7 7:7 0 44.4M 1 loop /snap/snapd/23545
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 49G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 29G 0 lvm /
sr0 11:0 1 1024M 0 rom
Logical volume resize
Verify that the logical volumes have new free space available
sudo vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 13
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <49.00 GiB
PE Size 4.00 MiB
Total PE 12543
Alloc PE / Size 7423 / <29.00 GiB
Free PE / Size 5120 / 20.00 GiB
VG UUID ClybuJ-I5NT-RZyU-00UY-Smk3-1BNq-lHclYh
Resize the logical volume to use all available free space.
sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <29.00 GiB (7423 extents) to <49.00 GiB (12543 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
Verify the new size of the logical volume
sudo vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 14
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <49.00 GiB
PE Size 4.00 MiB
Total PE 12543
Alloc PE / Size 12543 / <49.00 GiB
Free PE / Size 0 / 0
VG UUID ClybuJ-I5NT-RZyU-00UY-Smk3-1BNq-lHclYh
Expanding filesystem
Now that the logical volume is expanded, you can extend the filesystem that uses that logical volume.
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 795M 2.6M 792M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 29G 19G 8.2G 70% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/loop0 56M 56M 0 100% /snap/core18/2751
/dev/loop2 56M 56M 0 100% /snap/core18/2846
/dev/loop1 64M 64M 0 100% /snap/core20/1891
/dev/loop3 64M 64M 0 100% /snap/core20/2434
/dev/loop4 92M 92M 0 100% /snap/lxd/24061
/dev/sda2 974M 211M 696M 24% /boot
/dev/loop6 45M 45M 0 100% /snap/snapd/23258
/dev/loop5 92M 92M 0 100% /snap/lxd/29619
/dev/loop7 45M 45M 0 100% /snap/snapd/23545
tmpfs 795M 0 795M 0% /run/user/1000
As read from the output above, the filesystem on the logical volume is still 29GB even though both the partition is 49GB, and the logical volume actually is 49GB.
To resize the filesystem, run this command:
sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
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 = 4, new_desc_blocks = 7
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 12844032 (4k) blocks long.
Verify the changes to the filesystem size.
df -h
Filesystem Size Used Avail Use% Mounted on
udev 3.9G 0 3.9G 0% /dev
tmpfs 795M 2.6M 792M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 49G 19G 28G 42% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/loop0 56M 56M 0 100% /snap/core18/2751
/dev/loop2 56M 56M 0 100% /snap/core18/2846
/dev/loop1 64M 64M 0 100% /snap/core20/1891
/dev/loop3 64M 64M 0 100% /snap/core20/2434
/dev/loop4 92M 92M 0 100% /snap/lxd/24061
/dev/sda2 974M 211M 696M 24% /boot
/dev/loop6 45M 45M 0 100% /snap/snapd/23258
/dev/loop5 92M 92M 0 100% /snap/lxd/29619
/dev/loop7 45M 45M 0 100% /snap/snapd/23545
tmpfs 795M 0 795M 0% /run/user/1000
Congratulations, by following this guide, you have successfully added more storage to your virtual machine 🎉