Skip to content

Instantly share code, notes, and snippets.

@synchronizing
Created January 27, 2020 06:25
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 synchronizing/1c4d8d4e81d8797e63659d183f62aa2c to your computer and use it in GitHub Desktop.
Save synchronizing/1c4d8d4e81d8797e63659d183f62aa2c to your computer and use it in GitHub Desktop.
# For use with VirtualBox for COP4600.
# Make sure to update (if needed) all of the vm_ variables below.
# To check your vm name do:
#
# VBoxManage list vms
#
# Add the code below somewhere in your ~/.bash_profile.
# Type 'vm help' for all the available commands.
alias vm='virtual_machine'
virtual_machine () {
vm_name="18.04.3SB"
vm_username="reptilian"
vm_address="localhost"
vm_port="9474"
running() {
running=$(VBoxManage showvminfo "$vm_name" | grep -c "running (since")
}
usage() {
echo "Usage: "
echo " vm start Starts the Ubuntu vm."
echo " vm stop Ends the Ubuntu vm. "
echo " vm connect SSH into the vm."
echo " vm snapshot Lists, deletes, or creates snapshots for vm."
echo " vm help Opens up help dialog"
}
usage_snapshot() {
echo "Usage: "
echo " vm snapshot list"
echo " vm snapshot new -n \"<snapshot name>\" -d \"<description>\" "
echo " vm snapshot delete -n \"<snapshot name>\""
echo " vm snapshot restore -n \"<snapshot name>\""
}
usage_snapshot_new() {
echo "Usage:"
echo ' vm snapshot new -n "<snapshot_name>" -d "<snapshot_description>"'
echo ' vm snapshot new --name "<snapshot_name>" --desc "<snapshot_description>"'
}
snapshot() {
name=""
desc=""
while [[ $# -gt 0 ]]; do
opt="$3"
shift
case "$opt" in
"-n"|"--name")
name="$3"
shift
;;
"-d"|"--description")
desc="$3"
shift
;;
"")
;;
*)
echo "Command not recognized. $3"
usage_snapshot_new
;;
esac
done
if [[ $name == "" ]]; then
echo "Input param --name is missing."
usage_snapshot
return
fi
}
case $1 in
start)
running
if [ "$running" == "1" ] ; then
echo "Error! The vm is already powered on."
else
VBoxManage startvm $vm_name --type headless
fi
;;
stop)
running
if [ "$running" == "0" ] ; then
echo "Error! The vm is already powered off."
else
VBoxManage controlvm $vm_name poweroff
fi
;;
snapshot)
case $2 in
list)
vboxmanage snapshot $vm_name list
;;
new)
snapshot "$@"
if [[ $desc == "" ]]; then
echo "Input param --description is missing."
usage_snapshot
return
else
VBoxManage snapshot $vm_name take "$name" --description "$desc"
fi
;;
delete)
snapshot "$@"
VBoxManage snapshot $vm_name delete "$name"
;;
restore)
snapshot "$@"
VBoxManage snapshot $vm_name restore "$name"
;;
help)
usage_snapshot
;;
*)
echo "Command not recognized."
usage_snapshot
;;
esac
;;
connect)
running
if [ "$running" == "0" ] ; then
vm start
printf "\nMachine is booting up. Please wait a few seconds.\n"
fi
ssh $vm_username@$vm_address -p $vm_port
;;
help)
usage
;;
"")
usage
;;
*)
echo "Command not recognized."
usage
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment