xref: /freebsd/libexec/rc/rc.d/cfumass (revision 0696600c41600d80bcd993bfd8e675d0ae6951fe)
1*0696600cSBjoern A. Zeeb#!/bin/sh
2*0696600cSBjoern A. Zeeb#
3*0696600cSBjoern A. Zeeb# $FreeBSD$
4*0696600cSBjoern A. Zeeb#
5*0696600cSBjoern A. Zeeb
6*0696600cSBjoern A. Zeeb# PROVIDE: cfumass
7*0696600cSBjoern A. Zeeb# REQUIRE: var
8*0696600cSBjoern A. Zeeb# KEYWORD: nojail
9*0696600cSBjoern A. Zeeb
10*0696600cSBjoern A. Zeeb. /etc/rc.subr
11*0696600cSBjoern A. Zeeb
12*0696600cSBjoern A. Zeebname="cfumass"
13*0696600cSBjoern A. Zeebdesc="Configure the LUN for device mode USB mass storage"
14*0696600cSBjoern A. Zeebrcvar="cfumass_enable"
15*0696600cSBjoern A. Zeeb
16*0696600cSBjoern A. Zeebstart_cmd="${name}_start"
17*0696600cSBjoern A. Zeebstop_cmd="${name}_stop"
18*0696600cSBjoern A. Zeeb
19*0696600cSBjoern A. Zeebextra_commands="reload"
20*0696600cSBjoern A. Zeebreload_cmd="${name}_start"
21*0696600cSBjoern A. Zeeb
22*0696600cSBjoern A. Zeeb: ${cfumass_dir:=/var/cfumass}
23*0696600cSBjoern A. Zeeb: ${cfumass_image:=/var/tmp/cfumass.img}
24*0696600cSBjoern A. Zeeb: ${cfumass_vendor:="FreeBSD"}
25*0696600cSBjoern A. Zeeb: ${cfumass_product:="cfumass(4)"}
26*0696600cSBjoern A. Zeeb
27*0696600cSBjoern A. Zeebremove_luns()
28*0696600cSBjoern A. Zeeb{
29*0696600cSBjoern A. Zeeb	local _lun _luns
30*0696600cSBjoern A. Zeeb
31*0696600cSBjoern A. Zeeb	_luns=`ctladm devlist -b block -v | awk '
32*0696600cSBjoern A. Zeeb
33*0696600cSBjoern A. Zeeb	$1 ~ /^[0-9]+$/ {
34*0696600cSBjoern A. Zeeb		lun = $1
35*0696600cSBjoern A. Zeeb	}
36*0696600cSBjoern A. Zeeb
37*0696600cSBjoern A. Zeeb	$1 == "file='"${cfumass_image}"'" {
38*0696600cSBjoern A. Zeeb		print lun
39*0696600cSBjoern A. Zeeb	}'`
40*0696600cSBjoern A. Zeeb
41*0696600cSBjoern A. Zeeb	for _lun in ${_luns}; do
42*0696600cSBjoern A. Zeeb		ctladm remove -b block -l "${_lun}" > /dev/null
43*0696600cSBjoern A. Zeeb	done
44*0696600cSBjoern A. Zeeb}
45*0696600cSBjoern A. Zeeb
46*0696600cSBjoern A. Zeebcfumass_start()
47*0696600cSBjoern A. Zeeb{
48*0696600cSBjoern A. Zeeb	local err _files _template _new_template
49*0696600cSBjoern A. Zeeb
50*0696600cSBjoern A. Zeeb	if [ ! -d "${cfumass_dir}" ]; then
51*0696600cSBjoern A. Zeeb		warn "${cfumass_dir} does not exist"
52*0696600cSBjoern A. Zeeb		return 1
53*0696600cSBjoern A. Zeeb	fi
54*0696600cSBjoern A. Zeeb
55*0696600cSBjoern A. Zeeb	_files=`find "${cfumass_dir}" -newer "${cfumass_image}" -print 2> /dev/null`
56*0696600cSBjoern A. Zeeb	if [ ! -e "${cfumass_image}" -o -n "${_files}" ]; then
57*0696600cSBjoern A. Zeeb		# The image doesn't exist or is out of date.
58*0696600cSBjoern A. Zeeb		makefs -t cd9660 -o label="${cfumass_vendor}" \
59*0696600cSBjoern A. Zeeb		    -o rockridge "${cfumass_image}" "${cfumass_dir}"
60*0696600cSBjoern A. Zeeb		err=$?
61*0696600cSBjoern A. Zeeb		if [ "${err}" -ne 0 ]; then
62*0696600cSBjoern A. Zeeb			warn "unable to create ${cfumass_image}"
63*0696600cSBjoern A. Zeeb			return "${err}"
64*0696600cSBjoern A. Zeeb		fi
65*0696600cSBjoern A. Zeeb	fi
66*0696600cSBjoern A. Zeeb
67*0696600cSBjoern A. Zeeb	remove_luns
68*0696600cSBjoern A. Zeeb
69*0696600cSBjoern A. Zeeb	ctladm create -b block -o file="${cfumass_image}" -o readonly=on \
70*0696600cSBjoern A. Zeeb	    -o vendor="${cfumass_vendor}" -o product="${cfumass_product}" \
71*0696600cSBjoern A. Zeeb	    -S 0 > /dev/null
72*0696600cSBjoern A. Zeeb	err=$?
73*0696600cSBjoern A. Zeeb	if [ "${err}" -ne 0 ]; then
74*0696600cSBjoern A. Zeeb		warn "unable to create CTL LUN"
75*0696600cSBjoern A. Zeeb		return "${err}"
76*0696600cSBjoern A. Zeeb	fi
77*0696600cSBjoern A. Zeeb
78*0696600cSBjoern A. Zeeb	load_kld -e cfumass cfumass
79*0696600cSBjoern A. Zeeb
80*0696600cSBjoern A. Zeeb	# If the template is already switched to Mass Storage, then reset
81*0696600cSBjoern A. Zeeb	# it to -1 to force the host to reenumerate it; otherwise it might
82*0696600cSBjoern A. Zeeb	# not notice the new LUN.
83*0696600cSBjoern A. Zeeb	_template=`sysctl -n hw.usb.template`
84*0696600cSBjoern A. Zeeb	if [ "${_template}" -eq 0 ]; then
85*0696600cSBjoern A. Zeeb		sysctl hw.usb.template=-1 > /dev/null
86*0696600cSBjoern A. Zeeb		err=$?
87*0696600cSBjoern A. Zeeb		if [ "${err}" -ne 0 ]; then
88*0696600cSBjoern A. Zeeb			warn "unable to set hw.usb.template sysctl"
89*0696600cSBjoern A. Zeeb			return "${err}"
90*0696600cSBjoern A. Zeeb		fi
91*0696600cSBjoern A. Zeeb	fi
92*0696600cSBjoern A. Zeeb
93*0696600cSBjoern A. Zeeb	# Set the template number based on the current one.
94*0696600cSBjoern A. Zeeb	_template=`sysctl -n hw.usb.template`
95*0696600cSBjoern A. Zeeb	case "${_template}" in
96*0696600cSBjoern A. Zeeb	-1)
97*0696600cSBjoern A. Zeeb		_new_template="0"
98*0696600cSBjoern A. Zeeb		;;
99*0696600cSBjoern A. Zeeb	8)
100*0696600cSBjoern A. Zeeb		_new_template="10"
101*0696600cSBjoern A. Zeeb		;;
102*0696600cSBjoern A. Zeeb	*)
103*0696600cSBjoern A. Zeeb		warn "hw.usb.template sysctl set to neither -1 nor 8; not changing"
104*0696600cSBjoern A. Zeeb		_new_template=""
105*0696600cSBjoern A. Zeeb		;;
106*0696600cSBjoern A. Zeeb	esac
107*0696600cSBjoern A. Zeeb
108*0696600cSBjoern A. Zeeb	if [ -n "${_new_template}" ]; then
109*0696600cSBjoern A. Zeeb		sysctl hw.usb.template="${_new_template}" > /dev/null
110*0696600cSBjoern A. Zeeb		err=$?
111*0696600cSBjoern A. Zeeb		if [ "${err}" -ne 0 ]; then
112*0696600cSBjoern A. Zeeb			warn "unable to set hw.usb.template sysctl to ${_new_template}"
113*0696600cSBjoern A. Zeeb			return "${err}"
114*0696600cSBjoern A. Zeeb		fi
115*0696600cSBjoern A. Zeeb	fi
116*0696600cSBjoern A. Zeeb}
117*0696600cSBjoern A. Zeeb
118*0696600cSBjoern A. Zeebcfumass_stop()
119*0696600cSBjoern A. Zeeb{
120*0696600cSBjoern A. Zeeb	local err _template _new_template
121*0696600cSBjoern A. Zeeb
122*0696600cSBjoern A. Zeeb	remove_luns
123*0696600cSBjoern A. Zeeb
124*0696600cSBjoern A. Zeeb	_template=`sysctl -n hw.usb.template`
125*0696600cSBjoern A. Zeeb	case "${_template}" in
126*0696600cSBjoern A. Zeeb	0)
127*0696600cSBjoern A. Zeeb		_new_template="-1"
128*0696600cSBjoern A. Zeeb		;;
129*0696600cSBjoern A. Zeeb	10)
130*0696600cSBjoern A. Zeeb		_new_template="8"
131*0696600cSBjoern A. Zeeb		;;
132*0696600cSBjoern A. Zeeb	*)
133*0696600cSBjoern A. Zeeb		warn "hw.usb.template sysctl set to neither 0 nor 10; not changing"
134*0696600cSBjoern A. Zeeb		_new_template=""
135*0696600cSBjoern A. Zeeb		;;
136*0696600cSBjoern A. Zeeb	esac
137*0696600cSBjoern A. Zeeb
138*0696600cSBjoern A. Zeeb	if [ -n "${_new_template}" ]; then
139*0696600cSBjoern A. Zeeb		sysctl hw.usb.template="${_new_template}" > /dev/null
140*0696600cSBjoern A. Zeeb		err=$?
141*0696600cSBjoern A. Zeeb		if [ "${err}" -ne 0 ]; then
142*0696600cSBjoern A. Zeeb			warn "unable to set hw.usb.template sysctl to ${_new_template}"
143*0696600cSBjoern A. Zeeb			return "${err}"
144*0696600cSBjoern A. Zeeb		fi
145*0696600cSBjoern A. Zeeb	fi
146*0696600cSBjoern A. Zeeb}
147*0696600cSBjoern A. Zeeb
148*0696600cSBjoern A. Zeebload_rc_config $name
149*0696600cSBjoern A. Zeebrun_rc_command "$1"
150