You are not logged in.

#1 2021-05-24 11:55:18

Komisch
Member
Registered: 2018-05-30
Posts: 45

udev rule for automount usb devices

Greetings Community,

currently I try to setup automount for usb devices to the dir "/home/me/mnt/" prefearably by label (sda1, ..., sdw178) but whatever naming this udev rule provides on

https://wiki.archlinux.de/title/Udev

is also fine.

I am trying to use the following script:

KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"

# Do not mount devices already mounted somewhere else to avoid entries for all your local partitions in /media
ACTION=="add", PROGRAM=="/usr/bin/grep -q ' /dev/%k ' /proc/self/mountinfo", GOTO="media_by_label_auto_mount_end"

# Open LUKS partition if necessary
PROGRAM=="/sbin/blkid -o value -s TYPE %N",  RESULT=="crypto_LUKS", ENV{crypto}="mapper/", ENV{device}="/dev/mapper/%k"
ENV{crypto}=="", ENV{device}="%N"
ACTION=="add", ENV{crypto}!="", PROGRAM=="/usr/bin/alacritty -e 'echo Password for /dev/%k; /sbin/cryptsetup luksOpen %N %k'"
ACTION=="add", ENV{crypto}!="", TEST!="/dev/mapper/%k", GOTO="media_by_label_auto_mount_end"

# Global mount options
ACTION=="add", ENV{mount_options}="noatime"
# Filesystem-specific mount options
ACTION=="add", PROGRAM=="/sbin/blkid -o value -s TYPE %E{device}", RESULT=="vfat|ntfs", ENV{mount_options}="%E{mount_options},utf8,gid=100,umask=002"

# Get label if present, otherwise assign one
PROGRAM=="/sbin/blkid -o value -s LABEL %E{device}", ENV{dir_name}="%c"
# Use basename to correctly handle labels such as ../mnt/foo
PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"

# Mount the device
ACTION=="add", ENV{dir_name}!="", RUN+="/bin/mkdir -p '/home/me/mnt/%E{dir_name}'", RUN+="/bin/mount -o %E{mount_options} /dev/%E{crypto}%k '/home/me/mnt/%E{dir_name}'"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/home/me/mnt/%E{dir_name}'"
ACTION=="remove", ENV{crypto}!="", RUN+="/sbin/cryptsetup luksClose %k"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir '/home/me/mnt/%E{dir_name}'"

# Exit
LABEL="media_by_label_auto_mount_end"

In the wiki is no further hint given what else to do, still the script seems not to work. It detects devices I plug in an creates some creative naming (no idea which rules it follows but I am fine with whatever creative output it produces) for them in the intended folder. Afterwards it doesn't mount the device. When I unplug the device it removes the folder. The feature is nice and funny but misses the point of automounting. I tried to find logs that give me an idea of what is happening and at which point it fails but couldn't find any logs.
On the internet I found that

# udevadm control --log-priority=info

should give me some log but it prints an empty line. My user is in the group disk and I even reinstalled ntfs-3g with fuse so that users are allowed to mount devices. I have no idea why and at which point the udev rule fails and therefore I have no idea how to solve it.

I hope you can help. Thanks in advance.

Offline

#2 2021-05-24 12:17:47

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 25,334

Re: udev rule for automount usb devices

The english wiki has an additional tidbit here https://wiki.archlinux.org/title/Udev#M … s_in_rules and recommends to not use mount as part of the udev rule but systemd-mount instead, in the rules you posted there's also nothing that would ensure that your user is the owner of the mount point.

Last edited by V1del (2021-05-24 12:24:38)

Offline

#3 2021-05-24 18:22:32

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,491

Re: udev rule for automount usb devices

Also

ACTION=="add", ENV{crypto}!="", PROGRAM=="/usr/bin/alacritty -e 'echo Password for /dev/%k; /sbin/cryptsetup luksOpen %N %k'"

won't work because it's run as root w/o a display server.

Offline

Board footer

Powered by FluxBB