aboutsummaryrefslogtreecommitdiff
path: root/etc/private/vm-stop
diff options
context:
space:
mode:
Diffstat (limited to 'etc/private/vm-stop')
-rwxr-xr-xetc/private/vm-stop37
1 files changed, 37 insertions, 0 deletions
diff --git a/etc/private/vm-stop b/etc/private/vm-stop
new file mode 100755
index 0000000..cf64dee
--- /dev/null
+++ b/etc/private/vm-stop
@@ -0,0 +1,37 @@
+#! /usr/bin/env bash
+
+# Stop virtual machine started with vm-start.
+#
+usage="usage: $0 <pid-file> <monitor-socket>"
+
+trap "{ exit 1; }" ERR
+set -o errtrace # Trap in functions.
+
+function info () { echo "$*" 1>&2; }
+function error () { info "$*"; exit 1; }
+
+
+if [ -z "$1" -o ! -f "$1" ]; then
+ error "missing or invalid PID file"
+fi
+
+pid="$(sed -nr -e 's/([0-9]+)/\1/p' "$1")"
+
+if [ -z "$pid" ]; then
+ error "PID file $1 does not contain valid PID"
+fi
+
+if [ -z "$2" -o ! -S "$2" ]; then
+ error "missing or invalid monitor socket"
+fi
+
+mon="$2"
+
+echo system_powerdown | socat - "UNIX-CONNECT:$mon" >/dev/null
+
+# An alternative way to implement this would be to connect a pipe to the
+# monitor socket and wait for it to be closed.
+#
+while [ -e "/proc/$pid" ]; do
+ sleep 0.2
+done