1275681Strasz#!/bin/sh
2275681Strasz#
3275681Strasz# $FreeBSD$
4275681Strasz#
5275681Strasz
6275681Strasz# Print newline-separated list of devices available for mounting.
7275681Strasz# If there is a filesystem label - use it, otherwise use device name.
8275681Straszprint_available() {
9275681Strasz	local _fstype _fstype_and_label _label _p
10275681Strasz
11275681Strasz	for _p in ${providers}; do
12275681Strasz		_fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
13275681Strasz		if [ $? -ne 0 ]; then
14275681Strasz			# Ignore devices for which we were unable
15275681Strasz			# to determine filesystem type.
16275681Strasz			continue
17275681Strasz		fi
18275681Strasz
19275681Strasz		_fstype="${_fstype_and_label%% *}"
20275681Strasz		if [ "${_fstype}" != "${_fstype_and_label}" ]; then
21275681Strasz			_label="${_fstype_and_label#* }"
22275681Strasz			echo "${_label}"
23275681Strasz			continue
24275681Strasz		fi
25275681Strasz
26275681Strasz		echo "${_p}"
27275681Strasz	done
28275681Strasz}
29275681Strasz
30275681Strasz# Print a single map entry.
31275681Straszprint_one() {
32275681Strasz	local _fstype _fstype_and_label _label _key _p
33275681Strasz
34275681Strasz	_key="$1"
35275681Strasz
36275681Strasz	_fstype="$(fstyp "/dev/${_key}" 2> /dev/null)"
37275681Strasz	if [ $? -eq 0 ]; then
38275681Strasz		echo "-fstype=${_fstype},nosuid	:/dev/${_key}" 
39275681Strasz		return
40275681Strasz	fi
41275681Strasz
42275681Strasz	for _p in ${providers}; do
43275681Strasz		_fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
44275681Strasz		if [ $? -ne 0 ]; then
45275681Strasz			# Ignore devices for which we were unable
46275681Strasz			# to determine filesystem type.
47275681Strasz			continue
48275681Strasz		fi
49275681Strasz
50275681Strasz		_fstype="${_fstype_and_label%% *}"
51275681Strasz		if [ "${_fstype}" = "${_fstype_and_label}" ]; then
52275681Strasz			# No label, try another device.
53275681Strasz			continue
54275681Strasz		fi
55275681Strasz
56275681Strasz		_label="${_fstype_and_label#* }"
57275681Strasz		if [ "${_label}" != "${_key}" ]; then
58275681Strasz			# Labels don't match, try another device.
59275681Strasz			continue
60275681Strasz		fi
61275681Strasz
62275681Strasz		echo "-fstype=${_fstype},nosuid	:/dev/${_p}" 
63275681Strasz	done
64275681Strasz
65275681Strasz	# No matching device - don't print anything, autofs will handle it.
66275681Strasz}
67275681Strasz
68275681Strasz# Obtain a list of (geom-provider-name, access-count) pairs, turning this:
69275681Strasz#
70275681Strasz# z0xfffff80005085d00 [shape=hexagon,label="ada0\nr2w2e3\nerr#0\nsector=512\nstripe=0"];
71275681Strasz#
72275681Strasz# Into this:
73275681Strasz#
74275681Strasz# ada0 r2w2e3
75275681Strasz#
76275681Strasz# XXX: It would be easier to use kern.geom.conftxt instead, but it lacks
77275681Strasz#      access counts.
78275681Straszpairs=$(sysctl kern.geom.confdot | sed -n 's/^.*hexagon,label="\([^\]*\)\\n\([^\]*\).*/\1 \2/p')
79275681Strasz
80275681Strasz# Obtain a list of GEOM providers that are not already open - not mounted,
81275681Strasz# and without other GEOM class, such as gpart, attached.  In other words,
82275681Strasz# grep for "r0w0e0".  Skip providers with names containing slashes; we're
83275681Strasz# not interested in geom_label(4) creations.
84275681Straszproviders=$(echo "$pairs" | awk '$2 == "r0w0e0" && $1 !~ /\// { print $1 }')
85275681Strasz
86275681Straszif [ $# -eq 0 ]; then
87275681Strasz	print_available
88275681Strasz	exit 0
89275681Straszfi
90275681Strasz
91275681Straszprint_one "$1"
92275681Straszexit 0
93275681Strasz
94