-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxc-stop-vm
executable file
·52 lines (44 loc) · 1.06 KB
/
lxc-stop-vm
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
#!/bin/sh
# Note: assuming uid==0 is root -- might break with userns??
if [ "$(id -u)" != "0" ]; then
echo "This script should be run as 'root'"
exit 1
fi
function usage() {
cat << EOF
Usage:
$0 -n <container-name> stop the container with shutdown agent
EOF
exit 1
}
container=""
while getopts ":n:" opt; do
case $opt in
n)
container=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done
[[ ${container} == "" ]] && usage
retcode=0
typeset -i PID=0
lxc-ps -C init -opid | while read mycontainer PID ;do
[[ $PID -gt 1 ]] || continue
[[ "$mycontainer" != "$container" ]] && continue
/usr/bin/logger -t LXC-STOP "[${container}] Sending PWR signal to PID: ${PID}."
kill -PWR $PID
/usr/bin/logger -t LXC-STOP "[${container}] Waiting for container to stop."
lxc-wait --name=${container} --state=STOPPED
exit 1
done
found=$?
[[ ${found} -ne 1 ]] && echo "Container ${container} not found !" && retcode=1
exit ${retcode}