xref: /freebsd/libexec/rc/rc.d/cfumass (revision f99f0ee14e3af81c23150a6a340259ca8a33d01a)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb#
40696600cSBjoern A. Zeeb
50696600cSBjoern A. Zeeb# PROVIDE: cfumass
60696600cSBjoern A. Zeeb# REQUIRE: var
70696600cSBjoern A. Zeeb# KEYWORD: nojail
80696600cSBjoern A. Zeeb
90696600cSBjoern A. Zeeb. /etc/rc.subr
100696600cSBjoern A. Zeeb
110696600cSBjoern A. Zeebname="cfumass"
120696600cSBjoern A. Zeebdesc="Configure the LUN for device mode USB mass storage"
130696600cSBjoern A. Zeebrcvar="cfumass_enable"
140696600cSBjoern A. Zeeb
150696600cSBjoern A. Zeebstart_cmd="${name}_start"
160696600cSBjoern A. Zeebstop_cmd="${name}_stop"
170696600cSBjoern A. Zeeb
180696600cSBjoern A. Zeebextra_commands="reload"
190696600cSBjoern A. Zeebreload_cmd="${name}_start"
200696600cSBjoern A. Zeeb
210696600cSBjoern A. Zeeb: ${cfumass_dir:=/var/cfumass}
220696600cSBjoern A. Zeeb: ${cfumass_image:=/var/tmp/cfumass.img}
230696600cSBjoern A. Zeeb: ${cfumass_vendor:="FreeBSD"}
240696600cSBjoern A. Zeeb: ${cfumass_product:="cfumass(4)"}
250696600cSBjoern A. Zeeb
260696600cSBjoern A. Zeebremove_luns()
270696600cSBjoern A. Zeeb{
280696600cSBjoern A. Zeeb	local _lun _luns
290696600cSBjoern A. Zeeb
300696600cSBjoern A. Zeeb	_luns=`ctladm devlist -b block -v | awk '
310696600cSBjoern A. Zeeb
320696600cSBjoern A. Zeeb	$1 ~ /^[0-9]+$/ {
330696600cSBjoern A. Zeeb		lun = $1
340696600cSBjoern A. Zeeb	}
350696600cSBjoern A. Zeeb
360696600cSBjoern A. Zeeb	$1 == "file='"${cfumass_image}"'" {
370696600cSBjoern A. Zeeb		print lun
380696600cSBjoern A. Zeeb	}'`
390696600cSBjoern A. Zeeb
400696600cSBjoern A. Zeeb	for _lun in ${_luns}; do
410696600cSBjoern A. Zeeb		ctladm remove -b block -l "${_lun}" > /dev/null
420696600cSBjoern A. Zeeb	done
430696600cSBjoern A. Zeeb}
440696600cSBjoern A. Zeeb
450696600cSBjoern A. Zeebcfumass_start()
460696600cSBjoern A. Zeeb{
470696600cSBjoern A. Zeeb	local err _files _template _new_template
480696600cSBjoern A. Zeeb
490696600cSBjoern A. Zeeb	if [ ! -d "${cfumass_dir}" ]; then
500696600cSBjoern A. Zeeb		warn "${cfumass_dir} does not exist"
510696600cSBjoern A. Zeeb		return 1
520696600cSBjoern A. Zeeb	fi
530696600cSBjoern A. Zeeb
540696600cSBjoern A. Zeeb	_files=`find "${cfumass_dir}" -newer "${cfumass_image}" -print 2> /dev/null`
550696600cSBjoern A. Zeeb	if [ ! -e "${cfumass_image}" -o -n "${_files}" ]; then
560696600cSBjoern A. Zeeb		# The image doesn't exist or is out of date.
570696600cSBjoern A. Zeeb		makefs -t cd9660 -o label="${cfumass_vendor}" \
580696600cSBjoern A. Zeeb		    -o rockridge "${cfumass_image}" "${cfumass_dir}"
590696600cSBjoern A. Zeeb		err=$?
600696600cSBjoern A. Zeeb		if [ "${err}" -ne 0 ]; then
610696600cSBjoern A. Zeeb			warn "unable to create ${cfumass_image}"
620696600cSBjoern A. Zeeb			return "${err}"
630696600cSBjoern A. Zeeb		fi
640696600cSBjoern A. Zeeb	fi
650696600cSBjoern A. Zeeb
660696600cSBjoern A. Zeeb	remove_luns
670696600cSBjoern A. Zeeb
680696600cSBjoern A. Zeeb	ctladm create -b block -o file="${cfumass_image}" -o readonly=on \
690696600cSBjoern A. Zeeb	    -o vendor="${cfumass_vendor}" -o product="${cfumass_product}" \
700696600cSBjoern A. Zeeb	    -S 0 > /dev/null
710696600cSBjoern A. Zeeb	err=$?
720696600cSBjoern A. Zeeb	if [ "${err}" -ne 0 ]; then
730696600cSBjoern A. Zeeb		warn "unable to create CTL LUN"
740696600cSBjoern A. Zeeb		return "${err}"
750696600cSBjoern A. Zeeb	fi
760696600cSBjoern A. Zeeb
77b11974c2SChris Rees	load_kld -e cfumass cfumass
780696600cSBjoern A. Zeeb
790696600cSBjoern A. Zeeb	# If the template is already switched to Mass Storage, then reset
800696600cSBjoern A. Zeeb	# it to -1 to force the host to reenumerate it; otherwise it might
810696600cSBjoern A. Zeeb	# not notice the new LUN.
820696600cSBjoern A. Zeeb	_template=`sysctl -n hw.usb.template`
830696600cSBjoern A. Zeeb	if [ "${_template}" -eq 0 ]; then
840696600cSBjoern A. Zeeb		sysctl hw.usb.template=-1 > /dev/null
850696600cSBjoern A. Zeeb		err=$?
860696600cSBjoern A. Zeeb		if [ "${err}" -ne 0 ]; then
870696600cSBjoern A. Zeeb			warn "unable to set hw.usb.template sysctl"
880696600cSBjoern A. Zeeb			return "${err}"
890696600cSBjoern A. Zeeb		fi
900696600cSBjoern A. Zeeb	fi
910696600cSBjoern A. Zeeb
920696600cSBjoern A. Zeeb	# Set the template number based on the current one.
930696600cSBjoern A. Zeeb	_template=`sysctl -n hw.usb.template`
940696600cSBjoern A. Zeeb	case "${_template}" in
950696600cSBjoern A. Zeeb	-1)
960696600cSBjoern A. Zeeb		_new_template="0"
970696600cSBjoern A. Zeeb		;;
980696600cSBjoern A. Zeeb	8)
990696600cSBjoern A. Zeeb		_new_template="10"
1000696600cSBjoern A. Zeeb		;;
1010696600cSBjoern A. Zeeb	*)
1020696600cSBjoern A. Zeeb		warn "hw.usb.template sysctl set to neither -1 nor 8; not changing"
1030696600cSBjoern A. Zeeb		_new_template=""
1040696600cSBjoern A. Zeeb		;;
1050696600cSBjoern A. Zeeb	esac
1060696600cSBjoern A. Zeeb
1070696600cSBjoern A. Zeeb	if [ -n "${_new_template}" ]; then
1080696600cSBjoern A. Zeeb		sysctl hw.usb.template="${_new_template}" > /dev/null
1090696600cSBjoern A. Zeeb		err=$?
1100696600cSBjoern A. Zeeb		if [ "${err}" -ne 0 ]; then
1110696600cSBjoern A. Zeeb			warn "unable to set hw.usb.template sysctl to ${_new_template}"
1120696600cSBjoern A. Zeeb			return "${err}"
1130696600cSBjoern A. Zeeb		fi
1140696600cSBjoern A. Zeeb	fi
1150696600cSBjoern A. Zeeb}
1160696600cSBjoern A. Zeeb
1170696600cSBjoern A. Zeebcfumass_stop()
1180696600cSBjoern A. Zeeb{
1190696600cSBjoern A. Zeeb	local err _template _new_template
1200696600cSBjoern A. Zeeb
1210696600cSBjoern A. Zeeb	remove_luns
1220696600cSBjoern A. Zeeb
1230696600cSBjoern A. Zeeb	_template=`sysctl -n hw.usb.template`
1240696600cSBjoern A. Zeeb	case "${_template}" in
1250696600cSBjoern A. Zeeb	0)
1260696600cSBjoern A. Zeeb		_new_template="-1"
1270696600cSBjoern A. Zeeb		;;
1280696600cSBjoern A. Zeeb	10)
1290696600cSBjoern A. Zeeb		_new_template="8"
1300696600cSBjoern A. Zeeb		;;
1310696600cSBjoern A. Zeeb	*)
1320696600cSBjoern A. Zeeb		warn "hw.usb.template sysctl set to neither 0 nor 10; not changing"
1330696600cSBjoern A. Zeeb		_new_template=""
1340696600cSBjoern A. Zeeb		;;
1350696600cSBjoern A. Zeeb	esac
1360696600cSBjoern A. Zeeb
1370696600cSBjoern A. Zeeb	if [ -n "${_new_template}" ]; then
1380696600cSBjoern A. Zeeb		sysctl hw.usb.template="${_new_template}" > /dev/null
1390696600cSBjoern A. Zeeb		err=$?
1400696600cSBjoern A. Zeeb		if [ "${err}" -ne 0 ]; then
1410696600cSBjoern A. Zeeb			warn "unable to set hw.usb.template sysctl to ${_new_template}"
1420696600cSBjoern A. Zeeb			return "${err}"
1430696600cSBjoern A. Zeeb		fi
1440696600cSBjoern A. Zeeb	fi
1450696600cSBjoern A. Zeeb}
1460696600cSBjoern A. Zeeb
1470696600cSBjoern A. Zeebload_rc_config $name
148*f99f0ee1SAlexander Leidinger
149*f99f0ee1SAlexander Leidinger# doesn't make sense to run in a svcj: nojail keyword
150*f99f0ee1SAlexander Leidingercfumass_svcj="NO"
151*f99f0ee1SAlexander Leidinger
1520696600cSBjoern A. Zeebrun_rc_command "$1"
153