-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmount_usb_memory.sh
executable file
·91 lines (75 loc) · 2.51 KB
/
mount_usb_memory.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
ACTION=$1
DEVBASE=$2
DEVICE="/dev/${DEVBASE}"
MOUNT_INFO="/proc/mounts"
MOUNT_PARENT_PATH="/mnt/auto"
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
ScriptName="$(basename "$0")"
MOUNT_CMD="/usr/bin/mount"
UMOUNT_CMD="/usr/bin/umount"
GREPO_CMD="/usr/bin/grep"
LSBLK_CMD="/usr/bin/lsblk"
FIND_CMD="/usr/bin/find"
MKDIR_CMD="/usr/bin/mkdir"
LOGGER_CMD="/usr/bin/logger"
RMDIR_CMD="/usr/bin/rmdir"
# See if this drive is already mounted
MOUNT_POINT=$(findmnt -n -o TARGET -S ${DEVICE})
do_mount() {
if [[ -n ${MOUNT_POINT} ]]; then
# Already mounted, exit
${LOGGER_CMD} "${ScriptName}: ${DEVICE} is already mounted at ${MOUNT_POINT}. Done"
exit 1
fi
LABEL=$(${LSBLK_CMD} -no LABEL "${DEVICE}")
FS_TYPE=$(${LSBLK_CMD} -no FSTYPE "${DEVICE}")
if [[ -z "${LABEL}" ]]; then
LABEL=${DEVBASE}
elif ${GREPO_CMD} -q " ${MOUNT_PARENT_PATH}/${LABEL} " ${MOUNT_INFO}; then
# Already in use, make a unique one
LABEL+="-${DEVBASE}"
fi
MOUNT_POINT="${MOUNT_PARENT_PATH}/${LABEL}"
${LOGGER_CMD} "${ScriptName}: Mounting ${DEVICE} at ${MOUNT_POINT} ..."
if ! ${MKDIR_CMD} -p "${MOUNT_POINT}"; then
${LOGGER_CMD} "${ScriptName}: Failed to create mount point ${MOUNT_POINT}. Exiting."
exit 1
fi
# Global mount options
OPTS="rw,relatime"
# File system type specific mount options
if [[ ${FS_TYPE} == "vfat" ]]; then
OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"
fi
if ! ${MOUNT_CMD} -o ${OPTS} ${DEVICE} ${MOUNT_POINT}; then
# Error during mount process: cleanup mountpoint
${LOGGER_CMD} "${ScriptName}: Error during mount process. Cleanup & exit"
${RMDIR_CMD} "${MOUNT_POINT}"
exit 1
fi
${LOGGER_CMD} "${ScriptName}: ${DEVICE} - Mount done! Exit"
}
do_unmount() {
if [[ -n ${MOUNT_POINT} ]]; then
${UMOUNT_CMD} -l ${DEVICE}
fi
# Delete all empty dirs in MOUNT_PARENT_PATH that aren't being used as mount points
for f in ${MOUNT_PARENT_PATH}/* ; do
if [[ -n $(${FIND_CMD} "$f" -maxdepth 0 -type d -empty) ]]; then
if ! ${GREPO_CMD} -q " $f " ${MOUNT_INFO}; then
${RMDIR_CMD} "$f"
fi
fi
done
}
case "${ACTION}" in
add)
${LOGGER_CMD} "${ScriptName}: ${DEVICE} - Action is 'add'"
do_mount
;;
remove)
${LOGGER_CMD} "${ScriptName}: ${DEVICE} - Action is 'remove (umount)'"
do_unmount
;;
esac