]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/autofs/special_media
MFV r301238:
[FreeBSD/FreeBSD.git] / etc / autofs / special_media
1 #!/bin/sh
2 #
3 # $FreeBSD$
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.
8 print_available() {
9         local _fstype _fstype_and_label _label _p
10
11         for _p in ${providers}; 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.
35 print_map_entry() {
36         local _fstype _p
37
38         _fstype="$1"
39         _p="$2"
40
41         if [ "${_fstype}" = "ntfs" ]; then
42                 if [ -f "/usr/local/bin/ntfs-3g" ]; then
43                         echo "-mountprog=/usr/local/bin/ntfs-3g,fstype=${_fstype},nosuid        :/dev/${_p}" 
44                 else
45                         /usr/bin/logger -p info -t "special_media[$$]" \
46                             "Cannot mount ${_fstype} formatted device /dev/${_p}: Install sysutils/fusefs-ntfs first"
47                         exit 1
48                 fi
49         else
50                 echo "-fstype=${_fstype},nosuid :/dev/${_p}" 
51         fi
52 }
53
54 # Determine map entry contents for the given key and print out the entry.
55 print_one() {
56         local _fstype _fstype_and_label _label _key _p
57
58         _key="$1"
59
60         _fstype="$(fstyp "/dev/${_key}" 2> /dev/null)"
61         if [ $? -eq 0 ]; then
62                 print_map_entry "${_fstype}" "${_key}"
63                 return
64         fi
65
66         for _p in ${providers}; do
67                 _fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
68                 if [ $? -ne 0 ]; then
69                         # Ignore devices for which we were unable
70                         # to determine filesystem type.
71                         continue
72                 fi
73
74                 _fstype="${_fstype_and_label%% *}"
75                 if [ "${_fstype}" = "${_fstype_and_label}" ]; then
76                         # No label, try another device.
77                         continue
78                 fi
79
80                 _label="${_fstype_and_label#* }"
81                 # Replace plus signs and slashes with minuses;
82                 # leading plus signs have special meaning in maps,
83                 # and multi-component keys are just not supported.
84                 _label="$(echo ${_label} | sed 's,[+/],-,g')"
85                 if [ "${_label}" != "${_key}" ]; then
86                         # Labels don't match, try another device.
87                         continue
88                 fi
89
90                 print_map_entry "${_fstype}" "${_p}"
91         done
92
93         # No matching device - don't print anything, autofs will handle it.
94 }
95
96 # Obtain a list of (geom-provider-name, access-count) pairs, turning this:
97 #
98 # z0xfffff80005085d00 [shape=hexagon,label="ada0\nr2w2e3\nerr#0\nsector=512\nstripe=0"];
99 #
100 # Into this:
101 #
102 # ada0 r2w2e3
103 #
104 # XXX: It would be easier to use kern.geom.conftxt instead, but it lacks
105 #      access counts.
106 pairs=$(sysctl kern.geom.confdot | sed -n 's/^.*hexagon,label="\([^\]*\)\\n\([^\]*\).*/\1 \2/p')
107
108 # Obtain a list of GEOM providers that are not already open - not mounted,
109 # and without other GEOM class, such as gpart, attached.  In other words,
110 # grep for "r0w0e0".  Skip providers with names containing slashes; we're
111 # not interested in geom_label(4) creations.
112 providers=$(echo "$pairs" | awk '$2 == "r0w0e0" && $1 !~ /\// { print $1 }')
113
114 if [ $# -eq 0 ]; then
115         print_available
116         exit 0
117 fi
118
119 print_one "$1"
120 exit 0
121