How to Resize a VirtualBox partition (example with Ubuntu LVM2 PV)

Initially when I created the VM I thought would be temporary. It turned out to be not so temporary.

I thought I could just get away with using GParted and resizing the blocks around, but it turned out to be more tricky than expected.

Preparation

BEFORE YOU DO ANYTHING,

MAKE A BACKUP OF YOUR VIRTUAL DRIVE!

  • Make sure the VM is off
  • Find the VDI file for the drive
  • Make a copy of it

 

image

For example sake, here's my 10gb guinea pig.

Expanding the size of the drive

In your host, you have to run some commands to increase the capacity of the drive before you can do anything with partitions.

Use a calculator to calculate how much space you want to allocate in MB.

For reference, 100GB = 1024mb * 100 = 102,400mb

In command prompt, run these commands:

  • cd %PROGRAMFILES%\VirtualBox\
  • VBoxmanage modifyhd "path\to\your.vdi" --resize 51,200

This will only increase the capacity of the file itself.

You can check it in the VirtualBox VM details page, but it's not actually accessible yet.

You still have more work to do.

image

Lo and behold, I am now 50. Oh wait I'm not, I just look it.

Create a new VM which uses LiveCD

I used a copy of LinuxMint and configured it for Live CD usage. This was purely because I already had the ISO on hand. If you don't, you can save yourself the wait and just grab a GParted LiveCD from their website.

  • CD/DVD drive: LinuxMint or GParted ISO file
  • HDD: Select the VDI drive you want to resize

Using GParted

  • Once you're in GParted and can see the HDD, you'll notice there's a whole lot of free space now
  • If it's locked (there's an icon of a vault), right click and select "Deactivate"
  • Resize the partition (you may need to do this in multiple steps in order to get the right structure)
  • Apply changes
  • Shut down GParted VM

image

Remember to test the "resized" partition before continuing!

  • Load up your normal VM
  • Test that it boots and operates as normal
  • Check the amount of space remaining with df -kh

image

Extra steps for LVM partitions

If you're not seeing the space show up in df results, to make that space accessible to the partition you'll need to run two more commands.

The MOUNTPOINT for my VM was "/dev/mapper/ubuntu--vg-root"

  • sudo lvextend -l +100%FREE MOUNTPOINT
  • sudo resize2fs MOUNTPOINT

First command expands the LVM while the second resizes the filesystem.

coxGG

Now, are you sure that you don't have too much space?

Sources

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog