From: Olaf Hering Date: Wed, 1 Oct 2014 16:41:07 +0000 (+0200) Subject: tools/hotplug: use XEN_SCRIPT_DIR instead of hardcoded path X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~4249 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=d30c14b1f121b5302aad5135b2c04c6ea1300d85;p=xen.git tools/hotplug: use XEN_SCRIPT_DIR instead of hardcoded path Helper scripts get installed into XEN_SCRIPT_DIR, but initscripts, helper scripts and udev rules still refer to the hardcoded location /etc/xen/scripts/. Update scripts, rules and Makefile to refer to @XEN_SCRIPT_DIR@ instead. Update configure.ac to substitute the path in files using XEN_SCRIPT_DIR. Remove XEN_SCRIPT_DIR from StdGNU.mk and SunOS.mk, its already in Paths.mk. Note: In SunOS.mk it refered to PRIVATE_PREFIX, which was introduced by 9f6d07bac9f14aaa8bafe38b4a217e61a3a57842 ("tools: get rid of hardcoded config dirs"). Signed-off-by: Olaf Hering Acked-by: Ian Campbell Cc: Ian Campbell Cc: Ian Jackson Cc: Stefano Stabellini Cc: Wei Liu [ ijc -- ran autogen.sh as requested ] --- diff --git a/.gitignore b/.gitignore index 67ed7cb7b2..4344c52206 100644 --- a/.gitignore +++ b/.gitignore @@ -160,6 +160,12 @@ tools/flask/utils/flask-set-bool tools/flask/utils/flask-label-pci tools/fs-back/fs-backend tools/hotplug/common/hotplugpath.sh +tools/hotplug/Linux/init.d/xen-watchdog +tools/hotplug/Linux/init.d/xendomains +tools/hotplug/Linux/vif-setup +tools/hotplug/Linux/xen-backend.rules +tools/hotplug/Linux/xen-hotplug-common.sh +tools/hotplug/Linux/xendomains tools/include/xen/* tools/include/xen-foreign/*.(c|h|size) tools/include/xen-foreign/checker diff --git a/config/StdGNU.mk b/config/StdGNU.mk index ddf83d7a15..4cc3a01a88 100644 --- a/config/StdGNU.mk +++ b/config/StdGNU.mk @@ -48,7 +48,6 @@ XEN_PAGING_DIR = /var/lib/xen/xenpaging SYSCONFIG_DIR = $(CONFIG_DIR)/$(CONFIG_LEAF_DIR) XEN_CONFIG_DIR = $(CONFIG_DIR)/xen -XEN_SCRIPT_DIR = $(XEN_CONFIG_DIR)/scripts BOOT_DIR ?= /boot diff --git a/config/SunOS.mk b/config/SunOS.mk index d493eca24b..14705146b5 100644 --- a/config/SunOS.mk +++ b/config/SunOS.mk @@ -32,7 +32,6 @@ PRIVATE_PREFIX = $(LIBDIR)/xen PRIVATE_BINDIR = $(PRIVATE_PREFIX)/bin XEN_CONFIG_DIR = $(CONFIG_DIR)/xen -XEN_SCRIPT_DIR = $(PRIVATE_PREFIX)/scripts BOOT_DIR ?= /boot diff --git a/tools/configure b/tools/configure index b61d3e4c01..3ca8ee1a1d 100755 --- a/tools/configure +++ b/tools/configure @@ -2263,7 +2263,7 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu -ac_config_files="$ac_config_files ../config/Tools.mk hotplug/Linux/init.d/xencommons.in hotplug/Linux/init.d/sysconfig.xencommons hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/var-lib-xenstored.mount hotplug/Linux/systemd/xenstored.socket hotplug/Linux/systemd/xenstored_ro.socket hotplug/Linux/systemd/xenstored.service hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xen-watchdog.service" +ac_config_files="$ac_config_files ../config/Tools.mk hotplug/Linux/init.d/xencommons.in hotplug/Linux/init.d/sysconfig.xencommons hotplug/Linux/init.d/xen-watchdog hotplug/Linux/init.d/xendomains hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/var-lib-xenstored.mount hotplug/Linux/systemd/xenstored.socket hotplug/Linux/systemd/xenstored_ro.socket hotplug/Linux/systemd/xenstored.service hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xen-watchdog.service hotplug/Linux/vif-setup hotplug/Linux/xen-backend.rules hotplug/Linux/xen-hotplug-common.sh hotplug/Linux/xendomains" ac_config_headers="$ac_config_headers config.h" @@ -9516,6 +9516,8 @@ do "../config/Tools.mk") CONFIG_FILES="$CONFIG_FILES ../config/Tools.mk" ;; "hotplug/Linux/init.d/xencommons.in") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xencommons.in" ;; "hotplug/Linux/init.d/sysconfig.xencommons") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/sysconfig.xencommons" ;; + "hotplug/Linux/init.d/xen-watchdog") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xen-watchdog" ;; + "hotplug/Linux/init.d/xendomains") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/init.d/xendomains" ;; "hotplug/Linux/systemd/proc-xen.mount") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/proc-xen.mount" ;; "hotplug/Linux/systemd/var-lib-xenstored.mount") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/var-lib-xenstored.mount" ;; "hotplug/Linux/systemd/xenstored.socket") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xenstored.socket" ;; @@ -9525,6 +9527,10 @@ do "hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service" ;; "hotplug/Linux/systemd/xendomains.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xendomains.service" ;; "hotplug/Linux/systemd/xen-watchdog.service") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/systemd/xen-watchdog.service" ;; + "hotplug/Linux/vif-setup") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/vif-setup" ;; + "hotplug/Linux/xen-backend.rules") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/xen-backend.rules" ;; + "hotplug/Linux/xen-hotplug-common.sh") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/xen-hotplug-common.sh" ;; + "hotplug/Linux/xendomains") CONFIG_FILES="$CONFIG_FILES hotplug/Linux/xendomains" ;; "config.h") CONFIG_HEADERS="$CONFIG_HEADERS config.h" ;; *) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;; diff --git a/tools/configure.ac b/tools/configure.ac index cfa4dd674f..cb77811fbe 100644 --- a/tools/configure.ac +++ b/tools/configure.ac @@ -9,6 +9,8 @@ AC_CONFIG_FILES([ ../config/Tools.mk hotplug/Linux/init.d/xencommons.in hotplug/Linux/init.d/sysconfig.xencommons +hotplug/Linux/init.d/xen-watchdog +hotplug/Linux/init.d/xendomains hotplug/Linux/systemd/proc-xen.mount hotplug/Linux/systemd/var-lib-xenstored.mount hotplug/Linux/systemd/xenstored.socket @@ -18,6 +20,10 @@ hotplug/Linux/systemd/xenconsoled.service hotplug/Linux/systemd/xen-qemu-dom0-disk-backend.service hotplug/Linux/systemd/xendomains.service hotplug/Linux/systemd/xen-watchdog.service +hotplug/Linux/vif-setup +hotplug/Linux/xen-backend.rules +hotplug/Linux/xen-hotplug-common.sh +hotplug/Linux/xendomains ]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_AUX_DIR([../]) diff --git a/tools/hotplug/Linux/init.d/xen-watchdog b/tools/hotplug/Linux/init.d/xen-watchdog deleted file mode 100644 index 3592fda32d..0000000000 --- a/tools/hotplug/Linux/init.d/xen-watchdog +++ /dev/null @@ -1,90 +0,0 @@ -#! /bin/bash -# -# xen-watchdog -# -# chkconfig: 2345 21 79 -# description: Run domain watchdog daemon -### BEGIN INIT INFO -# Provides: xen-watchdog -# Required-Start: $syslog $remote_fs -# Should-Start: xend -# Required-Stop: $syslog $remote_fs -# Should-Stop: xend -# Default-Start: 2 3 5 -# Default-Stop: 0 1 6 -# Short-Description: Start/stop xen-watchdog -# Description: Run domain watchdog daemon. -### END INIT INFO -# - -. /etc/xen/scripts/hotplugpath.sh - -DAEMON=${SBINDIR}/xenwatchdogd -base=$(basename $DAEMON) - -# Source function library. -if [ -e /etc/init.d/functions ] ; then - . /etc/init.d/functions -elif [ -e /lib/lsb/init-functions ] ; then - . /lib/lsb/init-functions - success () { - log_success_msg $* - } - failure () { - log_failure_msg $* - } -else - success () { - echo $* - } - failure () { - echo $* - } -fi - -start() { - local r - echo -n $"Starting domain watchdog daemon: " - - $DAEMON 30 15 - r=$? - [ "$r" -eq 0 ] && success $"$base startup" || failure $"$base startup" - echo - - return $r -} - -stop() { - local r - echo -n $"Stopping domain watchdog daemon: " - - killall -USR1 $base 2>/dev/null - r=$? - [ "$r" -eq 0 ] && success $"$base stop" || failure $"$base stop" - echo - - return $r -} - -case "$1" in - start) - start - ;; - stop) - stop - ;; - restart) - stop - start - ;; - status) - ;; - condrestart) - stop - start - ;; - *) - echo $"Usage: $0 {start|stop|status|restart|condrestart}" - exit 1 -esac - diff --git a/tools/hotplug/Linux/init.d/xen-watchdog.in b/tools/hotplug/Linux/init.d/xen-watchdog.in new file mode 100644 index 0000000000..32c7268d6e --- /dev/null +++ b/tools/hotplug/Linux/init.d/xen-watchdog.in @@ -0,0 +1,90 @@ +#! /bin/bash +# +# xen-watchdog +# +# chkconfig: 2345 21 79 +# description: Run domain watchdog daemon +### BEGIN INIT INFO +# Provides: xen-watchdog +# Required-Start: $syslog $remote_fs +# Should-Start: xend +# Required-Stop: $syslog $remote_fs +# Should-Stop: xend +# Default-Start: 2 3 5 +# Default-Stop: 0 1 6 +# Short-Description: Start/stop xen-watchdog +# Description: Run domain watchdog daemon. +### END INIT INFO +# + +. @XEN_SCRIPT_DIR@/hotplugpath.sh + +DAEMON=${SBINDIR}/xenwatchdogd +base=$(basename $DAEMON) + +# Source function library. +if [ -e /etc/init.d/functions ] ; then + . /etc/init.d/functions +elif [ -e /lib/lsb/init-functions ] ; then + . /lib/lsb/init-functions + success () { + log_success_msg $* + } + failure () { + log_failure_msg $* + } +else + success () { + echo $* + } + failure () { + echo $* + } +fi + +start() { + local r + echo -n $"Starting domain watchdog daemon: " + + $DAEMON 30 15 + r=$? + [ "$r" -eq 0 ] && success $"$base startup" || failure $"$base startup" + echo + + return $r +} + +stop() { + local r + echo -n $"Stopping domain watchdog daemon: " + + killall -USR1 $base 2>/dev/null + r=$? + [ "$r" -eq 0 ] && success $"$base stop" || failure $"$base stop" + echo + + return $r +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + status) + ;; + condrestart) + stop + start + ;; + *) + echo $"Usage: $0 {start|stop|status|restart|condrestart}" + exit 1 +esac + diff --git a/tools/hotplug/Linux/init.d/xencommons.in.in b/tools/hotplug/Linux/init.d/xencommons.in.in index a723eb4e92..bd1d9ba538 100644 --- a/tools/hotplug/Linux/init.d/xencommons.in.in +++ b/tools/hotplug/Linux/init.d/xencommons.in.in @@ -20,7 +20,7 @@ XENSTORED=@XENSTORED@ -. /etc/xen/scripts/hotplugpath.sh +. @XEN_SCRIPT_DIR@/hotplugpath.sh if [ -d /etc/sysconfig ]; then xencommons_config=/etc/sysconfig diff --git a/tools/hotplug/Linux/init.d/xendomains b/tools/hotplug/Linux/init.d/xendomains deleted file mode 100644 index 385760267a..0000000000 --- a/tools/hotplug/Linux/init.d/xendomains +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash -# -# /etc/init.d/xendomains -# Wrapper to start / stop domains automatically when domain 0 boots / shuts down -# -# chkconfig: 345 99 00 -# description: Helper to start / stop Xen domains. -# -# This script is a wrapper init helper for the real workload horse script. -# It should work on LSB-compliant systems. -# -### BEGIN INIT INFO -# Provides: xendomains -# Required-Start: $syslog $remote_fs xenstored xenconsoled -# Should-Start: xend -# Required-Stop: $syslog $remote_fs xenstored xenconsoled -# Should-Stop: xend -# Default-Start: 2 3 5 -# Default-Stop: 0 1 6 -# Short-Description: Wrapper to start/stop secondary xen domains -# Description: Wrapper for starting / stopping domains automatically -# when domain 0 boots / shuts down on systems using init. -# The $LIBEXEC/xendomains helper is shared between init and -# systemd systems. -### END INIT INFO - -. /etc/xen/scripts/hotplugpath.sh - -case "$1" in - start) - $LIBEXEC/xendomains start - ;; - stop) - $LIBEXEC/xendomains stop - ;; - restart) - $LIBEXEC/xendomains restart - ;; - reload) - $LIBEXEC/xendomains reload - ;; - status) - $LIBEXEC/xendomains status - ;; - *) - echo "Usage: $0 {start|stop|restart|reload|status}" - exit 3 - ;; -esac -exit $? diff --git a/tools/hotplug/Linux/init.d/xendomains.in b/tools/hotplug/Linux/init.d/xendomains.in new file mode 100644 index 0000000000..5b88e2d9ee --- /dev/null +++ b/tools/hotplug/Linux/init.d/xendomains.in @@ -0,0 +1,50 @@ +#!/bin/bash +# +# /etc/init.d/xendomains +# Wrapper to start / stop domains automatically when domain 0 boots / shuts down +# +# chkconfig: 345 99 00 +# description: Helper to start / stop Xen domains. +# +# This script is a wrapper init helper for the real workload horse script. +# It should work on LSB-compliant systems. +# +### BEGIN INIT INFO +# Provides: xendomains +# Required-Start: $syslog $remote_fs xenstored xenconsoled +# Should-Start: xend +# Required-Stop: $syslog $remote_fs xenstored xenconsoled +# Should-Stop: xend +# Default-Start: 2 3 5 +# Default-Stop: 0 1 6 +# Short-Description: Wrapper to start/stop secondary xen domains +# Description: Wrapper for starting / stopping domains automatically +# when domain 0 boots / shuts down on systems using init. +# The $LIBEXEC/xendomains helper is shared between init and +# systemd systems. +### END INIT INFO + +. @XEN_SCRIPT_DIR@/hotplugpath.sh + +case "$1" in + start) + $LIBEXEC/xendomains start + ;; + stop) + $LIBEXEC/xendomains stop + ;; + restart) + $LIBEXEC/xendomains restart + ;; + reload) + $LIBEXEC/xendomains reload + ;; + status) + $LIBEXEC/xendomains status + ;; + *) + echo "Usage: $0 {start|stop|restart|reload|status}" + exit 3 + ;; +esac +exit $? diff --git a/tools/hotplug/Linux/vif-setup b/tools/hotplug/Linux/vif-setup deleted file mode 100644 index bcc462eaf5..0000000000 --- a/tools/hotplug/Linux/vif-setup +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -if test "$script" -then - exec $script $* -else - exec /etc/xen/scripts/vif-bridge $* -fi - diff --git a/tools/hotplug/Linux/vif-setup.in b/tools/hotplug/Linux/vif-setup.in new file mode 100644 index 0000000000..c3bc66290a --- /dev/null +++ b/tools/hotplug/Linux/vif-setup.in @@ -0,0 +1,9 @@ +#!/bin/bash + +if test "$script" +then + exec $script $* +else + exec @XEN_SCRIPT_DIR@/vif-bridge $* +fi + diff --git a/tools/hotplug/Linux/xen-backend.rules b/tools/hotplug/Linux/xen-backend.rules deleted file mode 100644 index a0d409e1c2..0000000000 --- a/tools/hotplug/Linux/xen-backend.rules +++ /dev/null @@ -1,15 +0,0 @@ -SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/blktap $env{ACTION}" -SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/block $env{ACTION}" -SUBSYSTEM=="xen-backend", KERNEL=="vif2-*", RUN+="/etc/xen/scripts/vif2 $env{ACTION}" -SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{UDEV_CALL}="1", ACTION=="online", RUN+="/etc/xen/scripts/vif-setup online type_if=vif" -SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{UDEV_CALL}="1", ACTION=="offline", RUN+="/etc/xen/scripts/vif-setup offline type_if=vif" -SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="/etc/xen/scripts/vscsi $env{ACTION}" -SUBSYSTEM=="xen-backend", ACTION=="remove", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/xen-hotplug-cleanup" -KERNEL=="evtchn", NAME="xen/%k" -SUBSYSTEM=="xen", KERNEL=="blktap[0-9]*", NAME="xen/%k", MODE="0600" -SUBSYSTEM=="blktap2", KERNEL=="blktap[0-9]*", NAME="xen/blktap-2/%k", MODE="0600" -KERNEL=="blktap-control", NAME="xen/blktap-2/control", MODE="0600" -KERNEL=="gntdev", NAME="xen/%k", MODE="0600" -KERNEL=="pci_iomul", NAME="xen/%k", MODE="0600" -KERNEL=="tapdev[a-z]*", NAME="xen/blktap-2/tapdev%m", MODE="0600" -SUBSYSTEM=="net", KERNEL=="vif*-emu", ACTION=="add", ENV{UDEV_CALL}="1", RUN+="/etc/xen/scripts/vif-setup $env{ACTION} type_if=tap" diff --git a/tools/hotplug/Linux/xen-backend.rules.in b/tools/hotplug/Linux/xen-backend.rules.in new file mode 100644 index 0000000000..7d2f914346 --- /dev/null +++ b/tools/hotplug/Linux/xen-backend.rules.in @@ -0,0 +1,15 @@ +SUBSYSTEM=="xen-backend", KERNEL=="tap*", ENV{UDEV_CALL}="1", RUN+="@XEN_SCRIPT_DIR@/blktap $env{ACTION}" +SUBSYSTEM=="xen-backend", KERNEL=="vbd*", ENV{UDEV_CALL}="1", RUN+="@XEN_SCRIPT_DIR@/block $env{ACTION}" +SUBSYSTEM=="xen-backend", KERNEL=="vif2-*", RUN+="@XEN_SCRIPT_DIR@/vif2 $env{ACTION}" +SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{UDEV_CALL}="1", ACTION=="online", RUN+="@XEN_SCRIPT_DIR@/vif-setup online type_if=vif" +SUBSYSTEM=="xen-backend", KERNEL=="vif-*", ENV{UDEV_CALL}="1", ACTION=="offline", RUN+="@XEN_SCRIPT_DIR@/vif-setup offline type_if=vif" +SUBSYSTEM=="xen-backend", KERNEL=="vscsi*", RUN+="@XEN_SCRIPT_DIR@/vscsi $env{ACTION}" +SUBSYSTEM=="xen-backend", ACTION=="remove", ENV{UDEV_CALL}="1", RUN+="@XEN_SCRIPT_DIR@/xen-hotplug-cleanup" +KERNEL=="evtchn", NAME="xen/%k" +SUBSYSTEM=="xen", KERNEL=="blktap[0-9]*", NAME="xen/%k", MODE="0600" +SUBSYSTEM=="blktap2", KERNEL=="blktap[0-9]*", NAME="xen/blktap-2/%k", MODE="0600" +KERNEL=="blktap-control", NAME="xen/blktap-2/control", MODE="0600" +KERNEL=="gntdev", NAME="xen/%k", MODE="0600" +KERNEL=="pci_iomul", NAME="xen/%k", MODE="0600" +KERNEL=="tapdev[a-z]*", NAME="xen/blktap-2/tapdev%m", MODE="0600" +SUBSYSTEM=="net", KERNEL=="vif*-emu", ACTION=="add", ENV{UDEV_CALL}="1", RUN+="@XEN_SCRIPT_DIR@/vif-setup $env{ACTION} type_if=tap" diff --git a/tools/hotplug/Linux/xen-hotplug-common.sh b/tools/hotplug/Linux/xen-hotplug-common.sh deleted file mode 100644 index 7af468836e..0000000000 --- a/tools/hotplug/Linux/xen-hotplug-common.sh +++ /dev/null @@ -1,120 +0,0 @@ -# -# Copyright (c) 2005 XenSource Ltd. -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of version 2.1 of the GNU Lesser General Public -# License as published by the Free Software Foundation. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -# Hack to prevent the execution of hotplug scripts from udev if the domain -# has been launched from libxl -if [ -n "${UDEV_CALL}" ] && \ - xenstore-read "libxl/disable_udev" >/dev/null 2>&1; then - exit 0 -fi - -dir=$(dirname "$0") -. "$dir/hotplugpath.sh" -. "$dir/logging.sh" -. "$dir/xen-script-common.sh" -. "$dir/locking.sh" - -exec 2>>/var/log/xen/xen-hotplug.log - -export PATH="${BINDIR}:${SBINDIR}:${LIBEXEC}:${PRIVATE_BINDIR}:/sbin:/bin:/usr/bin:/usr/sbin:$PATH" -export LD_LIBRARY_PATH="${LIBDIR}${LD_LIBRARY_PATH+:}$LD_LIBRARY_PATH" -export LANG="POSIX" -unset $(set | grep ^LC_ | cut -d= -f1) - -fatal() { - _xenstore_write "$XENBUS_PATH/hotplug-error" "$*" \ - "$XENBUS_PATH/hotplug-status" error - log err "$@" - exit 1 -} - -success() { - # Tell DevController that backend is "connected" - xenstore_write "$XENBUS_PATH/hotplug-status" connected -} - -do_or_die() { - "$@" || fatal "$@ failed" -} - -do_without_error() { - "$@" 2>/dev/null || log debug "$@ failed" -} - -sigerr() { - fatal "$0 failed; error detected." -} - -trap sigerr ERR - - -## -# xenstore_read + -# -# Read each of the given paths, returning each result on a separate line, or -# exit this script if any of the paths is missing. -# -xenstore_read() { - local v=$(xenstore-read "$@" || true) - [ "$v" != "" ] || fatal "xenstore-read $@ failed." - echo "$v" -} - - -## -# xenstore_read_default -# -# Read the given path, returning the value there or the given default if the -# path is not present. -# -xenstore_read_default() { - xenstore-read "$1" 2>/dev/null || echo "$2" -} - - -## -# _xenstore_write ( )+ -# -# Write each of the key/value pairs to the store. -# -_xenstore_write() { - log debug "Writing $@ to xenstore." - xenstore-write "$@" -} - -## -# xenstore_write ( )+ -# -# Write each of the key/value pairs to the store, and exit this script if any -# such writing fails. -# -xenstore_write() { - _xenstore_write "$@" || fatal "Writing $@ to xenstore failed." -} - -## -# call_hooks -# -# Execute each hook in the directory. -# -call_hooks() { - for f in /etc/xen/scripts/${1}-${2}.d/*.hook; do - if [ -x "$f" ]; then . "$f"; fi - done -} - -log debug "$@" "XENBUS_PATH=$XENBUS_PATH" diff --git a/tools/hotplug/Linux/xen-hotplug-common.sh.in b/tools/hotplug/Linux/xen-hotplug-common.sh.in new file mode 100644 index 0000000000..996e974973 --- /dev/null +++ b/tools/hotplug/Linux/xen-hotplug-common.sh.in @@ -0,0 +1,120 @@ +# +# Copyright (c) 2005 XenSource Ltd. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of version 2.1 of the GNU Lesser General Public +# License as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +# Hack to prevent the execution of hotplug scripts from udev if the domain +# has been launched from libxl +if [ -n "${UDEV_CALL}" ] && \ + xenstore-read "libxl/disable_udev" >/dev/null 2>&1; then + exit 0 +fi + +dir=$(dirname "$0") +. "$dir/hotplugpath.sh" +. "$dir/logging.sh" +. "$dir/xen-script-common.sh" +. "$dir/locking.sh" + +exec 2>>/var/log/xen/xen-hotplug.log + +export PATH="${BINDIR}:${SBINDIR}:${LIBEXEC}:${PRIVATE_BINDIR}:/sbin:/bin:/usr/bin:/usr/sbin:$PATH" +export LD_LIBRARY_PATH="${LIBDIR}${LD_LIBRARY_PATH+:}$LD_LIBRARY_PATH" +export LANG="POSIX" +unset $(set | grep ^LC_ | cut -d= -f1) + +fatal() { + _xenstore_write "$XENBUS_PATH/hotplug-error" "$*" \ + "$XENBUS_PATH/hotplug-status" error + log err "$@" + exit 1 +} + +success() { + # Tell DevController that backend is "connected" + xenstore_write "$XENBUS_PATH/hotplug-status" connected +} + +do_or_die() { + "$@" || fatal "$@ failed" +} + +do_without_error() { + "$@" 2>/dev/null || log debug "$@ failed" +} + +sigerr() { + fatal "$0 failed; error detected." +} + +trap sigerr ERR + + +## +# xenstore_read + +# +# Read each of the given paths, returning each result on a separate line, or +# exit this script if any of the paths is missing. +# +xenstore_read() { + local v=$(xenstore-read "$@" || true) + [ "$v" != "" ] || fatal "xenstore-read $@ failed." + echo "$v" +} + + +## +# xenstore_read_default +# +# Read the given path, returning the value there or the given default if the +# path is not present. +# +xenstore_read_default() { + xenstore-read "$1" 2>/dev/null || echo "$2" +} + + +## +# _xenstore_write ( )+ +# +# Write each of the key/value pairs to the store. +# +_xenstore_write() { + log debug "Writing $@ to xenstore." + xenstore-write "$@" +} + +## +# xenstore_write ( )+ +# +# Write each of the key/value pairs to the store, and exit this script if any +# such writing fails. +# +xenstore_write() { + _xenstore_write "$@" || fatal "Writing $@ to xenstore failed." +} + +## +# call_hooks +# +# Execute each hook in the directory. +# +call_hooks() { + for f in @XEN_SCRIPT_DIR@/${1}-${2}.d/*.hook; do + if [ -x "$f" ]; then . "$f"; fi + done +} + +log debug "$@" "XENBUS_PATH=$XENBUS_PATH" diff --git a/tools/hotplug/Linux/xendomains b/tools/hotplug/Linux/xendomains deleted file mode 100644 index c45f13277d..0000000000 --- a/tools/hotplug/Linux/xendomains +++ /dev/null @@ -1,579 +0,0 @@ -#!/bin/bash -# -# /etc/init.d/xendomains -# Start / stop domains automatically when domain 0 boots / shuts down. -# -# chkconfig: 345 99 00 -# description: Start / stop Xen domains. -# -# This script offers fairly basic functionality. It should work on Redhat -# but also on LSB-compliant SuSE releases and on Debian with the LSB package -# installed. (LSB is the Linux Standard Base) -# -# Based on the example in the "Designing High Quality Integrated Linux -# Applications HOWTO" by Avi Alkalay -# -# -### BEGIN INIT INFO -# Provides: xendomains -# Required-Start: $syslog $remote_fs xenstored xenconsoled -# Should-Start: xend -# Required-Stop: $syslog $remote_fs xenstored xenconsoled -# Should-Stop: xend -# Default-Start: 2 3 5 -# Default-Stop: 0 1 6 -# Short-Description: Start/stop secondary xen domains -# Description: Start / stop domains automatically when domain 0 -# boots / shuts down. -### END INIT INFO - -. /etc/xen/scripts/hotplugpath.sh - -CMD=${SBINDIR}/xl -HEADCOMP="Xen saved domain" -$CMD list &> /dev/null -if test $? -ne 0 -then - exit $? -fi - -$CMD list &> /dev/null -if test $? -ne 0 -then - exit 0; -fi - -# Correct exit code would probably be 5, but it's enough -# if xend complains if we're not running as privileged domain -if ! [ -e /proc/xen/privcmd ]; then - exit 0 -fi - -LOCKFILE=${XEN_LOCK_DIR}/xendomains - -if [ -d /etc/sysconfig ]; then - XENDOM_CONFIG=/etc/sysconfig/xendomains -else - XENDOM_CONFIG=/etc/default/xendomains -fi - -test -r $XENDOM_CONFIG || { echo "$XENDOM_CONFIG not existing"; - if [ "$1" = "stop" ]; then exit 0; - else exit 6; fi; } - -. $XENDOM_CONFIG - -# Use the SUSE rc_ init script functions; -# emulate them on LSB, RH and other systems -if test -e /etc/rc.status; then - # SUSE rc script library - . /etc/rc.status -else - _cmd=$1 - declare -a _SMSG - if test "${_cmd}" = "status"; then - _SMSG=(running dead dead unused unknown) - _RC_UNUSED=3 - else - _SMSG=(done failed failed missed failed skipped unused failed failed) - _RC_UNUSED=6 - fi - if test -e /etc/init.d/functions; then - # REDHAT - . /etc/init.d/functions - echo_rc() - { - #echo -n " [${_SMSG[${_RC_RV}]}] " - if test ${_RC_RV} = 0; then - success " [${_SMSG[${_RC_RV}]}] " - else - failure " [${_SMSG[${_RC_RV}]}] " - fi - } - elif test -e /lib/lsb/init-functions; then - # LSB - . /lib/lsb/init-functions - if alias log_success_msg >/dev/null 2>/dev/null; then - echo_rc() - { - echo " [${_SMSG[${_RC_RV}]}] " - } - else - echo_rc() - { - if test ${_RC_RV} = 0; then - log_success_msg " [${_SMSG[${_RC_RV}]}] " - else - log_failure_msg " [${_SMSG[${_RC_RV}]}] " - fi - } - fi - else - # emulate it - echo_rc() - { - echo " [${_SMSG[${_RC_RV}]}] " - } - fi - rc_reset() { _RC_RV=0; } - rc_failed() - { - if test -z "$1"; then - _RC_RV=1; - elif test "$1" != "0"; then - _RC_RV=$1; - fi - return ${_RC_RV} - } - rc_check() - { - return rc_failed $? - } - rc_status() - { - rc_failed $? - if test "$1" = "-r"; then _RC_RV=0; shift; fi - if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi - if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi - if test "$1" = "-v"; then echo_rc; shift; fi - if test "$1" = "-r"; then _RC_RV=0; shift; fi - return ${_RC_RV} - } - rc_exit() { exit ${_RC_RV}; } - rc_active() - { - if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi - if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi - return 1 - } -fi - -if ! which usleep >&/dev/null -then - usleep() - { - if [ -n "$1" ] - then - sleep $(( $1 / 1000000 )) - fi - } -fi - -# Reset status of this service -rc_reset - -## -# Returns 0 (success) if the given parameter names a directory, and that -# directory is not empty. -# -contains_something() -{ - if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ] - then - return 0 - else - return 1 - fi -} - -# read name from xen config file -rdname() -{ - NM=$($CMD create --quiet --dryrun --defconfig "$1" | - sed -n 's/^.*(name \(.*\))$/\1/p;s/^.*"name": "\(.*\)",$/\1/p') -} - -rdnames() -{ - NAMES= - if ! contains_something "$XENDOMAINS_AUTO" - then - return - fi - for dom in $XENDOMAINS_AUTO/*; do - rdname $dom - if test -z $NAMES; then - NAMES=$NM; - else - NAMES="$NAMES|$NM" - fi - done -} - -LIST_GREP='(domain\|(domid\|(name\|^ {$\|"name":\|"domid":' -parseln() -{ - if [[ "$1" =~ '(domain' ]] || [[ "$1" = "{" ]]; then - name=;id= - elif [[ "$1" =~ '(name' ]]; then - name=$(echo $1 | sed -e 's/^.*(name \(.*\))$/\1/') - elif [[ "$1" =~ '(domid' ]]; then - id=$(echo $1 | sed -e 's/^.*(domid \(.*\))$/\1/') - elif [[ "$1" =~ '"name":' ]]; then - name=$(echo $1 | sed -e 's/^.*"name": "\(.*\)",$/\1/') - elif [[ "$1" =~ '"domid":' ]]; then - id=$(echo $1 | sed -e 's/^.*"domid": \(.*\),$/\1/') - fi - - [ -n "$name" -a -n "$id" ] && return 0 || return 1 -} - -is_running() -{ - rdname $1 - RC=1 - name=;id= - while read LN; do - parseln "$LN" || continue - if test $id = 0; then continue; fi - case $name in - ($NM) - RC=0 - ;; - esac - done < <($CMD list -l | grep "$LIST_GREP") - return $RC -} - -start() -{ - if [ -f $LOCKFILE ]; then - echo -e "xendomains already running (lockfile exists)" - return; - fi - - saved_domains=" " - if [ "$XENDOMAINS_RESTORE" = "true" ] && - contains_something "$XENDOMAINS_SAVE" - then - mkdir -p $(dirname "$LOCKFILE") - touch $LOCKFILE - echo -n "Restoring Xen domains:" - saved_domains=`ls $XENDOMAINS_SAVE` - for dom in $XENDOMAINS_SAVE/*; do - if [ -f $dom ] ; then - HEADER=`head -c 16 $dom | head -n 1 2> /dev/null` - if [ "$HEADER" = "$HEADCOMP" ]; then - echo -n " ${dom##*/}" - XMR=`$CMD restore $dom 2>&1 1>/dev/null` - #$CMD restore $dom - if [ $? -ne 0 ]; then - echo -e "\nAn error occurred while restoring domain ${dom##*/}:\n$XMR" - rc_failed $? - echo -e '!' - else - # mv $dom ${dom%/*}/.${dom##*/} - rm $dom - fi - fi - fi - done - echo -e - fi - - if contains_something "$XENDOMAINS_AUTO" - then - touch $LOCKFILE - echo -n "Starting auto Xen domains:" - # We expect config scripts for auto starting domains to be in - # XENDOMAINS_AUTO - they could just be symlinks to files elsewhere - - # Create all domains with config files in XENDOMAINS_AUTO. - # TODO: We should record which domain name belongs - # so we have the option to selectively shut down / migrate later - # If a domain statefile from $XENDOMAINS_SAVE matches a domain name - # in $XENDOMAINS_AUTO, do not try to start that domain; if it didn't - # restore correctly it requires administrative attention. - for dom in $XENDOMAINS_AUTO/*; do - echo -n " ${dom##*/}" - shortdom=$(echo $dom | sed -n 's/^.*\/\(.*\)$/\1/p') - echo $saved_domains | grep -w $shortdom > /dev/null - if [ $? -eq 0 ] || is_running $dom; then - echo -n "(skip)" - else - XMC=`$CMD create --quiet --defconfig $dom` - if [ $? -ne 0 ]; then - echo -e "\nAn error occurred while creating domain ${dom##*/}: $XMC\n" - rc_failed $? - echo -e '!' - else - usleep $XENDOMAINS_CREATE_USLEEP - fi - fi - done - fi -} - -all_zombies() -{ - name=;id= - while read LN; do - parseln "$LN" || continue - if test $id = 0; then continue; fi - if test "$state" != "-b---d" -a "$state" != "-----d"; then - return 1; - fi - done < <($CMD list -l | grep "$LIST_GREP") - return 0 -} - -# Wait for max $XENDOMAINS_STOP_MAXWAIT for $CMD $1 to finish; -# if it has not exited by that time kill it, so the init script will -# succeed within a finite amount of time; if $2 is nonnull, it will -# kill the command as well as soon as no domain (except for zombies) -# are left (used for shutdown --all). Third parameter, if any, suppresses -# output of dots per working state (formatting issues) -watchdog_xencmd() -{ - if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then - exit - fi - - usleep 20000 - for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do - # exit if $CMD save/migrate/shutdown is finished - PSAX=`ps axlw | grep "$CMD $1" | grep -v grep` - if test -z "$PSAX"; then exit; fi - if ! test -n "$3"; then echo -n '.'; fi - sleep 1 - # go to kill immediately if there's only zombies left - if all_zombies && test -n "$2"; then break; fi - done - sleep 1 - read PSF PSUID PSPID PSPPID < <(echo "$PSAX") - # kill $CMD $1 - kill $PSPID >/dev/null 2>&1 - - echo -e . -} - -stop() -{ - exec 3>&2 2> /dev/null - - # Collect list of domains to shut down - if test "$XENDOMAINS_AUTO_ONLY" = "true"; then - rdnames - fi - echo -n "Shutting down Xen domains:" - name=;id= - while read LN; do - parseln "$LN" || continue - if test $id = 0; then continue; fi - echo -n " $name" - if test "$XENDOMAINS_AUTO_ONLY" = "true"; then - eval " - case \"\$name\" in - ($NAMES) - # nothing - ;; - (*) - echo -e '(skip)' - continue - ;; - esac - " - fi - # XENDOMAINS_SYSRQ chould be something like just "s" - # or "s e i u" or even "s e s i u o" - # for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so - if test -n "$XENDOMAINS_SYSRQ"; then - for sysrq in $XENDOMAINS_SYSRQ; do - echo -n "(SR-$sysrq)" - XMR=`$CMD sysrq $id $sysrq 2>&1 1>/dev/null` - if test $? -ne 0; then - echo -e "\nAn error occurred while doing sysrq on domain:\n$XMR\n" - rc_failed $? - echo -n '!' - fi - # usleep just ignores empty arg - usleep $XENDOMAINS_USLEEP - done - fi - if test "$state" = "-b---d" -o "$state" = "-----d"; then - echo -n "(zomb)" - continue - fi - if test -n "$XENDOMAINS_MIGRATE"; then - echo -n "(migr)" - watchdog_xencmd migrate & - WDOG_PID=$! - XMR=`$CMD migrate $id $XENDOMAINS_MIGRATE 2>&1 1>/dev/null` - if test $? -ne 0; then - echo -e "\nAn error occurred while migrating domain:\n$XMR\n" - rc_failed $? - echo -e '!' - - kill $WDOG_PID >/dev/null 2>&1 - else - kill $WDOG_PID >/dev/null 2>&1 - - echo -e . - usleep 1000 - continue - fi - fi - if test -n "$XENDOMAINS_SAVE"; then - echo -n "(save)" - watchdog_xencmd save & - WDOG_PID=$! - mkdir -p "$XENDOMAINS_SAVE" - XMR=`$CMD save $id $XENDOMAINS_SAVE/$name 2>&1 1>/dev/null` - if test $? -ne 0; then - echo -e "\nAn error occurred while saving domain:\n$XMR\n" - rc_failed $? - echo -e '!' - kill $WDOG_PID >/dev/null 2>&1 - else - kill $WDOG_PID >/dev/null 2>&1 - echo -e . - usleep 1000 - continue - fi - fi - if test -n "$XENDOMAINS_SHUTDOWN"; then - # XENDOMAINS_SHUTDOWN should be "--wait" - echo -n "(shut)" - watchdog_xencmd shutdown & - WDOG_PID=$! - XMR=`$CMD shutdown $XENDOMAINS_SHUTDOWN $id 2>&1 1>/dev/null` - if test $? -ne 0; then - echo -e "\nAn error occurred while shutting down domain:\n$XMR\n" - rc_failed $? - echo -e '!' - fi - kill $WDOG_PID >/dev/null 2>&1 - fi - done < <($CMD list -l | grep "$LIST_GREP") - - # NB. this shuts down ALL Xen domains (politely), not just the ones in - # AUTODIR/* - # This is because it's easier to do ;-) but arguably if this script is run - # on system shutdown then it's also the right thing to do. - if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then - # XENDOMAINS_SHUTDOWN_ALL should be "--all --wait" - echo -n " SHUTDOWN_ALL " - watchdog_xencmd shutdown 1 false & - WDOG_PID=$! - XMR=`$CMD shutdown $XENDOMAINS_SHUTDOWN_ALL 2>&1 1>/dev/null` - if test $? -ne 0; then - echo -e "\nAn error occurred while shutting down all domains: $XMR\n" - rc_failed $? - echo -e '!' - fi - kill $WDOG_PID >/dev/null 2>&1 - fi - - # Unconditionally delete lock file - rm -f $LOCKFILE - - exec 2>&3 -} - -check_domain_up() -{ - name=;id= - while read LN; do - parseln "$LN" || continue - if test $id = 0; then continue; fi - case $name in - ($1) - return 0 - ;; - esac - done < <($CMD list -l | grep "$LIST_GREP") - return 1 -} - -check_all_auto_domains_up() -{ - if ! contains_something "$XENDOMAINS_AUTO" - then - return 0 - fi - missing= - for nm in $XENDOMAINS_AUTO/*; do - rdname $nm - found=0 - if check_domain_up "$NM"; then - echo -n " $name" - else - missing="$missing $NM" - fi - done - if test -n "$missing"; then - echo -n " MISS AUTO:$missing" - return 1 - fi - return 0 -} - -check_all_saved_domains_up() -{ - if ! contains_something "$XENDOMAINS_SAVE" - then - return 0 - fi - missing=`/bin/ls $XENDOMAINS_SAVE` - echo -n " MISS SAVED: " $missing - return 1 -} - -# This does NOT necessarily restart all running domains: instead it -# stops all running domains and then boots all the domains specified in -# AUTODIR. If other domains have been started manually then they will -# not get restarted. -# Commented out to avoid confusion! - -restart() -{ - stop - start -} - -reload() -{ - restart -} - - -case "$1" in - start) - start - rc_status - if test -f $LOCKFILE; then rc_status -v; fi - ;; - - stop) - stop - rc_status -v - ;; - - restart) - restart - ;; - reload) - reload - ;; - - status) - echo -n "Checking for xendomains:" - if test ! -f $LOCKFILE; then - rc_failed 3 - else - check_all_auto_domains_up - rc_status - check_all_saved_domains_up - rc_status - fi - rc_status -v - ;; - - *) - echo "Usage: $0 {start|stop|restart|reload|status}" - rc_failed 3 - rc_status -v - ;; -esac - -rc_exit diff --git a/tools/hotplug/Linux/xendomains.in b/tools/hotplug/Linux/xendomains.in new file mode 100644 index 0000000000..de711b7d77 --- /dev/null +++ b/tools/hotplug/Linux/xendomains.in @@ -0,0 +1,579 @@ +#!/bin/bash +# +# /etc/init.d/xendomains +# Start / stop domains automatically when domain 0 boots / shuts down. +# +# chkconfig: 345 99 00 +# description: Start / stop Xen domains. +# +# This script offers fairly basic functionality. It should work on Redhat +# but also on LSB-compliant SuSE releases and on Debian with the LSB package +# installed. (LSB is the Linux Standard Base) +# +# Based on the example in the "Designing High Quality Integrated Linux +# Applications HOWTO" by Avi Alkalay +# +# +### BEGIN INIT INFO +# Provides: xendomains +# Required-Start: $syslog $remote_fs xenstored xenconsoled +# Should-Start: xend +# Required-Stop: $syslog $remote_fs xenstored xenconsoled +# Should-Stop: xend +# Default-Start: 2 3 5 +# Default-Stop: 0 1 6 +# Short-Description: Start/stop secondary xen domains +# Description: Start / stop domains automatically when domain 0 +# boots / shuts down. +### END INIT INFO + +. @XEN_SCRIPT_DIR@/hotplugpath.sh + +CMD=${SBINDIR}/xl +HEADCOMP="Xen saved domain" +$CMD list &> /dev/null +if test $? -ne 0 +then + exit $? +fi + +$CMD list &> /dev/null +if test $? -ne 0 +then + exit 0; +fi + +# Correct exit code would probably be 5, but it's enough +# if xend complains if we're not running as privileged domain +if ! [ -e /proc/xen/privcmd ]; then + exit 0 +fi + +LOCKFILE=${XEN_LOCK_DIR}/xendomains + +if [ -d /etc/sysconfig ]; then + XENDOM_CONFIG=/etc/sysconfig/xendomains +else + XENDOM_CONFIG=/etc/default/xendomains +fi + +test -r $XENDOM_CONFIG || { echo "$XENDOM_CONFIG not existing"; + if [ "$1" = "stop" ]; then exit 0; + else exit 6; fi; } + +. $XENDOM_CONFIG + +# Use the SUSE rc_ init script functions; +# emulate them on LSB, RH and other systems +if test -e /etc/rc.status; then + # SUSE rc script library + . /etc/rc.status +else + _cmd=$1 + declare -a _SMSG + if test "${_cmd}" = "status"; then + _SMSG=(running dead dead unused unknown) + _RC_UNUSED=3 + else + _SMSG=(done failed failed missed failed skipped unused failed failed) + _RC_UNUSED=6 + fi + if test -e /etc/init.d/functions; then + # REDHAT + . /etc/init.d/functions + echo_rc() + { + #echo -n " [${_SMSG[${_RC_RV}]}] " + if test ${_RC_RV} = 0; then + success " [${_SMSG[${_RC_RV}]}] " + else + failure " [${_SMSG[${_RC_RV}]}] " + fi + } + elif test -e /lib/lsb/init-functions; then + # LSB + . /lib/lsb/init-functions + if alias log_success_msg >/dev/null 2>/dev/null; then + echo_rc() + { + echo " [${_SMSG[${_RC_RV}]}] " + } + else + echo_rc() + { + if test ${_RC_RV} = 0; then + log_success_msg " [${_SMSG[${_RC_RV}]}] " + else + log_failure_msg " [${_SMSG[${_RC_RV}]}] " + fi + } + fi + else + # emulate it + echo_rc() + { + echo " [${_SMSG[${_RC_RV}]}] " + } + fi + rc_reset() { _RC_RV=0; } + rc_failed() + { + if test -z "$1"; then + _RC_RV=1; + elif test "$1" != "0"; then + _RC_RV=$1; + fi + return ${_RC_RV} + } + rc_check() + { + return rc_failed $? + } + rc_status() + { + rc_failed $? + if test "$1" = "-r"; then _RC_RV=0; shift; fi + if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi + if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi + if test "$1" = "-v"; then echo_rc; shift; fi + if test "$1" = "-r"; then _RC_RV=0; shift; fi + return ${_RC_RV} + } + rc_exit() { exit ${_RC_RV}; } + rc_active() + { + if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi + if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi + return 1 + } +fi + +if ! which usleep >&/dev/null +then + usleep() + { + if [ -n "$1" ] + then + sleep $(( $1 / 1000000 )) + fi + } +fi + +# Reset status of this service +rc_reset + +## +# Returns 0 (success) if the given parameter names a directory, and that +# directory is not empty. +# +contains_something() +{ + if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ] + then + return 0 + else + return 1 + fi +} + +# read name from xen config file +rdname() +{ + NM=$($CMD create --quiet --dryrun --defconfig "$1" | + sed -n 's/^.*(name \(.*\))$/\1/p;s/^.*"name": "\(.*\)",$/\1/p') +} + +rdnames() +{ + NAMES= + if ! contains_something "$XENDOMAINS_AUTO" + then + return + fi + for dom in $XENDOMAINS_AUTO/*; do + rdname $dom + if test -z $NAMES; then + NAMES=$NM; + else + NAMES="$NAMES|$NM" + fi + done +} + +LIST_GREP='(domain\|(domid\|(name\|^ {$\|"name":\|"domid":' +parseln() +{ + if [[ "$1" =~ '(domain' ]] || [[ "$1" = "{" ]]; then + name=;id= + elif [[ "$1" =~ '(name' ]]; then + name=$(echo $1 | sed -e 's/^.*(name \(.*\))$/\1/') + elif [[ "$1" =~ '(domid' ]]; then + id=$(echo $1 | sed -e 's/^.*(domid \(.*\))$/\1/') + elif [[ "$1" =~ '"name":' ]]; then + name=$(echo $1 | sed -e 's/^.*"name": "\(.*\)",$/\1/') + elif [[ "$1" =~ '"domid":' ]]; then + id=$(echo $1 | sed -e 's/^.*"domid": \(.*\),$/\1/') + fi + + [ -n "$name" -a -n "$id" ] && return 0 || return 1 +} + +is_running() +{ + rdname $1 + RC=1 + name=;id= + while read LN; do + parseln "$LN" || continue + if test $id = 0; then continue; fi + case $name in + ($NM) + RC=0 + ;; + esac + done < <($CMD list -l | grep "$LIST_GREP") + return $RC +} + +start() +{ + if [ -f $LOCKFILE ]; then + echo -e "xendomains already running (lockfile exists)" + return; + fi + + saved_domains=" " + if [ "$XENDOMAINS_RESTORE" = "true" ] && + contains_something "$XENDOMAINS_SAVE" + then + mkdir -p $(dirname "$LOCKFILE") + touch $LOCKFILE + echo -n "Restoring Xen domains:" + saved_domains=`ls $XENDOMAINS_SAVE` + for dom in $XENDOMAINS_SAVE/*; do + if [ -f $dom ] ; then + HEADER=`head -c 16 $dom | head -n 1 2> /dev/null` + if [ "$HEADER" = "$HEADCOMP" ]; then + echo -n " ${dom##*/}" + XMR=`$CMD restore $dom 2>&1 1>/dev/null` + #$CMD restore $dom + if [ $? -ne 0 ]; then + echo -e "\nAn error occurred while restoring domain ${dom##*/}:\n$XMR" + rc_failed $? + echo -e '!' + else + # mv $dom ${dom%/*}/.${dom##*/} + rm $dom + fi + fi + fi + done + echo -e + fi + + if contains_something "$XENDOMAINS_AUTO" + then + touch $LOCKFILE + echo -n "Starting auto Xen domains:" + # We expect config scripts for auto starting domains to be in + # XENDOMAINS_AUTO - they could just be symlinks to files elsewhere + + # Create all domains with config files in XENDOMAINS_AUTO. + # TODO: We should record which domain name belongs + # so we have the option to selectively shut down / migrate later + # If a domain statefile from $XENDOMAINS_SAVE matches a domain name + # in $XENDOMAINS_AUTO, do not try to start that domain; if it didn't + # restore correctly it requires administrative attention. + for dom in $XENDOMAINS_AUTO/*; do + echo -n " ${dom##*/}" + shortdom=$(echo $dom | sed -n 's/^.*\/\(.*\)$/\1/p') + echo $saved_domains | grep -w $shortdom > /dev/null + if [ $? -eq 0 ] || is_running $dom; then + echo -n "(skip)" + else + XMC=`$CMD create --quiet --defconfig $dom` + if [ $? -ne 0 ]; then + echo -e "\nAn error occurred while creating domain ${dom##*/}: $XMC\n" + rc_failed $? + echo -e '!' + else + usleep $XENDOMAINS_CREATE_USLEEP + fi + fi + done + fi +} + +all_zombies() +{ + name=;id= + while read LN; do + parseln "$LN" || continue + if test $id = 0; then continue; fi + if test "$state" != "-b---d" -a "$state" != "-----d"; then + return 1; + fi + done < <($CMD list -l | grep "$LIST_GREP") + return 0 +} + +# Wait for max $XENDOMAINS_STOP_MAXWAIT for $CMD $1 to finish; +# if it has not exited by that time kill it, so the init script will +# succeed within a finite amount of time; if $2 is nonnull, it will +# kill the command as well as soon as no domain (except for zombies) +# are left (used for shutdown --all). Third parameter, if any, suppresses +# output of dots per working state (formatting issues) +watchdog_xencmd() +{ + if test -z "$XENDOMAINS_STOP_MAXWAIT" -o "$XENDOMAINS_STOP_MAXWAIT" = "0"; then + exit + fi + + usleep 20000 + for no in `seq 0 $XENDOMAINS_STOP_MAXWAIT`; do + # exit if $CMD save/migrate/shutdown is finished + PSAX=`ps axlw | grep "$CMD $1" | grep -v grep` + if test -z "$PSAX"; then exit; fi + if ! test -n "$3"; then echo -n '.'; fi + sleep 1 + # go to kill immediately if there's only zombies left + if all_zombies && test -n "$2"; then break; fi + done + sleep 1 + read PSF PSUID PSPID PSPPID < <(echo "$PSAX") + # kill $CMD $1 + kill $PSPID >/dev/null 2>&1 + + echo -e . +} + +stop() +{ + exec 3>&2 2> /dev/null + + # Collect list of domains to shut down + if test "$XENDOMAINS_AUTO_ONLY" = "true"; then + rdnames + fi + echo -n "Shutting down Xen domains:" + name=;id= + while read LN; do + parseln "$LN" || continue + if test $id = 0; then continue; fi + echo -n " $name" + if test "$XENDOMAINS_AUTO_ONLY" = "true"; then + eval " + case \"\$name\" in + ($NAMES) + # nothing + ;; + (*) + echo -e '(skip)' + continue + ;; + esac + " + fi + # XENDOMAINS_SYSRQ chould be something like just "s" + # or "s e i u" or even "s e s i u o" + # for the latter, you should set XENDOMAINS_USLEEP to 1200000 or so + if test -n "$XENDOMAINS_SYSRQ"; then + for sysrq in $XENDOMAINS_SYSRQ; do + echo -n "(SR-$sysrq)" + XMR=`$CMD sysrq $id $sysrq 2>&1 1>/dev/null` + if test $? -ne 0; then + echo -e "\nAn error occurred while doing sysrq on domain:\n$XMR\n" + rc_failed $? + echo -n '!' + fi + # usleep just ignores empty arg + usleep $XENDOMAINS_USLEEP + done + fi + if test "$state" = "-b---d" -o "$state" = "-----d"; then + echo -n "(zomb)" + continue + fi + if test -n "$XENDOMAINS_MIGRATE"; then + echo -n "(migr)" + watchdog_xencmd migrate & + WDOG_PID=$! + XMR=`$CMD migrate $id $XENDOMAINS_MIGRATE 2>&1 1>/dev/null` + if test $? -ne 0; then + echo -e "\nAn error occurred while migrating domain:\n$XMR\n" + rc_failed $? + echo -e '!' + + kill $WDOG_PID >/dev/null 2>&1 + else + kill $WDOG_PID >/dev/null 2>&1 + + echo -e . + usleep 1000 + continue + fi + fi + if test -n "$XENDOMAINS_SAVE"; then + echo -n "(save)" + watchdog_xencmd save & + WDOG_PID=$! + mkdir -p "$XENDOMAINS_SAVE" + XMR=`$CMD save $id $XENDOMAINS_SAVE/$name 2>&1 1>/dev/null` + if test $? -ne 0; then + echo -e "\nAn error occurred while saving domain:\n$XMR\n" + rc_failed $? + echo -e '!' + kill $WDOG_PID >/dev/null 2>&1 + else + kill $WDOG_PID >/dev/null 2>&1 + echo -e . + usleep 1000 + continue + fi + fi + if test -n "$XENDOMAINS_SHUTDOWN"; then + # XENDOMAINS_SHUTDOWN should be "--wait" + echo -n "(shut)" + watchdog_xencmd shutdown & + WDOG_PID=$! + XMR=`$CMD shutdown $XENDOMAINS_SHUTDOWN $id 2>&1 1>/dev/null` + if test $? -ne 0; then + echo -e "\nAn error occurred while shutting down domain:\n$XMR\n" + rc_failed $? + echo -e '!' + fi + kill $WDOG_PID >/dev/null 2>&1 + fi + done < <($CMD list -l | grep "$LIST_GREP") + + # NB. this shuts down ALL Xen domains (politely), not just the ones in + # AUTODIR/* + # This is because it's easier to do ;-) but arguably if this script is run + # on system shutdown then it's also the right thing to do. + if ! all_zombies && test -n "$XENDOMAINS_SHUTDOWN_ALL"; then + # XENDOMAINS_SHUTDOWN_ALL should be "--all --wait" + echo -n " SHUTDOWN_ALL " + watchdog_xencmd shutdown 1 false & + WDOG_PID=$! + XMR=`$CMD shutdown $XENDOMAINS_SHUTDOWN_ALL 2>&1 1>/dev/null` + if test $? -ne 0; then + echo -e "\nAn error occurred while shutting down all domains: $XMR\n" + rc_failed $? + echo -e '!' + fi + kill $WDOG_PID >/dev/null 2>&1 + fi + + # Unconditionally delete lock file + rm -f $LOCKFILE + + exec 2>&3 +} + +check_domain_up() +{ + name=;id= + while read LN; do + parseln "$LN" || continue + if test $id = 0; then continue; fi + case $name in + ($1) + return 0 + ;; + esac + done < <($CMD list -l | grep "$LIST_GREP") + return 1 +} + +check_all_auto_domains_up() +{ + if ! contains_something "$XENDOMAINS_AUTO" + then + return 0 + fi + missing= + for nm in $XENDOMAINS_AUTO/*; do + rdname $nm + found=0 + if check_domain_up "$NM"; then + echo -n " $name" + else + missing="$missing $NM" + fi + done + if test -n "$missing"; then + echo -n " MISS AUTO:$missing" + return 1 + fi + return 0 +} + +check_all_saved_domains_up() +{ + if ! contains_something "$XENDOMAINS_SAVE" + then + return 0 + fi + missing=`/bin/ls $XENDOMAINS_SAVE` + echo -n " MISS SAVED: " $missing + return 1 +} + +# This does NOT necessarily restart all running domains: instead it +# stops all running domains and then boots all the domains specified in +# AUTODIR. If other domains have been started manually then they will +# not get restarted. +# Commented out to avoid confusion! + +restart() +{ + stop + start +} + +reload() +{ + restart +} + + +case "$1" in + start) + start + rc_status + if test -f $LOCKFILE; then rc_status -v; fi + ;; + + stop) + stop + rc_status -v + ;; + + restart) + restart + ;; + reload) + reload + ;; + + status) + echo -n "Checking for xendomains:" + if test ! -f $LOCKFILE; then + rc_failed 3 + else + check_all_auto_domains_up + rc_status + check_all_saved_domains_up + rc_status + fi + rc_status -v + ;; + + *) + echo "Usage: $0 {start|stop|restart|reload|status}" + rc_failed 3 + rc_status -v + ;; +esac + +rc_exit