1
0
Fork 0
mirror of https://github.com/iiab/iiab.git synced 2025-02-12 11:12:06 +00:00

Merge pull request #3929 from holta/usbmount-refine

usb_lib and usbmount refinements built on PR #3916
This commit is contained in:
A Holt 2025-01-31 03:38:52 -05:00 committed by GitHub
commit 073939a542
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 359 additions and 52 deletions

View file

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View file

@ -0,0 +1,17 @@
Format: http://dep.debian.net/deps/dep5/
Upstream-Name: usbmount
Upstream-Contact: Martin Dickopp <martin@zero-based.org>, Rogério Brito <rbrito@ime.usp.br>
Source: git://git.debian.org/usbmount/usbmount.git
Files: *
Copyright: 2004-2007, Martin Dickopp <martin@zero-based.org>
2008-2011, Rogério Brito <rbrito@ime.usp.br>
License: BSD-2
This package is free software; the copyright holder gives unlimited
permission to copy and/or distribute it, with or without
modifications, as long as this notice is preserved.
.
This package is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY, to the extent permitted by law; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

View file

@ -0,0 +1,212 @@
#!/bin/sh
# This script mounts USB mass storage devices when they are plugged in
# and unmounts them when they are removed.
# Copyright © 2004, 2005 Martin Dickopp
# Copyright © 2008, 2009, 2010 Rogério Theodoro de Brito
# Copyright © 2025, Jerry Vonau
#
# This file is free software; the copyright holder gives unlimited
# permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
# Add web link to copyright notice help in iiab on github
set -e
exec > /dev/null 2>&1
######################################################################
# Auxiliary functions
# Log a string via the syslog facility.
log()
{
if [ $1 != debug ] || expr "$VERBOSE" : "[yY]" > /dev/null; then
logger -p user.$1 -t "usbmount[$$]" -- "$2"
fi
}
# Test if the first parameter is in the list given by the second
# parameter.
in_list()
{
for v in $2; do
[ "$1" != "$v" ] || return 0
done
return 1
}
######################################################################
# Main program
# Default values for configuration variables.
ENABLED=1
MOUNTPOINTS=
FILESYSTEMS=
MOUNTOPTIONS=
FS_MOUNTOPTIONS=
VERBOSE=no
if [ -r /etc/usbmount/usbmount.conf ]; then
. /etc/usbmount/usbmount.conf
log debug "loaded usbmount configurations"
fi
if [ "${ENABLED:-1}" -eq 0 ]; then
log info "usbmount is disabled, see /etc/usbmount/usbmount.conf"
exit 0
fi
if [ ! -x /sbin/blkid ]; then
log err "cannot execute /sbin/blkid"
exit 1
fi
if [ "$1" = add ]; then
# Per Policy 9.3.2, directories under /var/run have to be created
# after every reboot.
if [ ! -e /var/run/usbmount ]; then
mkdir -p /var/run/usbmount
log debug "creating /var/run/usbmount directory"
else
log debug "/var/run/usbmount exists"
fi
umask 022
# Acquire lock.
log debug "trying to acquire lock /var/run/usbmount/.mount.lock"
lockfile-create --retry 6 /var/run/usbmount/.mount || \
{ log err "cannot acquire lock /var/run/usbmount/.mount.lock"; exit 1; }
trap '( lockfile-remove /var/run/usbmount/.mount )' 0
log debug "acquired lock /var/run/usbmount/.mount.lock"
# Grab device information from device and "divide it"
# FIXME: improvement: implement mounting by label (notice that labels
# can contain spaces, which makes things a little bit less comfortable).
DEVINFO=$(/sbin/blkid -p $DEVNAME)
FSTYPE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]TYPE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
UUID=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]UUID="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
USAGE=$(echo "$DEVINFO" | sed 's/.*[[:blank:]]USAGE="\([^"]*\)".*/\1/g; s/[[:blank:]]*//g;')
if ! echo $USAGE | egrep -q "(filesystem|disklabel)"; then
log info "$DEVNAME does not contain a filesystem or disklabel"
exit
fi
# Try to use specifications in /etc/fstab first.
if egrep -q "^[[:blank:]]*$DEVNAME" /etc/fstab; then
log info "executing command: mount $DEVNAME"
mount $DEVNAME || log err "mount by DEVNAME with $DEVNAME wasn't successful; return code $?"
elif grep -q "^[[:blank:]]*UUID=$UUID" /etc/fstab; then
log info "executing command: mount -U $UUID"
mount -U $UUID || log err "mount by UUID with $UUID wasn't successful; return code $?"
else
log debug "$DEVNAME contains filesystem type $FSTYPE"
fstype=$FSTYPE
# Test if the filesystem type is in the list of filesystem
# types to mount.
if in_list "$fstype" "$FILESYSTEMS"; then
# Search an available mountpoint.
for v in $MOUNTPOINTS; do
if [ -d "$v" ] && ! grep -q "^[^ ][^ ]* *$v " /proc/mounts; then
mountpoint="$v"
log debug "mountpoint $mountpoint is available for $DEVNAME"
break
fi
done
if [ -n "$mountpoint" ]; then
# Determine mount options.
options=
for v in $FS_MOUNTOPTIONS; do
if expr "$v" : "-fstype=$fstype,."; then
options="$(echo "$v" | sed 's/^[^,]*,//')"
break
fi
done
if [ -n "$MOUNTOPTIONS" ]; then
options="$MOUNTOPTIONS${options:+,$options}"
fi
# Mount the filesystem.
log info "executing command: mount -t$fstype ${options:+-o$options} $DEVNAME $mountpoint"
mount "-t$fstype" "${options:+-o$options}" "$DEVNAME" "$mountpoint"
# Determine vendor and model.
vendor=
if [ -r "/sys$DEVPATH/device/vendor" ]; then
vendor="`cat \"/sys$DEVPATH/device/vendor\"`"
elif [ -r "/sys$DEVPATH/../device/vendor" ]; then
vendor="`cat \"/sys$DEVPATH/../device/vendor\"`"
elif [ -r "/sys$DEVPATH/device/../manufacturer" ]; then
vendor="`cat \"/sys$DEVPATH/device/../manufacturer\"`"
elif [ -r "/sys$DEVPATH/../device/../manufacturer" ]; then
vendor="`cat \"/sys$DEVPATH/../device/../manufacturer\"`"
fi
vendor="$(echo "$vendor" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
model=
if [ -r "/sys$DEVPATH/device/model" ]; then
model="`cat \"/sys$DEVPATH/device/model\"`"
elif [ -r "/sys$DEVPATH/../device/model" ]; then
model="`cat \"/sys$DEVPATH/../device/model\"`"
elif [ -r "/sys$DEVPATH/device/../product" ]; then
model="`cat \"/sys$DEVPATH/device/../product\"`"
elif [ -r "/sys$DEVPATH/../device/../product" ]; then
model="`cat \"/sys$DEVPATH/../device/../product\"`"
fi
model="$(echo "$model" | sed 's/^[[:blank:]]\+//; s/[[:blank:]]\+$//')"
# Run hook scripts; ignore errors.
export UM_DEVICE="$DEVNAME"
export UM_MOUNTPOINT="$mountpoint"
export UM_FILESYSTEM="$fstype"
export UM_MOUNTOPTIONS="$options"
export UM_VENDOR="$vendor"
export UM_MODEL="$model"
log info "executing command: run-parts /etc/usbmount/mount.d"
run-parts /etc/usbmount/mount.d || :
else
# No suitable mount point found.
log warning "no mountpoint found for $DEVNAME"
exit 1
fi
fi
fi
elif [ "$1" = remove ]; then
# A block or partition device has been removed.
# Test if it is mounted.
while read device mountpoint fstype remainder; do
if [ "$DEVNAME" = "$device" ]; then
# If the mountpoint and filesystem type are maintained by
# this script, unmount the filesystem.
if in_list "$mountpoint" "$MOUNTPOINTS" &&
in_list "$fstype" "$FILESYSTEMS"; then
log info "executing command: umount -l $mountpoint"
umount -l "$mountpoint"
# Run hook scripts; ignore errors.
export UM_DEVICE="$DEVNAME"
export UM_MOUNTPOINT="$mountpoint"
export UM_FILESYSTEM="$fstype"
log info "executing command: run-parts /etc/usbmount/umount.d"
run-parts /etc/usbmount/umount.d || :
fi
break
fi
done < /proc/mounts
else
log err "unexpected: action '$1'"
exit 1
fi
log debug "usbmount execution finished"

