84636ba92ecea69c63fccd916ce8fdc7235554a9
[util-vserver.git] / sysv / util-vserver
1 #!/bin/bash
2 #
3 # util-vserver  sets the path to vshelper and kills all guest processes
4 #
5 # chkconfig: 2345 10 90
6 # description: Sets the path to vshelper and kills all guest processes
7
8 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
9 test -e "$UTIL_VSERVER_VARS" || {
10     echo $"Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' would be expected); aborting..." >&2
11     exit 1
12 }
13 . "$UTIL_VSERVER_VARS"
14
15 LOCKFILE=util-vserver
16 . "$_LIB_VSERVER_INIT_FUNCTIONS"
17 . "$_LIB_FUNCTIONS"
18 . "$__PKGLIBDIR/vserver.functions"
19
20
21 function set_helper()
22 {
23     local f="/proc/sys/kernel/vshelper"
24     if test -e "$f"; then
25         echo "$_VSHELPER" > "$f"
26         return 0
27     else
28         return 2
29     fi
30 }
31
32 function kill_contexts()
33 {
34     local xid
35     for xid in `ls -1 /proc/virtual`; do
36         test "$xid" = "info" -o "$xid" = "status" && continue
37         $_VATTRIBUTE --xid $xid --set --flag ~persistent
38         $_VKILL --xid $xid -s 15
39         sleep 3
40         $_VKILL --xid $xid -s 9
41     done
42     local alive=0
43     for xid in `ls -1 /proc/virtual`; do
44         test "$xid" = "info" -o "$xid" = "status" && continue
45         let alive+=1
46     done
47     test $alive = 0
48 }
49
50 function start()
51 {
52     _beginResult $"Setting path to vshelper"
53     set_helper
54     _endResult $?
55     local retval=$?
56     _beginResult $"Loading default device map"
57     loadDeviceMap 0 "$__CONFDIR/.defaults/apps/vdevmap"
58     _endResult $?
59     test "$retval" -ne 0 || touch "$lockfile"
60     return $retval
61 }
62
63 function stop()
64 {
65     # Stop all running, but non-default guests"
66     _beginResult $"Stopping all running guests"
67     $_START_VSERVERS -j 1 --all --stop
68     _endResult $?
69     _beginResult $"Killing all running contexts"
70     kill_contexts
71     _endResult $?
72     local retval=$?
73     $_RM -f "$lockfile"
74     return $retval
75 }
76
77 function restart()
78 {
79     stop
80     start
81 }
82
83 case "$1" in
84     start|stop|restart) $1;;
85     reload)             ;;
86     condrestart)
87         test -f $lockfile && restart || :
88         ;;
89     status)
90         test -f $lockfile && {
91             echo $"Path to vshelper has been set"
92             exit 0
93         }
94         echo $"Path to vshelper has not been set"
95         exit 1
96         ;;
97     *)
98         echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
99         exit 2
100         ;;
101 esac