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