Welcome to the Linux Foundation Forum!

[udev] how to set a rule depending on the manufacturer or the serial number?

For example, how to adapt the line
SUBSYSTEM=="usb", SYMLINK+="myusb"
to filter according to the manufacturer or the serial number? Adding ATTR{Manufacturer}==6989 or ATTRS{Manufacturer}==6989 didn't work.

Also, the Nautilus call didn't work neither:
SUBSYSTEM=="usb", SYMLINK+="myusb", RUN+="/usr/bin/nautilus"

Any idea what goes wrong?

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • For the serial number, it's ATTR{serial}=="serialnumber" as shown with command udevadm info --attribute-walk --name /dev/myusb"

  • And for the manufacturer, it's ATTR{manufacturer}=="number"

  • Posts: 380

    I was having an interesting time changing the usb-key's name as it was busy with systems-udev, but that is another day.
    Here is an example of some udev commands to write a file. There is the udev-rule file and two tiny scripts. The idea was write to a file if the usb-key was plugged in or pulled out.

    98-usb-stick.rules

    #

    #

    #

    used "udevadm info -a /dev/sdb > /tmp/udev"

    to list related device information

    #

    skip to end if not a block device

    #
    SUBSYSTEM!="block", GOTO="endoffile
    #

    look for the root device, not the partitions

    maybe add in "change" with "add"

    KERNEL=="sd?", ACTION=="add" \
    , ATTRS{manufacturer}=="Kingston"\
    , ATTRS{product}=="DataTraveler 2.0"\
    , ATTRS{serial}=="5B870E000068"\
    , RUN+="/usr/local/bin/usb-on.sh"

    KERNEL=="sd?", ACTION=="remove"\
    , ATTRS{manufacturer}=="Kingston"\
    , ATTRS{product}=="DataTraveler 2.0"\
    , ATTRS{serial}=="5B870E000068"\
    , RUN+="/usr/local/bin/usb-off.sh"

    LABEL="endoffile"

    usb-off.sh

    !/bin/bash

    echo "usb is offline " >>/tmp/usb.out
    /bin/date >> /tmp/usb.out

    usb-on.sh#!/bin/bash

    echo "usb is offline " >>/tmp/usb.out
    /bin/date >> /tmp/usb.out

    Yours should be similar.
    Regards, Lee

  • Hi Lee,
    Your scripts work great, thank you very much for the trick.
    At the end I tried to add /usr/bin/nautilus & but it doesn't launch, there must be something I don't know about Xorg that prevents it to do so...
    Regards, Thomas

  • Tried xdg-open /tmp/usb.out at the end but it doesn't work neither

  • Posts: 916

    I'm not 100 percent sure in this instance, but running Desktop GUI commands won't work when you launch things non-interactively from a script not attached to a terminal. This has nothing to do with udev, just try it from a bash script. They have to be run from the right context.

  • Posts: 207
    edited July 2021

    In this thread, it says to use ENV{SYSTEMD_WANTS}=="some.service": https://bbs.archlinux.org/viewtopic.php?id=206555

  • Posts: 1,271

    Hi @thomas.bucaioni ,

    It seems SYSTEMD_WANTS is intended for other stuff. From the udev(7) man page:

    In order to activate long-running processes from udev rules,
    provide a service unit and pull it in from a udev device
    using the SYSTEMD_WANTS device property. See
    systemd.device(5) for details.

    Perhaps you can try with the RUN key, as shown in the man page:

    RUN{type}
    Specify a program to be executed after processing of all the
    rules for the event. With "+=", this invocation is added to
    the list, and with "=" or ":=", it replaces any previous
    contents of the list. Please note that both "program" and
    "builtin" types described below share a common list, so
    clearing the list with ":=" and "=" affects both types.

    1. type may be:
    2.  
    3. "program"
    4. Execute an external program specified as the assigned
    5. value. If no absolute path is given, the program is
    6. expected to live in /usr/lib/udev; otherwise, the
    7. absolute path must be specified.
    8.  
    9. This is the default if no type is specified.

    I understand you are playing around with udev, because this doesn't seem to be a real case example.

    Regards,
    Luis.

  • Hi @luisviveropena,
    With SUBSYSTEM=="usb", ATTR{serial}=="number", SYMLINK+="myusb", RUN+="/usr/bin/nautilus", ACTION=="add" it doesn't work neither.
    But yes, it's not a real case example indeed... It was to try to do the same as in Windows, no big deal
    Regards,
    Thomas

  • Posts: 207
    edited July 2021

    This works: https://bbs.archlinux.org/viewtopic.php?pid=1709570#p1709570
    In Lee's script, it takes to add echo 'nautilus' >> execme.sh

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training