Skip to content

Instantly share code, notes, and snippets.

@boltronics
Created May 7, 2012 04:55
Show Gist options
  • Save boltronics/2625994 to your computer and use it in GitHub Desktop.
Save boltronics/2625994 to your computer and use it in GitHub Desktop.
Mount VirtualBox Guest Additions to current Vagrant instance.
#!/bin/bash
#
# Adam Bolte <adam.bolte@sitepoint.com>
#
# Description:
# Mount VirtualBox Guest Additions to current Vagrant instance.
declare -r option="${1}"
declare -r attach_to_instance=$(basename $(pwd))
declare guest_additions_iso="/usr/share/virtualbox/VBoxGuestAdditions.iso"
function print_usage()
{
{
echo "Mount VirtualBox Guest Additions ISO to current Vagrant guest."
echo
echo "Usage:"
echo " ${0} -m|-u [IMAGE.ISO]"
echo "where"
echo " -m = mount"
echo " -u = umount"
echo " IMAGE.ISO = VirtualBox Guest Additions ISO image path."
echo " (defaults to '${guest_additions_iso}')"
} >&2
}
function check_arguments()
{
if [ "${option}" != "-m" -a "${option}" != "-u" ]
then
print_usage
exit 1
fi
}
function check_image()
{
if [ ! -f "${guest_additions_iso}" ]
then
echo "File '${guest_additions_iso}' not found!"
exit 1
fi
}
check_arguments
check_image
for instance in $(VBoxManage list runningvms | grep ${attach_to_instance} | \
sed -e 's/^"\([^"]*\)".*/\1/')
do
if [ "${attach_to_instance}" = "${instance%_*}" ]
then
if [ "${option}" = "-m" ]
then
echo -n "Attaching ISO to '${instance}'... "
VBoxManage storageattach ${instance} \
--storagectl "IDE Controller" --port 1 --device 0 \
--type dvddrive --medium "${guest_additions_iso}"
elif [ "${option}" = "-u" ]
then
echo -n "Detaching ISO on '${instance}'... "
VBoxManage storageattach ${instance} \
--storagectl "IDE Controller" --port 1 --device 0 \
--type dvddrive --medium emptydrive
fi
if [ "${?}" -eq 0 ]
then
echo "OK."
else
echo "FAIL."
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment