047ef45dd70eb8e3de50d350a0f5547e332da42d
[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
18
19 function set_helper()
20 {
21     local f="/proc/sys/kernel/vshelper"
22     if test -e "$f"; then
23         echo "$_VSHELPER" > "$f"
24         return 0
25     else
26         return 2
27     fi
28 }
29
30 function kill_contexts()
31 {
32     local xid
33     for xid in `ls -1 /proc/virtual`; do
34         test "$xid" = "info" -o "$xid" = "status" && continue
35         $_VATTRIBUTE --xid $xid --set --flag ~persistent
36         $_VKILL --xid $xid -s 15
37         sleep 3
38         $_VKILL --xid $xid -s 9
39     done
40     local alive=0
41     for xid in `ls -1 /proc/virtual`; do
42         test "$xid" = "info" -o "$xid" = "status" && continue
43         let alive+=1
44     done
45     test $alive = 0
46 }
47
48 function start()
49 {
50     _beginResult $"Setting path to vshelper"
51     set_helper
52     _endResult $?
53     local retval=$?
54     test "$retval" -ne 0 || touch "$lockfile"
55     return $retval
56 }
57
58 function stop()
59 {
60     # Stop all running, but non-default guests"
61     _beginResult $"Stopping all running guests"
62     $_START_VSERVERS -j 1 --all --stop
63     _endResult $?
64     local retval=$?
65     _beginResult $"Killing all running contexts"
66     kill_contexts
67     _endResult $?
68     $_RM -f "$lockfile"
69 }
70
71 function restart()
72 {
73     stop
74     start
75 }
76
77 case "$1" in
78     start|stop|restart) $1;;
79     reload)             ;;
80     condrestart)
81         test -f $lockfile && restart || :
82         ;;
83     status)
84         test -f $lockfile && {
85             echo $"/proc entries were fixed"
86             exit 0
87         }
88         echo $"/proc entries are not fixed"
89         exit 1
90         ;;
91     *)
92         echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
93         exit 2
94         ;;
95 esac