View file

@ -0,0 +1,53 @@
# Configuration file for the usbmount package, which mounts removable
# storage devices when they are plugged in and unmounts them when they
# are removed.
# Change to zero to disable usbmount
ENABLED=1
# Mountpoints: These directories are eligible as mointpoints for
# removable storage devices. A newly plugged in device is mounted on
# the first directory in this list that exists and on which nothing is
# mounted yet.
MOUNTPOINTS="/media/usb0 /media/usb1 /media/usb2 /media/usb3
/media/usb4 /media/usb5 /media/usb6 /media/usb7"
# Filesystem types: removable storage devices are only mounted if they
# contain a filesystem type which is in this list.
FILESYSTEMS="vfat ext2 ext3 ext4 hfsplus exfat fuseblk ntfs"
#############################################################################
# WARNING! #
# #
# The "sync" option may not be a good choice to use with flash drives, as #
# it forces a greater amount of writing operating on the drive. This makes #
# the writing speed considerably lower and also leads to a faster wear out #
# of the disk. #
# #
# If you omit it, don't forget to use the command "sync" to synchronize the #
# data on your disk before removing the drive or you may experience data #
# loss. #
# #
# It is highly recommended that you use the pumount command (as a regular #
# user) before unplugging the device. It makes calling the "sync" command #
# and mounting with the sync option unnecessary---this is similar to other #
# operating system's "safely disconnect the device" option. #
#############################################################################
# Mount options: Options passed to the mount command with the -o flag.
# See the warning above regarding removing "sync" from the options.
MOUNTOPTIONS="sync,noexec,nodev,noatime,nodiratime"
# Filesystem type specific mount options: This variable contains a space
# separated list of strings, each which the form "-fstype=TYPE,OPTIONS".
#
# If a filesystem with a type listed here is mounted, the corresponding
# options are appended to those specificed in the MOUNTOPTIONS variable.
#
# For example, "-fstype=vfat,gid=floppy,dmask=0007,fmask=0117" would add
# the options "gid=floppy,dmask=0007,fmask=0117" when a vfat filesystem
# is mounted.
FS_MOUNTOPTIONS=""
# If set to "yes", more information will be logged via the syslog
# facility.
VERBOSE=yes

