| 1 | #! /bin/sh |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | VIRSH_DEFAULT_CONNECT_URI="qemu:///system" |
| 6 | export VIRSH_DEFAULT_CONNECT_URI |
| 7 | |
| 8 | usage="Usage: $0 COMMAND [DOMAIN ARGS...]" |
| 9 | groups="$VMCTL_GROUP $USERV_GROUP" |
| 10 | |
| 11 | case x${USERV_SERVICE+t} in |
| 12 | xt) set -- "$USERV_SERVICE" "$@" ;; |
| 13 | esac |
| 14 | |
| 15 | case "$#,$1" in |
| 16 | 1,help) |
| 17 | cat <<EOF |
| 18 | $usage |
| 19 | |
| 20 | Commands: |
| 21 | status DOM Show domain status. |
| 22 | reset DOM Forcibly restart the domain. |
| 23 | stop DOM Forcibly turn off the domain. |
| 24 | reboot DOM Send the domain a reboot request. |
| 25 | shutdown DOM Send the domain a shutdown request. |
| 26 | save DOM Suspend the domain; restart using \`start'. |
| 27 | start DOM [-c] Start up the domain (-c: and attach to console). |
| 28 | console DOM Attach to the domain's console. |
| 29 | EOF |
| 30 | exit 0 |
| 31 | ;; |
| 32 | 0,* | 1,*) echo >&2 "$usage"; exit 1 ;; |
| 33 | *) cmd=$1 dom=$2; shift 2 ;; |
| 34 | esac |
| 35 | |
| 36 | allow=nil |
| 37 | while read grps doms; do |
| 38 | case "$grps" in "" | \#*) continue ;; esac |
| 39 | gmatch=nil dmatch=nil |
| 40 | for g in $groups; do |
| 41 | case ",$grps," in *,$g,*) gmatch=t ;; esac |
| 42 | done |
| 43 | for d in $doms; do |
| 44 | case "$dom" in $d) dmatch=t ;; esac |
| 45 | done |
| 46 | case $gmatch,$dmatch in t,t) allow=t; break ;; esac |
| 47 | done <$HOME/vmctl.conf |
| 48 | |
| 49 | case $allow in nil) echo >&2 "$0: not permitted"; exit 1 ;; esac |
| 50 | |
| 51 | case "$#,$cmd" in |
| 52 | 0,status) virsh dominfo "$dom" ;; |
| 53 | 0,reset) virsh reset "$dom" ;; |
| 54 | 0,stop) virsh destroy "$dom" ;; |
| 55 | 0,reboot) virsh reboot "$dom" ;; |
| 56 | 0,shutdown) virsh shutdown "$dom" ;; |
| 57 | 0,start | 1,start) |
| 58 | args="" |
| 59 | case "$#,$1" in |
| 60 | 0,) ;; |
| 61 | 1,-c | 1,--console) args="$args --console" ;; |
| 62 | *) echo >&2 "Usage: $0 DOMAIN start [-c]"; exit 1 ;; |
| 63 | esac |
| 64 | virsh start "$dom" $args |
| 65 | ;; |
| 66 | 0,save) virsh managedsave "$dom" ;; |
| 67 | 0,console) virsh console "$dom" ;; |
| 68 | *) echo >&2 "$usage"; exit 1 ;; |
| 69 | esac |