Skip to main content
Code Review

Return to Question

replaced http://superuser.com/ with https://superuser.com/
Source Link

All I have to date is generic udev rules and mount/unmount scripts ready to add code to see if the config file exists and start processing. I heavily borrowed ideas, code and udev manual SuperUser Answer for udev SuperUser Answer for udev as well as the Mount Manager example

#!/bin/sh
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# httphttps://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check input
if [ -z "$LOG_FILE" ]; then
 exit 1
fi
if [ -z "$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "$DEVICE" ]; then
 exit 1
fi
if [ -z "$FILESYSTEM" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt" >> $LOG_FILE
# test that this device isn't already mounted
device_is_mounted=`grep ${DEVICE} /etc/mtab`
if [ -n "$device_is_mounted" ]; then
 echo "Error: seems /dev/${DEVICE} is already mounted" >> $LOG_FILE 2>&1
 exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "$MOUNT_DIR/${DEVICE}" ]; then
 # make the mountpoint
 sudo mkdir "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # mount the device base on USB file system
 case "$FILESYSTEM" in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 device_is_mounted=`grep ${DEVICE} /etc/mtab`
 if [ -n "$device_is_mounted" ]; then
 echo "/dev/${DEVICE} successfully mounted" >> $LOG_FILE 2>&1
 fi
 # all done here, return successful
 exit 0
fi
exit 1

All I have to date is generic udev rules and mount/unmount scripts ready to add code to see if the config file exists and start processing. I heavily borrowed ideas, code and udev manual SuperUser Answer for udev as well as the Mount Manager example

#!/bin/sh
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# http://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check input
if [ -z "$LOG_FILE" ]; then
 exit 1
fi
if [ -z "$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "$DEVICE" ]; then
 exit 1
fi
if [ -z "$FILESYSTEM" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt" >> $LOG_FILE
# test that this device isn't already mounted
device_is_mounted=`grep ${DEVICE} /etc/mtab`
if [ -n "$device_is_mounted" ]; then
 echo "Error: seems /dev/${DEVICE} is already mounted" >> $LOG_FILE 2>&1
 exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "$MOUNT_DIR/${DEVICE}" ]; then
 # make the mountpoint
 sudo mkdir "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # mount the device base on USB file system
 case "$FILESYSTEM" in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 device_is_mounted=`grep ${DEVICE} /etc/mtab`
 if [ -n "$device_is_mounted" ]; then
 echo "/dev/${DEVICE} successfully mounted" >> $LOG_FILE 2>&1
 fi
 # all done here, return successful
 exit 0
fi
exit 1

All I have to date is generic udev rules and mount/unmount scripts ready to add code to see if the config file exists and start processing. I heavily borrowed ideas, code and udev manual SuperUser Answer for udev as well as the Mount Manager example

#!/bin/sh
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# https://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check input
if [ -z "$LOG_FILE" ]; then
 exit 1
fi
if [ -z "$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "$DEVICE" ]; then
 exit 1
fi
if [ -z "$FILESYSTEM" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt" >> $LOG_FILE
# test that this device isn't already mounted
device_is_mounted=`grep ${DEVICE} /etc/mtab`
if [ -n "$device_is_mounted" ]; then
 echo "Error: seems /dev/${DEVICE} is already mounted" >> $LOG_FILE 2>&1
 exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "$MOUNT_DIR/${DEVICE}" ]; then
 # make the mountpoint
 sudo mkdir "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # mount the device base on USB file system
 case "$FILESYSTEM" in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 device_is_mounted=`grep ${DEVICE} /etc/mtab`
 if [ -n "$device_is_mounted" ]; then
 echo "/dev/${DEVICE} successfully mounted" >> $LOG_FILE 2>&1
 fi
 # all done here, return successful
 exit 0
fi
exit 1
Rollback to Revision 2
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

EDITED: using guidelines below

#!/bin/bash
# /home/pi/scripts/usb-initloader.sh
#
# make sure to chmod 0755 on these scripts
#
# script that runs when usb device is inserted
# checks to see if file is on USB drive and copies it over
# eventually run a python script that loads the config file
#
# ** DEVICE ADDED **
# should be called from a udev rule like:
# passes kernel device and filesystem type
# ACTION=="add", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh ADD %k %E{key}"
# Mounts usb device on /media/<dev>
# Logs changes to /var/log/syslog
# use tail /var/log/syslog to look at latest events in log
#
# ** DEVICE REMOVED **
# or remove - where we only need the kernel device
# should be called from a udev rule like:
# ACTION=="remove", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh %k"
#
#
# CONFIGURATION
#
SCRIPT_DIR=/home/pi/scripts
#
LOG_DIR=/home/pi/logs
LOG_FILE="${LOG_DIR}/usb-automount.log"
MOUNT_DIR=/media
# Call new script and leave this one (with trailing "&")
if [ "${1}""1ドル" == "ADD" ]; then
 DEVICE="${2}"DEVICE="2ドル" # USB device name (formed with kernel passed from rule)
 DEVTYPE="${3}"DEVTYPE="3ドル" # USB device formatting type
 ${SCRIPT_DIR}/home/pi/scripts/usb-automount.sh "${LOG_FILE}""$LOG_FILE" "${MOUNT_DIR}""$MOUNT_DIR" "${DEVICE}""$DEVICE" "${DEVTYPE}""$DEVTYPE" &
else
 DEVICE="${1}"DEVICE="1ドル" # USB device name (formed with kernel passed from rule)
 ${SCRIPT_DIR}/home/pi/scripts/usb-unloader.sh "${LOG_FILE}""$LOG_FILE" "${MOUNT_DIR}""$MOUNT_DIR" "${DEVICE}""$DEVICE" &
fi

EDITED: using guidelines below

#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
# Called from /home/pi/scripts/usb-initloader.sh
# make sure to chmod 0755 on file
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
# CONFIGURATION
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
# check for defined log fileinput
if [ -z "${LOG_FILE}""$LOG_FILE" ]; then
 exit 1
fi
autounload >> "${LOG_FILE}" 2>&1
# Functions:
# -----------

# Is device mounted (listed in current devices)?
is_mounted() {
 grep -q "${1}" /etc/mtab
}
# autounload function to unmount USB device and remove mount folder
autounload() {
 #check input parameters
 if [ -z "${MOUNT_DIR}""$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "${DEVICE}""$DEVICE" ]; then
 exit 1
fi

dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB Auto UnLoader --- $dt" >> $LOG_FILE
sudo umount "/dev/$DEVICE" >> $LOG_FILE 2>&1
sudo rmdir "$MOUNT_DIR/$DEVICE" 
 # test that this device isn't>> already$LOG_FILE mounted2>&1
 if ! is_mounted
device_is_mounted=`grep "$${DEVICE}"; then/etc/mtab`
if [ "$device_is_mounted" == "" ]; then
  echo "/dev/${DEVICE} successfully UnMounted" >> exit$LOG_FILE 12>&1
fi 
}

EDITED using guidelines below

#!/bin/bashsh
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# http://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
# 
# Edited with many suggestions from @janos (http://codereview.stackexchange.com/users/12390/janos)
#
# Also important to these files: http://www.shellcheck.net/
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check for defined log fileinput
if [ -z "${LOG_FILE}""$LOG_FILE" ]; then
 exit 1
fi
automount >> "${LOG_FILE}" 2>&1
# Functions:
# -----------
# Is device mounted (listed in current devices)?
is_mounted() {
 grep -q "${1}" /etc/mtab
}
# automount function to test/make/mount USB device 
automount() {
 #check input parameters
 if [ -z "${MOUNT_DIR}""$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "${DEVICE}""$DEVICE" ]; then
 exit 1
fi
if [ -z "${FILESYSTEM}""$FILESYSTEM" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt" >> $LOG_FILE
# test that this device isn't already mounted
 if is_mounteddevice_is_mounted=`grep "$${DEVICE}"; then/etc/mtab`
if [ -n "$device_is_mounted" ]; then
 echo "Error: seems /dev/${DEVICE} is already mounted" >> $LOG_FILE 2>&1
 exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "${MOUNT_DIR}"$MOUNT_DIR/${DEVICE}" ]; then
 echo "Error: seems ${MOUNT_DIR}/${DEVICE} already exists"
 exit 1
 fi

 # make the mountpoint
 sudo mkdir "${MOUNT_DIR}"$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "${MOUNT_DIR}"$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # mount the device base on USB file system
 case ${FILESYSTEM}"$FILESYSTEM" in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "${MOUNT_DIR}"$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "${MOUNT_DIR}"$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "${MOUNT_DIR}"$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 if is_mounteddevice_is_mounted=`grep "$${DEVICE}"; /etc/mtab`
 if [ -n "$device_is_mounted" ]; then
 echo "SUCCESS: "/dev/${DEVICE} successfully mounted" >> $LOG_FILE 2>&1
 fi

 # all done here, return successful
 exit 0
fi
}
exit 1

EDITED: using guidelines below

#!/bin/bash
# /home/pi/scripts/usb-initloader.sh
#
# make sure to chmod 0755 on these scripts
#
# script that runs when usb device is inserted
# checks to see if file is on USB drive and copies it over
# eventually run a python script that loads the config file
#
# ** DEVICE ADDED **
# should be called from a udev rule like:
# passes kernel device and filesystem type
# ACTION=="add", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh ADD %k %E{key}"
# Mounts usb device on /media/<dev>
# Logs changes to /var/log/syslog
# use tail /var/log/syslog to look at latest events in log
#
# ** DEVICE REMOVED **
# or remove - where we only need the kernel device
# should be called from a udev rule like:
# ACTION=="remove", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh %k"
#
#
# CONFIGURATION
#
SCRIPT_DIR=/home/pi/scripts
#
LOG_DIR=/home/pi/logs
LOG_FILE="${LOG_DIR}/usb-automount.log"
MOUNT_DIR=/media
# Call new script and leave this one (with trailing "&")
if [ "${1}" == "ADD" ]; then
 DEVICE="${2}" # USB device name (formed with kernel passed from rule)
 DEVTYPE="${3}" # USB device formatting type
 ${SCRIPT_DIR}/usb-automount.sh "${LOG_FILE}" "${MOUNT_DIR}" "${DEVICE}" "${DEVTYPE}" &
else
 DEVICE="${1}" # USB device name (formed with kernel passed from rule)
 ${SCRIPT_DIR}/usb-unloader.sh "${LOG_FILE}" "${MOUNT_DIR}" "${DEVICE}" &
fi

EDITED: using guidelines below

#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
# Called from /home/pi/scripts/usb-initloader.sh
# make sure to chmod 0755 on file
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
# CONFIGURATION
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
# check for defined log file
if [ -z "${LOG_FILE}" ]; then
 exit 1
fi
autounload >> "${LOG_FILE}" 2>&1
# Functions:
# -----------

# Is device mounted (listed in current devices)?
is_mounted() {
 grep -q "${1}" /etc/mtab
}
# autounload function to unmount USB device and remove mount folder
autounload() {
 #check input parameters
 if [ -z "${MOUNT_DIR}" ]; then
 exit 1
fi
if [ -z "${DEVICE}" ]; then
 exit 1
fi

dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB Auto UnLoader --- $dt" sudo umount "/dev/$DEVICE" sudo rmdir "$MOUNT_DIR/$DEVICE" 
 # test that this device isn't already mounted
 if ! is_mounted "${DEVICE}"; then
 echo "/dev/${DEVICE} successfully UnMounted" exit 1
fi 
}

EDITED using guidelines below

#!/bin/bash
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# http://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
# 
# Edited with many suggestions from @janos (http://codereview.stackexchange.com/users/12390/janos)
#
# Also important to these files: http://www.shellcheck.net/
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check for defined log file
if [ -z "${LOG_FILE}" ]; then
 exit 1
fi
automount >> "${LOG_FILE}" 2>&1
# Functions:
# -----------
# Is device mounted (listed in current devices)?
is_mounted() {
 grep -q "${1}" /etc/mtab
}
# automount function to test/make/mount USB device 
automount() {
 #check input parameters
 if [ -z "${MOUNT_DIR}" ]; then
 exit 1
fi
if [ -z "${DEVICE}" ]; then
 exit 1
fi
if [ -z "${FILESYSTEM}" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt" # test that this device isn't already mounted
 if is_mounted "${DEVICE}"; then
 echo "Error: seems /dev/${DEVICE} is already mounted" exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "${MOUNT_DIR}/${DEVICE}" ]; then
 echo "Error: seems ${MOUNT_DIR}/${DEVICE} already exists"
 exit 1
 fi

 # make the mountpoint
 sudo mkdir "${MOUNT_DIR}/${DEVICE}"
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "${MOUNT_DIR}/${DEVICE}"
 # mount the device base on USB file system
 case ${FILESYSTEM} in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "${MOUNT_DIR}/${DEVICE}"
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "${MOUNT_DIR}/${DEVICE}"
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "${MOUNT_DIR}/${DEVICE}"
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 if is_mounted "${DEVICE}"; then
 echo "SUCCESS: /dev/${DEVICE} successfully mounted"
 fi
} 
#!/bin/bash
# /home/pi/scripts/usb-initloader.sh
#
# make sure to chmod 0755 on these scripts
#
# script that runs when usb device is inserted
# checks to see if file is on USB drive and copies it over
# eventually run a python script that loads the config file
#
# ** DEVICE ADDED **
# should be called from a udev rule like:
# passes kernel device and filesystem type
# ACTION=="add", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh ADD %k %E{key}"
# Mounts usb device on /media/<dev>
# Logs changes to /var/log/syslog
# use tail /var/log/syslog to look at latest events in log
#
# ** DEVICE REMOVED **
# or remove - where we only need the kernel device
# should be called from a udev rule like:
# ACTION=="remove", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh %k"
#
#
# CONFIGURATION
#
LOG_DIR=/home/pi/logs
LOG_FILE="${LOG_DIR}/usb-automount.log"
MOUNT_DIR=/media
# Call new script and leave this one (with trailing "&")
if [ "1ドル" == "ADD" ]; then
 DEVICE="2ドル" # USB device name (formed with kernel passed from rule)
 DEVTYPE="3ドル" # USB device formatting type
 /home/pi/scripts/usb-automount.sh "$LOG_FILE" "$MOUNT_DIR" "$DEVICE" "$DEVTYPE" &
else
 DEVICE="1ドル" # USB device name (formed with kernel passed from rule)
 /home/pi/scripts/usb-unloader.sh "$LOG_FILE" "$MOUNT_DIR" "$DEVICE" &
fi
#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
# Called from /home/pi/scripts/usb-initloader.sh
# make sure to chmod 0755 on file
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
# CONFIGURATION
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
# check input
if [ -z "$LOG_FILE" ]; then
 exit 1
fi
if [ -z "$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "$DEVICE" ]; then
 exit 1
fi
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB Auto UnLoader --- $dt" >> $LOG_FILE
sudo umount "/dev/$DEVICE" >> $LOG_FILE 2>&1
sudo rmdir "$MOUNT_DIR/$DEVICE" >> $LOG_FILE 2>&1

device_is_mounted=`grep ${DEVICE} /etc/mtab`
if [ "$device_is_mounted" == "" ]; then
  echo "/dev/${DEVICE} successfully UnMounted" >> $LOG_FILE 2>&1
fi
#!/bin/sh
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# http://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check input
if [ -z "$LOG_FILE" ]; then
 exit 1
fi
if [ -z "$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "$DEVICE" ]; then
 exit 1
fi
if [ -z "$FILESYSTEM" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt" >> $LOG_FILE
# test that this device isn't already mounted
device_is_mounted=`grep ${DEVICE} /etc/mtab`
if [ -n "$device_is_mounted" ]; then
 echo "Error: seems /dev/${DEVICE} is already mounted" >> $LOG_FILE 2>&1
 exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "$MOUNT_DIR/${DEVICE}" ]; then
 # make the mountpoint
 sudo mkdir "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # mount the device base on USB file system
 case "$FILESYSTEM" in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 device_is_mounted=`grep ${DEVICE} /etc/mtab`
 if [ -n "$device_is_mounted" ]; then
 echo "/dev/${DEVICE} successfully mounted" >> $LOG_FILE 2>&1
 fi

 # all done here, return successful
 exit 0
fi

exit 1
Edit using suggested changes and guidelines below
Source Link
dbmitch
  • 295
  • 3
  • 12

EDITED: using guidelines below

#!/bin/bash
# /home/pi/scripts/usb-initloader.sh
#
# make sure to chmod 0755 on these scripts
#
# script that runs when usb device is inserted
# checks to see if file is on USB drive and copies it over
# eventually run a python script that loads the config file
#
# ** DEVICE ADDED **
# should be called from a udev rule like:
# passes kernel device and filesystem type
# ACTION=="add", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh ADD %k %E{key}"
# Mounts usb device on /media/<dev>
# Logs changes to /var/log/syslog
# use tail /var/log/syslog to look at latest events in log
#
# ** DEVICE REMOVED **
# or remove - where we only need the kernel device
# should be called from a udev rule like:
# ACTION=="remove", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh %k"
#
#
# CONFIGURATION
#
SCRIPT_DIR=/home/pi/scripts
#
LOG_DIR=/home/pi/logs
LOG_FILE="${LOG_DIR}/usb-automount.log"
MOUNT_DIR=/media
# Call new script and leave this one (with trailing "&")
if [ "1ドル""${1}" == "ADD" ]; then
 DEVICE="2ドル"DEVICE="${2}" # USB device name (formed with kernel passed from rule)
 DEVTYPE="3ドル"DEVTYPE="${3}" # USB device formatting type
 /home/pi/scripts${SCRIPT_DIR}/usb-automount.sh "$LOG_FILE""${LOG_FILE}" "$MOUNT_DIR""${MOUNT_DIR}" "$DEVICE""${DEVICE}" "$DEVTYPE""${DEVTYPE}" &
else
 DEVICE="1ドル"DEVICE="${1}" # USB device name (formed with kernel passed from rule)
 /home/pi/scripts${SCRIPT_DIR}/usb-unloader.sh "$LOG_FILE""${LOG_FILE}" "$MOUNT_DIR""${MOUNT_DIR}" "$DEVICE""${DEVICE}" &
fi

EDITED: using guidelines below

#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
# Called from /home/pi/scripts/usb-initloader.sh
# make sure to chmod 0755 on file
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
# CONFIGURATION
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
# check inputfor defined log file
if [ -z "$LOG_FILE""${LOG_FILE}" ]; then
 exit 1
fi
autounload >> "${LOG_FILE}" 2>&1
# Functions:
# -----------

# Is device mounted (listed in current devices)?
is_mounted() {
 grep -q "${1}" /etc/mtab
}
# autounload function to unmount USB device and remove mount folder
autounload() {
 #check input parameters
 if [ -z "$MOUNT_DIR""${MOUNT_DIR}" ]; then
 exit 1
fi
if [ -z "$DEVICE""${DEVICE}" ]; then
 exit 1
fi

dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB Auto UnLoader --- $dt" >> $LOG_FILE
sudo umount "/dev/$DEVICE" >> $LOG_FILE 2>&1
sudo rmdir "$MOUNT_DIR/$DEVICE" >> $LOG_FILE 2>&1
device_is_mounted=`grep ${DEVICE} /etc/mtab` # test that this device isn't already mounted
if [ "$device_is_mounted" == ""if ];! is_mounted "${DEVICE}"; then
 echo "/dev/${DEVICE} successfully UnMounted" >> $LOG_FILE 2>&1 exit 1
fi 
}

EDITED using guidelines below

#!/bin/shbash
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# http://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
# 
# Edited with many suggestions from @janos (http://codereview.stackexchange.com/users/12390/janos)
#
# Also important to these files: http://www.shellcheck.net/
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check inputfor defined log file
if [ -z "$LOG_FILE""${LOG_FILE}" ]; then
 exit 1
fi
automount >> "${LOG_FILE}" 2>&1
# Functions:
# -----------
# Is device mounted (listed in current devices)?
is_mounted() {
 grep -q "${1}" /etc/mtab
}
# automount function to test/make/mount USB device 
automount() {
 #check input parameters
 if [ -z "$MOUNT_DIR""${MOUNT_DIR}" ]; then
 exit 1
fi
if [ -z "$DEVICE""${DEVICE}" ]; then
 exit 1
fi
if [ -z "$FILESYSTEM""${FILESYSTEM}" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt" >> $LOG_FILE
# test that this device isn't already mounted
device_is_mounted=`grep $ if is_mounted "${DEVICE}"; /etc/mtab`then
if [ -n "$device_is_mounted" ]; then
 echo "Error: seems /dev/${DEVICE} is already mounted" >> $LOG_FILE 2>&1
 exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "$MOUNT_DIR"${MOUNT_DIR}/${DEVICE}" ]; then
 echo "Error: seems ${MOUNT_DIR}/${DEVICE} already exists"
 exit 1
 fi

 # make the mountpoint
 sudo mkdir "$MOUNT_DIR"${MOUNT_DIR}/${DEVICE}" >> $LOG_FILE 2>&1
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "$MOUNT_DIR"${MOUNT_DIR}/${DEVICE}" >> $LOG_FILE 2>&1
 # mount the device base on USB file system
 case "$FILESYSTEM"${FILESYSTEM} in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "$MOUNT_DIR"${MOUNT_DIR}/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "$MOUNT_DIR"${MOUNT_DIR}/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "$MOUNT_DIR"${MOUNT_DIR}/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 device_is_mounted=`grep ${DEVICE} /etc/mtab`
 if [ -n "$device_is_mounted"is_mounted ];"${DEVICE}"; then
 echo ""SUCCESS: /dev/${DEVICE} successfully mounted" >> $LOG_FILE 2>&1
 fi

 # all done here, return successful
 exit 0
fi
exit} 1
#!/bin/bash
# /home/pi/scripts/usb-initloader.sh
#
# make sure to chmod 0755 on these scripts
#
# script that runs when usb device is inserted
# checks to see if file is on USB drive and copies it over
# eventually run a python script that loads the config file
#
# ** DEVICE ADDED **
# should be called from a udev rule like:
# passes kernel device and filesystem type
# ACTION=="add", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh ADD %k %E{key}"
# Mounts usb device on /media/<dev>
# Logs changes to /var/log/syslog
# use tail /var/log/syslog to look at latest events in log
#
# ** DEVICE REMOVED **
# or remove - where we only need the kernel device
# should be called from a udev rule like:
# ACTION=="remove", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh %k"
#
#
# CONFIGURATION
#
LOG_DIR=/home/pi/logs
LOG_FILE="${LOG_DIR}/usb-automount.log"
MOUNT_DIR=/media
# Call new script and leave this one (with trailing "&")
if [ "1ドル" == "ADD" ]; then
 DEVICE="2ドル" # USB device name (formed with kernel passed from rule)
 DEVTYPE="3ドル" # USB device formatting type
 /home/pi/scripts/usb-automount.sh "$LOG_FILE" "$MOUNT_DIR" "$DEVICE" "$DEVTYPE" &
else
 DEVICE="1ドル" # USB device name (formed with kernel passed from rule)
 /home/pi/scripts/usb-unloader.sh "$LOG_FILE" "$MOUNT_DIR" "$DEVICE" &
fi
#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
# Called from /home/pi/scripts/usb-initloader.sh
# make sure to chmod 0755 on file
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
# CONFIGURATION
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
# check input
if [ -z "$LOG_FILE" ]; then
 exit 1
fi
if [ -z "$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "$DEVICE" ]; then
 exit 1
fi
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB Auto UnLoader --- $dt" >> $LOG_FILE
sudo umount "/dev/$DEVICE" >> $LOG_FILE 2>&1
sudo rmdir "$MOUNT_DIR/$DEVICE" >> $LOG_FILE 2>&1
device_is_mounted=`grep ${DEVICE} /etc/mtab`
if [ "$device_is_mounted" == "" ]; then
 echo "/dev/${DEVICE} successfully UnMounted" >> $LOG_FILE 2>&1
fi
#!/bin/sh
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# http://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check input
if [ -z "$LOG_FILE" ]; then
 exit 1
fi
if [ -z "$MOUNT_DIR" ]; then
 exit 1
fi
if [ -z "$DEVICE" ]; then
 exit 1
fi
if [ -z "$FILESYSTEM" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt" >> $LOG_FILE
# test that this device isn't already mounted
device_is_mounted=`grep ${DEVICE} /etc/mtab`
if [ -n "$device_is_mounted" ]; then
 echo "Error: seems /dev/${DEVICE} is already mounted" >> $LOG_FILE 2>&1
 exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "$MOUNT_DIR/${DEVICE}" ]; then
 # make the mountpoint
 sudo mkdir "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 # mount the device base on USB file system
 case "$FILESYSTEM" in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "$MOUNT_DIR/${DEVICE}" >> $LOG_FILE 2>&1
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 device_is_mounted=`grep ${DEVICE} /etc/mtab`
 if [ -n "$device_is_mounted" ]; then
 echo "/dev/${DEVICE} successfully mounted" >> $LOG_FILE 2>&1
 fi

 # all done here, return successful
 exit 0
fi
exit 1

EDITED: using guidelines below

#!/bin/bash
# /home/pi/scripts/usb-initloader.sh
#
# make sure to chmod 0755 on these scripts
#
# script that runs when usb device is inserted
# checks to see if file is on USB drive and copies it over
# eventually run a python script that loads the config file
#
# ** DEVICE ADDED **
# should be called from a udev rule like:
# passes kernel device and filesystem type
# ACTION=="add", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh ADD %k %E{key}"
# Mounts usb device on /media/<dev>
# Logs changes to /var/log/syslog
# use tail /var/log/syslog to look at latest events in log
#
# ** DEVICE REMOVED **
# or remove - where we only need the kernel device
# should be called from a udev rule like:
# ACTION=="remove", KERNEL=="sd*[0-9]", SUBSYSTEMS=="usb", RUN+="/home/pi/scripts/usb-initloader.sh %k"
#
#
# CONFIGURATION
#
SCRIPT_DIR=/home/pi/scripts
#
LOG_DIR=/home/pi/logs
LOG_FILE="${LOG_DIR}/usb-automount.log"
MOUNT_DIR=/media
# Call new script and leave this one (with trailing "&")
if [ "${1}" == "ADD" ]; then
 DEVICE="${2}" # USB device name (formed with kernel passed from rule)
 DEVTYPE="${3}" # USB device formatting type
 ${SCRIPT_DIR}/usb-automount.sh "${LOG_FILE}" "${MOUNT_DIR}" "${DEVICE}" "${DEVTYPE}" &
else
 DEVICE="${1}" # USB device name (formed with kernel passed from rule)
 ${SCRIPT_DIR}/usb-unloader.sh "${LOG_FILE}" "${MOUNT_DIR}" "${DEVICE}" &
fi

EDITED: using guidelines below

#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
# Called from /home/pi/scripts/usb-initloader.sh
# make sure to chmod 0755 on file
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
# CONFIGURATION
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
# check for defined log file
if [ -z "${LOG_FILE}" ]; then
 exit 1
fi
autounload >> "${LOG_FILE}" 2>&1
# Functions:
# -----------

# Is device mounted (listed in current devices)?
is_mounted() {
 grep -q "${1}" /etc/mtab
}
# autounload function to unmount USB device and remove mount folder
autounload() {
 #check input parameters
 if [ -z "${MOUNT_DIR}" ]; then
 exit 1
fi
if [ -z "${DEVICE}" ]; then
 exit 1
fi

dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB Auto UnLoader --- $dt"
sudo umount "/dev/$DEVICE" sudo rmdir "$MOUNT_DIR/$DEVICE" 
  # test that this device isn't already mounted
 if ! is_mounted "${DEVICE}"; then
 echo "/dev/${DEVICE} successfully UnMounted"  exit 1
fi 
}

EDITED using guidelines below

#!/bin/bash
#
# Script: /home/pi/scripts/usb-automount.sh
# make sure to chmod 0755 on this script
# In case process of mounting takes too long for udev
# we call this script from /home/pi/scripts/usb-initloader.sh
#
# USAGE: usb-automount.sh DEVICE FILESYSTEM
# DEVICE is the actual device node at /dev/DEVICE (returned by udev rules %k parameter)
# FILESYSTEM is the FileSystem type returned by rules (returned by udev rules %E{ID_FS_TYPE} or $env{ID_FS_TYPE}
#
# This script takes a device name, file system type, creates /media/DEVICE and mounts the partition. 
#
# Adapted for Raspberry Pi - Raspbian O/S
# from previous code found at:
# http://superuser.com/questions/53978/automatically-mount-external-drives-to-media-label-on-boot-without-a-user-logge
# and mount manager example at
# http://solvedforhome.com/?p=2806&v=3a52f3c22ed6
# 
# Edited with many suggestions from @janos (http://codereview.stackexchange.com/users/12390/janos)
#
# Also important to these files: http://www.shellcheck.net/
LOG_FILE="1ドル"
MOUNT_DIR="2ドル"
DEVICE="3ドル" # USB device name (from kernel parameter passed from rule)
FILESYSTEM="4ドル"
# check for defined log file
if [ -z "${LOG_FILE}" ]; then
 exit 1
fi
automount >> "${LOG_FILE}" 2>&1
# Functions:
# -----------
# Is device mounted (listed in current devices)?
is_mounted() {
 grep -q "${1}" /etc/mtab
}
# automount function to test/make/mount USB device 
automount() {
 #check input parameters
 if [ -z "${MOUNT_DIR}" ]; then
 exit 1
fi
if [ -z "${DEVICE}" ]; then
 exit 1
fi
if [ -z "${FILESYSTEM}" ]; then
 exit 1
fi
# Allow time for device to be added
sleep 1
dt=$(date '+%d/%m/%Y %H:%M:%S')
echo "--- USB AutoLoader --- $dt"
# test that this device isn't already mounted
  if is_mounted "${DEVICE}"; then
 echo "Error: seems /dev/${DEVICE} is already mounted" exit 1
fi
# test mountpoint - it shouldn't exist
if [ ! -e "${MOUNT_DIR}/${DEVICE}" ]; then
 echo "Error: seems ${MOUNT_DIR}/${DEVICE} already exists"
 exit 1
 fi

 # make the mountpoint
 sudo mkdir "${MOUNT_DIR}/${DEVICE}"
 # make sure the Pi user owns this folder
 sudo chown -R pi:pi "${MOUNT_DIR}/${DEVICE}"
 # mount the device base on USB file system
 case ${FILESYSTEM} in
 vfat) sudo mount -t vfat -o utf8,uid=pi,gid=pi "/dev/${DEVICE}" "${MOUNT_DIR}/${DEVICE}"
 ;;
 # use locale setting for ntfs
 ntfs) sudo mount -t auto -o uid=pi,gid=pi,locale=en_US.UTF-8 "/dev/${DEVICE}" "${MOUNT_DIR}/${DEVICE}"
 ;;
 # ext2/3/4 do not like uid option
 ext*) sudo mount -t auto -o sync,noatime "/dev/${DEVICE}" "${MOUNT_DIR}/${DEVICE}"
 ;;
 esac
 # Allow time for device to be mounted
 sleep 2
 if is_mounted "${DEVICE}"; then
 echo "SUCCESS: /dev/${DEVICE} successfully mounted"
 fi
} 
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
Source Link
dbmitch
  • 295
  • 3
  • 12
Loading
lang-bash

AltStyle によって変換されたページ (->オリジナル) /