Skip to main content
added 170 characters in body
Source Link

save it!
ENV{ID_FS_UUID}=="3CF6-EEA5" means
check if the environment variable ID_FS_UUID of the device is equal to 3CF6-EEA5,
if so ACTION=="add" means
check why the udev event is fired, because the device is added in the moment (other states removed, change)

if conditions are true, device identified, event: added -> RUN systemd-mount
$devnode ... expands to the device path, udev sets for the device (/dev/...)

save it!
ENV{ID_FS_UUID}=="3CF6-EEA5" means
check if the environment variable ID_FS_UUID of the device is equal to 3CF6-EEA5,
if so, device identified -> RUN systemd-mount
$devnode ... expands to the device path, udev sets for the device (/dev/...)

save it!
ENV{ID_FS_UUID}=="3CF6-EEA5" means
check if the environment variable ID_FS_UUID of the device is equal to 3CF6-EEA5,
ACTION=="add" means
check why the udev event is fired, because the device is added in the moment (other states removed, change)

if conditions are true, device identified, event: added -> RUN systemd-mount
$devnode ... expands to the device path, udev sets for the device (/dev/...)

added 30 characters in body
Source Link
ENV{ID_FS_UUID}=="3CF6-EEA5", ACTION=="add", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /my/mount/directory/" 
ATTRS{idVendor}=="0781", ATTRS{idProduct}=="5406", ENV{ID_FS_UUID}=="3CF6-EEA5", ACTION=="add", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /my/mount/directory/" 
ENV{ID_FS_UUID}=="3CF6-EEA5", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /my/mount/directory/" 
ATTRS{idVendor}=="0781", ATTRS{idProduct}=="5406", ENV{ID_FS_UUID}=="3CF6-EEA5", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /my/mount/directory/" 
ENV{ID_FS_UUID}=="3CF6-EEA5", ACTION=="add", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /my/mount/directory/" 
ATTRS{idVendor}=="0781", ATTRS{idProduct}=="5406", ENV{ID_FS_UUID}=="3CF6-EEA5", ACTION=="add", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /my/mount/directory/" 
Source Link

I'm on Linux Mint, which is debian based, but this should also work with debian bookworm.
This is a job for udev service, the extented device manager from systemd.
It gets notified by the kernel, if some hardware is plugged in and you can load some extra modules or start some running actions.
But you cant call the low level mount command of the bin folder in udev running action, that would not work and lead to problems.
you need systemd-mount https://manpages.debian.org/bookworm/systemd/systemd-mount.1.en.html, which does the job outside of udev, when udev finsihed executing.

  1. check if udev daemon is running systemctl list-units *udevd.service
    and check, if you have systemd-mount ls /usr/bin/systemd-mount
  2. get the device node/path - manually mount your usb-drive with your file manager, i do it with an USB Stick
    open terminal and run command df on a file or directory of the usb drive
    df "filename|directory on your USB drive"
    output:
Filesystem 1K-blocks Used Available Use% Mounted on /dev/sdf1 3997696 2821980 1175716 71% ... 

now you have the device node/path of your usb drive: /dev/sdf1

  1. get the Attributes and Environment variables for your device from udev database, needed for a udev rule.
    We must exactly identify the device, to which the udev rule shall be applied, because the dev path can change every time you disconnect and connect your USB Drive
    udevadm info /dev/sdf1 or
    udevadm info --attribute-walk /dev/sdf1 detailed

    output of first one:
