Quantcast
Channel: Linux Device Hacking
Viewing all articles
Browse latest Browse all 46983

Re: Automount USB drives

$
0
0
bohdi,
This is my current startup mount script. I still use pmount but you see its expanded since my original those years ago. It cleans up all stale mount points left over from an ugly shutdown before mounting the connected devices. And I found a few bugs with the original related to disk labels with spaces which required additional quoting and changing IFS to \n as I still make use of bash arrays. I do like your use of udevadm instead of blkid. I haven't touched this script in a while. It may be time for an update :)

#!/bin/bash

# remove any stale pmount locations
PMOUNT=".created_by_pmount"
ROOT="/media"
OLDIFS="$IFS"
IFS=$'\n'
for DIR in $( ls -d1 $ROOT/* )
do
  # a stale pmount location has exactly two entries
  # /media/foo
  # /media/foo/.created_by_pmount
  TEST=( `find "$DIR"` )
  if [ ${#TEST[@]} = 2 ] &&
     [ "${TEST[0]}" = "$DIR" ] &&
     [ "${TEST[1]}" = "$DIR/$PMOUNT" ];
     then
       # remove in the safest possible way
       # instead of using rm -fR $DIR
       echo "Removing stale pmount: $DIR"
       rm "${TEST[1]}"
       rmdir "${TEST[0]}"
  fi
done

# find pmount binary
PMOUNT=`which pmount`
if [ -z "$PMOUNT" ]
then
 echo "Error: cant find pmount: $0"
 IFS="$OLDIFS"
 exit 1
fi

pmount()
{
  local IFS="$OLDIFS"
  echo "Attempting to pmount: $DEV"
  BLKID=( `/sbin/blkid "$ROOT/$DEV"` )
  echo "${BLKID[@]}"
  for ITEM in ${BLKID[@]}
  do
    #echo "$ITEM"
    TEST=`expr match "$ITEM" 'TYPE=\"\(.*\)\"'`
    TYPE=${TEST//\"/}
    # only pmount partitions with a valid file system
    # which is not swap
    if [ -n "$TYPE" ] && [ "$TYPE" != "swap" ]
    then
      echo `$PMOUNT -t $TYPE $OPTIONS "$ROOT/$DEV" "$DEV"`
      break
    fi
  done
}

# pmount any usb storage devices attached at boot
OPTIONS="--sync --noatime --umask 000"
ROOT="/dev/usb-storage"
for DEV in $( ls -1 $ROOT )
do
  pmount
done

IFS="$OLDIFS"
exit 0

Viewing all articles
Browse latest Browse all 46983

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>