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