Skip to content

Commit fe6516b

Browse files
authored
Add lockfile removal before exit in usbmount script per Jerry's recommendation.
Add lockfile removal before exit in usbmount script per Jerry's recommendation. Also adjusted spacing! Read up on file locking a bit: - https://www.baeldung.com/linux/file-locking - https://linux.die.net/man/1/lockfile-create
1 parent a96b46c commit fe6516b

File tree

1 file changed

+93
-92
lines changed

1 file changed

+93
-92
lines changed

roles/usb_lib/files/usbmount/usbmount

Lines changed: 93 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ if [ "$1" = add ]; then
9696
USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
9797

9898
if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then
99-
log debug "/$DEVNAME does not contain a filesystem or disklabel"
100-
exit
99+
log debug "/$DEVNAME does not contain a filesystem or disklabel"
100+
lockfile-remove /var/run/usbmount/.mount
101+
exit
101102
fi
102103

103104
log debug "/$DEVNAME contains filesystem type $FSTYPE"
@@ -109,7 +110,7 @@ if [ "$1" = add ]; then
109110
log debug "BOOT_DEV $BOOT_DEV"
110111

111112
if [ $BOOTFW_DEV = /$DEVNAME ]; then
112-
log debug "skipping BOOTFS_DEV $BOOTFS_DEV mounted at /boot/firmware"
113+
log debug "skipping BOOTFS_DEV $BOOTFS_DEV mounted at /boot/firmware"
113114
lockfile-remove /var/run/usbmount/.mount
114115
exit
115116
elif [ $ROOT_DEV = /$DEVNAME ]; then
@@ -124,107 +125,107 @@ if [ "$1" = add ]; then
124125

125126
# Try to use specifications in /etc/fstab to skip.
126127
if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then
127-
log debug "skipping /$DEVNAME exit"
128-
lockfile-remove /var/run/usbmount/.mount
129-
exit
128+
log debug "skipping /$DEVNAME exit"
129+
lockfile-remove /var/run/usbmount/.mount
130+
exit
130131
elif grep -q "^[[:blank:]]*UUID=$UUID" /etc/fstab; then
131-
log debug "skipping $UUID"
132-
lockfile-remove /var/run/usbmount/.mount
132+
log debug "skipping $UUID"
133+
lockfile-remove /var/run/usbmount/.mount
133134
exit
134135
else
135-
log debug "/$DEVNAME contains filesystem type $FSTYPE"
136-
fstype=$FSTYPE
137-
# Test if the filesystem type is in the list of filesystem
138-
# types to mount.
139-
if in_list "$fstype" "$FILESYSTEMS"; then
140-
# Search an available mountpoint.
141-
for v in $MOUNTPOINTS; do
142-
if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then
143-
mountpoint="$v"
144-
log debug "mountpoint $mountpoint is available for /$DEVNAME"
145-
break
146-
fi
147-
done
148-
if [ -n "$mountpoint" ]; then
149-
# Determine mount options.
150-
options=
151-
for v in $FS_MOUNTOPTIONS; do
152-
if expr "$v" : "-fstype=$fstype,."; then
153-
options="$(echo "$v" | sed 's/^[^,]*,//')"
154-
break
155-
fi
156-
done
157-
if [ -n "$MOUNTOPTIONS" ]; then
158-
options="$MOUNTOPTIONS${options:+,$options}"
159-
fi
160-
161-
# Mount the filesystem.
162-
log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint"
163-
mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint"
164-
165-
# Determine vendor and model.
166-
vendor=
167-
if [ -r "/sys$DEVPATH/device/vendor" ]; then
168-
vendor="`cat \"/sys$DEVPATH/device/vendor\"`"
169-
elif [ -r "/sys$DEVPATH/../device/vendor" ]; then
170-
vendor="`cat \"/sys$DEVPATH/../device/vendor\"`"
171-
elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then
172-
vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`"
173-
elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then
174-
vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`"
175-
fi
176-
vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
177-
178-
model=
179-
if [ -r "/sys$DEVPATH/device/model" ]; then
180-
model="`cat \"/sys$DEVPATH/device/model\"`"
181-
elif [ -r "/sys$DEVPATH/../device/model" ]; then
182-
model="`cat \"/sys$DEVPATH/../device/model\"`"
183-
elif [ -r "/sys$DEVPATH/device/../product" ]; then
184-
model="`cat \"/sys$DEVPATH/device/../product\"`"
185-
elif [ -r "/sys$DEVPATH/../device/../product" ]; then
186-
model="`cat \"/sys$DEVPATH/../device/../product\"`"
187-
fi
188-
model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
189-
190-
# Run hook scripts; ignore errors.
191-
export UM_DEVICE="$DEVNAME"
192-
export UM_MOUNTPOINT="$mountpoint"
193-
export UM_FILESYSTEM="$fstype"
194-
export UM_MOUNTOPTIONS="$options"
195-
export UM_VENDOR="$vendor"
196-
export UM_MODEL="$model"
197-
log info "executing command: run-parts /etc/usbmount/mount.d"
198-
run-parts /etc/usbmount/mount.d || :
199-
else
200-
# No suitable mount point found.
201-
log warning "no mountpoint found for $DEVNAME"
202-
exit 1
203-
fi
136+
log debug "/$DEVNAME contains filesystem type $FSTYPE"
137+
fstype=$FSTYPE
138+
# Test if the filesystem type is in the list of filesystem
139+
# types to mount.
140+
if in_list "$fstype" "$FILESYSTEMS"; then
141+
# Search an available mountpoint.
142+
for v in $MOUNTPOINTS; do
143+
if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then
144+
mountpoint="$v"
145+
log debug "mountpoint $mountpoint is available for /$DEVNAME"
146+
break
147+
fi
148+
done
149+
if [ -n "$mountpoint" ]; then
150+
# Determine mount options.
151+
options=
152+
for v in $FS_MOUNTOPTIONS; do
153+
if expr "$v" : "-fstype=$fstype,."; then
154+
options="$(echo "$v" | sed 's/^[^,]*,//')"
155+
break
156+
fi
157+
done
158+
if [ -n "$MOUNTOPTIONS" ]; then
159+
options="$MOUNTOPTIONS${options:+,$options}"
160+
fi
161+
162+
# Mount the filesystem.
163+
log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint"
164+
mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint"
165+
166+
# Determine vendor and model.
167+
vendor=
168+
if [ -r "/sys$DEVPATH/device/vendor" ]; then
169+
vendor="`cat \"/sys$DEVPATH/device/vendor\"`"
170+
elif [ -r "/sys$DEVPATH/../device/vendor" ]; then
171+
vendor="`cat \"/sys$DEVPATH/../device/vendor\"`"
172+
elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then
173+
vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`"
174+
elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then
175+
vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`"
176+
fi
177+
vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
178+
179+
model=
180+
if [ -r "/sys$DEVPATH/device/model" ]; then
181+
model="`cat \"/sys$DEVPATH/device/model\"`"
182+
elif [ -r "/sys$DEVPATH/../device/model" ]; then
183+
model="`cat \"/sys$DEVPATH/../device/model\"`"
184+
elif [ -r "/sys$DEVPATH/device/../product" ]; then
185+
model="`cat \"/sys$DEVPATH/device/../product\"`"
186+
elif [ -r "/sys$DEVPATH/../device/../product" ]; then
187+
model="`cat \"/sys$DEVPATH/../device/../product\"`"
188+
fi
189+
model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
190+
191+
# Run hook scripts; ignore errors.
192+
export UM_DEVICE="$DEVNAME"
193+
export UM_MOUNTPOINT="$mountpoint"
194+
export UM_FILESYSTEM="$fstype"
195+
export UM_MOUNTOPTIONS="$options"
196+
export UM_VENDOR="$vendor"
197+
export UM_MODEL="$model"
198+
log info "executing command: run-parts /etc/usbmount/mount.d"
199+
run-parts /etc/usbmount/mount.d || :
200+
else
201+
# No suitable mount point found.
202+
log warning "no mountpoint found for $DEVNAME"
203+
exit 1
204+
fi
204205
fi
205-
fi
206+
fi
206207
elif [ "$1" = remove ]; then
207208

208209
# A block or partition device has been removed.
209210
# Test if it is mounted.
210211
while read device mountpoint fstype remainder; do
211-
if [ "$DEVNAME" = "$device" ]; then
212+
if [ "$DEVNAME" = "$device" ]; then
212213
# If the mountpoint and filesystem type are maintained by
213214
# this script, unmount the filesystem.
214215
if in_list "$mountpoint" "$MOUNTPOINTS" &&
215-
in_list "$fstype" "$FILESYSTEMS"; then
216-
log info "executing command: umount -l $mountpoint"
217-
umount -l "$mountpoint"
218-
219-
# Run hook scripts; ignore errors.
220-
export UM_DEVICE="$DEVNAME"
221-
export UM_MOUNTPOINT="$mountpoint"
222-
export UM_FILESYSTEM="$fstype"
223-
log info "executing command: run-parts /etc/usbmount/umount.d"
224-
run-parts /etc/usbmount/umount.d || :
225-
fi
226-
break
227-
fi
216+
in_list "$fstype" "$FILESYSTEMS"; then
217+
log info "executing command: umount -l $mountpoint"
218+
umount -l "$mountpoint"
219+
220+
# Run hook scripts; ignore errors.
221+
export UM_DEVICE="$DEVNAME"
222+
export UM_MOUNTPOINT="$mountpoint"
223+
export UM_FILESYSTEM="$fstype"
224+
log info "executing command: run-parts /etc/usbmount/umount.d"
225+
run-parts /etc/usbmount/umount.d || :
226+
fi
227+
break
228+
fi
228229
done < /proc/mounts
229230
else
230231
log err "unexpected: action '$1'"

0 commit comments

Comments
 (0)