xref: /netbsd-src/etc/autofs/special_media (revision b985414b8f8688f3bfa6718c583260b26b0bf1d1)
1*b985414bSchristos#!/bin/sh
2*b985414bSchristos#
3*b985414bSchristos# $NetBSD: special_media,v 1.1 2018/01/09 03:31:14 christos Exp $
4*b985414bSchristos#
5*b985414bSchristos
6*b985414bSchristos# Print newline-separated list of devices available for mounting.
7*b985414bSchristos# If there is a filesystem label - use it, otherwise use device name.
8*b985414bSchristosprint_available() {
9*b985414bSchristos	local _fstype _fstype_and_label _label _p
10*b985414bSchristos
11*b985414bSchristos	for _p in ${DEVICES}; do
12*b985414bSchristos		_fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
13*b985414bSchristos		if [ $? -ne 0 ]; then
14*b985414bSchristos			# Ignore devices for which we were unable
15*b985414bSchristos			# to determine filesystem type.
16*b985414bSchristos			continue
17*b985414bSchristos		fi
18*b985414bSchristos
19*b985414bSchristos		_fstype="${_fstype_and_label%% *}"
20*b985414bSchristos		if [ "${_fstype}" != "${_fstype_and_label}" ]; then
21*b985414bSchristos			_label="${_fstype_and_label#* }"
22*b985414bSchristos			# Replace plus signs and slashes with minuses;
23*b985414bSchristos			# leading plus signs have special meaning in maps,
24*b985414bSchristos			# and multi-component keys are just not supported.
25*b985414bSchristos			_label="$(echo ${_label} | sed 's,[+/],-,g')"
26*b985414bSchristos			echo "${_label}"
27*b985414bSchristos			continue
28*b985414bSchristos		fi
29*b985414bSchristos
30*b985414bSchristos		echo "${_p}"
31*b985414bSchristos	done
32*b985414bSchristos}
33*b985414bSchristos
34*b985414bSchristos# Print a single map entry.
35*b985414bSchristosprint_map_entry() {
36*b985414bSchristos	local _fstype _p
37*b985414bSchristos
38*b985414bSchristos	_fstype="$1"
39*b985414bSchristos	_p="$2"
40*b985414bSchristos
41*b985414bSchristos	case "${_fstype}" in
42*b985414bSchristos	"exfat")
43*b985414bSchristos		if [ -f "/usr/local/sbin/mount.exfat" ]; then
44*b985414bSchristos			echo "-mountprog=/usr/local/sbin/mount.exfat,fstype=${_fstype},nosuid	:/dev/${_p}"
45*b985414bSchristos		else
46*b985414bSchristos			/usr/bin/logger -p info -t "special_media[$$]" \
47*b985414bSchristos			    "Cannot mount ${_fstype} formatted device /dev/${_p}: Install sysutils/fusefs-exfat first"
48*b985414bSchristos			exit 1
49*b985414bSchristos		fi
50*b985414bSchristos		;;
51*b985414bSchristos	"ntfs")
52*b985414bSchristos		if [ -f "/usr/local/bin/ntfs-3g" ]; then
53*b985414bSchristos			echo "-mountprog=/usr/local/bin/ntfs-3g,fstype=${_fstype},nosuid	:/dev/${_p}"
54*b985414bSchristos		else
55*b985414bSchristos			/usr/bin/logger -p info -t "special_media[$$]" \
56*b985414bSchristos			    "Cannot mount ${_fstype} formatted device /dev/${_p}: Install sysutils/fusefs-ntfs first"
57*b985414bSchristos			exit 1
58*b985414bSchristos		fi
59*b985414bSchristos		;;
60*b985414bSchristos	"ext2fs" | "msdosfs")
61*b985414bSchristos		echo "-fstype=${_fstype},nosuid,async	:/dev/${_p}"
62*b985414bSchristos		;;
63*b985414bSchristos	*)
64*b985414bSchristos		echo "-fstype=${_fstype},nosuid	:/dev/${_p}"
65*b985414bSchristos		;;
66*b985414bSchristos	esac
67*b985414bSchristos}
68*b985414bSchristos
69*b985414bSchristos# Determine map entry contents for the given key and print out the entry.
70*b985414bSchristosprint_one() {
71*b985414bSchristos	local _fstype _fstype_and_label _label _key _p
72*b985414bSchristos
73*b985414bSchristos	_key="$1"
74*b985414bSchristos
75*b985414bSchristos	_fstype="$(fstyp "/dev/${_key}" 2> /dev/null)"
76*b985414bSchristos	if [ $? -eq 0 ]; then
77*b985414bSchristos		print_map_entry "${_fstype}" "${_key}"
78*b985414bSchristos		return
79*b985414bSchristos	fi
80*b985414bSchristos
81*b985414bSchristos	for _p in ${DEVICES}; do
82*b985414bSchristos		_fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
83*b985414bSchristos		if [ $? -ne 0 ]; then
84*b985414bSchristos			# Ignore devices for which we were unable
85*b985414bSchristos			# to determine filesystem type.
86*b985414bSchristos			continue
87*b985414bSchristos		fi
88*b985414bSchristos
89*b985414bSchristos		_fstype="${_fstype_and_label%% *}"
90*b985414bSchristos		if [ "${_fstype}" = "${_fstype_and_label}" ]; then
91*b985414bSchristos			# No label, try another device.
92*b985414bSchristos			continue
93*b985414bSchristos		fi
94*b985414bSchristos
95*b985414bSchristos		_label="${_fstype_and_label#* }"
96*b985414bSchristos		# Replace plus signs and slashes with minuses;
97*b985414bSchristos		# leading plus signs have special meaning in maps,
98*b985414bSchristos		# and multi-component keys are just not supported.
99*b985414bSchristos		_label="$(echo ${_label} | sed 's,[+/],-,g')"
100*b985414bSchristos		if [ "${_label}" != "${_key}" ]; then
101*b985414bSchristos			# Labels don't match, try another device.
102*b985414bSchristos			continue
103*b985414bSchristos		fi
104*b985414bSchristos
105*b985414bSchristos		print_map_entry "${_fstype}" "${_p}"
106*b985414bSchristos	done
107*b985414bSchristos
108*b985414bSchristos	# No matching device - don't print anything, autofs will handle it.
109*b985414bSchristos}
110*b985414bSchristos
111*b985414bSchristos# XXX: -media map is unusable at the moment.
112*b985414bSchristos# This script needs to be able to get the list of devices/partitions. FreeBSD can do this via GEOM sysctl.
113*b985414bSchristos#HW_DISKNAMES=$(sysctl hw.disknames)
114*b985414bSchristos#if [ $? -ne 0 ]; then
115*b985414bSchristos#	exit 1
116*b985414bSchristos#fi
117*b985414bSchristos# Cut "hw.disknames =" and ignore the next " " before names start.
118*b985414bSchristos#DEVICES=$(echo ${HW_DISKNAMES} | awk '{$1=$2=""; print substr($0,3)}' | awk '{gsub(" ", "\n"); print}' | sort)
119*b985414bSchristos
120*b985414bSchristos# ${DEVICES} should contain one device/partition for each line.
121*b985414bSchristosDEVICES=""
122*b985414bSchristos
123*b985414bSchristosif [ $# -eq 0 ]; then
124*b985414bSchristos	print_available
125*b985414bSchristos	exit 0
126*b985414bSchristosfi
127*b985414bSchristos
128*b985414bSchristosprint_one "$1"
129*b985414bSchristosexit 0
130*b985414bSchristos
131