#! /bin/sh # ext-mount, a tool for mounting whichever drive (in a pool) is connected. # Version 1.1 # # Usage: ext-mount [ -vq ] [ -o ] [ ] # Where: # -v means verbose # -q means don't print out the line from the pool file # -o allows mount options to be given to mount (not arbitrary switches) # # The mount point must not contain whitespace. If not specified, the # file system must be listed by UUID in /etc/fstab. # # The pool is listed in /etc/ext-mount/media.conf in the following format, # where the wildcard is the UUID of the desired file system on the disk: # # xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Optional arbitrary description # # Space or tab delimiters are supported. Comments start with # at the # beginning of the line. # # # Copyright (C) 2011 Alastair Irvine # Warpspace IT # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # On Debian systems, the complete text of the GNU General Public License # can be found in `/usr/share/common-licenses/GPL'. # # # TO-DO: # + attempt to unmount all pool drives beforehand (blame HAL) # + fail if a non most recently connected drive is mounted on # + mount the most recently connected drive confdir=/etc/ext-mount conf_hdds=$confdir/media.conf cmd_verbose=false cmd_quiet=false self=`basename $0` allowed_options=vqo: log() { if $cmd_verbose ; then echo "$@" fi } llog() { if $cmd_verbose ; then echo -n "$@" fi } # *** MAINLINE *** # == command-line handling == # process options set -e eval set -- `getopt --shell=sh -n $self +$allowed_options "$@"` set +e # getopt would have already reported the error while [ x"$1" != x-- ] ; do case "$1" in -v) cmd_verbose=true ;; -q) cmd_quiet=true ;; -o) mount_opts="-o $2" ; shift ;; esac shift # get rid of the option (or its arg if the inner shift already got rid it) done shift # get rid of the "--" # process remaining args mount_point=$1 # == processing == # get the UUIDs from the first word of the non-comment lines in the pool file uuids=`awk '/^[^#]/ { print $1; }' $conf_hdds` # iterate through each UUID seeing if the partition is present for uuid in $uuids ; do llog "Trying $uuid... " devpath=/dev/disk/by-uuid/$uuid if [ -e $devpath ] ; then found=$uuid found_devpath=$devpath llog "found: `file /dev/disk/by-uuid/$uuid`" if ! $cmd_quiet ; then # print the relevant line from the pool file grep $uuid $conf_hdds fi fi log # print a newline (see llog at start of loop) done # if present, try to mount it if [ $found ] ; then set -e # assumes that fs type detection works mount $mount_opts $found_devpath $mount_point else echo no listed devices found >&2 exit 2 fi