Skip to content

Instantly share code, notes, and snippets.

@dillera
Created July 20, 2013 19:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dillera/6046134 to your computer and use it in GitHub Desktop.
Save dillera/6046134 to your computer and use it in GitHub Desktop.
This bash script will create a vagrant box for you from a valid vmware fusion virtual machine directory. Currently, this is a manual process that is begging for any automation.
#!/bin/bash
#
# simple script to create a vagrant.box from a vmware fusion source VM
# andrew_diller@gmail.com July 2013
#
FUSIONTOOL='/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager'
METADATAFILE=metadata.json
if [ $# -lt 3 ]
then
echo "Not proper arguments"
echo "useage: make_fusion_box.sh [source vmware vm directory] [target dir for box] [name of box]"
echo "this script will add the .box to the name"
echo "sample: make_fusion_box.sh ~/VirtualMachines/myvm ~/vagrant/myboxes vagrant-centos6"
exit 1
fi
SRC=$1
TRG=$2
NAME=$3
echo ----------------------------------------------------------
echo "Starting parameters:"
echo Source vm dir: $1
echo Target box dir: $2
echo Name: $3
if [ -d "${SRC}" ];
then
cd $SRC
echo "- Moved into source directory"
echo "CURRENT DIR: `pwd`"
else
echo "Source is not a proper directory, quitting."
exit 1
fi
if [ -d "${TRG}" ];
then
echo "Target is a valid directory"
else
echo "Target is not a proper directory, quitting."
exit 1
fi
################################################################
# determine if the current dir has vmdk files in it....
any_with_ext () (
ext="vmdk"
any=false
shopt -s nullglob
for f in *."$ext"; do
any=true
break
done
echo $any
)
# if we have vmdks, then lets do the thing.
# working with Spaces in names of directory paths is not fun with bash
if $( any_with_ext vmdk ); then
echo "This looks like a valid vmware virtual machine home..."
CMD="${FUSIONTOOL} -d ${SRC}/Virtual\ Disk.vmdk"
echo $CMD
sh -c "${CMD}"
echo 'defragged disk'
CMD="${FUSIONTOOL} -k $SRC/Virtual\ Disk.vmdk"
echo $CMD
sh -c "${CMD}"
echo 'kompressed disk'
if [ -f $METADATAFILE ];
then
echo "File $METADATAFILE exists."
else
echo "File $METADATAFILE does not exist, creating"
cat << _EOF_ > metadata.json
{
"provider": "vmware_fusion"
}
_EOF_
echo "Created $METADATAFILE for you"
fi
echo --------------------------------------------------------
echo Creating box....
tar cvzf $3.box --exclude="*.log" --exclude="*.lck" ./*
echo --------------------------------------------------------
mv $3.box $2
echo "Moved new box $3 into $2"
else
echo "This is not a vmware vm directory, bailing..."
exit 1
fi
echo DONE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment