forked from Ferk/udev-media-automount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia-automount
executable file
·166 lines (138 loc) · 4.15 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
#$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
declare -A devalias
[ $(id -u) != 0 ] && {
echo "This tool requires root permissions"
exit 1
}
log() {
echo "$*" | systemd-cat -p info -t "media-automount"
}
debuglog() {
echo "$*" | systemd-cat -p debug -t "media-automount"
}
errorlog() {
echo "$*" | systemd-cat -p err -t "media-automount"
}
acquire_lock() {
exec 3>/var/run/media-automount.lock
flock -x 3
}
release_lock() {
exec 3>&-
}
if ! [ "$1" ]
then
errorlog "missing arguments! a device name must be provided"
exit 1
else
dev=/dev/${1##*/}
fi
rootdev=$(findmnt -n -o SOURCE /)
if [ "$dev" == "$rootdev" ]
then
log "$dev is used as rootfs, automount won't manage it"
exit 0
fi
acquire_lock
# 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
# remove empty directories
find "$mediadir"/* -maxdepth 0 -empty -exec rm -rf {} \;
release_lock; 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"
release_lock; exit 0
fi
# Check /etc/fstab for an entry corresponding to the device
[ "$UUID" ] && fstab=$(grep /etc/fstab -e "^[^#]*UUID=${UUID}") || \
[ "$LABEL" ] && fstab=$(grep /etc/fstab -e "^[^#]*LABEL=${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}"
release_lock; exit 0
fi
# Load board-specific device aliases
if [ -e "$confdir/devalias" ]
then
. "$confdir/devalias"
fi
DEVBASE=$(lsblk -no pkname $dev)
ROOTBASE=$(lsblk -no pkname $rootdev)
# If partition is on the same device as the root partition and has label "boot" then mount it at /boot
if [ "$DEVBASE" == "$ROOTBASE" ] && [ "${LABEL,,}" == "boot" ] && ! mountpoint -q "/boot"; then
AUTOMOUNT_DIR="/boot"
else
DEVALIAS=${devalias["$DEVBASE"]}
AUTOMOUNT_DIR="${mediadir}/${DEVALIAS:-$DEVBASE}/${LABEL:-${dev##*/}}"
fi
# 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"
release_lock; exit 0
else
errorlog "Mount error: $?"
errorlog "Command was : mount -t $AUTOMOUNT_TYPE -o $AUTOMOUNT_OPTS $dev $AUTOMOUNT_DIR"
rmdir "$AUTOMOUNT_DIR"
release_lock; exit 1
fi