68

I am running Ubuntu as a guest on an iMac with VirtualBox version 4.1.8.

What is the easiest way to resize the virtual drive?

Please provide as much detail as possible including the correct format for any commands.

Bruno Pereira
  • 73,643
james
  • 681
  • I would do it piping tar -cf across disks, but I'm sure there's a better way to do it. – RobotHumans Feb 05 '12 at 21:42
  • I had the same question, running Ubuntu as a guest on a Windows 7 host. @Joni's answer was a great help, with the addition of http://askubuntu.com/questions/154921/how-do-i-get-ubuntu-12-04-to-recognize-swap-partition-so-that-i-can-hibernate to get the swap working again. – Luke Mills Oct 22 '12 at 11:02

2 Answers2

97

This answer is directed at a Windows host, but if you use bash in place of the PowerShell and replace '\' with '/' it should work just fine.

Enlarge virtual drive

  1. From VirtualBox
    1. Release the VDI file: File -> Virtual Media Manager -> Select VDI -> Release
    2. Copy the location of the VDI inside the properties box 'C:\Users\campbell\VirtualBox VMs\Ubuntu14\Ubuntu14.vdi'
    3. Backup the VDI file
      1. Copy the VDI file to a new location.
      2. Assign a new UUID to the original VDI file:
        1. Start Powershell (not as an administrator):
        2. Change to your Oracle VirtualBox directory cd C:\Program Files\Oracle\VirtualBox
        3. .\VBoxManage.exe internalcommands sethduuid "C:\Users\campbell\VirtualBox VMs\Ubuntu14\Ubuntu14.vdi"
    4. Remove and re-add your machine's .vdi file to update its UUID.
      1. File -> Virtual Media Manager -> Select VDI -> Remove
      2. Apply.
      3. Right click your VM -> Configuration -> Storage -> Controller: SATA -> Add new hard drive. Select your .vdi file.
  2. From host
    1. Work out desired size: you can google it, eg. '40 Gb=MB' returns 40000 MB
    2. Start PowerShell (not as administrator)
    3. Change to your Oracle VirtualBox directory cd C:\Program Files\Oracle\VirtualBox
    4. Resize your .vdi file .\VBoxManage.exe modifyhd "C:\Users\campbell\VirtualBox VMs\Ubuntu14\Ubuntu14.vdi" --resize 40000
    5. Now start your virtual machine. You will receive the same warning about space that prompted you to engage in this procedure. Not to worry, we are near the end.
  3. On your virtual machine
    1. Start the partition manager gparted (install it if is is missing sudo apt-get install gparted)
    2. Get rid of the swap partition, which prevents you from expanding the root partition. Note that you cannot harm the rest of your machine - this is all happening inside a single file. Worst case scenario you trash this file and you have to use your backup instead.
      1. Make a note of the size of the linux-swap partition 4 GiB in my case
      2. Right click on it and Swapoff
      3. Right click on it and Delete
      4. Apply by clicking on the checkmark (Apply all operations). Ignore the dire warning - life is too short to indulge Cassandras
      5. right click on the extended file system that once housed the swap partition (/dev/sda2 in all likelihood) and delete it
      6. right click on the root partition (/dev/sda1) and resize it. Tab to the 'Free space following' field and enter the size of the swap partition. Shift-Tab and the machine will work out the new size for you automatically.
      7. Right click in the unallocated space at the end and make it an extended partition
      8. Right click in the new partition and select linux-swap in the File system field.
      9. Commit your changes as before
      10. Right click on your swap partition and select swapon
      11. Tell the Fat Lady to commence singing.

References:

  1. https://tinyapps.org/blog/misc/201204120700_virtualbox_increase_disk_space.html
  2. Resize Ubuntu 10.04 VirtualBox VM virtual disk
