forked from phbaer/lxc-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lxc-halt
executable file
·56 lines (48 loc) · 1.37 KB
/
lxc-halt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
#
# Halts a running container
#
# Triggers a 'halt' in the machine, then waits for the init process to be the
# only one left, then runs lxc-stop. There is a configurable timeout length for
# if containers are taking too long to shutdown.
#
# Copyright (c) 2010 Nigel McNie
#
# Default timeout for container shutdown
TIMEOUT=90
quiet=0
[ -f /etc/default/lxc ] && . /etc/default/lxc
if [ "$(id -u)" != "0" ]; then
echo "This script should be run as 'root'"
exit 1
fi
while getopts "qn:" flag
do
case $flag in
n) CONTAINER="$OPTARG" ;;
q) quiet=1 ;;
*) echo "Unknown flag: $flag"; exit 1; ;;
esac
done
if [ -z "$CONTAINER" ]; then
echo "lxc-halt: missing container name, use -n option"
exit 1
fi
if [ ! -d /var/lib/lxc/$CONTAINER/rootfs ]; then
echo "lxc-halt: container $CONTAINER does not exist"
exit 1
fi
# try to shut it down properly, then default on a destroy after $TIMEOUT seconds
if lxc-info -n $CONTAINER | grep -q "RUNNING" ; then
if [ $quiet -eq 0 ] ; then
echo "Shutting down $CONTAINER..."
fi
ssh -o StrictHostKeyChecking=no root@$CONTAINER "halt"
timeout $TIMEOUT lxc-wait -n $CONTAINER -s STOPPED
if [ $? -eq 124 ] ; then
echo "$CONTAINER took too long (> $TIMEOUT seconds), destroying it..."
lxc-stop -n $CONTAINER
fi
else
echo "Container $CONTAINER already shut down, skipping..."
fi