Skip to content

Instantly share code, notes, and snippets.

@batandwa
Last active January 3, 2016 00:39
Show Gist options
  • Select an option

  • Save batandwa/8383871 to your computer and use it in GitHub Desktop.

Select an option

Save batandwa/8383871 to your computer and use it in GitHub Desktop.
Vigrant - Export a VirtualBox VM and import it as a Vagrant box
#!/bin/bash
if [ -z "$2" ];then
echo "We need two parameters, boet."
echo " Usage: $FUNCNAME vagrant-name vbox-machine-name"
return 1
fi
export lowername=$2
export tempdir=createvmbox.$((RANDOM))
mkdir -p /tmp/$tempdir
cd /tmp/$tempdir
VBoxManage export "$2" --output "box.ovf" --manifest
export retval=$?
if [ $retval -ne 0 ]; then
echo "Error exporting VirtualBox machine. Exiting."
return 2
fi
# Remove existing box
if [[ `vagrant box list` =~ "$1" ]]; then
echo "Removing old box."
vagrant box remove $1 virtualbox
fi
echo '{"provider":"virtualbox"}' >> metadata.json
zip "$lowername.box" *
vagrant box add $1 "$lowername.box"
cd -
rm /tmp/$tempdir -R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment