Friday, April 29, 2011

Increasing disk size for virtualbox running Ubuntu

Yes, I am running Ubuntu in a VirtualBox, and have been refusing to upgrade to version 10.10 or even version 11, since I have no compelling reasons to do so (actually, I have good reason not to do so). However, end of last week, I hit to an issue with MySQL - a limitation that I will write in another blog entry.

Anyway, I did run out of disk space on my virtualbox guest. Came across a few web entries that discussed on how to overcome it, but most of them are applicable to Windows guest virtualbox setup.

Here are some of my attempts that didn't work out that well, and possible reasons:

1. Used Clonezilla

Strategy
Use Clonezilla to back up the old VM and restore it into the new expanded disk.

Outcome
Yup, I boot up my old version of Ubuntu using Clonezilla LiveCD and make a clone copy. Then boot up my new empty virtual image using Clonezilla LiveCD and tried to restore the image to the expanded disk image.

Reason
I was able to backup image of old Ubuntu VM, and managed to restore the image to the expanded VM volume, but when I boot up, it was not able to start up. This is due to GRUB loader. During the restoration of image, I briefly saw a message that there could be an issue since they detected GRUB and was not able to change to root to change some settings... I guess.

Result
Failed (you might have better luck than me)

2. Use GPart 

Strategy

Create an empty (larger diskspace disk) virtual disk using VirtualBox Virtual Media Manager. Attached the larger virtual disk to the SATA port 1 (the old smaller virtual disk on SATA port 0). Mount GPart LiveCD  and boot from it. Copy the old disk (/sda1) to the new disk (/sdb). Mark the new disk as bootable. Reboot with the new disk as primary (might also want to unmount the old disk).

Outcome
No problem with the copy and paste using GPart. No problem with the flag "boot" for the new disk. Just didn't boot after when switch to primary boot disk!


Reason
Unknown. Still figuring it out.



Here is what worked for me.

Background

I have the following:

1. Oracle VirtualBox 3.2.12 r68302
2. Ubuntu 9.10.2 (8 GB HD)

What I want to do is to add more hard disk space (increase to 16 GB HD).

Steps

1. Create a virtual disk of larger size
  •  Through Oracle VirtualBox GUI, select File > Virtual Media Manager > Actions > New
  • Fill up the 'Create New Virtual Disk' form
2. Open a command prompt and do:

C:\Documents and Settings\user\.VirtualBox\HardDisks>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" clonehd "MyFirstSmallerDisk.vdi" "MyLargerSecondDisk.vdi" --existing

Oracle VM VirtualBox Command Line Management Interface Version 3.2.12
(C) 2005-2010 Oracle Corporation
All rights reserved.

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 2fe48578-ade7-48fa-873b-2245714074f5



(some of the information are scrumbled and replaced, especially those in blue. Replace the name of the vdi files appropriately)

This will clone the smaller virtual disk to the larger one.

3. Create a new VM through the Oracle VirtualBox GUI, select Machine > New
  • Enter a new name for this VM (e.g. "MyLargerSecondVM")
  • Choose Linux, Ubuntu for OS Type
  • Enter the memory size
  • Check "Boot Hard Disk" and select "Use existing hard disk" > "MyLargerSecondDisk.vdi"
4. Reboot the new VM ("MyLargerSecondVM") to see that things are still bootable and in order, but do note that the primary partition is still the same and has not been modified. The extra disk space has not been allocated yet.

5. Shutdown the VM after everything looks fine. Back up the VMs.
  • I copied all the files under "C:\Documents and Settings\user\.VirtualBox" to another secondary storage.

It is now time to resize the partition to fill up the entire new virtual disk.

6. Ensure that the boot order for the new VM ("MyLargerSecondVM") is (CD/DVD-ROM, Floppy, Hard disk) via Setting > System > Motherboard

7. Ensure that the GPart LiveCD is mounted. Setting > Storage > IDE Controller > CD/DVD Device > [select the gpart iso file, e.g. gparted-live-0.8.0-5.iso]

8. Reboot the VM ("MyLargerSecondVM")

9. Enlarge the primary boot partition. You might need to move the swap partition to the end of the disk before enlarging the primary boot partition. I did the changes in a series of small steps rather than applying all changes at one go. I actually hit to a slight problem initially if I will to consolidate all my steps.

10. Once the swap partition is moved to the end, and primary partition has been enlarged successfully, quit GPart and reboot. Remember to unmount the GPart LiveCD through Setting > Storage > IDE Controller > CD/DVD Device > [Select "Empty"]

11. Reboot "MyLargerSecondVM" and login. You can check that you have more disk space now. e.g.

% cd /
% df -k .

Good luck.

7 comments:

Anonymous said...

You have saved my day sir!! I cannot thank you enough...

PCash said...

This helped me from redoing a ton of work. Thanks.

' said...

There is no "New" option on the Actions menu in VirtualBox 4.1.6

Paul Ho said...

Yes, there might not be a 'New' option in VirtualBox 4.1.6. The instructions above only applies to Oracle VirtualBox 3.2.12 r68302 as indicated in the background section.

Anonymous said...

Thanks a lot, I used your instructions on VB 4.1.12. The only correction is about how to add a new hard drive. It can be now made from Machine/Properties/Disks

Ariel Rocholl said...

Everything worked as described here, except that I couldn't "move" the swap partition, I actually had to copy it over, then delete the old one. Still the outcome was success.

Unknown said...

this worked for me with vbox 4.1.18. but i did find a shortcut. instead of creating a new virtual drive in a new vbox, just backup your vbox, and then resize your current drive with the following command:
vboxmanage modifyhd mysmallhdd.vdi --resize 50000
(this command needs to be run in the same folder as your vbox drive, and you will need to add the path to vboxmanage)
this will resize your current drive to 50GB. then boot from gparted image and move the swap to the end and resize main partition.