Skip to content

Instantly share code, notes, and snippets.

@Maigre
Last active May 3, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maigre/f24cf281f28f0b17f1f8e812b6568008 to your computer and use it in GitHub Desktop.
Save Maigre/f24cf281f28f0b17f1f8e812b6568008 to your computer and use it in GitHub Desktop.
USB automount
#!/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} ]]; then
echo "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_TYPE
#eval $(/sbin/blkid -o udev ${DEVICE})
## Figure out a mount point to use
#LABEL=${ID_FS_UUID}
#if /bin/grep -q " /media/${LABEL} " /etc/mtab; then
# # Already in use, make a unique one
# LABEL+="-${DEVBASE}"
#fi
#MOUNT_POINT="/media/${LABEL}"
MOUNT_POINT="/media/usb"
echo "Mount point: ${MOUNT_POINT}"
#/bin/mkdir -p ${MOUNT_POINT}
# Global mount options
OPTS="ro,relatime"
# File system type specific mount options
if [[ ${ID_FS_TYPE} == "vfat" ]]; then
OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"
fi
if ! /bin/mount -o ${OPTS} ${DEVICE} ${MOUNT_POINT}; then
echo "Error mounting ${DEVICE} (status = $?)"
#/bin/rmdir ${MOUNT_POINT}
exit 1
fi
echo "**** Mounted ${DEVICE} at ${MOUNT_POINT} ****"
# Symlink to /media/usb
#ln -sf ${MOUNT_POINT} /media/usb
}
do_unmount()
{
if [[ -z ${MOUNT_POINT} ]]; then
echo "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...
#for f in /media/* ; do
# if [[ -n $(/usr/bin/find "$f" -maxdepth 0 -type d -empty) ]]; then
# if ! /bin/grep -q " $f " /etc/mtab; then
# echo "**** Removing mount point $f"
# /bin/rmdir "$f"
# fi
# fi
#done
}
case "${ACTION}" in
add)
do_mount
;;
remove)
do_unmount
;;
*)
usage
;;
esac
##
# SYSTEMD
##
# > /etc/systemd/system/usb-automount\@.service
[Unit]
Description=Mount USB Drive on %i
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/kxkm/usb-automount.sh add %i
ExecStop=/kxkm/usb-automount.sh remove %i
##
# UDEV
##
> /etc/udev/rules.d/99-local.rules
KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/bin/systemctl start usb-automount@%k.service"
KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="remove", RUN+="/bin/systemctl stop usb-automount@%k.service"
## ORIGINAL
# https://serverfault.com/questions/766506/automount-usb-drives-with-systemd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment