NMT CSE Wiki

Dept. of Computer Science & Engineering

User Tools

Site Tools


commons:resize_vdi

# Resizing VirtualBox

It is possible for the virtual disk image (VDI) that VirtualBox uses as your “hard drive” to run out of space sooner than you'd expect. When this happens, you need to either start deleting files, or resize your virtual disk.

This involves changing your partition table in linux, which can lead to irreparable data loss if done incorrectly! So if you are not sure what is going on, consider seeing asking a sysadmin for help rather than doing this on your own!

## Finding your disk image

To find the file virtualbox is using for your hard disk, go to Settings → Storage for your virtual machine. You should see a window that looks like this:

If you click on the disk at the bottom of your storage tree (attached to the VM's SATA controller), you will see the full path to your disk appear in the right pane. You can select that text and copy/paste it into other windows.

## Converting VMDK to VDI

Depending on your setup, your virtual disk is either in VDI or VMDK format. The main difference is that a VDI file can be resized dynamically, while a VMDK cannot. So if the extension is .vmdk, then you'll need to convert it first. If the extension is already .vdi, you can skip this step.

VirtualBox comes with a command line tool called VBoxManage, which you can find in different locations on OS X or Windows.

### Windows

Open Windows PowerShell (or cmd.exe, if PowerShell is not available), and cd to the directory containing your VMDK. You can copy and paste the location from the Storage pane above, but you may need to wrap the path in quotes. For example, if your path says: C:\Users\My Name\Path To\VirtualBox\VMs\Original Ubuntu.vmdk, then the commands you use should be:

  > cd "C:\Users\My Name\Path To\VirtualBox\VMs"
  > "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" clonehd --format VDI "Original Ubuntu.vmdk" "New Ubuntu.vdi"
  

The second command may take a few minutes to finish. This creates an exact duplicate of your disk, but in a new format.

### OS X

In OS X, open a Terminal. If the path to your virtual disk was /Users/myname/Path To/VirtualBox/VMs/Original Ubuntu.vmdk, then you can use a command similar to the one above:

  $ cd ~/"Path To/VirtualBox/VMs"
  $ /Applications/VirtualBox.app/Contents/Resources/VirtualBoxVM.app/Contents/MacOS/VBoxManage clonehd --format VDI "Original Ubuntu.vmdk" "New Ubuntu.vdi"
  

The command is exactly the same as on Windows, the files are just in different locations

## Resizing the VDI

Now you have the path to New Ubuntu.vdi. The command to resize it will be VBoxManage modifyhd –resize [new size in MB] [vdi file].

### Windows

  > cd "C:\Users\My Name\Path To\VirtualBox\VMs"
  > "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd --resize 20480 "New Ubuntu.vdi"

### OS X

  $ cd ~/"Path To/VirtualBox/VMs"
  $ /Applications/VirtualBox.app/Contents/Resources/VirtualBoxVM.app/Contents/MacOS/VBoxManage modifyhd --resize 20480 "New Ubuntu.vdi"
  

The size we're using here is 20480 MBs, which is 20GB. You can adjust this to whatever will work for you. You may have to close and reopen VirtualBox for it to notice the change in size.

## Changing the Storage

If you didn't have to convert from VMDK → VDI, then you can also skip this step. If you did, then you need to tell VirtualBox to boot from the new image instead of the old one.

Go back to Settings → Storage and click on the VMDK file in your storage tree. Detach the disk by clicking the “Remove Attachment” button, shown below:

You can then click the button on the right to add a disk to the SATA controller, then navigate to New Ubuntu.vdi and add it as your new disk.

## Resizing the Partition

Now you have a virtual disk image with a larger size, but the partition table in Ubuntu is still the same size as ever. The operating system doesn't know it has the extra space yet. If you boot your VM into Ubuntu and open up a terminal, then you can fix the problem relatively simply.

WARNING: Doing this incorrectly can result in lost data! If you're not sure what you're doing, then don't do it!

If you'd like to resize your the partitions on your own, this is how you can do that:

  $ sudo parted /dev/sda
  

opens an interactive tool that lets you edit your partition table. Type print into the prompt to see a summary of your partitions. If you see this warning:

  Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to use all of the space (an extra 26318040 blocks) or continue with the current setting?
  Fix/Ignore?

Then type Fix before hitting enter. Your output should look like this:

  (parted) print
  Model: ATA VBOX HARDDISK (scsi)
  Disk /dev/sda: 21.5GB
  Sector size (logical/physical): 512B/512B
  Partition Table: gpt
  Disk Flags:
  
  Number  Start   End     Size    File system  Name                 Flags
   1      1049kB  2097kB  1049kB               BIOS boot partition  bios_grub
   2      2097kB  212MB   210MB   fat16        EFI System           boot, esp
   3      212MB   8000MB  7788MB  ext4         Linux filesystem
  

Notice that the size of the disk is 21.5GB, but the final partition ends at 8000MB. This is the problem we're here to solve! In this example, partition 3 is the root partition that needs to be resized. If you aren't sure which partition is the root, you can open another shell and run this command:

  $ df -h
  Filesystem      Size  Used Avail Use% Mounted on
  ...
  /dev/sda3       7.1G  7.1G     0 100% /
  ...

This tells you that the third partition, /dev/sda3, is mounted as the root, /.

WARNING: The following operation cannot be easily undone! The reason this is expected to work perfectly is because the partition /dev/sda3 is at the END of the disk. There are no partitions after it in the partition table. In that case, resizing it should be perfectly safe. Otherwise, don't follow this next step!

If everything looks good, go back to parted and run this command:

  (parted) resizepart 3
  Warning: Partition /dev/sda3 is being used. Are you sure you want to continue?
  Yes/No? Yes
  End?  [8000MB]? 100%

The warning should be safe to ignore, as long as you are growing and not shrinking the partition. Telling the partition to end at 100% simply tells it to resize to the end of the disk. To exit parted, type quit.

## Extended SWAP Space

This section covers an alternative, slightly more complicated case, in case the previous example did not apply to you.

Some people will have a swap partition after their root partition. A swap space is like additional RAM stored on the hard disk (or virtual disk, in this case). If your partition table looked something like this:

  (parted) print   
  Number  Start   End     Size    Type      File system  Flags
   1      1049kB  8000MB  7999MB  primary   ext4
   2      8000MB  10.0GB  2025MB  extended
   5      8000MB  10.0GB  2025MB  logical   linux-swap

Then it is possible for you to resize your root partition by first deleting the swap space.

WARNING: This assumes that the linux-swap is the only thing that comes after the root partition. Keep in mind the partition numbers - if yours are different then what is above, do not just copy paste. Never delete a partition unless you are sure you won't miss it!

Before deleting the swap space, you need to open another shell and unmount it with this command:

  $ sudo swapoff -a
  

You should also edit your /etc/fstab:

  $ sudo nano /etc/fstab
  

Just comment out the line that says swap in the <type> column. Then go back to parted, and delete the partitions:

  (parted) rm 5    # Remove the linux-swap partition
  (parted) rm 2    # Remove the extended partition

Now you can resize your root partition:

  (parted) resizepart
  Warning: Partition /dev/sda3 is being used. Are you sure you want to continue?
  Yes/No? Yes
  Partition number? 1     # The partition number for /
  End?  [8000MB]? 18GB    # Leave room for the new swap space

Then replace the partitions you deleted:

  (parted) mkpart
  Partition type?  primary/extended? extended
  Start? 18GB             # Start at the end of the last partition
  End? 100%
  (parted) mkpart
  Partition type?  primary/logical? logical
  File system type?  [ext2]? linux-swap
  Start? 18GB
  End? 100%

If everything went well, then your new partition table should look like this:

  (parted) print
  Model: ATA VBOX HARDDISK (scsi)
  Disk /dev/sda: 21.5GB
  Sector size (logical/physical): 512B/512B
  Partition Table: gpt
  Disk Flags:
  
  Number  Start   End     Size    Type      File system  Flags
   1      1049kB  18.0GB  18.0GB  primary   ext4
   2      18.0GB  21.5GB  3474MB  extended
   5      8000MB  21.5GB  3474GB  logical   linux-swap

### Recreate SWAP

Before moving on to resizing the filesystem, you can take a moment to fix your swap space:

  $ sudo mkswap /dev/sda5
  Setting up swapspace version 1, size = 3.2 GiB (3472879616 bytes)
  no label, UUID=0880b61e-33ed-43ca-8d8c-b6911e57a84d
  

Now copy the UUID=0880b61e… line, and open /etc/fstab in a text editor:

  $ sudo nano /etc/fstab

Find the line that specifies swap in the <type> column. Delete the first word in that line, up to the first space (it will either say /dev/sda5 or UUID=…). Replace that word with the UUID=… that you got from mkswap, then save the file. Now your system is configured to use the new swap space when you boot up.

## Resizing the filesystem

Finally, you can resize the filesystem to take advantage of the new partition size with this command:

  $ sudo resize2fs /dev/sda3
  The filesystem on /dev/sda3 is now 5191163 (4k) blocks long.

After that, everything should work perfectly. You can use df again to confirm you have more disk space:

  $ df -h
  Filesystem      Size  Used Avail Use% Mounted on
  ...
  /dev/sda3        20G  7.1G   13G  35% /
  ...

This shows the filesystem now has 13GBs available. Success!

commons/resize_vdi.txt · Last modified: 2019/10/16 16:45 (external edit)