Add cfumass rc script, to create a LUN for cfumass(4).

MFC after:	2 weeks
Relnotes:	yes
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D14844
This commit is contained in:
Edward Tomasz Napierala 2018-04-21 14:56:41 +00:00
parent ff415f05bf
commit e73154c1d7
5 changed files with 151 additions and 2 deletions

View File

@ -592,6 +592,9 @@ cron_enable="YES" # Run the periodic job daemon.
cron_program="/usr/sbin/cron" # Which cron executable to run (if enabled).
cron_dst="YES" # Handle DST transitions intelligently (YES/NO)
cron_flags="" # Which options to pass to the cron daemon.
cfumass_enable="NO" # Create default LUN for cfumass(4).
cfumass_dir="/var/cfumass" # File to LUN's contents.
cfumass_image="/var/tmp/cfumass.img" # LUN's backing file path.
lpd_enable="NO" # Run the line printer daemon.
lpd_program="/usr/sbin/lpd" # path to lpd, if you want a different one.
lpd_flags="" # Flags to lpd (if enabled).

View File

@ -21,6 +21,7 @@ FILES= DAEMON \
${_bluetooth} \
bridge \
${_bthidd} \
cfumass \
cleanvar \
cleartmp \
cron \

125
etc/rc.d/cfumass Executable file
View File

@ -0,0 +1,125 @@
#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: cfumass
# REQUIRE: var
# KEYWORD: nojail
. /etc/rc.subr
name="cfumass"
desc="Configure the LUN for device mode USB mass storage"
rcvar="cfumass_enable"
start_cmd="${name}_start"
stop_cmd="${name}_stop"
extra_commands="reload"
reload_cmd="${name}_start"
: ${cfumass_dir:=/var/cfumass}
: ${cfumass_image:=/var/tmp/cfumass.img}
: ${cfumass_vendor:="FreeBSD"}
: ${cfumass_product:="cfumass(4)"}
remove_luns()
{
local _lun _luns
_luns=`ctladm devlist -b block -v | awk '
$1 ~ /^[0-9]+$/ {
lun = $1
}
$1 == "file='"${cfumass_image}"'" {
print lun
}'`
for _lun in ${_luns}; do
ctladm remove -b block -l "${_lun}" > /dev/null
done
}
cfumass_start()
{
local err _files _template
if [ ! -d "${cfumass_dir}" ]; then
warn "${cfumass_dir} does not exist"
return 1
fi
_files=`find "${cfumass_dir}" -newer "${cfumass_image}" -print 2> /dev/null`
if [ ! -e "${cfumass_image}" -o -n "${_files}" ]; then
# The image doesn't exist or is out of date.
makefs -t cd9660 -o rockridge "${cfumass_image}" "${cfumass_dir}"
err=$?
if [ "${err}" -ne 0 ]; then
warn "unable to create ${cfumass_image}"
return "${err}"
fi
fi
remove_luns
ctladm create -b block -o file="${cfumass_image}" -o readonly=on \
-o vendor="${cfumass_vendor}" -o product="${cfumass_product}" \
-t 5 -S 0 > /dev/null
err=$?
if [ "${err}" -ne 0 ]; then
warn "unable to create CTL LUN"
return "${err}"
fi
load_kld -e cfumass cfumass
# If the template is already switched to Mass Storage, then reset
# it to -1 to force the host to reenumerate it; otherwise it might
# not notice the new LUN.
_template=`sysctl -n hw.usb.template`
if [ "${_template}" -eq 0 ]; then
sysctl hw.usb.template=-1 > /dev/null
err=$?
if [ "${err}" -ne 0 ]; then
warn "unable to set hw.usb.template sysctl"
return "${err}"
fi
fi
_template=`sysctl -n hw.usb.template`
if [ "${_template}" -lt 0 ]; then
sysctl hw.usb.template=0 > /dev/null
err=$?
if [ "${err}" -ne 0 ]; then
warn "unable to set hw.usb.template sysctl"
return "${err}"
fi
else
# Otherwise don't touch the sysctl - we could lock the user
# out of the machine otherwise.
warn "hw.usb.template sysctl set to neither -1 nor 0"
fi
}
cfumass_stop()
{
local err _template
_template=`sysctl -n hw.usb.template`
if [ "${_template}" -eq 0 ]; then
sysctl hw.usb.template=-1 > /dev/null
err=$?
if [ "${err}" -ne 0 ]; then
warn "unable to set hw.usb.template sysctl"
return "${err}"
fi
fi
remove_luns
}
load_rc_config $name
run_rc_command "$1"

View File

@ -26,7 +26,7 @@
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.Dd April 9, 2018
.Dd April 21, 2018
.Dt CFUMASS 4
.Os
.Sh NAME
@ -88,6 +88,14 @@ See
and
.Xr ctld 8
for details on configuring the LUN.
See the
.Cm cfumass_enable
and
.Cm cfumass_dir
.Xr rc 8
variables in
.Xr rc.conf 5
for an automated way to configure it at boot.
.Sh SYSCTL VARIABLES
These variables are available as both
.Xr sysctl 8

View File

@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd March 10, 2018
.Dd April 21, 2018
.Dt RC.CONF 5
.Os
.Sh NAME
@ -4482,6 +4482,18 @@ The default is
which configures sessions based on the
.Pa /etc/iscsi.conf
configuration file.
.It Va cfumass_enable
.Pq Vt bool
If set to
.Dq Li YES ,
create and export an USB LUN using
.Xr cfumass 4
at boot time.
.It Va cfumass_dir
.Pq Vt str
The directory where the files exported by USB LUN are located.
The default directory is
.Pa /var/cfumass .
.El
.Sh FILES
.Bl -tag -width ".Pa /etc/defaults/rc.conf" -compact