Skip to content

Commit

Permalink
Add lxc-shutdown script
Browse files Browse the repository at this point in the history
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@ubuntu.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
  • Loading branch information
hallyn authored and dlezcano committed Jul 31, 2012
1 parent 427bffc commit 72be4f8
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 0 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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
])
Expand Down
97 changes: 97 additions & 0 deletions doc/lxc-shutdown.sgml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!--

Copyright (C) 2012 Canonical, Inc

Authors: Serge Hallyn <serge.hallyn@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@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:
-->
1 change: 1 addition & 0 deletions lxc.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/lxc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ bin_SCRIPTS = \
lxc-version \
lxc-create \
lxc-clone \
lxc-shutdown \
lxc-destroy

bin_PROGRAMS = \
Expand Down
147 changes: 147 additions & 0 deletions src/lxc/lxc-shutdown.in
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 72be4f8

Please sign in to comment.