Index: etc/auto_master =================================================================== --- etc/auto_master +++ etc/auto_master @@ -3,3 +3,6 @@ # Automounter master map, see auto_master(5) for details. # /net -hosts -nobrowse,nosuid +# When using the -media special map, make sure to uncomment +# the call to "automount -c" in devd.conf. +#/media -media -nosuid Index: etc/autofs/Makefile =================================================================== --- etc/autofs/Makefile +++ etc/autofs/Makefile @@ -1,6 +1,6 @@ # $FreeBSD$ -FILES= include_ldap special_hosts special_null +FILES= include_ldap special_hosts special_media special_null NO_OBJ= FILESDIR= /etc/autofs Index: etc/autofs/special_media =================================================================== --- /dev/null +++ etc/autofs/special_media @@ -0,0 +1,118 @@ +#!/bin/sh +# +# $FreeBSD$ +# + +# Obtain a list of (geom-provider-name, access-count) pairs, turning this +# +# z0xfffff80005085d00 [shape=hexagon,label="ada0\nr2w2e3\nerr#0\nsector=512\nstripe=0"]; +# +# Into this: +# +# ada0 r2w2e3 +# +# It would be easier to use kern.geom.conftxt instead, but it lacks +# access counts. +pairs=$(sysctl kern.geom.confdot | grep hexagon | sed 's/.*label="\([^\]*\)\\n\([^\]*\).*/\1 \2/') + +# Obtain a list of GEOM providers that are not already open - not mounted, +# and without other GEOM class, such as gpart, attached. In other words, +# grep for "r0w0e0". +providers=$(echo "$pairs" | awk '$2 == "r0w0e0" { print $1 }') + +fstype_for_geom() { + local _fstype _geom _type + + _fstype="" + _geom="$1" + + # XXX: Instead of file(1) we should have an utility to determine + # filesystem type. It should also provide a way to obtain + # filesystem label, to replace the glabel hack. + _type="`file -bs /dev/${_geom}`" + case "${_type}" in + (*FAT*) # must be before NTFS section because: newfs_msdos -O NTFS -L NTFS + _fstype="msdosfs" + ;; + (*ext2*) + _fstype="ext2fs" + ;; + (*ext3*) + _fstype="ext2fs" + ;; + (*Unix\ Fast\ File*) + _fstype="ufs" + ;; + (*ISO\ 9660*) + _fstype="cd9660" + ;; + esac + + echo "${_fstype}" +} + +label_for_geom() { + local _geom _label + + _label="" + _geom="$1" + + # We cannot use "glabel status $geom", because it returns + # first label, which might be of uninteresting kind, eg. gptid. + _label="`glabel status -s | awk '/gptid|ufsid/ { next }; { if($3 == "'"${_geom}"'") print $1 }'`" + + # Return label, stripping the "whatever/" prefix (eg. "ufs/"). + echo "${_label}" | sed 's!^.*/!!' +} + +geom_for_label() { + local _label + + _label="$1" + + glabel status -s | awk '/gptid|ufsid/ { next }; { if($1 ~ /'"${_label}"'/) print $3 }' +} + +if [ $# -eq 0 ]; then + # Called without any arguments; print newline-separated list + # of devices available for mounting. + for p in $providers; do + # Ignore providers containing slashes; we're not interested + # in geom_label creations. + case $p in + (*/*) + continue; + ;; + esac + + fstype="`fstype_for_geom $p`" + if [ -z "$fstype" ]; then + # Ignore devices for which we were unable + # to determine filesystem type. + continue + fi + + label="`label_for_geom $p`" + if [ -n "$label" ]; then + p="$label" + fi + + echo "$p" + done + + exit 0 +fi; + +key="$1" +p="`geom_for_label $key`" +if [ -n "$p" ]; then + key="$p" +fi + +fstype="`fstype_for_geom $key`" +if [ -z "$fstype" ]; then + # Unrecognized filesystem; don't return anything. + exit 0 +fi + +echo "-fstype=$fstype,nosuid :/dev/$key" Index: etc/devd.conf =================================================================== --- etc/devd.conf +++ etc/devd.conf @@ -318,4 +318,16 @@ action "/usr/local/etc/rc.d/postgresql restart"; }; +# Discard autofs caches, useful for the -media special map. The one +# second delay is for GEOM to finish tasting. +# +# XXX: We should probably have a devctl(4) event that fires after GEOM +# tasting. +# +notify 100 { + match "system" "DEVFS"; + match "cdev" "(da|mmcsd)[0-9]+"; + action "sleep 1 && /usr/sbin/automount -c"; +}; + */ Index: usr.sbin/autofs/auto_master.5 =================================================================== --- usr.sbin/autofs/auto_master.5 +++ usr.sbin/autofs/auto_master.5 @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd November 21, 2014 +.Dd November 22, 2014 .Dt AUTO_MASTER 5 .Os .Sh NAME @@ -219,6 +219,10 @@ It enables access to files on a remote NFS server by accessing .Pa /net/nfs-server-ip/share-name/ directory, without the need for any further configuration. +.It Li -media +This map queries devices that are not yet mounted, but contain +valid filesystems. +It enables access to files on removable media. .It Li -null This map prevents the .Xr automountd 8