- https://gist.github.com/christopher-hopper/9755310
- http://dantwining.co.uk/2011/07/18/how-to-shrink-a-dynamically-expanding-guest-virtualbox-image/
- Host machine runs OSX
- VAGRANT_VM_PATH is the location of the Vagrantfile for the VM you want to shrink
- VirtualBox/Vagrant creates new Virtual Machines in the default location
~/VirtualBox\ VMs/
-
Stop the virtual machine using Vagrant.
# cd VAGRANT_VM_PATH # vagrant halt
-
Locate the VirtuaBox VM and the HDD attached to its SATA Controller. In this instance we're assuming the VM is located in the default location and is named
mybox_default_1382400620
.# cd ~/VirtualBox\ VMs/mybox_default_1382400620 # VBoxManage showvminfo mybox_default_1382400620 | grep ".vmdk"
The
showvminfo
command should show you the location on the file-system of the HDD of type VMDK along with the name of the Controller it is attached to - it will look something like this:IDE Controller (0, 0): /Users/myuser/VirtualBox VMs/mybox_default_1382400620/debian-7.6.0-amd64-disk1.vmdk (UUID: UUID: 2f79610e-6c06-46d5-becb-448386ea40ec)
-
clone the VMDK type disk to a VDI type disk so it can be resized.
# cd ~/VirtualBox\ VMs/mybox_default_1382400620 # VBoxManage clonehd "debian-7.6.0-amd64-disk1.vmdk" "debian-7.6.0-amd64-disk1.vdi" --format vdi
NOTE: We do this because VMDK type disks cannot be resized by VirtualBox. It has the added benefit of allowing us to keep our original disk backed-up during the resize operation.
-
Find out how big the disk is currently, to determine how large to make it when resized. The information will show the current size and the Format variant. If Dynamic Allocation was used to create the disk, the Format variant will be "dynamic default".
# VboxManage showhdinfo "debian-7.6.0-amd64-disk1.vdi"
-
Find out the name of the SATA Storage Controller to attach the newly resized disk to.
# VBoxManage showvminfo mybox_default_1382400620 | grep "Storage"
-
Attach the newly resized disk to the SATA Controller of the Virtual Machine.
# VBoxManage storageattach mybox_default_1382400620 --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium debian-7.6.0-amd64-disk1.vdi
-
Reboot the Virtual Machine using Vagrant.
# cd provisioning/boxes/mybox # vagrant up
-
Open a command-line shell as root on the Virtual Machine via ssh.
# vagrant ssh # sudo su -
-
Install zerofree
# apt-get install zerofree
-
Find the name of the logical volume mapping the file-system is on (ie.
/dev/mapper/debian--7-root
).# df -h
-
Stop services using rootfs
# service rsyslog stop # service dnsmasq stop # service apache2 stop # other services... (use *fuser -v -m /* to find out which ones) # killall dhclient
-
Remount read-only
# mount -n -o remount,ro -t ext4 /dev/mapper/debian--7-root /
-
Zerofree
# zerofree -v /dev/mapper/debian--7-root
-
Reboot the machine, then ssh back in when it is up again and switch to the root user once more.
# shutdown -h now
-
Compact the cloned disk.
# VBoxManage modifyhd debian-7.6.0-amd64-disk1.vdi --compact
NOTE: If the disk was created using dynamic allocation (see previous step) then the physical size of the disk will not need to match its logical size - meaning you can create a very large logical disk that will increase in physical size only as space is used.
-
Check the size on disk
# VboxManage showhdinfo debian-7.6.0-amd64-disk1.vdi
-
If you want to prevent the vdi to dynamically expend you can add the
--variant Fixed
option to the clonehd command. Also following these instructions might allow to also shrink the logical volume size.