Add an rc script for starting vkernels upon boot.
[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                         echo
23                         warn "Vkernel ${_vkernel} already running? (check ${pidfile})"
24                         continue
25                 fi
26
27                 # Configure vkernel binary
28                 eval _bin=\"\${vkernel_${_vkernel}_bin}\"
29                 if [ -z "${_bin}" -o ! -x "${_bin}" ]; then
30                         echo
31                         warn "Missing or undefined binary for vkernel '${_vkernel}'. Skipping."
32                         continue
33                 else
34                         bin=${_bin}
35                 fi
36
37                 # Configure vkernel memory
38                 eval _memsize=\"\$vkernel_${_vkernel}_memsize\"
39                 if [ -z "${_memsize}" ]; then
40                         echo
41                         warn "No memsize has been defined for vkernel '${_vkernel}'. Skipping."
42                         continue
43                 else
44                         memsize="-m ${_memsize}"
45                 fi
46
47                 # Configure vkernel root image(s)
48                 eval _rootimgs=\"\${vkernel_${_vkernel}_rootimg_list}\"
49                 if [ -z "${_rootimgs}" ]; then
50                         echo
51                         warn "No root image has been defined for vkernel '${_vkernel}'. Skipping."
52                         continue
53                 else
54                         for _rootimg in ${_rootimgs}
55                         do
56                                 eval rootimgs=\"${rootimgs} -r ${_rootimg}\"
57                         done
58                 fi
59
60                 # Configure optional vkernel network interface(s)
61                 eval _ifaces=\"\${vkernel_${_vkernel}_iface_list}\"
62                 if [ -n "${_ifaces}" ]; then
63                         for _iface in ${_ifaces}
64                         do
65                                 eval ifaces=\"${ifaces} -I ${_iface}\"
66                         done
67                 fi
68
69                 # Configure optional console logfile
70                 eval logfile=\"\${vkernel_${_vkernel}_logfile}\"
71                 [ -z "${logfile}" ] && logfile="/dev/null"
72
73                 # Configure optional flags
74                 eval flags=\"\${vkernel_${_vkernel}_flags}\"
75
76                 eval "daemon ${bin} ${memsize} ${rootimgs} ${ifaces} ${flags} -p ${pidfile} >>${logfile} 2>&1"
77                 echo -n " ${_vkernel}"
78         done
79         echo '.'
80 }
81
82 vkernel_stop()
83 {
84         for _vkernel in ${vkernel_list}
85         do
86                 eval pidfile="/var/run/vkernel.${_vkernel}.pid"
87                 if [ -f "${pidfile}" ]; then
88                         eval pid=`cat ${pidfile}`
89                 else
90                         eval _bin=\"\${vkernel_${_vkernel}_bin}\"
91                         if [ -n "$_bin" ]; then
92                                 eval pid=`ps auxwww | grep ${_bin} | grep -v grep | awk '{print $2}'`
93                         fi
94                 fi
95                 if [ -z "${pid}" ]; then
96                         warn "vkernel '${_vkernel}' not running?"
97                 else
98                         eval kill -TERM ${pid}
99                 fi
100         done
101 }
102
103
104 load_rc_config $name
105 run_rc_command "$1"