Skip to content

Instantly share code, notes, and snippets.

@mkoertgen
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkoertgen/74f270297514f826593e to your computer and use it in GitHub Desktop.
Save mkoertgen/74f270297514f826593e to your computer and use it in GitHub Desktop.
How to export and optimize a linux VirtualBox Vagrant VM

How to export and optimize a linux VirtualBox Vagrant VM

You can export a base box from your current Vagrant VM work with

vagrant package --base <vm_name> --output <vm_name>.box

However, before doing this you may try to reduce the size of the base box.

Shrinking the box

You should remove any installation files from the vagrant home directory in the guest VM.

Then do a 1) general clean up, 2) use zerofree and 3) compact your virtual disk image as described here, here, here and here.

Here is what i did:

	sudo aptitude install zerofree
	sudo apt-get clean -y
	sudo apt-get autoclean -y
	sudo rm -rf /usr/src/vboxguest*
	sudo rm -rf /usr/share/doc
	sudo find /var/cache -type f -exec rm -rf {} \;
	
	sudo umount /vagrant
	sudo rm -rf /usr/src/linux-headers*
	sudo rm -rf /usr/share/locale/{af,an,am,ar,ary,as,ast,az,bal,be,bg,bn,bn_IN,bo,br,bs,byn,ca,ca@valencia,ckb,cr,crh,cs,csb,cv,cy,da,de,de_AT,dv,dz,el,en_AU,en_CA,en_GB,eo,es,et,et_EE,eu,fa,fa_AF,fi,fil,fo,fr,frp,fur,fy,ga,gd,gez,gl,gu,gv,haw,he,hi,hr,ht,hu,hy,id,is,it,ja,jv,ka,kk,km,kn,ko,kok,ku,ky,lb,lg,ln,lt,lo,lv,mg,mhr,mi,mk,ml,mn,mr,ms,mt,my,nb,nds,ne,nl,nn,no,nso,oc,or,os,pa,pam,pl,ps,pt,pt_BR,qu,ro,ru,rw,sc,sd,shn,si,sk,sl,so,sq,sr,sr*latin,sv,sw,ta,te,th,ti,tig,tk,tl,tr,trv,tt,ug,uk,ur,urd,uz,ve,vec,vi,wa,wal,wo,xh,zh,zh_HK,zh_CN,zh_TW,zu}

Check what works for you by using sudo du -hs / to check the size of your installation.

Finally reboot the machine using VirtualBox and enter recovery mode through grub menu (hold shift key during boot). Check partitions using df -h. In my case (Ubuntu Precise x64) it is /dev/mapper/precise64-root. Then wipe free disk space with zeroes

	zerofree /dev/mapper/precise64-root

which may take some minutes. Finally shut down

	shutdown -h now

Delete all snapshots,

	vagrant snapshot list
	vagrant snapshot delete ...

Compact the disk using VirtualBox console tool

	VBoxManage clonehd box-disk1.vmdk box-disk1.vdi --format vdi
	VBoxManage modifyhd box-disk1.vdi --compact
	del box-disk1.vmdk
	VBoxManage clonehd box-disk1.vdi box-disk0.vmdk --format vmdk
	del box-disk1.vdi

Finally, attach the newly shrunk disk to the VM using VirtualBox GUI and export your base box

	vagrant package --base <vm_name> --output <vm_name>.box
@wkoertgen
Copy link

Well done. Unfortunately - after cloning and formatting the disks - I got an error with the package command:
There was an error while executing VBoxManage, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["export", "TrainOracle12", "--output", "/home/oracle/.vagrant.d/tmp/1432117867/box.ovf"]

Stderr: 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...
Progress state: NS_ERROR_FAILURE
VBoxManage: error: Appliance write failed
VBoxManage: error: UUID {d2245084-56e2-4361-b29c-8da7434711e8} of the medium '/home/oracle/VirtualBox VMs/TrainOracle12/oel71-disk1.vmdk' does not match the value {13c63238-709d-4a43-96cf-3e58bc6f258d} stored in the media registry ('/home/oracle/.config/VirtualBox/VirtualBox.xml')
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component Medium, interface IMedium
VBoxManage: error: Context: "int handleExportAppliance(HandlerArg*)" at line 1121 of file VBoxManageAppliance.cpp

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