Cam Cairns
  • 1,071
  • Worked, with a few extra steps (1.1) I had to remove the VDI and then add it back to the virtual machine after giving it a new uuid and resizing it. (1.5) I had to cd into c:\Program Files\Oracle\VirtualBox http://stackoverflow.com/questions/15074878/virtual-box-uuid-07c3-does-not-match-the-value-2c1b-stored-in-th – WiredIn Aug 31 '16 at 15:46
  • 4
    This one is freaking awesome answer !!! – prayagupa Sep 19 '16 at 21:30
  • Very good description but you can shorten it to: ".\VBoxManage modifyhd myUbuntu14.vdi --resize 40000" followed by the steps under point "3. On your virtual machine". Nevertheless I gave you an upvote for your good answer – Anonymous Mar 27 '17 at 08:04
  • Very good answer. You've saved my day. – elvisrusu Dec 04 '17 at 11:55
  • Note that you may have to activate swap and edit /etc/fstab after doing this https://askubuntu.com/a/819689/100362 – copper.hat Jan 12 '18 at 21:20
  • Your answer is awesome! I just spent last hour to read lots of posts but couldn't fix this issue, and now I have done it with your step based instructions. Thank you. – Tien Do Jul 13 '18 at 08:51
  • Worked great for me, and I didn't have to re-install my debian guest! – Bogatyr Aug 20 '19 at 09:29
  • This looks like a great answer but on #1 of 3 I cannot install gparted because No space left on device which is the original problem using me to find his issue. – demongolem Jan 31 '20 at 20:13
  • I didn't have a swap partition to start with, but the rest worked fine! Edit: I used Ubuntu on top of a Windows Host with Hyper-V Paravirtualization. – Param Siddharth Jun 17 '21 at 07:41
47

If you are making the disk bigger, you would

  1. first enlarge the disk from VirtualBox, and then
  2. enlarge the partition, and
  3. the filesystem it contains.

To enlarge a disk you can use the VBoxManage modifyhd command. Suppose you want to resize the disk to 20,000 megabytes (~20GB). First locate the disk file that you want to expand. Then, in terminal, give this command to resize the disk:

VBoxManage modifyhd "path-of-disk-file" --resize 20000

To enlarge the partition and file system, probably it's easiest to boot the virtual machine using a Ubuntu livecd and do the job with GParted, as it does both at the same time and gives you a graphical user interface for it.

Attach a livecd ISO image to the virtual machine and change the boot order to first boot from CD. If you don't have a Ubuntu livecd at hand you can use any livecd that comes with the appropriate tools. SliTAZ for example is only 35MB to download. Open GParted and choose the disk you want to resize. Then right-click on the partition that you want to expand and choose the option "resize-move." In the dialog box that opens, in the graphic that represents the partition, drag the triangle at the end of the partition all the way to the right to maximize it. Then close the dialog and choose "Apply" on the toolbar. Since no data has to be moved this should be a quick operation.

When done, don't forget to detach the livecd from the virtual machine and change the boot order.

You'll find a pretty good tutorial of the whole process with screenshots included here: http://trivialproof.blogspot.com/2011/01/resizing-virtualbox-virtual-hard-disk.html

Joni
  • 2,527
  • 2
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. While some info is given here, a bit more info would be good, in case the blog you linked to is down. – nanofarad Jul 18 '12 at 17:55
  • What information would you add? – Joni Jul 19 '12 at 13:20
  • 1
    I recommend the main steps from the instructions so that if the blog were taken down, anyone viewing this answer would be able to fix their issue. This helps avoid linkrot – nanofarad Jul 19 '12 at 13:21
  • @ObsessiveFOSS Better now? – Joni Jul 22 '12 at 12:50
  • +1 It looks much better now. – nanofarad Jul 22 '12 at 17:13
  • I used these instructions to resize my virtual disk. During the process I had to delete the swap partition to enable me to expand the root partition. I found the instructions relating to http://askubuntu.com/questions/154921/how-do-i-get-ubuntu-12-04-to-recognize-swap-partition-so-that-i-can-hibernate helpful to get swap working again. – Luke Mills Oct 22 '12 at 11:04
  • 1
    Realised the previous link to get swap working wasn't exactly what I wanted. It was for making swap work with hibernation. The answer to this question is more what I was looking for: http://askubuntu.com/questions/180730/how-do-i-restore-a-swap-partition-i-accidentally-deleted. – Luke Mills Oct 22 '12 at 12:06
  • 1
    I had to remove the *.vdi images from Virtual Box (v4.1.12) before VBoxManage modifyhd would work without spitting a fatal error at me. Use VirtualBox's File > Virtual Media Manager to bring up a dialog where you can remove the association. – RobM Nov 25 '12 at 00:43
  • i vote up your answer but i recommend to install gparted in the ubuntu guest vm and with sudo just resize the disk (and delete and create at the end the swap partition), take only 2 minutes including the apt-get install :) ) – Enrique San Martín May 12 '15 at 15:34
  • I followed the blog instructions but I had swap partition. The resize/move didn't work so I had to resize the extended partition to also use unnaloccated size, then move swap partition all the way to the right, then shrink the extended partition back to what it was and finally I could use that unnaloccated size to the main partition. – Edson Horacio Junior Jan 13 '17 at 19:04