#!/bin/bash # Hotplug script to automatically make symlinks to hotplugged devices. # Requires a configuration file in /etc/hotplug.d/block called symlinks.rc # This file should have the following format: # /dev/muvo NOMAD MuVo NX # /dev/other Some other device # The first word on each line should be the desired symlink position, and the # second argument should be the model, as found in /sys/block/sd?/device/model # when the device is plugged in. Hence, if you plug in the device with model # name NOMAD MuVo NX, a symlink to its device node (e.g. /dev/sda) will be # created in /dev/muvo. You can then set up an fstab entry to use /dev/muvo # as the device for the MuVo USB device. CONFIG="/etc/hotplug.d/block/symlinks.rc" # Read the config file and figure out the symlink # Arguments: the full model string get_symlink () { # Get the model, but make it whitespace agnostic, since sometimes the name # is padded with whitespace local target_model=$(echo $1) local link="" link=$(cat "${CONFIG}" | grep -i "${target_model}" | awk '{ print $1 }') if test -n "${link}" ; then echo "${link}" return 0 else return 1 fi } # Wait for a file to appear (stolen from Wout Mertens, wmertens@gentoo.org) wait_for_file () { local count=0 while test $count -lt 10 && ! test -r "$1" ; do sleep 1 count=$((count + 1)) done test $count -eq 10 && return 1 return 0 } # Only handle block devices test "${1}" = "block" || exit 1 # (Not called for block hotplug) # Ensure we have a partition device partition=$(echo "${DEVPATH}" | awk -F\/ '{ print $4 }') test -n "${partition}" || exit 2 # (Invalid $DEVPATH; should be /block/sd?/sd??) # Figure which device to mount device=$(echo "${DEVPATH}" | awk -F\/ '{ print $3 }') test -n "${device}" || exit 2 # Get the model we're looking for model_file="/sys/block/${device}/device/model" wait_for_file "${model_file}" test -r "${model_file}" || exit 3 # (Could not read model) model=$(< ${model_file}) test -n "${model}" || exit 3 link=$(get_symlink "${model}") test -n "${link}" || exit 4 # (Could not find link for model) case "${ACTION}" in add) ln -sf "/dev/${partition}" "${link}" ;; remove) test -L "${link}" && rm -f "${link}" ;; esac