From 72be4f89b0564bbecab3c5dc934197aefcecccc3 Mon Sep 17 00:00:00 2001 From: Serge Hallyn Date: Tue, 31 Jul 2012 16:04:33 +0200 Subject: [PATCH] Add lxc-shutdown script It optionally waits (an optional timeout # of seconds) for the container to be STOPPED. If given -r, it reboots the container (and exits immediately). I decided to add the timeout after all because it's harder to finagle into an upstart post-stop script than a full bash script. Signed-off-by: Serge Hallyn Signed-off-by: Daniel Lezcano --- configure.ac | 1 + doc/lxc-shutdown.sgml.in | 97 ++++++++++++++++++++++++++ lxc.spec.in | 1 + src/lxc/Makefile.am | 1 + src/lxc/lxc-shutdown.in | 147 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 247 insertions(+) create mode 100644 doc/lxc-shutdown.sgml.in create mode 100644 src/lxc/lxc-shutdown.in diff --git a/configure.ac b/configure.ac index 20825393c..8470e8a49 100644 --- a/configure.ac +++ b/configure.ac @@ -159,6 +159,7 @@ AC_CONFIG_FILES([ src/lxc/lxc-version src/lxc/lxc-create src/lxc/lxc-clone + src/lxc/lxc-shutdown src/lxc/lxc-destroy ]) diff --git a/doc/lxc-shutdown.sgml.in b/doc/lxc-shutdown.sgml.in new file mode 100644 index 000000000..c54e54d44 --- /dev/null +++ b/doc/lxc-shutdown.sgml.in @@ -0,0 +1,97 @@ + + + + +]> + + + + @LXC_GENERATE_DATE@ + + + lxc-shutdown + 1 + + + + lxc-shutdown + + + externally shut down or reboot a container + + + + + + lxc-shutdown -n name + -w -r + + + + + + Description + + + lxc-shutdown sends a SIGPWR signal to the + specified container to request it to cleanly shut down. If + -w is specified, then lxc-shutdown + will wait until the container has shut down before exiting. + If -r is specified, the container will be + asked to reboot (using a SIGINT signal), and -w + will be ignored. If the container ignore these signals, then + nothing will happen. In that case, you can use lxc-stop + to force the container to stop. + + + + + &commonoptions; + + &seealso; + + + Author + Serge Hallyn serge.hallyn@canonical.com + + + + + diff --git a/lxc.spec.in b/lxc.spec.in index d0bb7a121..da7339a82 100644 --- a/lxc.spec.in +++ b/lxc.spec.in @@ -83,6 +83,7 @@ rm -rf %{buildroot} %attr(4111,root,root) %{_bindir}/lxc-attach %attr(4111,root,root) %{_bindir}/lxc-create %attr(4111,root,root) %{_bindir}/lxc-clone +%attr(4111,root,root) %{_bindir}/lxc-shutdown %attr(4111,root,root) %{_bindir}/lxc-start %attr(4111,root,root) %{_bindir}/lxc-netstat %attr(4111,root,root) %{_bindir}/lxc-unshare diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index 1c26952e9..26137fc16 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -78,6 +78,7 @@ bin_SCRIPTS = \ lxc-version \ lxc-create \ lxc-clone \ + lxc-shutdown \ lxc-destroy bin_PROGRAMS = \ diff --git a/src/lxc/lxc-shutdown.in b/src/lxc/lxc-shutdown.in new file mode 100644 index 000000000..17f5e0c89 --- /dev/null +++ b/src/lxc/lxc-shutdown.in @@ -0,0 +1,147 @@ +#!/bin/bash + +# (C) Copyright Canonical 2011,2012 + +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. + +# 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 + +set -e + +usage() { + echo "usage: lxc-shutdown -n name [-w] [-r]" + echo " Cleanly shut down a container." + echo " -w: wait for shutdown to complete." + echo " -r: reboot (ignore -w)." + echo " -t timeout: wait at most timeout seconds (implies -w), then kill" + echo " the container." +} + +alarm() { + pid=$1 + timeout=$2 + sleep $timeout + kill $pid +} + +dolxcstop() +{ + echo "Calling lxc-stop on $lxc_name" + lxc-stop -n $lxc_name + exit 0 +} + +shortoptions='hn:rwt:' +longoptions='help,name:,wait,reboot,timeout:' + +timeout="-1" + +getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@") +if [ $? != 0 ]; then + usage + exit 1; +fi + +eval set -- "$getopt" + +reboot=0 +dowait=0 + +while true; do + case "$1" in + -h|--help) + help + exit 1 + ;; + -n|--name) + shift + lxc_name=$1 + shift + ;; + -w|--wait) + dowait=1 + shift + ;; + -r|--reboot) + reboot=1 + shift + ;; + -t|--timeout) + shift + timeout=$1 + dowait=1 + shift + ;; + --) + shift + break;; + *) + echo $1 + usage + exit 1 + ;; + esac +done + +if [ -z "$lxc_name" ]; then + echo "no container name specified" + usage + exit 1 +fi + +if [ "$(id -u)" != "0" ]; then + echo "This command has to be run as root" + exit 1 +fi + +type lxc-info > /dev/null || { echo "lxc-info not found."; exit 1; } +type lxc-wait > /dev/null || { echo "lxc-wait not found."; exit 1; } + +pid=`lxc-info -n $lxc_name -p 2>/dev/null | awk '{ print $2 }'` +if [ "$pid" = "-1" ]; then + echo "$lxc_name is not running" + exit 1 +fi + +if [ $reboot -eq 1 ]; then + kill -INT $pid + exit 0 +else + kill -PWR $pid +fi + +if [ $dowait -eq 0 ]; then + exit 0 +fi + +if [ $timeout != "-1" ]; then + trap dolxcstop EXIT + alarm $$ $timeout & + alarmpid=$! +fi + +while [ 1 ]; do + s=`lxc-info -s -n $lxc_name | awk '{ print $2 }'` + if [ "$s" = "STOPPED" ]; then + break; + fi + sleep 1 +done + +if [ $timeout != "-1" ]; then + kill $alarmpid +fi + +echo "Container $lxc_name has shut down" + +exit 0 -- 2.47.2