Given: a vagrant base box with 10GB VMDK hard disk
Required: a vagrant base box with the same configuration and a 50GB hard disk
Import the .box file into VirtualBox
Add box from a local file to vagrant boxes
vagrant box add vagrantBoxName file:///C:boxesboxName.box
Notice that the file location is in URL formal
Verify that the box was added:
vagrant box list
By default, the box is created in C:Users%USERNAME%.vagrant.dboxesvagrantBoxName
Verify that the VM works:
Open VirtualBox -> File -> Import Appliance
Browse to C:Users%USERNAME%.vagrant.dboxesvagrantBoxNamevirtualbox
Select the box.ovf file
Import and start the machine.
If the VM runs successfully and the configuration is correct, shut the VM down. Make sure it’s in Powered Off state.
Resize the hard disk
Currently you cannot resize VMDK files with VirtualBox, but it is possible to clone VMDK to VDI and resize the VDI, and this is what we are going to do.
Open PowerShell (this will also work from cmd)
Navigate to the VirtualBox directory to be able to run VBoxManage
cd “C:Program FilesOracleVirtualBox”
By default the new VirtualBox VM is located at C:Users%USERNAME%VirtualBox VMsboxName. Let’s assume that your vmdk hard disk name is disk1.vmdk. Then the commands you need to run are the following:
Clone .vmdk to .vdi:
./VBoxManage clonehd "C:Users%USERNAME%VirtualBox VMsboxNamedisk1.vmdk" "C:Users%USERNAME%VirtualBox VMsboxNamecloned.vdi" --format vdi
Resize the .vdi 50GB (give size in MB)
./VBoxManage modifyhd "C:Users%USERNAME%VirtualBox VMsboxNamecloned.vdi" --resize 51200
Clone to a new .vmdk (to avoid file conflicts)
./VBoxManage clonehd "C:Users%USERNAME%VirtualBox VMsboxNamecloned.vdi" "C:Users%USERNAME%VirtualBox VMsboxNamedisk2.vmdk" --format vmdk
Replace the disk in VirtualBox
Open VirtualBox -> Select VM Name -> Storage -> Controller SATA -> Add Storage (Diskette with a plus sign on the bottom) -> Choose Existing Disk -> Browse and choose new VMDK
Click on the old disk, click on remove (Diskette with a minus sign on the bottom)
Extend disk partition
On the VM, hit WinKey and type “disk management” into search
Select Create and format hard disk partitions
In the disk management tool select the disk you need to expand, right click -> Extend Volume
Follow the wizard until finished.
Check that your disk has the correct size now.
Package the box with vagrant:
There are a couple of things you need to take care of prior to creating a base box. Most of them, however, should already be in place since you have started off with a configured VM. Here is the bare minimum list:
- Note the VM’s MAC address from VirtualBox -> Select VM Name -> Settings -> Network -> Advanced
- Make sure VirtualBox Guest Additions are installed.
For more info take a look at Vagrant Base Box
With PowerShell, navigate to the location on disk where you would like the box to be created.
Now, package the box:
vagrant package --base VirtualBoxVmName --output newBoxName.box
Your new base box was created!
Now I would go through the first few steps of this post to verify that the VM created from the box will work as expected.
Happy boxing 😉