Magellan Linux

Contents of /trunk/vzctl/vz.rc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 272 - (show annotations) (download)
Sat Jul 21 22:43:26 2007 UTC (16 years, 9 months ago) by niro
File size: 8474 byte(s)
-magellan specific vzctl files

1 #!/bin/bash
2 # $Header: /root/magellan-cvs/src/vzctl/vz.rc,v 1.1 2007-07-21 22:43:26 niro Exp $
3
4 #%rlevels: 3:s 4:s 5:s 0:k 1:k 2:k 6:k
5 #%start: 96
6 #%stop: 88
7
8 #deps
9 #%needs:
10 #%before:
11 #%after:
12
13 source /etc/sysconfig/rc
14 source $rc_functions
15
16 check_config()
17 {
18 source /etc/vz/vz.conf
19
20 # if we don't want openvz running, say so
21 [ "${VIRTUOZZO}" = "yes" ] || return 1
22
23 # set default values
24 : ${VZCTL:=/usr/sbin/vzctl}
25 : ${VZQUOTA:=/usr/sbin/vzquota}
26 : ${VZVEINFO:=/proc/vz/veinfo}
27 : ${VESTAT:=/proc/vz/vestat}
28 : ${VPSCONFDIR:=/etc/vz/conf}
29 : ${VZDEV:=venet0}
30 : ${SRC_CRONSCRIPT_DIR:=/etc/vz/cron}
31 : ${DST_CRONSCRIPT_DIR:=/etc/cron.d}
32
33 if [ "${MODULES_DISABLED}" != "yes" ]
34 then
35 : ${PRELOAD_MODULES:="af_packet"}
36 : ${MODULES:="vzmon vzdquota vzdev"}
37 : ${MIGRATE_MODULES:="vzcpt vzrst"}
38 : ${NET_MODULES="vznetdev vznet"}
39
40 # check if you should load vzwdog module
41 [ "${VZWDOG}" = "yes" ] && MODULES="${MODULES} vzwdog"
42 fi
43
44 # we need a working vzctl
45 if [ ! -x "${VZCTL}" ]
46 then
47 ${FAILURE} "vzctl missing (${VZCTL})"
48 return 1
49 fi
50
51 if [ -z "${VE0CPUUNITS}" ]
52 then
53 ${WARNING} "VE0CPUUNITS is not set in /etc/conf.d/vz; using default value of 1000"
54 VE0CPUUNITS=1000
55 fi
56 return 0
57 }
58
59 # We used to install OpenVZ cron job when the vzctl package was
60 # installed, irrespective of whether OpenVZ was actually being
61 # run. Although the cron jobs didn't create any problems if someone
62 # wasn't running OpenVZ some users complained about the cron log file
63 # filling up, resource usage, and power consumption since systems
64 # wouldn't really idle. It really only makes sense to run the OpenVZ
65 # cron job if the vz service is turned on and not just merely
66 # having the package installed. This init.d script is an obvious place
67 # to install or remove the cron jobs based on the service
68 # being enabled or not.
69 setup_cron()
70 {
71 [ -z "${SRC_CRONSCRIPT_DIR}" ] && return
72 [ -d "${SRC_CRONSCRIPT_DIR}" ] || return
73 install -m644 -o root -g root ${SRC_CRONSCRIPT_DIR}/vz ${DST_CRONSCRIPT_DIR}
74 }
75
76 remove_cron()
77 {
78 [ -z "${SRC_CRONSCRIPT_DIR}" ] && return
79 [ -d "${SRC_CRONSCRIPT_DIR}" ] || return
80 cat > ${DST_CRONSCRIPT_DIR}/vz <<EOF
81 # DO NOT EDIT THIS FILE!
82 #
83 # Contents of this file managed by /etc/init.d/vz script
84 # Master copy is ${SRC_CRONSCRIPT_DIR}/vz.
85 # Consult that file for documentation.
86 EOF
87 }
88
89 setup_ve0()
90 {
91 local msg retval ve0conf="${VPSCONFDIR}/0.conf"
92
93 msg=$(${VZCTL} set 0 --cpuunits ${VE0CPUUNITS} 2>&1)
94 [ $? -ne 0 ] && ${WARNING} "vzctl set 0 --cpuunits ${VE0CPUUNITS} failed: ${msg}"
95
96 test -f ${ve0conf} || return
97 egrep -q '^ONBOOT=yes\|^ONBOOT=\"yes\"' ${ve0conf} || return
98
99 echo -en ${COLOREDSTAR}"Configuring hardware node UB resources: "
100 msg=$(${VZCTL} set 0 --reset_ub 2>&1)
101 retval=$?
102 echo "${msg}"
103 evaluate_retval ${retval}
104 }
105
106 start_net()
107 {
108 local mod
109
110 # we don't operate on a running interface
111 if /sbin/ip addr list | grep -q "venet0:.*UP" 2>/dev/null
112 then
113 return 0
114 fi
115
116 # load necessary modules
117 for mod in ${NET_MODULES}
118 do
119 /sbin/modprobe ${mod} 2>/dev/null
120 done
121
122 if [ ! -f ${VZVEINFO} ]
123 then
124 return 0
125 fi
126
127 # configure the device
128 echo -e ${COLOREDSTAR}"Bringing up interface ${VZDEV}"
129 /sbin/ip link set ${VZDEV} up
130 evaluate_retval
131
132 /sbin/ip addr add 0.0.0.0/0 dev ${VZDEV}
133
134 echo -e ${COLOREDSTAR}"Configuring interface ${VZDEV}"
135 /sbin/sysctl -q -w net.ipv4.conf.${VZDEV}.send_redirects=0
136 evaluate_retval
137 }
138
139 stop_net()
140 {
141 local mod
142
143 if /sbin/ip addr list | grep -q "venet0:.*UP" 2>/dev/null
144 then
145 echo -e ${COLOREDSTAR}"Bringing down interface ${VZDEV}"
146 /sbin/ip link set ${VZDEV} down 2>/dev/null
147 evaluate_retval
148 fi
149
150 # remove all modules we probably loaded on start_net
151 for mod in ${NET_MODULES}
152 do
153 /sbin/modprobe -r ${mod} > /dev/null 2>&1
154 done
155 }
156
157 start_ve()
158 {
159 local veid velist msg need_restart="" retval
160
161 # get all VEs we should start on boot
162 if ! cd ${VPSCONFDIR}
163 then
164 ${FAILURE} "Unable to cd to ${VPSCONFDIR}"
165 return 1
166 fi
167 velist=$(grep -l '^ONBOOT=yes\|^ONBOOT=\"yes\"' [0-9]*.conf 2>/dev/null | sed -e 's/.conf//g' | sort -n)
168 cd - >/dev/null
169
170 /sbin/sysctl -q -w net.ipv4.route.src_check=0
171
172 for veid in ${velist}
173 do
174 echo -en ${COLOREDSTAR}"Starting VE ${veid}"
175 if [ "${VZFASTBOOT}" = "yes" -a "${DISK_QUOTA}" = "yes" ]
176 then
177 ${VZQUOTA} stat ${veid} >/dev/null 2>&1
178 if [ $? -eq 6 ]
179 then
180 if ${VZQUOTA} show ${veid} 2>&1 | grep "vzquota : (warning) Quota is running" >/dev/null 2>&1
181 then
182 ${VZQUOTA} on ${veid} --nocheck >/dev/null 2>&1
183 need_restart="${need_restart} ${veid}"
184 fi
185 fi
186 fi
187 msg=$(${VZCTL} start ${veid} 2>&1)
188 retval=$?
189 echo "${mesg}"
190 evaluate_retval ${retval}
191 done
192
193 for veid in ${need_restart}
194 do
195 echo -en ${COLOREDSTAR}"Stopping VE ${veid}"
196 msg=$(${VZCTL} stop ${veid})
197 retval=$?
198 echo "${mesg}"
199 evaluate_retval ${retval}
200
201 echo -e ${COLOREDSTAR}"Starting VE ${veid}"
202 msg=$($VZCTL start ${veid} 2>&1)
203 retval=$?
204 echo "${mesg}"
205 evaluate_retval ${retval}
206 done
207
208 # we're ok even if some VEs failed to start
209 return 0
210 }
211
212 get_parallel()
213 {
214 [ -n "${PARALLEL}" ] && return
215 PARALLEL=`awk '
216 BEGIN { num=0; }
217 $1 == "processor" { num++; }
218 END { print num * 4; }' /proc/cpuinfo`
219 }
220
221 stop_ve()
222 {
223 local veid velist msg m mounts fail i iter pid pids quota retval
224
225 if [ -f ${VESTAT} ]
226 then
227 get_parallel
228 for ((i = 0; i <= 2; i++))
229 do
230 iter=0
231 pids=
232 velist=$(awk '$1 != "VEID" && $1 != "Version:" {print $1}' ${VESTAT})
233 for veid in ${velist}
234 do
235 echo -e ${COLOREDSTAR}"Shutting down VE $veid"
236 # Set fairsched parameters to maximum so
237 # VE will stop fast
238 ${VZCTL} set ${veid} --cpuunits 2000 --cpulimit 0 >/dev/null 2>&1
239 ${VZCTL} --skiplock stop ${veid} >/dev/null 2>&1 &
240 pids="${pids} $!"
241 let iter++
242 if [ ${iter} -gt ${PARALLEL} ]
243 then
244 for pid in ${pids}
245 do
246 wait ${pid}
247 done
248 pids=
249 iter=0
250 fi
251 done
252
253 for pid in ${pids}
254 do
255 wait ${pid}
256 done
257 done
258 fi
259
260 iter=0
261 fail=1
262
263 while [ ${iter} -lt 5 -a ${fail} -ne 0 ]
264 do
265 fail=0
266 mounts=$(awk '{if ($3=="simfs") print $2}' /proc/mounts)
267
268 for m in ${mounts}
269 do
270 echo -en ${COLOREDSTAR}"Unmounting VE area ${m}"
271 msg=$(umount ${m} 2>&1)
272 retval=$?
273 echo "${mesg}"
274 evaluate_retval ${retval}
275 if [ $? -ne 0 ]
276 then
277 let fail++
278 fuser -k -m ${m} > /dev/null 2>&1
279 fi
280 done
281
282 let iter++
283 done
284
285 # turn quota off
286 quota=$(awk -F: '/^[0-9]+:/{print $1}' /proc/vz/vzquota 2>/dev/null)
287
288 for m in ${quota}
289 do
290 echo -e ${COLOREDSTAR}"Turning quota off for VE ${m}"
291 msg=$(vzquota off ${m} 2>&1)
292 retval=$?
293 echo "${mesg}"
294 evaluate_retval ${retval}
295 done
296 }
297
298 status_ve()
299 {
300 local velist veid
301
302 check_config
303
304 if [ -f ${VESTAT} ]
305 then
306 velist=$(awk '$1 != "VEID" && $1 != "Version:" {print $1}' ${VESTAT})
307 for veid in ${velist}
308 do
309 ${VZCTL} status ${veid}
310 done
311 fi
312 }
313
314 case "$1" in
315 start)
316 check_config || exit
317
318 echo -e ${COLOREDSTAR}"Loading OpenVZ modules"
319 for mod in ${PRELOAD_MODULES}
320 do
321 /sbin/modprobe -r ${mod} >/dev/null 2>&1
322 /sbin/modprobe ${mod} >/dev/null 2>&1
323 done
324
325 for mod in ${MODULES}
326 do
327 /sbin/modprobe ${mod} >/dev/null 2>&1
328 retval=$?
329 if [ ${retval} -ne 0 ]
330 then
331 ${FAILURE} "failed to load module ${mod}"
332 exit ${retval}
333 fi
334 done
335
336 for mod in ${MIGRATE_MODULES}
337 do
338 /sbin/modprobe ${mod} >/dev/null 2>&1
339 done
340 evaluate_retval
341
342 if [ ! -e /dev/vzctl ]
343 then
344 ${FAILURE} "Missing device node /dev/vzctl"
345 echo
346 echo "Please create the vzctl device node using the following command:"
347 echo " /bin/mknod /dev/vzctl c 126 0"
348 echo
349 exit 1
350 fi
351
352 start_net
353 setup_ve0
354 setup_cron
355 start_ve
356
357 update_svcstatus $1
358 splash svc_started "$(basename $0)" 0
359 ;;
360
361 stop)
362 check_config || exit
363
364 stop_ve
365 remove_cron
366 stop_net
367
368 echo -e ${COLOREDSTAR}"Unloading OpenVZ modules"
369 for mod in ${MIGRATE_MODULES}
370 do
371 /sbin/modprobe -r ${mod} > /dev/null 2>&1
372 done
373
374 for mod in ${MODULES}
375 do
376 /sbin/modprobe -r ${mod} > /dev/null 2>&1
377 done
378
379 for mod in ${PRELOAD_MODULES}
380 do
381 /sbin/modprobe -r ${mod} > /dev/null 2>&1
382 done
383 # Even if some modules failed to unload (say they were not loaded)
384 # we return success for the service to be marked as stopped.
385 print_status success
386
387 update_svcstatus $1
388 splash svc_stopped "$(basename $0)" 0
389 ;;
390
391 restart)
392 $0 stop
393 sleep 1
394 $0 start
395 ;;
396
397 status)
398 status_ve
399 ;;
400
401 *)
402 echo "Usage: $0 {start|stop|restart|status}"
403 exit 1
404 ;;
405 esac