Resize Virtualbox Ubuntu LVM Storage in MacOS

I've seen examples of how to accomplish this in Windows. The syntax is slightly different on MacOS. I also find the most straightforward way to do this is a little bit scary for those unfamiliar with Linux partitions. This should work on any LVM based *nix VM; I just happened to use it on my Ubuntu image.

Let's jump in.

  1. Shutdown your VM
  2. Detach existing Storage
    Screen-Shot-2023-01-03-at-9.35.26-AM
    In your VMs settings for Storage select the SATA attached disk and click the 'remove' icon below.
  3. Find the location of the storage and run VBoxManage to resize, below resizes the disk image to 25GB:
VBoxManage modifymedium disk Ubuntu.vdi --resize 25000
  1. Re-attach the drive to the VM, same approach as detaching except when under the SATA controller click 'Add Storage.' The window that appears should already have your drive image listed.
  2. Start your VM
  3. Log in to your VM and become root.
  4. Resize the physical partition. This part can look scary. As with any storage operation you should have a backup of your data. We're going to delete the partition the LV lives on and the re-create it starting at the same location on the drive but with a last sector that uses all the space on our new larger drive. This only works because the LVM lives on the last physical partition. In the example below my drive is /dev/sda and the LVM lives on sda3:
fdisk /dev/sda

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (40959999 != 51199999) will be corrected by write.
The backup GPT table is not on the end of the device. This problem will be corrected by write.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p

Disk /dev/sda: 24.41 GiB, 26214400000 bytes, 51200000 sectors
Disk model: VBOX HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 1934161A-4DAE-4D24-9EAA-A28FC324B93F

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  3674111  3670016  1.8G Linux filesystem
/dev/sda3  3674112 40959966 37285855 17.8G Linux filesystem

Command (m for help): d
Partition number (1-3, default 3): 

Partition 3 has been deleted.

Command (m for help): n
Partition number (3-128, default 3): 
First sector (3674112-51199966, default 3674112): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (3674112-51199966, default 51199966): 

Created a new partition 3 of type 'Linux filesystem' and of size 22.7 GiB.
Partition #3 contains a LVM2_member signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): w

The partition table has been altered.
Syncing disks.
  1. Reboot your VM
  2. Log back in to your VM and become root
  3. Reized the LVM Physical Volume:
# pvresize /dev/sda3
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
  1. Grow the Logical Volume. My LVM is at /dev/mapper/ubuntu--vg-ubuntu--lv, change if yours is different:
# lvextend -L +10G /dev/mapper/ubuntu--vg-ubuntu--lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <17.78 GiB (4551 extents) to <19.78 GiB (5063 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

Depending on how much your increased the size of your drive you'll need to change the +10G parameter. You can always re-run the command with smaller sizes to keep adding a little more space until all extents are consumed.
12. Resize the filesystem. Again, my LVM is at /dev/mapper/ubuntu--vg-ubuntu--lv:

# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 3
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 5940224 (4k) blocks long.

Enjoy your extra space!

# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              393M  756K  392M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   23G   17G  4.5G  79% /
tmpfs                              2.0G     0  2.0G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          1.7G  127M  1.5G   8% /boot
tmpfs                              393M  4.0K  393M   1% /run/user/1000