2788413e97900fb13329056a051f5408240852d3
[rrq/overlay-boot.git] / overlay-stop
1 #!/bin/sh
2 #
3 # Script to stop the nominated overlay subhost
4
5 OVERLAYDIR="$(dirname $(realpath $0))"
6
7 [ $(id -u) = 0 ] || exec sudo $0 $@
8 . $OVERLAYDIR/functions $*
9
10 CONFIG="$1"
11 [ -r "$CONFIG" ] || die "Missing configuration $CONFIG"
12
13 config NAME $(basename $1 .${1##*.})
14 config START "ssh networking"
15 config LIVE
16
17 read USPID RSPID <<EOF
18 $(is_live $NAME)
19 EOF
20
21 [ -z "$USPID" ] && echo "$NAME is snot running" >&2 && exit 1
22
23 if [ -z "$RSPID" ] ; then
24     cat <<EOF >&2
25 ***  $NAME is started (pid $USPID) but doesn't seem to be running
26 *** /.reaper and might need manual fixing.
27 EOF
28     exit 1
29 fi
30
31 # function to reverse the $* words
32 reverse() {
33     local OUT=""
34     for w in $* ; do OUT="$w $OUT" ; done
35     echo "${OUT% }"
36 }
37
38 START="$(reverse "$START")"
39 if nsenter -t $RSPID -p -m -i -u ip netns exec $NAME chroot $LIVE /bin/sh \
40            -c "for srv in $START ; do service \$srv stop ; done" ; then
41     CHILDPIDS="$(nsenter -t $RSPID -p -m ps -hopid --ppid 1)"
42     nsenter -t $RSPID -p -m kill $CHILDPIDS
43 fi