]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.d/cfumass
Rename all Unbound binaries and man pages from unbound* to local-unbound*.
[FreeBSD/FreeBSD.git] / etc / rc.d / cfumass
1 #!/bin/sh
2 #
3 # $FreeBSD$
4 #
5
6 # PROVIDE: cfumass
7 # REQUIRE: var
8 # KEYWORD: nojail
9
10 . /etc/rc.subr
11
12 name="cfumass"
13 desc="Configure the LUN for device mode USB mass storage"
14 rcvar="cfumass_enable"
15
16 start_cmd="${name}_start"
17 stop_cmd="${name}_stop"
18
19 extra_commands="reload"
20 reload_cmd="${name}_start"
21
22 : ${cfumass_dir:=/var/cfumass}
23 : ${cfumass_image:=/var/tmp/cfumass.img}
24 : ${cfumass_vendor:="FreeBSD"}
25 : ${cfumass_product:="cfumass(4)"}
26
27 remove_luns()
28 {
29         local _lun _luns
30
31         _luns=`ctladm devlist -b block -v | awk '
32
33         $1 ~ /^[0-9]+$/ {
34                 lun = $1
35         }
36
37         $1 == "file='"${cfumass_image}"'" {
38                 print lun
39         }'`
40
41         for _lun in ${_luns}; do
42                 ctladm remove -b block -l "${_lun}" > /dev/null
43         done
44 }
45
46 cfumass_start()
47 {
48         local err _files _template
49
50         if [ ! -d "${cfumass_dir}" ]; then
51                 warn "${cfumass_dir} does not exist"
52                 return 1
53         fi
54
55         _files=`find "${cfumass_dir}" -newer "${cfumass_image}" -print 2> /dev/null`
56         if [ ! -e "${cfumass_image}" -o -n "${_files}" ]; then
57                 # The image doesn't exist or is out of date.
58                 makefs -t cd9660 -o rockridge "${cfumass_image}" "${cfumass_dir}"
59                 err=$?
60                 if [ "${err}" -ne 0 ]; then
61                         warn "unable to create ${cfumass_image}"
62                         return "${err}"
63                 fi
64         fi
65
66         remove_luns
67
68         ctladm create -b block -o file="${cfumass_image}" -o readonly=on \
69             -o vendor="${cfumass_vendor}" -o product="${cfumass_product}" \
70             -t 5 -S 0 > /dev/null
71         err=$?
72         if [ "${err}" -ne 0 ]; then
73                 warn "unable to create CTL LUN"
74                 return "${err}"
75         fi
76
77         load_kld -e cfumass cfumass
78
79         # If the template is already switched to Mass Storage, then reset
80         # it to -1 to force the host to reenumerate it; otherwise it might
81         # not notice the new LUN.
82         _template=`sysctl -n hw.usb.template`
83         if [ "${_template}" -eq 0 ]; then
84                 sysctl hw.usb.template=-1 > /dev/null
85                 err=$?
86                 if [ "${err}" -ne 0 ]; then
87                         warn "unable to set hw.usb.template sysctl"
88                         return "${err}"
89                 fi
90         fi
91
92         _template=`sysctl -n hw.usb.template`
93         if [ "${_template}" -lt 0 ]; then
94                 sysctl hw.usb.template=0 > /dev/null
95                 err=$?
96                 if [ "${err}" -ne 0 ]; then
97                         warn "unable to set hw.usb.template sysctl"
98                         return "${err}"
99                 fi
100         else
101                 # Otherwise don't touch the sysctl - we could lock the user
102                 # out of the machine otherwise.
103                 warn "hw.usb.template sysctl set to neither -1 nor 0"
104         fi
105 }
106
107 cfumass_stop()
108 {
109         local err _template
110
111         _template=`sysctl -n hw.usb.template`
112         if [ "${_template}" -eq 0 ]; then
113                 sysctl hw.usb.template=-1 > /dev/null
114                 err=$?
115                 if [ "${err}" -ne 0 ]; then
116                         warn "unable to set hw.usb.template sysctl"
117                         return "${err}"
118                 fi
119         fi
120
121         remove_luns
122 }
123
124 load_rc_config $name
125 run_rc_command "$1"