Skip to content

Instantly share code, notes, and snippets.

@jmatsushita
Forked from christopher-hopper/vm-resize-hard-disk.md
Last active August 29, 2015 14:10
Show Gist options
  • Save jmatsushita/8fb1f29c5739ba9f544a to your computer and use it in GitHub Desktop.
Save jmatsushita/8fb1f29c5739ba9f544a to your computer and use it in GitHub Desktop.

Shrink a Vagrant VirtualBox image file

Sources

Some assumptions

  • 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/

Steps to resize the hard disk

  1. Stop the virtual machine using Vagrant.

     # cd VAGRANT_VM_PATH
     # vagrant halt
    
  2. 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)
    
  3. 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.

  4. 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"
    
  5. Find out the name of the SATA Storage Controller to attach the newly resized disk to.

     # VBoxManage showvminfo mybox_default_1382400620 | grep "Storage"
    
  6. 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
    
  7. Reboot the Virtual Machine using Vagrant.

     # cd provisioning/boxes/mybox
     # vagrant up
    
  8. Open a command-line shell as root on the Virtual Machine via ssh.

     # vagrant ssh
     # sudo su -
    
  9. Install zerofree

     # apt-get install zerofree
    
  10. Find the name of the logical volume mapping the file-system is on (ie. /dev/mapper/debian--7-root).

     # df -h
    
  11. 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
    
  12. Remount read-only

     # mount -n -o remount,ro -t ext4 /dev/mapper/debian--7-root /
    
  13. Zerofree

     # zerofree -v /dev/mapper/debian--7-root
    
  14. Reboot the machine, then ssh back in when it is up again and switch to the root user once more.

     # shutdown -h now
    
  15. 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.

  16. Check the size on disk

     # VboxManage showhdinfo debian-7.6.0-amd64-disk1.vdi 
    
  17. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment