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