autofs: Port autofs from FreeBSD
[dragonfly.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 ${DEVICES}; do
12                 # XXX: Ingore /dev/md*
13                 if [ "`echo ${_p} | sed -n '/^md/p'`" != "" ]; then
14                         continue
15                 fi
16
17                 _fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
18                 if [ $? -ne 0 ]; then
19                         # Ignore devices for which we were unable
20                         # to determine filesystem type.
21                         continue
22                 fi
23
24                 _fstype="${_fstype_and_label%% *}"
25                 if [ "${_fstype}" != "${_fstype_and_label}" ]; then
26                         _label="${_fstype_and_label#* }"
27                         # Replace plus signs and slashes with minuses;
28                         # leading plus signs have special meaning in maps,
29                         # and multi-component keys are just not supported.
30                         _label="$(echo ${_label} | sed 's,[+/],-,g')"
31                         echo "${_label}"
32                         continue
33                 fi
34
35                 echo "${_p}"
36         done
37 }
38
39 # Print a single map entry.
40 print_map_entry() {
41         local _fstype _p
42
43         _fstype="$1"
44         _p="$2"
45
46         if [ "${_fstype}" = "ntfs" ]; then
47                 if [ -f "/usr/local/bin/ntfs-3g" ]; then
48                         echo "-mountprog=/usr/local/bin/ntfs-3g,fstype=${_fstype},nosuid        :/dev/${_p}" 
49                 else
50                         /usr/bin/logger -p info -t "special_media[$$]" \
51                             "Cannot mount ${_fstype} formatted device /dev/${_p}: Install sysutils/fusefs-ntfs first"
52                         exit 1
53                 fi
54         else
55                 echo "-fstype=${_fstype},nosuid :/dev/${_p}" 
56         fi
57 }
58
59 # Determine map entry contents for the given key and print out the entry.
60 print_one() {
61         local _fstype _fstype_and_label _label _key _p
62
63         _key="$1"
64
65         _fstype="$(fstyp "/dev/${_key}" 2> /dev/null)"
66         if [ $? -eq 0 ]; then
67                 print_map_entry "${_fstype}" "${_key}"
68                 return
69         fi
70
71         for _p in ${DEVICES}; do
72                 # XXX: Ingore /dev/md*
73                 if [ "`echo ${_p} | sed -n '/^md/p'`" != "" ]; then
74                         continue
75                 fi
76
77                 _fstype_and_label="$(fstyp -l "/dev/${_p}" 2> /dev/null)"
78                 if [ $? -ne 0 ]; then
79                         # Ignore devices for which we were unable
80                         # to determine filesystem type.
81                         continue
82                 fi
83
84                 _fstype="${_fstype_and_label%% *}"
85                 if [ "${_fstype}" = "${_fstype_and_label}" ]; then
86                         # No label, try another device.
87                         continue
88                 fi
89
90                 _label="${_fstype_and_label#* }"
91                 # Replace plus signs and slashes with minuses;
92                 # leading plus signs have special meaning in maps,
93                 # and multi-component keys are just not supported.
94                 _label="$(echo ${_label} | sed 's,[+/],-,g')"
95                 if [ "${_label}" != "${_key}" ]; then
96                         # Labels don't match, try another device.
97                         continue
98                 fi
99
100                 print_map_entry "${_fstype}" "${_p}"
101         done
102
103         # No matching device - don't print anything, autofs will handle it.
104 }
105
106 # XXX: Find a better way to get a list of media devices
107 KERN_DISKS=`sysctl kern.disks`
108 if [ $? -ne 0 ]; then
109         exit 1
110 fi
111 DEVICES=`echo ${KERN_DISKS} | awk '{$1=""; print substr($0,2)}' | awk '{gsub(" ", "\n"); print}' | sort`
112
113 if [ $# -eq 0 ]; then
114         print_available
115         exit 0
116 fi
117
118 print_one "$1"
119 exit 0
120