P: /devices/pci0000:00/0000:00:09.0/0000:02:00.0/usb8/8-1/8-1:1.0/host6/target6:0:0/6:0:0:0/block/sdf/sdf1 N: sdf1 L: 0 S: disk/by-id/usb-SanDisk_U3_Cruzer_Micro_0000186F6A60343E-0:0-part1 S: disk/by-uuid/3CF6-EEA5 S: disk/by-path/pci-0000:02:00.0-usb-0:1:1.0-scsi-0:0:0:0-part1 S: disk/by-partuuid/e41c7c97-01 E: DEVPATH=/devices/pci0000:00/0000:00:09.0/0000:02:00.0/usb8/8-1/8-1:1.0/host6/target6:0:0/6:0:0:0/block/sdf/sdf1 E: DEVNAME=/dev/sdf1 E: DEVTYPE=partition E: DISKSEQ=21 E: PARTN=1 E: MAJOR=8 E: MINOR=81 E: SUBSYSTEM=block E: USEC_INITIALIZED=2220353618 E: ID_VENDOR=SanDisk E: ID_VENDOR_ENC=SanDisk\x20 E: ID_VENDOR_ID=0781 E: ID_MODEL=U3_Cruzer_Micro E: ID_MODEL_ENC=U3\x20Cruzer\x20Micro\x20 E: ID_MODEL_ID=5406 E: ID_REVISION=3.21 E: ID_SERIAL=SanDisk_U3_Cruzer_Micro_0000186F6A60343E-0:0 E: ID_SERIAL_SHORT=0000186F6A60343E E: ID_TYPE=disk E: ID_INSTANCE=0:0 E: ID_BUS=usb E: ID_USB_INTERFACES=:080650: E: ID_USB_INTERFACE_NUM=00 E: ID_USB_DRIVER=usb-storage E: ID_PATH=pci-0000:02:00.0-usb-0:1:1.0-scsi-0:0:0:0 E: ID_PATH_TAG=pci-0000_02_00_0-usb-0_1_1_0-scsi-0_0_0_0 E: ID_PART_TABLE_UUID=e41c7c97 E: ID_PART_TABLE_TYPE=dos E: ID_DRIVE_THUMB=1 E: ID_FS_UUID=3CF6-EEA5 E: ID_FS_UUID_ENC=3CF6-EEA5 E: ID_FS_VERSION=FAT32 E: ID_FS_TYPE=vfat E: ID_FS_USAGE=filesystem E: ID_PART_ENTRY_SCHEME=dos E: ID_PART_ENTRY_UUID=e41c7c97-01 E: ID_PART_ENTRY_TYPE=0xc E: ID_PART_ENTRY_NUMBER=1 E: ID_PART_ENTRY_OFFSET=2048 E: ID_PART_ENTRY_SIZE=8011776 E: ID_PART_ENTRY_DISK=8:80 E: DEVLINKS=/dev/disk/by-id/usb-SanDisk_U3_Cruzer_Micro_0000186F6A60343E-0:0-part1 /dev/disk/by-uuid/3CF6-EEA5 /dev/disk/by-path/pci-0000:02:00.0-usb-0:1:1.0-scsi-0:0:0:0-part1 /dev/disk/by-partuuid/e41c7c97-01 E: TAGS=:systemd: E: CURRENT_TAGS=:systemd: 

E: means, this is an environment variable
for my udev rule i use variable ID_FS_UUID=3CF6-EEA5 to identify my device
This is the uuid number of the filesystem on my usbstick.

  1. write an udev rule and save it to /etc/udev/rules.d directory
    rules must be saved with the extension .rules
    udev rules are worked through in a lexicographical order and can overwrite each other, so i use numbers in the beginning of the name.
    get root rights -> open file 99_myrule.rules

udev rule:

ENV{ID_FS_UUID}=="3CF6-EEA5", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /my/mount/directory/" 

save it!
ENV{ID_FS_UUID}=="3CF6-EEA5" means
check if the environment variable ID_FS_UUID of the device is equal to 3CF6-EEA5,
if so, device identified -> RUN systemd-mount
$devnode ... expands to the device path, udev sets for the device (/dev/...)

The device will be mounted to /my/mount/directory/, be sure, that it exists!
Here is the complete syntax for udev rules: https://www.linux.org/docs/man7/udev.html

  1. reload udev rules
    sudo udevadm control --reload-rules

  2. You are ready to disconnect and connect the usb drive and see, if it works!

you can also additionally identify your device by vendor id and product id, but than you need a detailed look at the udev database.
udevadm info --attribute-walk /dev/sdf1
i found in SUBSYSTEMS=="usb":

ATTRS{idProduct}=="5406" ATTRS{idVendor}=="0781" 

udev rule:

ATTRS{idVendor}=="0781", ATTRS{idProduct}=="5406", ENV{ID_FS_UUID}=="3CF6-EEA5", RUN{program}+="/usr/bin/systemd-mount --no-block --collect $devnode /my/mount/directory/"