Centos – Why is the udev mounted device not staying put

centosmountudev

I have a computer running CentOS 7. I'm trying to set up a udev rule to automatically mount a USB drive (named 'sdb1') to /mnt/flash whenever it is connected.

The udev script was not working (see below), so I instead put my commands into a bash script and had udev run the bash script so I could see what was going on.

Problem #1:
If I try to directly create /mnt/flash from my bash script, it fails due to the root filesystem being read-only. I can confirm the root filesystem is NOT read-only. My system is already booted. However, if I insert my USB drive, let udev run my script, sure enough when I run 'mount | logger' in my script it shows root is read only.

I worked around this issue by running 'mount -o remount,rw /' at the top of my script.

Problem #2: I'm able to create /mnt/flash and mount /dev/sdb1 to /mnt/flash successfully. I run 'mount | logger' after doing this and see it mounted in /var/log/messages. However, when all is said and done, /dev/sdb1 is not mounted. I even put a 5 second delay in my script and ran 'mount | logger' a second time. Both times show /dev/sdb1 is mounted to /mnt/flash. However, if I run 'mount' from a different terminal while all of this is going on, I never see /dev/sdb1 mounted anywhere.

Am I going crazy, or is udev doing some strange things to the filesystems while it runs?

udev script: /etc/udev/rules.d/99-usb-automount.rules

#Only operate on sdb1
KERNEL!="sdb1", GOTO="usb-automount-end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="usb-automount-end"

#import useful variables from blkid program
IMPORT{program}="/sbin/blkid -o udev -p %N"

#ignore anything other than vfat filesystems
ACTION=="add", ENV{ID_FS_TYPE}!="vfat", GOTO="usb-automount-end"

#remount root as read-write.  Not sure why we have to do this!
#ACTION=="add", RUN+="/bin/mount -o remount,rw /"

#mount to /mnt/flash
#ACTION=="add", RUN+="/bin/mkdir -p /mnt/flash"
#ACTION=="add", RUN+="/bin/mount -t vfat -o dmask=000,fmask=111 /dev/%k /mnt/flash"
ACTION=="add", RUN+="/root/test_run.sh", OPTIONS="last_rule"

#clean up after removal
#ACTION=="remove", RUN+="/bin/umount -l /mnt/flash"

#label for goto end
LABEL="usb-automount-end"

Here is /root/test_run.sh:

#!/bin/bash -x
logger "running mount"
mount | logger
logger "remounting root"
mount -o remount,rw /  2>&1 | logger
logger "remount done"
mount | logger
logger "Running script.  Adding dir"
mkdir -p /mnt/flash2 2>&1 | logger
logger "Directory added... mounting."
mount -t vfat -o dmask=000,fmask=111 /dev/sdb1 /mnt/flash2 2>&1 | logger
logger "Mounted"
mount | logger

logger "Sleeping 5 then re-checking"
sleep 5

mount | logger

Edit 1 : Disabling SELinux fixed problem #1. However, I still cannot get the /dev/sdb1 to stay mounted after udev. It still appears there is almost a second mount table that is maintained from within the udev context.

Best Answer

Problem #1 was caused by SELinux. Since I don't need it for this system, I simply disabled it.

Problem #2 was caused by a udev setting (specified in systemd script) that makes the udev namespace keep a 'slave' copy of the mount flags. Changing this to 'shared' fixed the problem. See a more detailed answer here: https://unix.stackexchange.com/a/154318/41988

Related Question