Skip to content

Instantly share code, notes, and snippets.

@vorce
Created Apr 12, 2013
Embed
What would you like to do?
SHahara. Shell script sandbox for VirtualBox. Basically just a wrapper for VBoxManage snapshot.
#!/bin/sh
# SHahara
# Inspired by: https://github.com/jedi4ever/sahara
#
# Sandbox for virtualbox for shell scripts
# Wrapper for VBoxManage and snapshots
SH_TEST_SNAPSHOT=sh_test_snapshot
VBOX=VBoxManage
removeSnapshot() {
local VM_NAME="$1"
VM_KEY=`VBoxManage list vms | grep $VM_NAME | awk '{print $2}'`
$VBOX snapshot $VM_KEY delete "$SH_TEST_SNAPSHOT" &> /dev/null
}
takeSnapshot() {
local VM_NAME="$1"
VM_KEY=`VBoxManage list vms | grep $VM_NAME | awk '{print $2}'`
local ALREADY_ON=`VBoxManage showvminfo --machinereadable $VM_KEY | grep SnapshotName | grep $SH_TEST_SNAPSHOT`
if [ -z "$ALREADY_ON" ]; then
$VBOX snapshot $VM_KEY take "$SH_TEST_SNAPSHOT"
$VBOX controlvm $VM_KEY resume
fi
}
restoreSnapshot() {
$VBOX controlvm $VM_KEY poweroff
$VBOX snapshot $VM_KEY restore "$SH_TEST_SNAPSHOT"
$VBOX startvm --type headless $VM_KEY
}
commitSnapshot() {
local VM_NAME="$1"
$VBOX controlvm $VM_KEY poweroff
$VBOX snapshot $VM_KEY delete "$SH_TEST_SNAPSHOT"
takeSnapshot $VM_NAME && $VBOX startvm --type headless $VM_KEY
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment