Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution resize ubuntu vm virtualbox disk

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution

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;

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 22 10 Oracle VM VirtualBox Manager

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
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%
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
0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%

Step 3: Attach the new VDI disk and the Ubuntu boot ISO image

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 32 03

Step 4: Boot the VM

Boot the VM and press F12 in the boot screen. Boot from CDROM

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 32 44

Click Try Ubuntu

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 34 03 try ubuntu

Step 5: Configure the disks with GParted

Start GParted

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 36 12 gparted

Deactivate the disk

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 37 21 deactivate

Resize the extended partition to fill all available space.

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 38 09 resize the extended partition

Resize the partition itself to fill all available space.

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 38 58 resize the disk

Finally check the disk.

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 39 39 check the disk

Click the green V icon: Apply All Operations

Step 6: Make the assigned space available

sudo pvresize /dev/sda5Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 47 05 pvresize

sudo lvresize -l +100%FREE /dev/mapper/ubuntu–vg-root

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 48 28 lvresize
sudo e2fsck -f /dev/mapper/ubuntu–vg-root
sudo resize2fs /dev/mapper/ubuntu–vg-root

Start the machine and check the result

Ubuntu VM in VirtualBox: How to increase the size of a disk and make small(er) exports for distribution 2017 01 30 14 54 41 result

Distribution

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.

Reboot

As root: telinit 1
zerofree /dev/mapper/ubuntu–vg-root
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.

Have fun!

26 Comments

  1. Stephen August 17, 2020
  2. henrique amaral March 25, 2020
  3. Hans September 3, 2019
  4. l November 5, 2018
    • Amit January 12, 2019
      • Jigar January 17, 2019
      • Dani January 30, 2020
    • Ahesanali Suthar April 9, 2019
    • Lochan January 9, 2020
    • Roy May 3, 2020
  5. Christopher Kelly September 20, 2018
  6. Onyx July 23, 2018
    • Lochan January 9, 2020
  7. Uma Maheswar July 19, 2018
  8. jaytho July 11, 2018
  9. meh November 28, 2017
  10. Carlos Flores November 1, 2017
  11. Paul October 24, 2017
  12. Marta October 3, 2017
  13. Tobias September 27, 2017
  14. Susan C September 5, 2017
  15. Frank Arico September 4, 2017
    • Ravil January 15, 2018
  16. David July 16, 2017
    • Maarten Smeets July 17, 2017
      • Kjell Inge August 29, 2017