Skip to content

Instantly share code, notes, and snippets.

@kalkin
Created April 25, 2016 09:04
Show Gist options
  • Save kalkin/835fb98f5175e689cfe59d82452aeb37 to your computer and use it in GitHub Desktop.
Save kalkin/835fb98f5175e689cfe59d82452aeb37 to your computer and use it in GitHub Desktop.
Execute an action or command on currently focused domain (This script is for QubesOS)
#!/bin/sh
set -e
# vim: fdm=marker sw=4 ts=4 tw=80
# Author: Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
print_help() {
echo "Usage: vmm [-hHKRSX] [-n VM_NAME] [COMMAND]"
echo
echo "Execute an action or command on currently focused domain"
echo 'Commands:'
echo " -H, --halt : Shutdown VM"
echo " -K, --kill : Kill VM"
echo " -R, --restart : Retart VM"
echo " -S, --start : Start VM"
echo " -X, --exec : Run command on VM (VM is started if needed)"
}
TMP=$(getopt --name vmm -o hHKRSX --long help,halt,kill,restart,start,exec -- "$@")
if [ $? = 0 ]; then
eval set -- "$TMP"
else
exit 1
fi
COMMAND=
set_vm_command(){
if [ ! -z "$COMMAND" ]; then
echo "Multiple commands specified: $COMMAND, $1"
print_help
exit 1
else
COMMAND="$1"
fi
}
while true; do
case "$1" in
-h | --help ) print_help; exit 0 ;;
-H | -K | -R | -S | -X | --halt | --kill | --restart | --start | --exec )
set_vm_command "$1"; shift ;;
-- ) shift; break;;
* ) break ;;
esac
done
if [ -z "$COMMAND" ]; then
echo No command specified
print_help
exit 1
fi
ID=$(xdotool getactivewindow)
VM_NAME=$(xprop -id "$ID"|grep QUBES_VMNAME|cut -d'"' -f2)
if [ -z "$VM_NAME" ]; then
echo "ERROR: Called $COMMAND $* on dom0"
exit 1;
fi
case "$COMMAND" in
-H | --halt ) qvm-shutdown "$VM_NAME" && exit 0;;
-K | --kill ) qvm-kill "$VM_NAME" && exit 0;;
-R | --restart ) qvm-shutdown "$VM_NAME" && qvm-start "$VM_NAME" && exit 0;;
-S | --start ) qvm-start "$VM_NAME" && exit 0;;
-X | --exec ) qvm-run -a "$VM_NAME" "$@" && exit 0;;
* ) exit 1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment