This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

udev rules aren't being run

I'm trying to set up some udev rules in order to auto-mount USB drives in the system. First I mounted a USB drive to test how it would come up and ran

udevadm monitor --env 

to check what events were thrown. I saw there was a block device being added for /dev/sda1:

KERNEL[5321.019898] add      /devices/ocp.2/47400000.usb/47401400.usb/musb-hdrc.0.auto/usb1/1-1/1-1:1.0/host12/target12:0:0/12:0:0:0/block/sda/sda1 (block)
ACTION=add
DEVNAME=/dev/sda1
DEVPATH=/devices/ocp.2/47400000.usb/47401400.usb/musb-hdrc.0.auto/usb1/1-1/1-1:1.0/host12/target12:0:0/12:0:0:0/block/sda/sda1
DEVTYPE=partition
MAJOR=8
MINOR=1
SEQNUM=1581
SUBSYSTEM=block

So I chose to write a quick rule to test this out. In /etc/udev/rules.d I made a new rule. I named it: 01-usbmount.rules. I chose the 01 so it would be the first selected rule. I made sure it was by running

udevadm test 01-usbmount.rules

This provided the list of rules that would be run and my rule was listed amongst the top of the list:

[root /etc/udev/rules.d]# udevadm test 01-usbmount.rules
run_command: calling: test
adm_test: version 182
This program is for debugging only, it does not run any program,
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

builtin_kmod_init: load module index
add_matching_files: unable to open '/var/run/udev/rules.d': No such file or directory
parse_file: reading '/etc/udev/rules.d/01-usbmount.rules' as rules file
parse_file: reading '/lib/udev/rules.d/42-usb-hid-pm.rules' as rules file
parse_file: reading '/lib/udev/rules.d/50-udev-default.rules' as rules file

The rule itself I made just a quick test. On any "block" device being "add"ed into the system I would create a symbolic link with a long obvious name so I could find it quickly. Here's the rule:

[root /etc/udev/rules.d]# cat 01-usbmount.rules
# Run the mount rules for USB insertion
ACTION=="add", SUBSYSTEMS=="block", SYMLINK+="helloooooo", MODE="0456"

When I insert a USB stick into my TI AM335x based board, I do not see this rule executed (no symlink shows up in /dev). Just to make sure I did everything correctly, I did the exact same thing on my Ubuntu development machine and tested out the rule. Everything worked and the symlink was created successfully.

What's wrong with the TI board? Does something need to be enabled to get udev to work?

  • Hi Mike,

    I will ask somebody from the SW team to look at this.

  • Mike,

    The AMSDK filesystem has such udev rule for block device automount, /etc/udev/rules.d/automount.rules. Please use it as a reference.

    First please ensure the USB works. For example, run 'lsusb' command after plugged in the USB device to see if the device is enumerated.

  • Bin Liu,

       I did verify that the USB enumerated correctly:

    before usb is inserted:

    [root /var]# lsusb
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    

    after usb is inserted:

    [root /var]# lsusb
    Bus 001 Device 015: ID 154b:005b PNY
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    

    So you can see my "PNY" usb stick was found and enumerated correctly. I did see the automount rules in /etc/udev/rules.d however my script being named "01..." should have run first since the udev rules are sorted numerically/alphabetically. I think both scripts should run anyway. That's how it works on my Ubuntu development machine anyway.

  • Mike,

    I just ran a test with an EVM using SDK 7.00.  I plugged in a USB device and saw it automatically mount at /var/volatile/run/media/sda1.

    It looks like the main files controlling this are:

    /etc/udev/rules.d/automount.rules said:

    # There are a number of modifiers that are allowed to be used in some
    # of the different fields. They provide the following subsitutions:
    #
    # %n the "kernel number" of the device.
    #    For example, 'sda3' has a "kernel number" of '3'
    # %e the smallest number for that name which does not matches an existing node
    # %k the kernel name for the device
    # %M the kernel major number for the device
    # %m the kernel minor number for the device
    # %b the bus id for the device
    # %c the string returned by the PROGRAM
    # %s{filename} the content of a sysfs attribute
    # %% the '%' char itself
    #

    # Media automounting
    SUBSYSTEM=="block", ACTION=="add"    RUN+="/etc/udev/scripts/mount.sh"
    SUBSYSTEM=="block", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh"

    And the corresponding script it invokes (watch permissions when creating the script!):

    /etc/udev/scripts/mount.sh said:

    #!/bin/sh
    #
    # Called from udev
    #
    # Attempt to mount any added block devices and umount any removed devices


    MOUNT="/bin/mount"
    PMOUNT="/usr/bin/pmount"
    UMOUNT="/bin/umount"
    for line in `grep -v ^# /etc/udev/mount.blacklist`
    do
            if [ ` expr match "$DEVNAME" "$line" ` -gt 0 ];
            then
                    logger "udev/mount.sh" "[$DEVNAME] is blacklisted, ignoring"
                    exit 0
            fi
    done

    automount() {
            name="`basename "$DEVNAME"`"

            ! test -d "/run/media/$name" && mkdir -p "/run/media/$name"
            # Silent util-linux's version of mounting auto
            if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ;
            then
                    MOUNT="$MOUNT -o silent"
            fi

            if ! $MOUNT -t auto $DEVNAME "/run/media/$name"
            then
                    #logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/med
    ia/$name\" failed!"
                    rm_dir "/run/media/$name"
            else
                    logger "mount.sh/automount" "Auto-mount of [/run/media/$name] su
    ccessful"
                    touch "/tmp/.automount-$name"
            fi
    }

    rm_dir() {
            # We do not want to rm -r populated directories
            if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1"
            then
                    ! test -z "$1" && rm -r "$1"
            else
                    logger "mount.sh/automount" "Not removing non-empty directory [$
    1]"
            fi
    }

    if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" ]; then
            if [ -x "$PMOUNT" ]; then
                    $PMOUNT $DEVNAME 2> /dev/null
            elif [ -x $MOUNT ]; then
                    $MOUNT $DEVNAME 2> /dev/null
            fi

            # If the device isn't mounted at this point, it isn't
            # configured in fstab (note the root filesystem can show up as
            # /dev/root in /proc/mounts, so check the device number too)
            if expr $MAJOR "*" 256 + $MINOR != `stat -c %d /`; then
                    grep -q "^$DEVNAME " /proc/mounts || automount
            fi
    fi



    if [ "$ACTION" = "remove" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
            for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
            do
                    $UMOUNT $mnt
            done

            # Remove empty directories from auto-mounter
            name="`basename "$DEVNAME"`"
            test -e "/tmp/.automount-$name" && rm_dir "/run/media/$name"
    fi

  • Brad, I've got the same scripts in the same locations, but they don't seem to be doing anything:

    [root /var/volatile/run]# ls
    dbus           ifstate        sshd           syslog-ng.pid  xinetd.pid
    fbmgr.pid      snmpd.pid      sshd.pid       uim2.pid
    [root /var/volatile/run]# lsusb
    Bus 001 Device 002: ID 154b:005b PNY
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

    There is no media directroy created under /var/volatile/run so the USB drive doesn't auto mount. That's why I'm thinking that udev rules aren't working for some reason.

  • Make sure your script is executable in the file system.  The script creates the mount directory using "mkdir -p" so it should be creating all the directories it needs (even media).

  • I checked and the script is executable:


    [root /etc/udev]# ls cache.data rules.d udev.conf mount.blacklist scripts [root /etc/udev]# cd rules.d/ [root /etc/udev/rules.d]# ls 01-usbmount.rules automount.rules local.rules 99-bufferclass.rules autonet.rules localextra.rules [root /etc/udev/rules.d]# cd ../scripts/ [root /etc/udev/scripts]# ls -al total 16 drwxr-xr-x 2 root root 4096 Jan 26 2015 . drwxr-xr-x 4 root root 4096 Jan 1 00:37 .. -rwxr-xr-x 1 root root 1763 Jan 26 2015 mount.sh -rwxr-xr-x 1 root root 1402 Jan 26 2015 network.sh [root /etc/udev/scripts]#

    And I checked permissions on /var /volatile and /run:

    drwxrwxrwx   12 root     root          4096 Jan 30  2015 var
    drwxrwxrwx    8 root     root          4096 Jan  1 00:00 volatile
    drwxrwxrwx    4 root     root          4096 Jan  1 00:01 run

    I even tried making the media directory myself under run and no changes. Finally I added some debug into the mount.sh script, when I run it manually I can see:

    /bin/sh: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
    checking blacklist
    checking blacklist
    checking blacklist
    checking blacklist
    

    when I remove/insert the USB stick again nothing comes up at all. Clearly, the udev rules aren't being run for some reason...

     

  • Mike,

    Can you please post the dmesg log after inserted the drive? I want to check the enumeration log.
  • Please add the following line to the start of mount.sh (right after the opening comment block):

    echo hi > /home/root/myusbtext.txt

    I read in another thread that you can't actually see a normal "echo" on the console in these scripts but I have confirmed you can still write to a file. When I plug in a USB device with that command it generates the corresponding file. I assume your script is not running at all, but this should confirm.
  • Brad,

        Here's the updated script:

    [root /etc/udev/scripts]# cat mount.sh | head
    #!/bin/sh
    #
    # Called from udev
    #
    # Attempt to mount any added block devices and umount any removed devices
    
    echo hi > /home/myusbtext.txt
    MOUNT="/bin/mount"
    PMOUNT="/usr/bin/pmount"
    UMOUNT="/bin/umount"
    

    upon removal/insertion of the USB stick there is no file generated:

    [root /home]# ls
    [root /home]#
    

    If I run the script manually it wil generate the file. So the script is good, but it's not being run as per the udev rule.

    Bin Liu,

    Here's the dmesg | tail snap shot after insterting the usb:

    [ 6465.830007] usb 1-1: new high-speed USB device number 6 using musb-hdrc
    [ 6465.972012] usb 1-1: New USB device found, idVendor=154b, idProduct=005b
    [ 6465.979124] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    [ 6465.986700] usb 1-1: Product: USB 2.0 FD
    [ 6465.990858] usb 1-1: Manufacturer: PNY Technologies
    [ 6465.996004] usb 1-1: SerialNumber: AA00000000003099
    [ 6466.013459] usb-storage 1-1:1.0: USB Mass Storage device detected
    [ 6466.025430] scsi4 : usb-storage 1-1:1.0
    [ 6467.229672] scsi 4:0:0:0: Direct-Access     PNY      USB 2.0 FD       1100 PQ: 0 ANSI: 4
    [ 6467.254018] sd 4:0:0:0: [sda] 15950592 512-byte logical blocks: (8.16 GB/7.60 GiB)
    [ 6467.277982] sd 4:0:0:0: [sda] Write Protect is off
    [ 6467.283170] sd 4:0:0:0: [sda] Mode Sense: 43 00 00 00
    [ 6467.287841] sd 4:0:0:0: [sda] No Caching mode page found
    [ 6467.293549] sd 4:0:0:0: [sda] Assuming drive cache: write through
    [ 6467.305607] sd 4:0:0:0: [sda] No Caching mode page found
    [ 6467.311320] sd 4:0:0:0: [sda] Assuming drive cache: write through
    [ 6467.325247]  sda: sda1
    [ 6467.336814] sd 4:0:0:0: [sda] No Caching mode page found
    [ 6467.342538] sd 4:0:0:0: [sda] Assuming drive cache: write through
    [ 6467.348986] sd 4:0:0:0: [sda] Attached SCSI removable disk
    [root /home]#
    

    It looks correct to me... but maybe I'm missing something.

  • It seems to me the drive only got mounted after I manually ran 'df' command. Brad, do you have this problem on your evm?
  • I don't have that issue.  I just ran a clean test to be sure:

    1. Booted board (no USB present).
    2. Logged in as root.
    3. Plugged mass storage device into USB1.
    4. Ran "ls /var/volatile/run/media/sda1/" and saw the data of the USB drive.

  • PS. Bin, which board are you using? I was using the EVM.
  • Brad,

    The problem on my side is the thumb drive was somehow corrupted. I re-partitioned and re-formated it again. Now automount works as expected.

    I use AM335x GP EVM.