forked from Ferk/udev-media-automount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media-automount
executable file
·123 lines (105 loc) · 3.21 KB
/
media-automount
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/sh
#$1 = <dev>
# Default options to use for mounting
AUTOMOUNT_OPTS='users'
# Default type to use for mounting
AUTOMOUNT_TYPE='auto'
# Directory to look for type-specific settings
confdir=/etc/media-automount.d
# Directory to use as parent media dir for mountpoints
mediadir=/media
[ $(id -u) != 0 ] && {
echo "This tool requires root permissions"
exit 1
}
log() {
echo "$*" | systemd-cat -p ${loglevel:-info} -t "media-automount"
}
alias debuglog="loglevel=debug log"
alias errorlog="loglevel=err log"
if ! [ "$1" ]
then
errorlog "missing arguments! a device name must be provided"
exit 1
else
dev=/dev/${1##*/}
fi
# Check if the device exists, if not but mounted, umount it
if ! [ -b $dev ]
then
if grep /etc/mtab -qe "^$dev"
then
log "$dev device removed, umounting and cleaning /media"
if umount "$dev"
then
exitcode=0
else
exitcode=$?
errorlog "Error umounting $dev errcode:$exitcode"
errorlog "Command was: umount $dev"
fi
else
# prevent it from failing on nonexistent devices and degrading systemctl boot
exitcode=0
errorlog "device doesn't exist anymore or is not a block device: $dev"
fi
# cleanup
for dir in "$mediadir"/*
do
# Only clean non active mountpoints that have no /etc/fstab entry
if [ -d "$dir" ] && ! mountpoint -q "$dir" && ! grep -q "^\s*[^#\s]\+\s\+${dir}" /etc/fstab; then
rmdir "$dir"
fi
done
exit $exitcode
fi
# Load additional info for the block device
eval $(blkid -po export $dev)
# Devices with unknown type will be ignored
if [ -z "$TYPE" ]
then
debuglog "$dev has no known filesystem type, ignoring mount request"
exit 0
fi
# Check /etc/fstab for an entry corresponding to the device
[ "$UUID" ] && fstab=$(grep /etc/fstab -e "^[^#]*${UUID}") || \
[ "$LABEL" ] && fstab=$(grep /etc/fstab -e "^[^#]*${LABEL}") || \
fstab=$(grep /etc/fstab -e "^[ \t]*$dev[ \t]")
# Don't manage devices that are already in fstab
if [ "$fstab" ]
then
debuglog "$dev already in /etc/fstab, automount won't manage it: ${fstab#\t}"
exit 0
fi
# directory name
AUTOMOUNT_DIR="${mediadir}/${LABEL:-${dev##*/}}.$TYPE"
# Avoid conflicts when multiple devices have the same label
if [ -e "$AUTOMOUNT_DIR" ] && mountpoint -q "$AUTOMOUNT_DIR"
then
dups=$(find "${AUTOMOUNT_DIR}*" -maxdepth 0 -printf '.' | wc -c)
AUTOMOUNT_DIR="${AUTOMOUNT_DIR}_$((dups+1))"
fi
# Load Filesystem-specific configuration for mounting
if [ -e "$confdir/$TYPE" ]
then
debuglog "loading configuration for fs type $TYPE"
. "$confdir/$TYPE"
elif [ -e "$confdir/auto" ]
then
. "$confdir/auto"
fi
log "mounting device $dev in $AUTOMOUNT_DIR"
mkdir -p "$AUTOMOUNT_DIR"
if mount -t "$AUTOMOUNT_TYPE" -o "$AUTOMOUNT_OPTS" "$dev" "$AUTOMOUNT_DIR"
then
# Notify
username="$(ps au | awk '$11 ~ /^xinit/ { print $1; exit }')"
[ "$username" ] && DISPLAY=:0 runuser -u "$username" xdg-open "$AUTOMOUNT_DIR"
log "Device successfully mounted: $AUTOMOUNT_DIR"
exit 0
else
errorlog "Mount error: $?"
errorlog "Command was : mount -t $AUTOMOUNT_TYPE -o $AUTOMOUNT_OPTS $dev $AUTOMOUNT_DIR"
rmdir "$AUTOMOUNT_DIR"
exit 1
fi