#!/bin/bash # Filepath: /usr/local/bin/usbdrive.sh # Script to automount usb drive # This script should be executed from a udev rule. # Run directly with: bash /usr/local/bin/usbdrive.sh # Copyright 2015 Per Stenebo # Created 2015-01-27 by Per Stenebo # Updated 2015-01-28: Dev and testing on a Raspberry Pi B+ with Raspbian. #----------------------------------------------------------------------- # Configuration #----------------------------------------------------------------------- # Check command path: which mount mountpath=/bin/mount # Array of device files to try, in given order: devicefiles=( /dev/sda1 /dev/sda2 /dev/sdb1 /dev/sdb2 /dev/sdc1 /dev/sdc2 ) mountpoint=/media/usbdrive #----------------------------------------------------------------------- # Check that this script is only running in one instance #----------------------------------------------------------------------- # Build absolute path to process lock file, like /tmp/usbdrive.pid lockfile=/tmp/$(basename $0 | sed 's/\.sh//g').pid; # Check if lock file exist if [ -f $lockfile ]; then # Fetch PID from file read filepid < $lockfile # Check if PID is alive if [ ! -z "$filepid" ] && [ -d /proc/$filepid ]; then # Debug logger "${0}: PID $filepid seems alive, I kill myself now" # Terminate exit 1; fi fi # Write current PID to file echo $$ > $lockfile # Debug logger "${0}:64 started | ACTION: ${ACTION} | param 1: [${1}] | param 2: [${2}]" # Allow device to get ready sleep 3 #----------------------------------------------------------------------- # Prepare mount #----------------------------------------------------------------------- # Check mount point if [ ! -d "${mountpoint}" ]; then # Create mount point mkdir -p ${mountpoint} logger "${0}: ${mountpoint} created" elif [ $(mount | grep -c ${mountpoint}) -ne 0 ]; then logger "${0}: ERROR: ${mountpoint} already mounted" mountpoint='' fi # Set device file deviceFound=0 # Loop configured device files for devicefile in ${devicefiles[@]}; do # Check device file if [ -b "${devicefile}" ] && [ $(mount | grep -c ${devicefile}) -eq 0 ]; then # We found available unmounted device deviceFound=1 break fi done #----------------------------------------------------------------------- # Mount #----------------------------------------------------------------------- deviceMounted=0 if [ -z ${mountpoint} ]; then : # Pass elif [ ${deviceFound} -eq 0 ]; then logger "${0}: ERROR: device file not found" else # /bin/mount /dev/sda1 /media/usbdrive ${mountpath} ${devicefile} ${mountpoint} if [ $(mount | grep -c ${mountpoint}) -eq 1 ]; then logger "${0}: ${devicefile} mounted on ${mountpoint}" deviceMounted=1 else logger "${0}: ERROR: Failed to mount ${devicefile} on ${mountpoint}" fi fi #----------------------------------------------------------------------- # Do other actions #----------------------------------------------------------------------- if [ ${deviceMounted} -eq 1 ]; then logger "${0}: We can do som stuff with ${mountpoint}" fi #----------------------------------------------------------------------- # Unmount #----------------------------------------------------------------------- if [ ${deviceMounted} -eq 1 ]; then umount ${mountpoint} if [ $(mount | grep -c ${mountpoint}) -eq 0 ]; then logger "${0}: ${devicefile} unmounted from ${mountpoint}" deviceMounted=1 else logger "${0}: ERROR: Failed to unmount ${devicefile} from ${mountpoint}" fi fi #----------------------------------------------------------------------- # Finish #----------------------------------------------------------------------- exit 0