udev rule cannot mount USB storage

Latest response

Red Hat Enterprise Linux release 8.0

I want to automount a USB storage device when it is plugged in.

Therefore I created a udev rule, but the storage is never mounted:

KERNEL!="sd[a-z][0-9]", GOTO="exit"
# Set directory name to mount usb device
ACTION=="add", ENV{dir_name}="drive-%k"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
# Create the mount directory
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}"
# Mount the device
ACTION=="add", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
# Unmount the device on removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}"
# Remove mount directory
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir /media/%E{dir_name}"
# Exit
LABEL="exit"

After trying different rules, I was able to capture the error of the mount command. It is "mount: /media/rdx: permission denied".

So I assume, the device is somehow locked either by the udev processing itself or by another process.

GNOME auto mounting is disabled.

Thank you for any help

Responses

Examine files under /etc/modprobe.conf and /etc/modprobe.d/CHECKALLFILES for directives you'll find in https://access.redhat.com/solutions/18978 that solution.

That solution shows the method to block usb storage. Evaluate that to see if the system you're using has those directives. If those directives were put in for a corporate security policy, I'd recommend performing proper coordination with your company in case your company does this for a specific reason.

Regards

RJ

Hello RJ

Actually I have no directives in /etc/modprobe.d related to the usb-storage. And there is no corporate security policy, defined by us. The files contain all default settings.

Do you mean, that such a policy is inhibiting the mount? Or do you mean, I have to add a "Fake install" or "blacklist" directive, so that I can do it using UDEV?

Thanks for your help

Thomas

Hi Thomas.

Plug the USB storage and see if you can "see" it using commands such as lsblk, blkid, fdisk -l | egrep Disk | egrep -v mapper

Is there a chance the disk you're connecting is NTFS and if so, have you installed the NTFS drivers from EPEL (this sentence here is a wild stab).

Regards

RJ

Thomas,

Are you able to mount the drive at all, even manually? Have you been able to do so? And welcome to the RH discussion area

Regards

RJ

Hi RJ

Manual mounting is possible without any problem. Once mounted I can read and write the usb storage.

And yes, I can see the usb storage i.e. with lsblk.

Best regards, Thomas

I know for my RHEL 7 workstation, when I connect an external USB Hard drive, it gets mounted under /run/media/myuserid/nameofdevice

I've never had to go out of my way to configure this, it was just a feature.

You say in your original description that GNOME automounting is disabled. Is that the "automount" service you were speaking of or something else? I'd have to dig further, but how did you determine GNOME automatic mounting was disabled? was it systemctl status autofs or something else?

Can you also check the permissions for /media (you said the output said permission denied)?

Perhaps very temporarily set /media/rdx if it exists (or /media) to 777 permissions (and make note of the permissions prior so you can set it back)

My /media is set to permissions 755 on my RHEL 7.7 workstation.

Let us know, sorry I missed your details earlier

Regards

RJ

Thomas, This is for RHEL 7 and below, but evaluate it for RHEL 8 https://access.redhat.com/solutions/20107 - and note this is to disable automatic mounting, so you'd undo these steps if it is actually applicable to your RHEL 8 system.

I honestly don't know if it will resolve your issue.

Let us know how it goes

Regards

RJ

This is a server, so there will not be any user starting the desktop. But I did it and then the usb storage gets mounted as you have it done on your computer.

I found an article that described how to disable this feature by setting two variables in configuration files, because I feared it can disturb UDEV mounting. I can send you the link on Monday.

I think directory permission cannot be the problem, because manual mounting works fine and I assume, udev does it also as root. But I can try with full permission...

What about locking? Could the device be locked by an UDEV handling process while it executes mounting? Only mounting fails. The mkdir command executes fine.

Best regards, Thomas

https://access.redhat.com/solutions/20107 this is exactly what I did!

Hi Thomas Such UDEV rules for this matter are not typical unless someone goes out of their way to put one in.

You can check /etc/udev/rules.d/ directory for any such rules.

I didn’t realize this is a server, and with RHEL 8 it could be either with the new way they did the ISO/repos.

I have a server that I occasionally connect a USB external drive to, and it never mounts automatically, I’ll have to dig to see a method for this to work on RHEL server 8, or maybe someone else might chime in here, I’ll look further

Regards

RJ

Hello

Could it be a 'namespace' problem? I found this https://unix.stackexchange.com/questions/330094/udev-rule-to-mount-disk-does-not-work, but the solution does not work for me.

Sorry Thomas, I don't have an immediate answer for this. I'd recommend putting in a case for this. I might be able to look into this later this week, or perhaps one of the other usual folks who participate here can chime in.

Regards,

RJ