3b0a928c50b57cff0b7f4bcdadf3a298602ac867
[dragonfly.git] / etc / rc.d / vkernel
1 #!/bin/sh
2 #
3
4 # PROVIDE: vkernel
5 # REQUIRE: LOGIN NETWORKING
6
7 . /etc/rc.subr
8
9 name="vkernel"
10 rcvar=`set_rcvar`
11 start_cmd="vkernel_start"
12 stop_cmd="vkernel_stop"
13
14 vkernel_start()
15 {
16         echo -n 'Starting virtual kernels:'
17         for _vkernel in ${vkernel_list}
18         do
19                 # Check if already running
20                 eval pidfile="/var/run/vkernel.${_vkernel}.pid"
21                 if [ -f "${pidfile}" ]; then
22                         eval pid=`cat ${pidfile}`
23                         if [ ! -z "${pid}" -a ! -z "`ps axwwp ${pid} | grep -wE "^[ ]+${pid}" | grep -v grep | awk '{print $1}'`" ]; then
24                                 echo
25                                 warn "Vkernel ${_vkernel} already running? (check ${pidfile})"
26                                 continue
27                         fi
28                 fi
29
30                 # Configure vkernel binary
31                 eval _bin=\"\${vkernel_${_vkernel}_bin}\"
32                 if [ -z "${_bin}" -o ! -x "${_bin}" ]; then
33                         eval _bin=\"\${vkernel_bin}\"
34                         if [ -z "${_bin}" -o ! -x "${_bin}" ]; then
35                                 echo
36                                 warn "Missing or undefined binary for vkernel '${_vkernel}'. Skipping."
37                                 continue
38                         fi
39                 fi
40                 bin=${_bin}
41
42                 # Configure vkernel memory
43                 eval _memsize=\"\$vkernel_${_vkernel}_memsize\"
44                 if [ -z "${_memsize}" ]; then
45                         echo
46                         warn "No memsize has been defined for vkernel '${_vkernel}'. Skipping."
47                         continue
48                 else
49                         memsize="-m ${_memsize}"
50                 fi
51
52                 # Configure vkernel root image(s)
53                 eval _rootimgs=\"\${vkernel_${_vkernel}_rootimg_list}\"
54                 if [ -z "${_rootimgs}" ]; then
55                         echo
56                         warn "No root image has been defined for vkernel '${_vkernel}'."
57                 else
58                         for _rootimg in ${_rootimgs}
59                         do
60                                 eval rootimgs=\"${rootimgs} -r ${_rootimg}\"
61                         done
62                 fi
63
64                 # Configure optional vkernel network interface(s)
65                 eval _ifaces=\"\${vkernel_${_vkernel}_iface_list}\"
66                 if [ -n "${_ifaces}" ]; then
67                         for _iface in ${_ifaces}
68                         do
69                                 eval ifaces=\"${ifaces} -I ${_iface}\"
70                         done
71                 fi
72
73                 # Configure optional console logfile
74                 eval logfile=\"\${vkernel_${_vkernel}_logfile}\"
75                 [ -z "${logfile}" ] && logfile="/dev/null"
76
77                 # Configure optional flags
78                 eval flags=\"\${vkernel_${_vkernel}_flags}\"
79
80                 eval "daemon ${bin} ${memsize} ${rootimgs} ${ifaces} ${flags} -p ${pidfile} >>${logfile} 2>&1 </dev/null"
81                 echo -n " ${_vkernel}"
82         done
83         echo '.'
84 }
85
86 vkernel_stop()
87 {
88         for _vkernel in ${vkernel_list}
89         do
90                 eval pidfile="/var/run/vkernel.${_vkernel}.pid"
91                 if [ -f "${pidfile}" ]; then
92                         eval pid=`cat ${pidfile}`
93                 fi
94                 if [ -z "${pid}" ]; then
95                         warn "vkernel '${_vkernel}' not running?"
96                         continue
97                 fi
98                 eval kill -TERM ${pid}
99
100                 eval _kill_timeout=\"\${vkernel_${_vkernel}_kill_timeout}\"
101                 if [ -z "${_kill_timeout}" ]; then
102                         eval _kill_timeout=\"\${vkernel_kill_timeout}\"
103                         if [ -z "${_kill_timeout}" ]; then
104                                 _kill_timeout=60
105                         fi
106                 fi
107
108                 # Check that the vkernel ended properly
109                 cnt=1
110                 eval temp_pid=`ps axwwp ${pid} | grep -wE "^[ ]+${pid}" | grep -v grep | awk '{print $1}'`
111                 while [ ! -z "${temp_pid}" -a "${cnt}" -lt "${_kill_timeout}" ]; do
112                         eval temp_pid=`ps axwwp ${pid} | grep -wE "^[ ]+${pid}" | grep -v grep | awk '{print $1}'`
113                         cnt=`expr $cnt + 1`
114                         sleep 1
115                 done
116
117                 if [ ! -z "${temp_pid}" ]; then
118                         eval kill -KILL ${pid}
119                         warn "'${_vkernel}' had to be forcefully killed."
120                 fi
121         done
122 }
123
124
125 load_rc_config $name
126 run_rc_command "$1"