You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#!/bin/bash# This script is called from our systemd unit file to mount or unmount# a USB drive.usage()
{
echo"Usage: $0 {add|remove} device_name (e.g. sdb1)"exit 1
}
if [[ $#-ne 2 ]];then
usage
fi
ACTION=$1
DEVBASE=$2
DEVICE="/dev/${DEVBASE}"# See if this drive is already mounted, and if so where
MOUNT_POINT=$(/bin/mount | /bin/grep ${DEVICE}| /usr/bin/awk '{ print $3 }')do_mount()
{
if [[ -n${MOUNT_POINT} ]];thenecho"Warning: ${DEVICE} is already mounted at ${MOUNT_POINT}"exit 1
fi# Get info for this drive: $ID_FS_LABEL, $ID_FS_UUID, and $ID_FS_TYPEeval$(/sbin/blkid -o udev ${DEVICE})# Figure out a mount point to use
LABEL=${ID_FS_LABEL}if [[ -z"${LABEL}" ]];then
LABEL=${DEVBASE}elif /bin/grep -q " /media/${LABEL}" /etc/mtab;then# Already in use, make a unique one
LABEL+="-${DEVBASE}"fi
MOUNT_POINT="/media/${LABEL}"echo"Mount point: ${MOUNT_POINT}"
/bin/mkdir -p ${MOUNT_POINT}# Global mount options
OPTS="rw,relatime"# File system type specific mount optionsif [[ ${ID_FS_TYPE}=="vfat" ]];then
OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"fiif! /bin/mount -o ${OPTS}${DEVICE}${MOUNT_POINT};thenecho"Error mounting ${DEVICE} (status = $?)"
/bin/rmdir ${MOUNT_POINT}exit 1
fiecho"**** Mounted ${DEVICE} at ${MOUNT_POINT} ****"
}
do_unmount()
{
if [[ -z${MOUNT_POINT} ]];thenecho"Warning: ${DEVICE} is not mounted"else
/bin/umount -l ${DEVICE}echo"**** Unmounted ${DEVICE}"fi# Delete all empty dirs in /media that aren't being used as mount# points. This is kind of overkill, but if the drive was unmounted# prior to removal we no longer know its mount point, and we don't# want to leave it orphaned...forfin /media/*;doif [[ -n$(/usr/bin/find "$f" -maxdepth 0 -type d -empty) ]];thenif! /bin/grep -q "$f" /etc/mtab;thenecho"**** Removing mount point $f"
/bin/rmdir "$f"fifidone
}
case"${ACTION}"in
add)
do_mount
;;
remove)
do_unmount
;;
*)
usage
;;
esac