-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ bin_SCRIPTS = \ | |
lxc-version \ | ||
lxc-create \ | ||
lxc-clone \ | ||
lxc-shutdown \ | ||
lxc-destroy | ||
|
||
bin_PROGRAMS = \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |