[Lxc-users] [PATCH 8/9] Add lxc-shutdown script

Serge Hallyn serge at hallyn.com
Thu Apr 26 05:09:40 UTC 2012


From: Serge Hallyn <serge.hallyn at ubuntu.com>

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 <serge.hallyn at ubuntu.com>
---
 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 e8f0cb5..72582e2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -160,6 +160,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 0000000..c54e54d
--- /dev/null
+++ b/doc/lxc-shutdown.sgml.in
@@ -0,0 +1,97 @@
+<!--
+
+Copyright (C) 2012 Canonical, Inc
+
+Authors: Serge Hallyn <serge.hallyn at canonical.com>
+
+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
+
+-->
+
+<!DOCTYPE refentry PUBLIC "-//Davenport//DTD DocBook V3.0//EN" [
+
+<!ENTITY commonoptions SYSTEM "@builddir@/common_options.sgml">
+<!ENTITY seealso SYSTEM "@builddir@/see_also.sgml">
+]>
+
+<refentry>
+
+  <docinfo><date>@LXC_GENERATE_DATE@</date></docinfo>
+
+  <refmeta>
+    <refentrytitle>lxc-shutdown</refentrytitle>
+    <manvolnum>1</manvolnum>
+  </refmeta>
+
+  <refnamediv>
+    <refname>lxc-shutdown</refname>
+
+    <refpurpose>
+      externally shut down or reboot a container
+    </refpurpose>
+  </refnamediv>
+
+  <refsynopsisdiv>
+    <cmdsynopsis>
+      <command>lxc-shutdown <replaceable>-n name</replaceable>
+      <optional>-w</optional> <optional>-r</optional>
+      </command>
+    </cmdsynopsis>
+  </refsynopsisdiv>
+
+  <refsect1>
+    <title>Description</title>
+
+    <para>
+      <command>lxc-shutdown</command> sends a SIGPWR signal to the
+      specified container to request it to cleanly shut down.  If
+      <optional>-w</optional> is specified, then <command>lxc-shutdown</command>
+      will wait until the container has shut down before exiting.
+      If <optional>-r</optional> is specified, the container will be
+      asked to reboot (using a SIGINT signal), and <optional>-w</optional>
+      will be ignored.  If the container ignore these signals, then
+      nothing will happen.  In that case, you can use <command>lxc-stop</command>
+      to force the container to stop.
+    </para>
+
+  </refsect1>
+
+  &commonoptions;
+
+  &seealso;
+
+  <refsect1>
+    <title>Author</title>
+    <para>Serge Hallyn <email>serge.hallyn at canonical.com</email></para>
+  </refsect1>
+
+</refentry>
+
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:2
+sgml-indent-data:t
+sgml-parent-document:nil
+sgml-default-dtd-file:nil
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+-->
diff --git a/lxc.spec.in b/lxc.spec.in
index d0bb7a1..da7339a 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 3a3816e..c4201ea 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 0000000..c0d1702
--- /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)
+        usage
+        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
-- 
1.7.9.5





More information about the lxc-users mailing list