cal(1): Fix locales handling.
[dragonfly.git] / etc / rc.d / vkernel
blob59fd2a1c2e69cfc584e9a2ba6a193b5270a9500f
1 #!/bin/sh
4 # PROVIDE: vkernel
5 # REQUIRE: LOGIN NETWORKING
7 . /etc/rc.subr
9 name="vkernel"
10 rcvar=`set_rcvar`
12 start_cmd="vkernel_start"
13 stop_cmd="vkernel_stop"
15 vkernel_start()
17 echo -n 'Starting virtual kernels:'
18 for _vkernel in ${vkernel_list}
20 # Start with a clean set of variables
21 unset pidfile bin memsize rootimgs ifaces logfile flags
22 unset vkuser vkgroup memimg
24 # Configure user to run vkernel
25 eval vkuser=\"\${vkernel_${_vkernel}_user}\"
26 if [ -z "${vkuser}" ]; then
27 vkuser="root"
28 vkgroup="wheel"
29 else
30 vkgroup=$(id -ng ${vkuser})
33 # Check if already running
34 eval pidfile="/var/run/vkernel.${_vkernel}.pid"
35 if [ -f "${pidfile}" ]; then
36 eval pid=`cat ${pidfile}`
37 if [ ! -z "${pid}" -a ! -z "`ps axwwp ${pid} | grep -wE "^[ ]*${pid}" | grep -v grep | awk '{print $1}'`" ]; then
38 echo
39 warn "vkernel ${_vkernel} already running? (check ${pidfile})"
40 continue
42 else
43 # Regular users do not have write access in /var/run
44 install -c -o ${vkuser} -g ${vkgroup} /dev/null ${pidfile}
47 # Configure vkernel binary
48 eval _bin=\"\${vkernel_${_vkernel}_bin}\"
49 if [ -z "${_bin}" -o ! -x "${_bin}" ]; then
50 eval _bin=\"\${vkernel_bin}\"
51 if [ -z "${_bin}" -o ! -x "${_bin}" ]; then
52 echo
53 warn "Missing or undefined binary for vkernel '${_vkernel}'. Skipping."
54 continue
57 bin=${_bin}
59 # Configure vkernel memory
60 eval _memsize=\"\$vkernel_${_vkernel}_memsize\"
61 if [ -z "${_memsize}" ]; then
62 echo
63 warn "No memsize has been defined for vkernel '${_vkernel}'. Skipping."
64 continue
65 else
66 memsize="-m ${_memsize}"
69 # Configure vkernel memory image file
70 eval _memimg=\"\${vkernel_${_vkernel}_memimg}\"
71 if [ -z "${_memimg}" ]; then
72 mkdir -m 1777 -p /var/vkernel
74 for n in $(seq 0 999999)
76 eval _memf=$(printf "/var/vkernel/memimg.%06s" ${n})
77 if [ ! -f "${_memf}" ]
78 then
79 memimg=" -i ${_memf}"
80 break
81 else
82 if lockf -kst 0 ${_memf} echo >/dev/null
83 then
84 # Test if memimg is writable by vkuser
85 su - ${vkuser} -c "test -w ${_memf}"
86 if [ $? -eq 0 ]
87 then
88 memimg="-i ${_memf}"
89 break
93 done
94 else
95 memimg="-i ${_memimg}"
98 # Configure vkernel root image(s)
99 eval _rootimgs=\"\${vkernel_${_vkernel}_rootimg_list}\"
100 if [ -z "${_rootimgs}" ]; then
101 echo
102 warn "No root image has been defined for vkernel '${_vkernel}'."
103 else
104 for _rootimg in ${_rootimgs}
106 eval rootimgs=\"${rootimgs} -r ${_rootimg}\"
107 done
110 # Configure optional vkernel network interface(s)
111 eval _ifaces=\"\${vkernel_${_vkernel}_iface_list}\"
112 if [ -n "${_ifaces}" ]; then
113 for _iface in ${_ifaces}
115 eval ifaces=\"${ifaces} -I ${_iface}\"
116 done
119 # Configure optional console logfile
120 eval logfile=\"\${vkernel_${_vkernel}_logfile}\"
121 [ -z "${logfile}" ] && logfile="/dev/null"
123 # Configure optional flags
124 eval flags=\"\${vkernel_${_vkernel}_flags}\"
126 eval "daemon -u ${vkuser} ${bin} ${memsize} ${rootimgs} " \
127 "${memimg} ${ifaces} ${flags} -p ${pidfile}" \
128 ">>${logfile} 2>&1 </dev/null"
129 echo -n " ${_vkernel}"
130 done
131 echo '.'
134 vkernel_stop()
136 for _vkernel in ${vkernel_list}
138 eval pidfile="/var/run/vkernel.${_vkernel}.pid"
139 if [ -f "${pidfile}" ]; then
140 eval pid=`cat ${pidfile}`
142 if [ -z "${pid}" ]; then
143 warn "vkernel '${_vkernel}' not running?"
144 continue
146 eval kill -TERM ${pid}
148 eval _kill_timeout=\"\${vkernel_${_vkernel}_kill_timeout}\"
149 if [ -z "${_kill_timeout}" ]; then
150 eval _kill_timeout=\"\${vkernel_kill_timeout}\"
151 if [ -z "${_kill_timeout}" ]; then
152 _kill_timeout=60
156 # Check that the vkernel ended properly
157 cnt=1
158 eval temp_pid=`ps axwwp ${pid} | grep -wE "^[ ]*${pid}" | grep -v grep | awk '{print $1}'`
159 while [ ! -z "${temp_pid}" -a "${cnt}" -lt "${_kill_timeout}" ]; do
160 eval temp_pid=`ps axwwp ${pid} | grep -wE "^[ ]*${pid}" | grep -v grep | awk '{print $1}'`
161 cnt=`expr $cnt + 1`
162 sleep 1
163 done
165 if [ ! -z "${temp_pid}" ]; then
166 eval kill -KILL ${pid}
167 warn "'${_vkernel}' had to be forcefully killed."
169 done
173 load_rc_config $name
174 run_rc_command "$1"