Sometimes I create a VirtualBox disk for usage in a VM with a certain size. I think it will be enough for what I want to use the VM for. After using the VM for a while, it happens (often) that I need more space than I initially assigned. For example if want to install additional software.
In this blog I describe the steps I’ve done to increase the size a VirtualBox Ubuntu guest disk without loosing data. I’ve done a pretty default Ubuntu 16.04 installation as guest in VirtualBox. If your partition layout differs, the steps to take are similar but might differ slightly. Do think before you execute these (or similar) steps since if you do not do them correctly, you might screw up your partitions and potentially lose data.
If you have created a nice VM and want to distribute it, it helps if the VM export is not too large. This makes uploading and copying the VM much easier. At the end of this blog post I give some tips on how to make an export of a VM small.
The below described method has caused problems for some people (see comments) so make sure you have a backup of your VM before starting.
Step by step
Step 1: Make sure you have a VDI disk image
If you imported an OVA file, you will end up with a VMDK disk image. First detach the image from the VM;
Clone the VMDK to a VDI file. This operation also has the benefit of creating a backup; the original VMDK file is still there. If you have already done this step before, you might have a registered VDI file with the same name inside your VirtualBox Media Manager. If that is the case, the clone operation will fail. First remove the VDI file from the VirtualBox Media Manager before proceeding. Below is the clone command for a VMDK file to a VDI file.
“c:\Program Files\Oracle\VirtualBox\VBoxManage.exe” clonemedium –format VDI kafkaworkshoposajava-disk1.vmdk kafkaworkshoposajava-disk1.vdi
Clone medium created in format ‘VDI’. UUID: cabc9b44-386d-41af-af70-6b8e73df5596
Step 2: Resize the VDI disk image
The below command resizes the VDI disk image to 20000Mb.
D:\VM\KafkaWorkshop>”c:\Program Files\Oracle\VirtualBox\VBoxManage.exe” modifymedium kafkaworkshoposajava-disk1.vdi –resize 20000
Step 3: Attach the new VDI disk and the Ubuntu boot ISO image
Step 4: Boot the VM
Boot the VM and press F12 in the boot screen. Boot from CDROM
Click Try Ubuntu
Step 5: Configure the disks with GParted
Deactivate the disk
Resize the extended partition to fill all available space.
Resize the partition itself to fill all available space.
Finally check the disk.
Click the green V icon: Apply All Operations
Step 6: Make the assigned space available
sudo lvresize -l +100%FREE /dev/mapper/ubuntu–vg-root
sudo e2fsck -f /dev/mapper/ubuntu–vg-root
sudo resize2fs /dev/mapper/ubuntu–vg-root
Start the machine and check the result
If you want to distribute an export of your VM and want to have it as small as possible, the following might help:
In the Ubuntu guest OS install and run BleachBit to remove temporary files. You can compare this tool with CCleaner for Windows.
sudo telinit 1
mount -o remount,ro /dev/sda1
zerofree -v /dev/sda1
Update /etc/fstab and set the options of /dev/mapper/ubuntu–vg-root to ro but remember the previous options! In my case before the change it was errors=remount-ro.
As root: telinit 1
mount -o remount,rw /dev/mapper/ubuntu-vg-root
Update fstab with vi and restore the previously set options. Restart. Now the free space has been zeroed out. On your host OS (Windows in my case) do the following:
“c:\Program Files\Oracle\VirtualBox\VBoxManage.exe” modifymedium –compact kafkaworkshoposajava-disk1.vdi
This makes sure if you export the VDI, the exported disks will be as small as they can get with their contents. You can make the export even smaller by using 7-zip to compress the result. If you want good compression, set the dictionary size and block size in 7-zip to large values.
26 thoughts on “Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution”
I needed to delete my snapshots first. You may need to do this too.
it worked for me, thanks
For me it did not work. In vbox I can see virtual size: 20 GB, actual size: 10 GB (as expected). In gpart I see no unallocated space at all (just the 10GB) so it stops there for me.
I can confirm that step 6 is unnecessary for my Ubuntu 18.04.
I can also confirm, step 6 not required for Ubuntu 18.04. Also in step 5 there was no deactivate disk option, so I just resized the disk and it worked like charm.
Thanks for this helpful blog.
for me booting stuck saying “Started GNOME Display Manager…. finishes up…ce….” for Ubuntu 18.04
Thanks worked for me!
I can also confirm that , step 6 not required for Ubuntu 18.04.
True for me as well.
I also didn’t use live CD on step 5. Instead, I apt installed gparted and ran it from the VM. No need even to reboot.
That was easy! Thank you.
This saved me! 🙂
This saved me as well.
My partitions looks same as described except /dev/sda5 file system is encrypted. I am having challenge in re-sizing the /dev/sda5/. I am able to do re-size of /dev/sda2 extended file system from 24.75GB to 97.18GB. However when i try to re-size /dev/sda5 to the same size of /dev/sda2 or any size(lower than sda2) i am getting error message. Following error message is displayed when i tried to re-size /dev/sda5 to the same size of /dev/sda2
“Could not add this operation to the list. A partition with used sectors (203798528) greater than its length (203796480) is not valid
works great. thanks so much. I had to re-activate the drive before you run the terminal steps, and customize the commands to match the volumes in /dev/mapper to your environment- ours was –vg-root
this is the only thing that worked for me on an ubuntu 16.04 server guest that I’d accidentally made too small. thanks! All the other tutorials neglected the part about e2fsck and resize2fs
I’d like to add:
Step 0> Create a backup of your Hard Disk file
Step 2.5> If you already have a DVI from start, detachit and reattach it in your VBox conf to successfully reload the available space
In step 5>
– there is no option of “deactivate” so, just skip it.
– Also, if you swap is in the middle, delete it, just leave around 1022Mib At the end of the new disk
– Re-create the swap partition at the end, just create a new extended partition at the end and inside create a new logic partition and in filesystem select swap.
Step 6 was totally unnecessary to me, just shutdown the virtual machine and start it again without the live CD image.
Boot normally the machine, might take a little more to load this new-first time (my guess: its re-creating swap)
After all that, the new VMachine was changed from 10Gb to 100Gb 🙂
Final Step: If everything works, delete the backup
A couple of notes from my experience of following this article:
Step 1: I skipped over this step the first time because I thought it was only if I had a VMDK image, but in my case, the VM had been created as a VDI file. When things weren’t working, I went back and read it more carefully. The key bit I’d missed was removing the disk from “File –> Virtual Media Manger”
Step 6: I think something about the website formatting might be causing some confusion here… When I typed the second command exactly as it appears in the website text, I got an error, same as Frank Arico reported (comment dated September 4, 2017 00:28). Checking more carefully, I realised “ubuntu–vg-root” had two “-” characters between “ubuntu” and “vg”. This can be seen in your screenshot, but the website text has converted it into a single em-dash character (I think that’s the right term?), so even copying and pasting doesn’t help! The same obviously applies to the rest of the commands in this step.
As for people complaining about the steps not working, breaking things and having to configure again from new, I’m afraid I only have a little sympathy. It’s a tough lesson, but one that should only need to be learnt the hard way once. Always have a backout plan before making a change and practice on something you don’t mind breaking first! 🙂
Everything worked perfectly!
Didn’t work for me either. Unfortunately I was left with an unusable system. Installing again 🙁
In order to move the swap out of the way to do the resize in step 5, I needed to use the instructions here: http://blog.mwpreston.net/2012/06/22/expanding-a-linux-disk-with-gparted-and-getting-swap-out-of-the-way/. Otherwise the resize was not allows as they were not adjacent.
It does not work for me either , fails at step 6. After step 5 I tried rebooting into ubuntu image I was trying to resize and then doing step 6.. Was that right? This is result
 sudo lvresize -l +100%FREE /dev/mapper/ubuntu-vg-root
skip_dev_dir: Couldn’t split up device name ubuntu-vg-root.
Path required for Logical Volume “ubuntu-vg-root”.
Please provide a volume group name
Run `lvresize –help’ for more information.
I had similar error too. It was because logical volume name was copied/pasted. Correct spelling is with 2 dash symbols betwee5 ubuntu and vg-root: ubuntu–vg-root.
Yes, as commented above, Step 6 is critical – without it, partition size is increased, but actual available size, displayed by disks tool, stays unchanged.
Thank you for this guide!
This does not work!!! Thanks for allowing me to have to configure a new VM and the 20 or so hours of pleasure….like I did not have anything better to do.
I’m sorry to hear something went wrong when you executed the described method. Thank you for taking the time to comment on the post. Could you be a bit more specific as to where it did not work for you? I have done the procedure several times in different VMs and have not encountered any issues. If a specific step fails, I would like to update the post to add an additional warning or check in order to help avoid your issue for other people in the future.
With kind regards,
I followed your description and it worked perfectly. Thank you for publishing! I would suggest to add a note about mounting the volume prior to step number 6, as this seems to be a prerequisite to execute the final steps.
Comments are closed.