View file

@ -47,27 +47,53 @@
when: udev_unit.stat.exists is defined and udev_unit.stat.exists
# http://raspbian.raspberrypi.org/raspbian/pool/main/u/usbmount/usbmount_0.0.22_all.deb
- name: Install {{ iiab_download_url }}/usbmount_0.0.22_all.deb, no longer supported by {RasPiOS, Debian, Ubuntu}
apt:
deb: "{{ iiab_download_url }}/usbmount_0.0.22_all.deb"
# when: is_debian
#- name: Install {{ iiab_download_url }}/usbmount_0.0.22_all.deb, no longer supported by {RasPiOS, Debian, Ubuntu}
# apt:
# deb: "{{ iiab_download_url }}/usbmount_0.0.22_all.deb"
# # when: is_debian
# check status of usbmount on mintlinux - should be ok Ubuntu variant
# - name: Install usbmount from OS repo for Ubuntu variants
# package:
# name: usbmount
# state: present
# when: is_ubuntu
- name: Install lockfile-progs and util-linux for usbmount from OS repo
package:
name:
- lockfile-progs
- util-linux
state: present
- name: Add dir {{ doc_root }}/local_content, where USB drive links can appear (0775)
- name: Add dir {{ doc_root }}/local_content, where USB drive links can appear (0775) owned by {{ apache_user }}:{{ apache_user }}
file:
state: directory
path: "{{ doc_root }}/local_content"
owner: "{{ apache_user }}"
path: "{{ doc_root }}/local_content" # /library/www/html
owner: "{{ apache_user }}" # www-data
group: "{{ apache_user }}" # 2020-02-13: changed from iiab_admin_user, after discussion on weekly call (#1228, #2222)
mode: 0775
- name: 'Install from template: /etc/udev/rules.d/usbmount.rules, /etc/systemd/system/usbmount@.service, /usr/sbin/iiab-clean-usb.sh'
- name: Set up dirs /etc/usbmount/mount.d, /etc/usbmount/umount.d, /media/usb0-7
file:
state: directory
path: "{{ item }}"
mode: 0755
with_items:
- /etc/usbmount/mount.d
- /etc/usbmount/umount.d
- /media/usb0
- /media/usb1
- /media/usb2
- /media/usb3
- /media/usb4
- /media/usb5
- /media/usb6
- /media/usb7
- name: Copy files from files/usbmount to filesystem
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- { src: 'usbmount/usbmount.conf', dest: '/etc/usbmount/', mode: '0644' }
- { src: 'usbmount/usbmount', dest: '/usr/local/sbin/', mode: '0755' }
- name: 'Install from template: /etc/udev/rules.d/usbmount.rules, /etc/systemd/system/usbmount@.service, /usr/local/sbin/iiab-clean-usb.sh'
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
@ -75,36 +101,36 @@
with_items:
- { src: 'usbmount.rules.j2', dest: '/etc/udev/rules.d/usbmount.rules', mode: '0644' }
- { src: 'usbmount@.service.j2', dest: '/etc/systemd/system/usbmount@.service', mode: '0644' }
- { src: 'iiab-clean-usb.sh', dest: '/usr/sbin/', mode: '0755' }
- { src: 'iiab-clean-usb.sh', dest: '/usr/local/sbin/', mode: '0755' }
- name: '2025-01-05: Add upload2usb app (#3875) directory to doc_root'
- name: Add dir {{ doc_root }}/upload2usb (0775) owned by {{ apache_user }}:{{ apache_user }}
file:
state: directory
path: "{{ doc_root }}/upload2usb"
owner: "{{ apache_user }}"
group: "{{ apache_user }}"
group: "{{ apache_user }}"
mode: 0755
- name: '2025-01-05: Copy upload2usb app (#3875) files from files/upload/ into {{ doc_root }}/upload2usb/'
- name: Copy files from files/upload2usb/ into {{ doc_root }}/upload2usb/
copy:
src: "{{ item }}"
dest: "{{ doc_root }}/upload2usb/" # /library/www/html
dest: "{{ doc_root }}/upload2usb/"
with_fileglob:
- upload/*
- upload2usb/*
# 2021-03-21: If usbmount is repackaged by apt as a result of Linux kernel 5.4+
# supporting exFAT, the stanza below (might) in future no longer be needed...
# SEE ALSO: https://github.com/iiab/iiab/blob/586bfc5cb1abf6b4333a21d3fa89695f115432dc/roles/2-common/tasks/packages.yml#L11-L12
- name: Add ' exfat fuseblk ntfs' to FILESYSTEMS var in /etc/usbmount/usbmount.conf
lineinfile:
regexp: '^FILESYSTEMS=.*'
line: 'FILESYSTEMS="vfat ext2 ext3 ext4 hfsplus exfat fuseblk ntfs"'
path: /etc/usbmount/usbmount.conf
#- name: Add ' exfat fuseblk ntfs' to FILESYSTEMS var in /etc/usbmount/usbmount.conf
# lineinfile:
# regexp: '^FILESYSTEMS=.*'
# line: 'FILESYSTEMS="vfat ext2 ext3 ext4 hfsplus exfat fuseblk ntfs"'
# path: /etc/usbmount/usbmount.conf
- name: Remove /etc/usbmount/mount.d/00_create_model_symlink
file:
path: /etc/usbmount/mount.d/00_create_model_symlink
state: absent
#- name: Remove /etc/usbmount/mount.d/00_create_model_symlink
# file:
# path: /etc/usbmount/mount.d/00_create_model_symlink
# state: absent
# RECORD 'USB_LIB' AS INSTALLED

View file

@ -1,14 +1,14 @@
#!/bin/bash
# Remove symlink in /library/content to automounted usb drive
#
DEVICE=$(echo $@ | sed -s 's|-|/|')
MNT_POINT=$(findmnt -n /$DEVICE | awk '{print $1}')
# Remove symlink in /library/www/html/local_content to automounted USB drive
DEVICE="/$(echo $1 | sed 's|-|/|')"
MNT_POINT=$(findmnt -no target $DEVICE)
CONTENT_LINK_USB=$(basename $MNT_POINT | awk '{print toupper($0)}')
CONTENT_LINK="/library/www/html/local_content/$CONTENT_LINK_USB"
logger -p user.notice -t "usb_lib (iiab-clean-usb.sh)" -- "Attempting to remove link $CONTENT_LINK."
logger -t "usb_lib (iiab-clean-usb.sh)" "Attempting to remove symlink $CONTENT_LINK, as auto-created earlier by usbmount."
if [ -L $CONTENT_LINK ]; then
/bin/rm $CONTENT_LINK
logger -p user.notice -t "usb_lib (iiab-clean-usb.sh)" -- "$CONTENT_LINK removed."
/usr/bin/rm $CONTENT_LINK
logger -t "usb_lib (iiab-clean-usb.sh)" "Symlink $CONTENT_LINK removed, as auto-created earlier by usbmount."
fi

View file

@ -30,16 +30,16 @@ logger -t "usb_lib (70-usb-library)" "BOOT_DEV is: $BOOT_DEV"
logger -t "usb_lib (70-usb-library)" "BOOTFW_DEV is: $BOOTFW_DEV"
if [ "$UM_DEV" == "$LIB_DEV" ]; then
logger -p user.notice -t "usb_lib (70-usb-library)" -- "Skipping $UM_MOUNTPOINT containing /library"
logger -t "usb_lib (70-usb-library)" "Skipping $UM_MOUNTPOINT containing /library"
exit
elif [ "$UM_DEV" == "$ROOT_DEV" ]; then
logger -p user.notice -t "usb_lib (70-usb-library)" -- "Skipping $UM_MOUNTPOINT containing rootfs"
logger -t "usb_lib (70-usb-library)" "Skipping $UM_MOUNTPOINT containing rootfs"
exit
elif [ "$UM_DEV" == "$BOOT_DEV" ]; then
logger -p user.notice -t "usb_lib (70-usb-library)" -- "Skipping $UM_MOUNTPOINT containing /boot"
logger -t "usb_lib (70-usb-library)" "Skipping $UM_MOUNTPOINT containing /boot"
exit
elif [ "$UM_DEV" == "$BOOTFW_DEV" ]; then
logger -p user.notice -t "usb_lib (70-usb-library)" -- "Skipping $UM_MOUNTPOINT containing /boot/firmware"
logger -t "usb_lib (70-usb-library)" "Skipping $UM_MOUNTPOINT containing /boot/firmware"
exit
fi
@ -48,13 +48,13 @@ fi
# "public artwork" — as summarized here: https://github.com/iiab/iiab/blob/master/roles/usb_lib/README.rst
if [ -d $UM_MOUNTPOINT/PUBLIC ]; then
SHARE_DIR=$UM_MOUNTPOINT/PUBLIC
logger -p user.notice -t "usb_lib (70-usb-library)" -- "Found /PUBLIC on $UM_MOUNTPOINT"
logger -t "usb_lib (70-usb-library)" "Found /PUBLIC on $UM_MOUNTPOINT"
else
SHARE_DIR=$UM_MOUNTPOINT
logger -p user.notice -t "usb_lib (70-usb-library)" -- "Did not find /PUBLIC on $UM_MOUNTPOINT"
logger -t "usb_lib (70-usb-library)" "Did not find /PUBLIC on $UM_MOUNTPOINT"
fi
CONTENT_LINK_USB=$(basename $UM_MOUNTPOINT | awk '{print toupper($0)}')
CONTENT_LINK="{{ doc_root }}/local_content/$CONTENT_LINK_USB"
logger -p user.notice -t "usb_lib (70-usb-library)" -- "Creating link from $CONTENT_LINK to $SHARE_DIR"
ln -s $SHARE_DIR $CONTENT_LINK
logger -t "usb_lib (70-usb-library)" "Creating link from $CONTENT_LINK to $SHARE_DIR"
ln -sf $SHARE_DIR $CONTENT_LINK

View file

@ -1,5 +1,3 @@
KERNEL=="sd*", DRIVERS=="sbp2", ACTION=="add", PROGRAM="/bin/systemd-escape -p --template=usbmount@.service $env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
KERNEL=="sd*", SUBSYSTEMS=="usb", ACTION=="add", PROGRAM="/bin/systemd-escape -p --template=usbmount@.service $env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
KERNEL=="ub*", SUBSYSTEMS=="usb", ACTION=="add", PROGRAM="/bin/systemd-escape -p --template=usbmount@.service $env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
KERNEL=="sd*", SUBSYSTEMS=="usb", ACTION=="remove", PROGRAM="/usr/share/usbmount/usbmount remove"
ACTION=="add", SUBSYSTEMS=="usb", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem" PROGRAM="/bin/systemd-escape -p --template=usbmount@.service $env{DEVNAME}", ENV{SYSTEMD_WANTS}+="%c"
ACTION=="remove", SUBSYSTEMS=="usb", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem" PROGRAM="/bin/systemd-escape -p /usr/share/usbmount/usbmount remove"

View file

@ -2,13 +2,14 @@
BindTo=%i.device
After=%i.device
After=systemd-udev-trigger.service
ConditionPathExists=/var/run
[Service]
#Type=oneshot
TimeoutStartSec=0
Environment=DEVNAME=%I
ExecStart=/usr/share/usbmount/usbmount add
ExecStop=/usr/sbin/iiab-clean-usb.sh %I
ExecStart=/usr/local/sbin/usbmount add
ExecStop=/usr/local/sbin/iiab-clean-usb.sh %I
ExecStopPost=/bin/umount /%I
RemainAfterExit=yes
RuntimeDirectory=usbmount