Thursday, July 15, 2010

Resizing my VirtualBox GNU/Linux disk

I recently faced a space crunch on my Ubuntu installation running in VirtualBox when I was trying to setup a build environment, and was looking for a way to get an increased virtual disk capacity without affecting my existing installation.

I wanted more disk space without having to re-install or re-do my partially setup environment within Ubuntu in any way.

With some searching and a little effor managed to get this done, here's how.

I had used the Ubuntu ISO image (whihc I had downloaded earlier from http://www.ubuntu.com) for doing this but you could alternatively use the GParted ISO

1. My first step was to create a bigger hard disk image in VirtualBox with a capacity that could fit my requirements.
This was done with the Virtual Media Manager (File > Virtual Media Manager) within the "Hard Disks" tab and using the "New" wizard.

2. The next step was to attach this new Hard Disk as the "IDE Primary Slave" (the earlier Hard Disk image is 'IDE Primary Master').
To do this go to the "Settings" window of your Ubuntu Virtual Machine (VM) and select the "Storage" from the right side menu. This display a "Storage Tree" on the left, where you can add an virtual storage device under "IDE Controller"

3. I then added the LiveCD ISO to the Virtual Ubuntu installation the same way (seen in the above pic as well).
Then modified the boot order of my Virtual Ubuntu installation to boot from CD (the ISO) first.


4. Now powered on the Ubuntu VirtualMachine and used the LiveCD option (i.e. "Try Ubuntu without any change to your computer") from the menu that appeared.

5. Once this was up and I was logged into the Ubuntu Live CD. I launched the "GNU Partition Editor" (GParted) from System > Administration > GParted, as shown


6. Within the GParted window the first device displayed device was /dev/sda (the Primary Master). As I had all my data in the /dev/sda1 partition I did a right click within this and chose "Copy",


7. Then selected the bigger capacity disk (/dev/sdb) from the drop down on the right top corner).
This was an empty partition (hence appeared blank) and I had to create a partition table first ('Device > Create Partition Table' in GParted).
Once this was done I right clicked and selected the "Paste"option in the new partition.
Here I had to drag the right edge of the partition to the desired size of my new disk (you could refer to the images here showing this GParted step) and then click on the Apply button (the green tick symbol) to start the image copy.

8. Once this was completed (could take a while depending on your disk size), I made the new partition bootable (right click and tick the 'boot' option in Manage Flags).
I also created another Extended partition and created a linux swap space (as a logical drive) in this. YMMV here.
My new disk looked something like this in GParted,


9. With this done. I powered down the VM, removed the old disk (Primary Master) and made the new disk the Primary Master.

10. Then again booted from the Live CD for the final step of (re-)installing the grub2 boot loader.

11. Once I was logged in to the Ubuntu Live CD, I mounted the new disk partition (all this from the Terminal prompt)

sudo mount /dev/sda1
Which should mount it to a location like /media/xxxxxxxxxxx
and then installed grub2 on it
sudo grub-install --root-directory=/media/xxxxxxxxxxx /dev/sda

12. After this I powered down the VM and made the new Hard Disk my first boot option (you could also remove the ISO image if you no longer need it) and restarted it.

Once up I was using the new bigger capacity disk now and was able to proceed with my previous setup.

Note: Although I had done all of this within VirtualBox, I don't see why it couldn't apply to a real disk upgrade on a system running Ubuntu as well.

Hope you found this informative. Please do share if you found this useful or have any other info / tips related to this.