Resizing the disk of a virtual machine

Posted on
software virtual machines vmware virtualbox disk resize partition

If you are like me and work a lot with virtual machines you will probably have the same problem and regularly run out of disk space in your virtual machines because the person that created it was too stingy regarding resources. In this post, I want to document how you can resize and extend your virtual machine disk, even if you do not have any paid versions of the virtualization software of your choice.

To extend the disk of a virtual machine there are usually two steps needed:

  1. Extend the hard disk file on your host system.
  2. Extend the partitions to use the newly added space within your vm guest system.

Note: My host system is usually a Linux machine and the guest system is a Windows machine. The basic approach is the same with other constellations, but the concrete commands and actions might differ. So please turn on your brain and at least make a backup before making experiments.

Extending the hard disk file on your host machine.

Depending on your virtualization software the approach is slightly different.

If you are using VMWare Player you can simply resize the disk file from within the graphical interface, just look for it in the menu.

If you are using VirtualBox, the approach is slightly more complicated but still manageable. First of all, check the format of your disk file. If it is a .vmdk you have to convert it first to a vdi file. You can do this with the following command:

> VboxManage clonemedium "source.vmdk" "cloned.vdi" --format vdi 

If it is a .vdi file, you can resize it directly. Use this command for it:

> VboxManage modifymedium disk "yourvirtualmachine.vdi" --resize=200000
// Whereas the resize parameter takes the new size in megabytes

Now you can either select the vdi in the settings of your virtual machine directly, or you can do an extra step and convert it back to vmdk with:

> VboxManage clonemedium "cloned.vdi" "cloned.vmdk" --format vmdk 

If you now boot your virtual machine and open the disk management you’ll see, that the partitions are still the same size as before, but that there is now additional unallocated space available on the end of the disk.

Disc space and partition overview

Extending the partitions within your guest system.

Please do not buy any software to do this simple disk management step for you. These are solved problems and should be free. I am disgusted that there is still an industry for these things.

In the Windows disk management tool you are only able to delete, create and extend partitions, but not move them around. But if you have the same setup as me, then there is a recovery disk directly after the C: partition, and only after the recovery disk comes the new unallocated space.

1. Disable recovery disk

> reagentc /disable

2. Delete recovery disk

Use the diskpart tool (Enter diskpart in powershell) to do this. It is a bit archaic, but it does the job.

# List the available disks
> list disk

# select the disk with the unallocated space
> select disk 0

#  List the partitions on this disk
> list partition

# Select the recovery disk partition (In my case it had the id 4)
> select partition 4

# Delete the recovery disk
> delete partition override

3. Extend the partition of your choice

In my case, this is the C: drive partition. For this you can the diskmgmt tool. For this enter the diskmgmgt in the start menu. When it is started, select the partition you want to extend, right click and choose the “Extend volume…” menu entry. From there on follow the instructions. You should leave some unallocated space at the end of the disk for the new recovery partition we will create in the next step.

4. Create a new recovery disk

Since we deleted the old recovery disk, it is about time we create a new one, but at a better location than before. For this you can use the diskmgmgttool too.

Select the unallocated space and choose the “New Simple Volume…” menu entry from the context menu and follow the instructions from the dialog. Please just ensure, that you do not assign a drive letter.

Next, we need the diskpart tool again from before (the archaic one). Now select the partition of the newly created recovery disk like before in step 2. Then do the following:

# Show information about the partition, ensure that it is the correct one
> detail partition

# Set the recovery flag on the partition
> set id=de94bba4-06d1-4d40-a16a-bfd50179d6ac

# Enable additional protections
> gpt attributes=0x8000000000000001

5. Enable recovery disk

The only thing left to do is enable the recovery disk again. Open a powershell window and do the following:

> reagentc /info
> reagentc /enable command

That’s it. Congratulations, now you should have all the disk space you need.