11.1Schristos#!/bin/sh
21.1Schristos#
31.1Schristos# $NetBSD: special_media,v 1.1 2018/01/09 03:31:14 christos Exp $
41.1Schristos#
51.1Schristos
61.1Schristos# Print newline-separated list of devices available for mounting.
71.1Schristos# If there is a filesystem label - use it, otherwise use device name.
81.1Schristosprint_available() {
91.1Schristos	local _fstype _fstype_and_label _label _p
101.1Schristos
111.1Schristos	for _p in ${DEVICES}; do
121.1Schristos		_fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
131.1Schristos		if [ $? -ne 0 ]; then
141.1Schristos			# Ignore devices for which we were unable
151.1Schristos			# to determine filesystem type.
161.1Schristos			continue
171.1Schristos		fi
181.1Schristos
191.1Schristos		_fstype="${_fstype_and_label%% *}"
201.1Schristos		if [ "${_fstype}" != "${_fstype_and_label}" ]; then
211.1Schristos			_label="${_fstype_and_label#* }"
221.1Schristos			# Replace plus signs and slashes with minuses;
231.1Schristos			# leading plus signs have special meaning in maps,
241.1Schristos			# and multi-component keys are just not supported.
251.1Schristos			_label="$(echo ${_label} | sed 's,[+/],-,g')"
261.1Schristos			echo "${_label}"
271.1Schristos			continue
281.1Schristos		fi
291.1Schristos
301.1Schristos		echo "${_p}"
311.1Schristos	done
321.1Schristos}
331.1Schristos
341.1Schristos# Print a single map entry.
351.1Schristosprint_map_entry() {
361.1Schristos	local _fstype _p
371.1Schristos
381.1Schristos	_fstype="$1"
391.1Schristos	_p="$2"
401.1Schristos
411.1Schristos	case "${_fstype}" in
421.1Schristos	"exfat")
431.1Schristos		if [ -f "/usr/local/sbin/mount.exfat" ]; then
441.1Schristos			echo "-mountprog=/usr/local/sbin/mount.exfat,fstype=${_fstype},nosuid	:/dev/${_p}"
451.1Schristos		else
461.1Schristos			/usr/bin/logger -p info -t "special_media[$$]" \
471.1Schristos			    "Cannot mount ${_fstype} formatted device /dev/${_p}: Install sysutils/fusefs-exfat first"
481.1Schristos			exit 1
491.1Schristos		fi
501.1Schristos		;;
511.1Schristos	"ntfs")
521.1Schristos		if [ -f "/usr/local/bin/ntfs-3g" ]; then
531.1Schristos			echo "-mountprog=/usr/local/bin/ntfs-3g,fstype=${_fstype},nosuid	:/dev/${_p}"
541.1Schristos		else
551.1Schristos			/usr/bin/logger -p info -t "special_media[$$]" \
561.1Schristos			    "Cannot mount ${_fstype} formatted device /dev/${_p}: Install sysutils/fusefs-ntfs first"
571.1Schristos			exit 1
581.1Schristos		fi
591.1Schristos		;;
601.1Schristos	"ext2fs" | "msdosfs")
611.1Schristos		echo "-fstype=${_fstype},nosuid,async	:/dev/${_p}"
621.1Schristos		;;
631.1Schristos	*)
641.1Schristos		echo "-fstype=${_fstype},nosuid	:/dev/${_p}"
651.1Schristos		;;
661.1Schristos	esac
671.1Schristos}
681.1Schristos
691.1Schristos# Determine map entry contents for the given key and print out the entry.
701.1Schristosprint_one() {
711.1Schristos	local _fstype _fstype_and_label _label _key _p
721.1Schristos
731.1Schristos	_key="$1"
741.1Schristos
751.1Schristos	_fstype="$(fstyp "/dev/${_key}" 2> /dev/null)"
761.1Schristos	if [ $? -eq 0 ]; then
771.1Schristos		print_map_entry "${_fstype}" "${_key}"
781.1Schristos		return
791.1Schristos	fi
801.1Schristos
811.1Schristos	for _p in ${DEVICES}; do
821.1Schristos		_fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
831.1Schristos		if [ $? -ne 0 ]; then
841.1Schristos			# Ignore devices for which we were unable
851.1Schristos			# to determine filesystem type.
861.1Schristos			continue
871.1Schristos		fi
881.1Schristos
891.1Schristos		_fstype="${_fstype_and_label%% *}"
901.1Schristos		if [ "${_fstype}" = "${_fstype_and_label}" ]; then
911.1Schristos			# No label, try another device.
921.1Schristos			continue
931.1Schristos		fi
941.1Schristos
951.1Schristos		_label="${_fstype_and_label#* }"
961.1Schristos		# Replace plus signs and slashes with minuses;
971.1Schristos		# leading plus signs have special meaning in maps,
981.1Schristos		# and multi-component keys are just not supported.
991.1Schristos		_label="$(echo ${_label} | sed 's,[+/],-,g')"
1001.1Schristos		if [ "${_label}" != "${_key}" ]; then
1011.1Schristos			# Labels don't match, try another device.
1021.1Schristos			continue
1031.1Schristos		fi
1041.1Schristos
1051.1Schristos		print_map_entry "${_fstype}" "${_p}"
1061.1Schristos	done
1071.1Schristos
1081.1Schristos	# No matching device - don't print anything, autofs will handle it.
1091.1Schristos}
1101.1Schristos
1111.1Schristos# XXX: -media map is unusable at the moment.
1121.1Schristos# This script needs to be able to get the list of devices/partitions. FreeBSD can do this via GEOM sysctl.
1131.1Schristos#HW_DISKNAMES=$(sysctl hw.disknames)
1141.1Schristos#if [ $? -ne 0 ]; then
1151.1Schristos#	exit 1
1161.1Schristos#fi
1171.1Schristos# Cut "hw.disknames =" and ignore the next " " before names start.
1181.1Schristos#DEVICES=$(echo ${HW_DISKNAMES} | awk '{$1=$2=""; print substr($0,3)}' | awk '{gsub(" ", "\n"); print}' | sort)
1191.1Schristos
1201.1Schristos# ${DEVICES} should contain one device/partition for each line.
1211.1SchristosDEVICES=""
1221.1Schristos
1231.1Schristosif [ $# -eq 0 ]; then
1241.1Schristos	print_available
1251.1Schristos	exit 0
1261.1Schristosfi
1271.1Schristos
1281.1Schristosprint_one "$1"
1291.1Schristosexit 0
1301.1Schristos
131