New import
[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 2>/dev/null`; 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 2>/dev/null`; do
44         test "$xid" = "info" -o "$xid" = "status" && continue
45         let alive+=1
46     done
47     test $alive = 0
48 }
49
50 function create_dirs()
51 {
52     $_MKDIR -p "$__RUNDIR" && $_MKDIR -p "$__VSHELPERSTATEDIR" && $_MKDIR -p `$_READLINK "$__PKGSTATEREVDIR"`
53 }
54
55 function mount_cgroup()
56 {
57     _generateCgroupOptions
58     test -n "$CGROUP_MNT" || return 0
59     $_MKDIR -p "$CGROUP_MNT"
60     $_MOUNT -t cgroup -o "$CGROUP_SUBSYS" vserver "$CGROUP_MNT"
61 }
62
63 function umount_cgroup()
64 {
65     _generateCgroupOptions
66     test -n "$CGROUP_MNT" || return 0
67     $_UMOUNT "$CGROUP_MNT"
68 }
69
70 function start()
71 {
72     _beginResult $"Creating required directories"
73     create_dirs
74     _endResult $?
75     _beginResult $"Setting path to vshelper"
76     set_helper
77     _endResult $?
78     local retval=$?
79     _beginResult $"Loading default device map"
80     handleDeviceMap --set 0 "$__CONFDIR/.defaults/apps/vdevmap"
81     _endResult $?
82     if hasCgroup; then
83         _beginResult $"Mounting cgroup-hierarchy"
84         mount_cgroup
85         _endResult $?
86     fi
87     test "$retval" -ne 0 || touch "$lockfile"
88     return $retval
89 }
90
91 function stop()
92 {
93     # Stop all running, but non-default guests"
94     _beginResult $"Stopping all running guests"
95     $_START_VSERVERS -j 1 --all --stop
96     _endResult $?
97     _beginResult $"Killing all running contexts"
98     kill_contexts
99     _endResult $?
100     local retval=$?
101     if hasCgroup; then
102         _beginResult $"Unmounting cgroup-hierarchy"
103         umount_cgroup
104         _endResult $?
105     fi
106     $_RM -f "$lockfile"
107     return $retval
108 }
109
110 function restart()
111 {
112     stop
113     start
114 }
115
116 case "$1" in
117     start|stop|restart) $1;;
118     reload)             ;;
119     condrestart)
120         test -f $lockfile && restart || :
121         ;;
122     status)
123         test -f $lockfile && {
124             echo $"Path to vshelper has been set"
125             exit 0
126         }
127         echo $"Path to vshelper has not been set"
128         exit 1
129         ;;
130     *)
131         echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
132         exit 2
133         ;;
134 esac