Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 21 Apr 2018 14:56:41 +0000 (UTC)
From:      Edward Tomasz Napierala <trasz@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r332857 - in head: etc/defaults etc/rc.d share/man/man4 share/man/man5
Message-ID:  <201804211456.w3LEufpK091527@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trasz
Date: Sat Apr 21 14:56:41 2018
New Revision: 332857
URL: https://svnweb.freebsd.org/changeset/base/332857

Log:
  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

Added:
  head/etc/rc.d/cfumass   (contents, props changed)
Modified:
  head/etc/defaults/rc.conf
  head/etc/rc.d/Makefile
  head/share/man/man4/cfumass.4
  head/share/man/man5/rc.conf.5

Modified: head/etc/defaults/rc.conf
==============================================================================
--- head/etc/defaults/rc.conf	Sat Apr 21 13:46:07 2018	(r332856)
+++ head/etc/defaults/rc.conf	Sat Apr 21 14:56:41 2018	(r332857)
@@ -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).

Modified: head/etc/rc.d/Makefile
==============================================================================
--- head/etc/rc.d/Makefile	Sat Apr 21 13:46:07 2018	(r332856)
+++ head/etc/rc.d/Makefile	Sat Apr 21 14:56:41 2018	(r332857)
@@ -21,6 +21,7 @@ FILES=	DAEMON \
 	${_bluetooth} \
 	bridge \
 	${_bthidd} \
+	cfumass \
 	cleanvar \
 	cleartmp \
 	cron \

Added: head/etc/rc.d/cfumass
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/etc/rc.d/cfumass	Sat Apr 21 14:56:41 2018	(r332857)
@@ -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"

Modified: head/share/man/man4/cfumass.4
==============================================================================
--- head/share/man/man4/cfumass.4	Sat Apr 21 13:46:07 2018	(r332856)
+++ head/share/man/man4/cfumass.4	Sat Apr 21 14:56:41 2018	(r332857)
@@ -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

Modified: head/share/man/man5/rc.conf.5
==============================================================================
--- head/share/man/man5/rc.conf.5	Sat Apr 21 13:46:07 2018	(r332856)
+++ head/share/man/man5/rc.conf.5	Sat Apr 21 14:56:41 2018	(r332857)
@@ -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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201804211456.w3LEufpK091527>