chiark / gitweb /
Initial version.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 26 Aug 2012 19:49:25 +0000 (12:49 -0700)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 26 Aug 2012 19:49:25 +0000 (12:49 -0700)
.gitignore [new file with mode: 0644]
.userv/rc [new file with mode: 0644]
bin/setup [new file with mode: 0755]
bin/vmctl [new file with mode: 0755]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..1b2b105
--- /dev/null
@@ -0,0 +1,3 @@
+.userv/svc
+.ssh/authorized_keys
+vmctl.conf
diff --git a/.userv/rc b/.userv/rc
new file mode 100644 (file)
index 0000000..88bc231
--- /dev/null
+++ b/.userv/rc
@@ -0,0 +1,8 @@
+### -*-conf-*-
+
+if grep calling-user-shell /etc/shells
+       no-suppress-args
+       require-fd 0 read
+       require-fd 1-2 write
+       execute-from-directory .userv/svc
+fi
diff --git a/bin/setup b/bin/setup
new file mode 100755 (executable)
index 0000000..3482e55
--- /dev/null
+++ b/bin/setup
@@ -0,0 +1,14 @@
+#! /bin/sh
+
+set -e
+rm -fr .userv/svc
+mkdir .userv/svc
+{ echo help;
+  bin/vmctl help | sed '
+       1,/Commands:/ d
+       s/^     //
+       s/ .*$//
+  '
+} | while read cmd; do
+  ln -s ../../bin/vmctl .userv/svc/$cmd
+done
diff --git a/bin/vmctl b/bin/vmctl
new file mode 100755 (executable)
index 0000000..ebdaad9
--- /dev/null
+++ b/bin/vmctl
@@ -0,0 +1,69 @@
+#! /bin/sh
+
+set -e
+
+VIRSH_DEFAULT_CONNECT_URI="qemu:///system"
+export VIRSH_DEFAULT_CONNECT_URI
+
+usage="Usage: $0 COMMAND [DOMAIN ARGS...]"
+groups="$VMCTL_GROUP $USERV_GROUP"
+
+case x${USERV_SERVICE+t} in
+  xt) set -- "$USERV_SERVICE" "$@" ;;
+esac
+
+case "$#,$1" in
+  1,help)
+    cat <<EOF
+$usage
+
+Commands:
+       status DOM      Show domain status.
+       reset DOM       Forcibly restart the domain.
+       stop DOM        Forcibly turn off the domain.
+       reboot DOM      Send the domain a reboot request.
+       shutdown DOM    Send the domain a shutdown request.
+       save DOM        Suspend the domain; restart using \`start'.
+       start DOM [-c]  Start up the domain (-c: and attach to console).
+       console DOM     Attach to the domain's console.
+EOF
+    exit 0
+    ;;
+  0,* | 1,*) echo >&2 "$usage"; exit 1 ;;
+  *) cmd=$1 dom=$2; shift 2 ;;
+esac
+
+allow=nil
+while read grps doms; do
+  case "$grps" in "" | \#*) continue ;; esac
+  gmatch=nil dmatch=nil
+  for g in $groups; do
+    case ",$grps," in *,$g,*) gmatch=t ;; esac
+  done
+  for d in $doms; do
+    case "$dom" in $d) dmatch=t ;; esac
+  done
+  case $gmatch,$dmatch in t,t) allow=t; break ;; esac
+done <$HOME/vmctl.conf
+
+case $allow in nil) echo >&2 "$0: not permitted"; exit 1 ;; esac
+
+case "$#,$cmd" in
+  0,status) virsh dominfo "$dom" ;; 
+  0,reset) virsh reset "$dom" ;;
+  0,stop) virsh destroy "$dom" ;;
+  0,reboot) virsh reboot "$dom" ;;
+  0,shutdown) virsh shutdown "$dom" ;;
+  0,start | 1,start)
+    args=""
+    case "$#,$1" in
+      0,) ;;
+      1,-c | 1,--console) args="$args --console" ;;
+      *) echo >&2 "Usage: $0 DOMAIN start [-c]"; exit 1 ;;
+    esac
+    virsh start "$dom" $args
+    ;;
+  0,save) virsh managedsave "$dom" ;;
+  0,console) virsh console "$dom" ;;
+  *) echo >&2 "$usage"; exit 1 ;;
+esac