Resize Virtual Disk Image and Expand Linux LVM

May 01, 2020

If you are like me and create virtual machines with only a reasonable virtual disk size to start with (because you can always expand them in the future, right?), it is very much possible you encountering a situation where you want to increase the virtual disk size. And if you run Fedora like me, it is also possible you suddenly finding yourself looking for a way to expand a LVM partition to utilize the gained space once the VDI size is increased. Well, today, I ran into both. Here is how I went about them.

Resizing the VDI

A virtual disk image is easily resizable1. First, I fired up a cmd window and ran the following (i.e. we want to expand Fedora31.vdi to 100GB).

VBoxManage modifyhd Fedora31.vdi --resize 100000

When the output is like:

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

…we can be assured that the task is done.

Expanding the Physical Partition

I created a new VM2 in VBox. Then attached the virtual disk (i.e. Fedora31.vdi) and inserted a GParted ISO to optical drive before starting the VM. GParted booted up3. Here, I simply expanded the partition I needed to claim new space since no partitions were sitting between it and unallocated space. However, I only expanded it up to 25GB, leaving out the rest of gained 100GB unallocated on purpose.

Expanding the LVM Partition

Once I booted my Fedora VM, I found out though the disk space had increased, the LVM partition (i.e. fedora_localhost–live-root) had not expanded.

# lsblk
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                               8:0    0 97.7G  0 disk 
├─sda1                            8:1    0    1G  0 part /boot
└─sda2                            8:2    0 24.8G  0 part 
  ├─fedora_localhost--live-root 253:0    0 12.5G  0 lvm  /
  └─fedora_localhost--live-swap 253:1    0  1.5G  0 lvm  [SWAP]
sr0                              11:0    1 73.6M  0 rom

# fdisk -l /dev/sda2
Disk /dev/mapper/fedora_localhost--live-root: 12.51 GiB, 13417578496 bytes, 26206208 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

Fortunately enough, without much trouble, a couple web searches later I found out what4 I was looking for.

Afterwards, I ran following in order to expand the LVM partition (i.e. fedora_localhost–live-root) to take up maximum space available for it inside the physical partition.

# lvextend -r -l +100%FREE /dev/mapper/fedora_localhost--live-root

Which gave following as the output.

  Size of logical volume fedora_localhost-live/root changed from <12.50 GiB (3199 extents) to <23.24 GiB (5949 extents).
  Logical volume fedora_localhost-live/root successfully resized.
resize2fs 1.45.3 (14-Jul-2019)
Filesystem at /dev/mapper/fedora_localhost--live-root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/fedora_localhost--live-root is now 6091776 (4k) blocks long.

Now, that the task is already done, the final state of things is as follows.

# lsblk
NAME                            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                               8:0    0 97.7G  0 disk 
├─sda1                            8:1    0    1G  0 part /boot
└─sda2                            8:2    0 24.8G  0 part 
  ├─fedora_localhost--live-root 253:0    0 23.2G  0 lvm  /
  └─fedora_localhost--live-swap 253:1    0  1.5G  0 lvm  [SWAP]
sr0                              11:0    1 73.6M  0 rom

# fdisk -l /dev/sda2
Disk /dev/sda2: 24.76 GiB, 26565672960 bytes, 51886080 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. Expandable only, cannot shrink using VirtualBox VBoxManage utility as of version 6.0.16r135674. Shout out to Derek Molly↩︎

  2. In the creation wizard, Type: Linux, Version: Linux. ↩︎

  3. If it does not for you, change boot device order in VM settings under System. Also note there are other ways to accomplish the same. ↩︎

  4. https://ask.fedoraproject.org/t/how-to-add-unallocated-disk-space-to-lvm-home/3746 ↩︎

VirtualBoxFedoraLinux

Face Detection in an Image with Computer Vision

Bypassing Auth to Router Config Page in a Tozed M60