Programster's Blog

Tutorials focusing on Linux, programming, and open-source

VirtualBox - Expand Disk Image

Navigate to the folder containing your .vdi disk image file.

Run the following command to expand it:

VBoxManage modifyhd disk1.vdi --resize $SIZE_IN_MB

Then boot up the guest and open a terminal.

Enter the interactive fdisk tool for our disk.

sudo fdisk /dev/sda

Print out the partition table if you like. This can be useful.

p

Now we are going to delete all the existing partitions before recreating the root one (don't worry this works).

For each partition, delete it by pressing:

d

... and then entering the partition number. Repeat until all partitions are gone.

1

Create the root partition with:

n

Make it a primary partion

p

Accept the defaults if you wish for the root partition to fill the entire disk. If you get asked whether you wish to delete a signature, I did when testing and it still worked.

Make the partition bootable:

a

Write changes and quit:

w

If you feel you made a mistake and want to quit out without saving changes, use q.

Tell the system to perform a filesystem check next boot:

sudo touch /forcefsck

If you deleted a swap partition (or any other partitions that auto mount), make sure to remove them from /etc/fstab.

sudo vim /etc/fstab

If you accidentally forget this step, don't panic, you will just have to wait a loooong time for the VM to boot up.

Now reboot for the changes to take effect.

sudo reboot

Don't panic if it takes a little longer for your guest to boot up than normal.

Once your guest has rebooted and you have logged in again, resize the filesystem to fill the partition:

sudo resize2fs /dev/sda1

References

Last updated: 11th August 2022
First published: 22nd December 2019

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch