I have an installation of Ubuntu 11.04 running as a virtual machine under VirtualBox. It recently ran out of space, so I needed to expand the partition.
First I made a new partition that was larger than my existing partition (i.e. I created a new disc image in VirtualBox).
I then added the Ubuntu 11.04 DVD image to my VM as a new DVD device. I also needed to remove the VBoxGuestAdditions.iso, as it turns out that the VirtualBox bios only lets you boot off the first CD/DVD it finds.
That done, I fired up my VM and pressed F12 to get into the boot device menu, then chose to boot from CD. When this booted up I chose the option to run the live cd rather than install Ubuntu.
Once I hit the gnome2 desktop, I fired up a terminal and ran
$ sudo gparted
gparted showed two discs - sda and sdb. sda1 was my linux partition, and there was an extended partition containing a swap partition on sda5. sdb was empty as expected. I performed the following actions:
- select and copy the sda1 partition.
- switch to sdb and create a partition table.
- paste the sda1 partition into sdb, and change its size so it fills the entire disc minus 500MB or whatever you want for the swap partition.
- create an extended partition on sdb
- create a new swap partition inside the extended partition.
- I set the partition flag bootable on sdb1 although I don't think this was necessary.
Having done this I now needed to install grub. To get grub to work, I had to chroot into the new partition before installing it.
$ sudo mkdir /mnt/sdb1 $ sudo mount /dev/sdb1 /mnt/sdb1 -t ext2 $ sudo mount --bind /dev /mnt/sdb1/dev $ sudo mount --bind /proc /mnt/sdb1/proc $ sudo mount --bind /sys /mnt/sdb1/sys $ sudo chroot /mnt/sdb1 $ sudo grub-install $ sudo update-grub
At this stage I shutdown, and removed the old linux partition from my VM. When I started up again, it succesfully booted into my VM, from where I am writing this article!