You are not logged in.

#1 2016-08-20 18:52:50

FlowIt
Member
Registered: 2014-10-25
Posts: 239

[Solved] udev rule tries to mount device instead of partition

I want to write a udev rule to automount my usb storage, but I can't get it working. This is my rule:

# start at sdb to ignore the system hard drive
KERNEL!="sd[b-z]*", GOTO="my_media_automount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="my_media_automount_end"

# import some useful filesystem info as variables
IMPORT{program}="/sbin/blkid -o udev -p %N"

# get the label if present, otherwise assign one based on device/partition
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# create the dir in /media
ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"

# automount
ACTION=="add", RUN+="/bin/mount -t auto -o relatime,rw,exec,uid=me,gid=users /dev/%k '/media/%E{dir_name}'"

# clean up after device removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# exit
LABEL="my_media_automount_end

This creates two directories in /media: usbhd-sdb and usbhd-sdb1. It should only be the latter. my journal shows:

ystemd-udevd[1381]: Process '/bin/mount -t auto -o relatime,rw,exec,uid=me,gid=users /dev/sdb '/media/usbhd-sdb'' failed with exit code 32.

I am not suprised by the failing mount since /dev/sdb cannot be mounted. So how can I change my rule to exclude the device and only work on partitions?

Last edited by FlowIt (2016-08-20 20:22:22)

Offline

#2 2016-08-20 19:09:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,524
Website

Re: [Solved] udev rule tries to mount device instead of partition

Your regex is wrong.  KERNEL!=sd[b-z]* will match sdb and sdb1 which should result in exactly the results you are getting.

That should be sd[b-z][0-9], or at least sd[b-z]? not sd[b-z]*.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2016-08-20 19:43:26

FlowIt
Member
Registered: 2014-10-25
Posts: 239

Re: [Solved] udev rule tries to mount device instead of partition

Ah, a second pair of eyes is really helpful.
This clears the error in the journal but still the drive is not mounted. The appropriate mountpoint is created when plugged in and deleted when plugged out, but I still cannot access the drive. The journal shows nothing. I can manually mount and umount the drive. The wiki says not to usemount directly in the rules but I am not using fuse so this shouldn't be a problem.

Edit: Actually, it is a problem. I followed the wiki and edited systemd-udevd.service and not it works fine.

Last edited by FlowIt (2016-08-20 20:22:04)

Offline

#4 2016-08-20 22:07:38

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: [Solved] udev rule tries to mount device instead of partition

You don't need to do all that blkid stuff. Just order your .rules after “60-persistent-storage.rules” and all those ID_* environment vars will be present.

If you don't mind hardcoding the name (perhaps based on which USB port the device is plugged into) you can use systemd to do the mounting. That really simplifies the udev rules.

Offline

Board footer

Powered by FluxBB