From patchwork Mon Mar 24 10:09:55 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ian Campbell X-Patchwork-Id: 26926 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-yh0-f71.google.com (mail-yh0-f71.google.com [209.85.213.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1F60A20143 for ; Mon, 24 Mar 2014 10:12:53 +0000 (UTC) Received: by mail-yh0-f71.google.com with SMTP id a41sf14461468yho.6 for ; Mon, 24 Mar 2014 03:12:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:message-id:from:to:date:in-reply-to :references:organization:mime-version:cc:subject:precedence:list-id :list-unsubscribe:list-post:list-help:list-subscribe:sender :errors-to:x-original-sender:x-original-authentication-results :mailing-list:list-archive:content-type:content-transfer-encoding; bh=XyIiBd3KRNbb+y9CCF3q/sPRmJLft9SOaCJqWxl1Ev8=; b=jkjIeUEqTDKyR2rRgWXgEOg4JjHncRClfDEjJIh3isQ0ie7Vf4c77yVpOeLlhQiV/y 4gjhYgpcWPAYLWap1RjOJd4SggFNAgnbrhNIS5E87NNu2WrREdOk0r06vjl8YoJuR5mj eauZs06PgAop6dMgwuc2wq62Vpjpt0aeHXe7X0P7M/Zpiq9Xnq2AxANmoOI5ELPxMV07 wViYJEOdFikP9BmFIniLsuoa+LRsXF6Xre+8q6nhGQadR+hwj1ecgbI3e7jjYQJ0qU5e N00/3Opc20IBrtXgybjYBS5EQPW6yldhbXH0dSeI3ANKfQudXytiv0Uwy2jcK92PZ0VP HKWg== X-Gm-Message-State: ALoCoQkqujwH5E4QPzqbuY39oIp+qj7e4+WtwP0AyEa2DLvKeGGxsu0AQdsdA+PEkTBo7P3HBEP2 X-Received: by 10.236.69.74 with SMTP id m50mr3995571yhd.0.1395655973801; Mon, 24 Mar 2014 03:12:53 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.106.165 with SMTP id e34ls1629067qgf.29.gmail; Mon, 24 Mar 2014 03:12:53 -0700 (PDT) X-Received: by 10.221.27.8 with SMTP id ro8mr110597vcb.30.1395655973670; Mon, 24 Mar 2014 03:12:53 -0700 (PDT) Received: from mail-ve0-f177.google.com (mail-ve0-f177.google.com [209.85.128.177]) by mx.google.com with ESMTPS id e10si2892862vcn.124.2014.03.24.03.12.53 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 24 Mar 2014 03:12:53 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.128.177 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.128.177; Received: by mail-ve0-f177.google.com with SMTP id sa20so5313025veb.8 for ; Mon, 24 Mar 2014 03:12:53 -0700 (PDT) X-Received: by 10.52.78.231 with SMTP id e7mr772574vdx.28.1395655973531; Mon, 24 Mar 2014 03:12:53 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.220.78.9 with SMTP id i9csp211295vck; Mon, 24 Mar 2014 03:12:53 -0700 (PDT) X-Received: by 10.224.29.4 with SMTP id o4mr52969355qac.55.1395655972514; Mon, 24 Mar 2014 03:12:52 -0700 (PDT) Received: from lists.xen.org (lists.xen.org. [50.57.142.19]) by mx.google.com with ESMTPS id 50si8194372qgh.27.2014.03.24.03.12.51 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 24 Mar 2014 03:12:52 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of xen-devel-bounces@lists.xen.org designates 50.57.142.19 as permitted sender) client-ip=50.57.142.19; Received: from localhost ([127.0.0.1] helo=lists.xen.org) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WS1pV-0008Cz-DF; Mon, 24 Mar 2014 10:10:05 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WS1pT-0008Ca-Nj for xen-devel@lists.xenproject.org; Mon, 24 Mar 2014 10:10:04 +0000 Received: from [85.158.139.211:15516] by server-1.bemta-5.messagelabs.com id 5F/41-10259-A7400335; Mon, 24 Mar 2014 10:10:02 +0000 X-Env-Sender: Ian.Campbell@citrix.com X-Msg-Ref: server-9.tower-206.messagelabs.com!1395655798!2936929!1 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n X-StarScan-Received: X-StarScan-Version: 6.11.1; banners=-,-,- X-VirusChecked: Checked Received: (qmail 11835 invoked from network); 24 Mar 2014 10:10:00 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-9.tower-206.messagelabs.com with RC4-SHA encrypted SMTP; 24 Mar 2014 10:10:00 -0000 X-IronPort-AV: E=Sophos;i="4.97,719,1389744000"; d="scan'208";a="112909796" Received: from accessns.citrite.net (HELO FTLPEX01CL02.citrite.net) ([10.9.154.239]) by FTLPIPO02.CITRIX.COM with ESMTP; 24 Mar 2014 10:09:58 +0000 Received: from [10.80.2.80] (10.80.2.80) by FTLPEX01CL02.citrite.net (10.13.107.79) with Microsoft SMTP Server id 14.2.342.4; Mon, 24 Mar 2014 06:09:57 -0400 Message-ID: <1395655795.19365.16.camel@kazak.uk.xensource.com> From: Ian Campbell To: "Luis R. Rodriguez" Date: Mon, 24 Mar 2014 10:09:55 +0000 In-Reply-To: <20140322021741.GH5011@wotan.suse.de> References: <1395262733-11885-1-git-send-email-mcgrof@do-not-panic.com> <1395262733-11885-7-git-send-email-mcgrof@do-not-panic.com> <1395416050.19839.153.camel@kazak.uk.xensource.com> <20140322021741.GH5011@wotan.suse.de> Organization: Citrix Systems, Inc. X-Mailer: Evolution 3.4.4-3 MIME-Version: 1.0 X-Originating-IP: [10.80.2.80] X-DLP: MIA1 Cc: Stefano Stabellini , Jan =?UTF-8?Q?R=C4=99korajski?= , Ian Jackson , Jacek Konieczny , M A Young , xen-devel@lists.xenproject.org, "Luis R. Rodriguez" Subject: Re: [Xen-devel] [PATCH v2 6/7] tools/xendomains: move to sbin and use init helper X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: List-Unsubscribe: , List-Post: , List-Help: , List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ian.campbell@citrix.com X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.128.177 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Archive: On Sat, 2014-03-22 at 03:17 +0100, Luis R. Rodriguez wrote: > On Fri, Mar 21, 2014 at 03:34:10PM +0000, Ian Campbell wrote: > > On Wed, 2014-03-19 at 13:58 -0700, Luis R. Rodriguez wrote: > > > Cc: Ian Jackson > > > Cc: Stefano Stabellini > > > Cc: Ian Campbell > > > Cc: Jan Rękorajski > > > Cc: M A Young > > > Cc: Jacek Konieczny > > > Cc: xen-devel@lists.xenproject.org > > > Signed-off-by: Luis R. Rodriguez > > > --- > > > tools/hotplug/Linux/Makefile | 4 +++- > > > tools/hotplug/Linux/init.d/xendomains-init | 33 +++++++++++++++++++++++++++++ > > > tools/hotplug/Linux/{init.d => }/xendomains | 0 > > > > For some reason we also have tools/hotplug/NetBSD/rc.d/xendomains. I > > presume there is a non-zero chance that it could call this new tool as > > well. > > I rather have someone with NetBSD verify and then make that change. OK > > > > 3 files changed, 36 insertions(+), 1 deletion(-) > > > create mode 100644 tools/hotplug/Linux/init.d/xendomains-init > > > rename tools/hotplug/Linux/{init.d => }/xendomains (100%) > > > > > > diff --git a/tools/hotplug/Linux/Makefile b/tools/hotplug/Linux/Makefile > > > index 47655f6..0be2e8a 100644 > > > --- a/tools/hotplug/Linux/Makefile > > > +++ b/tools/hotplug/Linux/Makefile > > > @@ -3,7 +3,8 @@ include $(XEN_ROOT)/tools/Rules.mk > > > > > > # Init scripts. > > > XEND_INITD = init.d/xend > > > -XENDOMAINS_INITD = init.d/xendomains > > > +XENDOMAINS_INITD = init.d/xendomains-init > > > > The suffix is unnecessary given the directory, isn't it? Plus it avoids > > upgrade hassle by not changing the name of the initscript. > > It does and I tried to use the same name but if one does a git mv of a file > git adds that old file name and path to a temporary .gitignore requiring you > to use git add -f on that file. It does this because if you insist you loose > the history of the file after the move. Its also why I changed the name of > this modified init. > > If you don't want to keep the history I can move forward with a mv but the > history will be lost. I did explain this in the commit log so hope is that > anyone with the same WTF reaction would understand why this as done, I know > I said WTF as well, I tried to avoid this at all costs, let me know if anyone > thinks of a better way. > > What I mean by keeping the history is you can do git log --follow on the > script after the move. The history is never lost. If git was actually losing history there would be uproar, and the structure of the repo prevents it anyway. I skanked up an example commit (see below) and git comes up with: 2 files changed, 54 insertions(+), 610 deletions(-) rewrite tools/hotplug/Linux/init.d/xendomains (91%) rename tools/hotplug/Linux/{init.d => }/xendomains (93%) and "git log --follow tools/hotplug/Linux/xendomains" works exactly as I expected, so I think things are fine. Even if this weren't the case (perhaps the copy/rename detection doesn't notice it in some versions of git) you could still get at the history manually, you just don't get it automatically, which I think is fine in this case. Ian. commit e9861de906d91fd2a3c3aad0aa5485d0f90cad67 Author: Ian Campbell Date: Mon Mar 24 10:06:45 2014 +0000 testing diff --git a/tools/hotplug/Linux/init.d/xendomains b/tools/hotplug/Linux/init.d/xendomains index 59f1e3d..c91e1a5 100644 --- a/tools/hotplug/Linux/init.d/xendomains +++ b/tools/hotplug/Linux/init.d/xendomains @@ -27,559 +27,27 @@ # boots / shuts down. ### END INIT INFO -. /etc/xen/scripts/hotplugpath.sh - -CMD=${SBINDIR}/xm -HEADCOMP="LinuxGuestRecord" -$CMD list &> /dev/null -if test $? -ne 0 -then - CMD=${SBINDIR}/xl - HEADCOMP="Xen saved domain" -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 - -# See docs/misc/distro_mapping.txt -if [ -d /var/lock/subsys ]; then - LOCKFILE=/var/lock/subsys/xendomains -else - LOCKFILE=/var/lock/xendomains -fi - -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 + foo start ;; stop) - stop - rc_status -v + foo stop ;; restart) - restart + foo restart ;; reload) - reload + foo 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 + foo 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 b/tools/hotplug/Linux/xendomains new file mode 100644 index 0000000..7364522 --- /dev/null +++ b/tools/hotplug/Linux/xendomains @@ -0,0 +1,561 @@ +#!/bin/bash +# +# $libexec/xendomains +# Start / stop domains automatically when domain 0 boots / shuts down. + +. /etc/xen/scripts/hotplugpath.sh + +CMD=${SBINDIR}/xm +HEADCOMP="LinuxGuestRecord" +$CMD list &> /dev/null +if test $? -ne 0 +then + CMD=${SBINDIR}/xl + HEADCOMP="Xen saved domain" +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 + +# See docs/misc/distro_mapping.txt +if [ -d /var/lock/subsys ]; then + LOCKFILE=/var/lock/subsys/xendomains +else + LOCKFILE=/var/lock/xendomains +fi + +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