umount them all
[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 ### BEGIN INIT INFO
9 # Provides:          util-vserver
10 # Required-Start:    $remote_fs $syslog $time
11 # Required-Stop:     $remote_fs $syslog $time
12 # Default-Start:     2 3 4 5
13 # Default-Stop:      0 1 6
14 # Short-Description: Sets the path to vshelper and kills all guest processes
15 # Description:       Sets the path to vshelper and kills all guest processes
16 ### END INIT INFO
17
18 : ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
19 test -e "$UTIL_VSERVER_VARS" || {
20     echo $"Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' would be expected); aborting..." >&2
21     exit 1
22 }
23 . "$UTIL_VSERVER_VARS"
24
25 LOCKFILE=util-vserver
26 . "$_LIB_VSERVER_INIT_FUNCTIONS"
27 . "$_LIB_FUNCTIONS"
28 . "$__PKGLIBDIR/vserver.functions"
29
30
31 function set_helper()
32 {
33     local f="/proc/sys/kernel/vshelper"
34     if test -e "$f"; then
35         echo "$_VSHELPER" > "$f"
36         return 0
37     else
38         return 2
39     fi
40 }
41
42 function kill_contexts()
43 {
44     local xid
45     for xid in `ls -1 /proc/virtual 2>/dev/null`; do
46         [ "$xid" = "info" -o "$xid" = "status" ] && continue
47         $_VATTRIBUTE --xid $xid --set --flag ~persistent
48         $_VKILL --xid $xid -s 15
49         sleep 3
50         $_VKILL --xid $xid -s 9
51     done
52     local alive=0
53     for xid in `ls -1 /proc/virtual 2>/dev/null`; do
54         [ "$xid" = "info" -o "$xid" = "status" ] && continue
55         let alive+=1
56     done
57     test $alive = 0
58 }
59
60 function create_dirs()
61 {
62     $_MKDIR -p "$__RUNDIR" && $_MKDIR -p "$__VSHELPERSTATEDIR" && $_MKDIR -p `$_READLINK "$__PKGSTATEREVDIR"`
63 }
64
65 function mount_cgroup()
66 {
67     _generateCgroupOptions
68     test -n "$CGROUP_MNT" || return 0
69     $_MKDIR -p "$CGROUP_MNT"
70     if test -n "$CGROUP_MNT_PER_SS"; then
71         for ss in "${CGROUP_SUBSYS[@]}"; do
72             $_MKDIR -p "$CGROUP_MNT/$ss"
73             $_MOUNT -t cgroup -o "$ss" vserver "$CGROUP_MNT/$ss"
74         done
75     else
76         oIFS="$IFS"
77         IFS=,
78         ss="${CGROUP_SUBSYS[*]}"
79         IFS="$oIFS"
80         $_MOUNT -t cgroup -o "$ss" vserver "$CGROUP_MNT"
81     fi
82 }
83
84 function umount_cgroup()
85 {
86     _generateCgroupOptions
87     test -n "$CGROUP_MNT" || return 0
88     if test -n "$CGROUP_MNT_PER_SS"; then
89         for ss in "${CGROUP_SUBSYS[@]}"; do
90             $_UMOUNT "$CGROUP_MNT/$ss"
91         done
92     else
93         $_UMOUNT "$CGROUP_MNT"
94     fi
95 }
96
97 function start()
98 {
99     _beginResult $"Creating required directories"
100     create_dirs
101     _endResult $?
102     _beginResult $"Setting path to vshelper"
103     set_helper
104     _endResult $?
105     local retval=$?
106     _beginResult $"Loading default device map"
107     handleDeviceMap --set 0 "$__CONFDIR/.defaults/apps/vdevmap"
108     _endResult $?
109     if hasCgroup; then
110         _beginResult $"Mounting cgroup-hierarchy"
111         mount_cgroup
112         _endResult $?
113     fi
114     test "$retval" -ne 0 || touch "$lockfile"
115     return $retval
116 }
117
118 function stop()
119 {
120     # Stop all running, but non-default guests"
121     _beginResult $"Stopping all running guests"
122     $_START_VSERVERS -j 1 --all --stop
123     _endResult $?
124     _beginResult $"Killing all running contexts"
125     kill_contexts
126     _endResult $?
127     local retval=$?
128     if hasCgroup; then
129         _beginResult $"Unmounting cgroup-hierarchy"
130         umount_cgroup
131         _endResult $?
132     fi
133     $_RM -f "$lockfile"
134     return $retval
135 }
136
137 function restart()
138 {
139     stop
140     start
141 }
142
143 case "$1" in
144     start|stop|restart) $1;;
145     reload)             ;;
146     condrestart)
147         test -f $lockfile && restart || :
148         ;;
149     status)
150         test -f $lockfile && {
151             echo $"Path to vshelper has been set"
152             exit 0
153         }
154         echo $"Path to vshelper has not been set"
155         exit 1
156         ;;
157     *)
158         echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
159         exit 2
160         ;;
161 esac