Create symlink for USB device

I use a USB RTL-SDR dongle to read 433mhz sensors and publish to an MQTT server.
I run the software in Docker with the --device tag /dev/bus/usb/001/003

lsusb -v outputs:

Bus 003 Device 009: ID 0bda:2838 Realtek RTL2838UHIDIR

idVendor           0x0bda
idProduct          0x2838
bcdDevice            1.00

So the hotplug variable PRODUCT must be bda/2838/100

This bus number and/or device number do change every boot and caught it changing even If I don't unplug/reinsert the dongle. It is not possible to get a by-id path as with serial devices.

In a system with udev I could create a rule that creates a symlink when the bus ID changes or the dongle is inserted.

How can this be done on OpenWRT? I read that there is a way to do it with hotplug to but I do not understand how to write the script that reads the bus and device number based on vendor/device ID.

I rewrote the script under the usb section but I am unsure if the middle block is valid. Udev rules has a "changed" variable to detect changes but I do not know if it is possible with hotplug scripts. The device path has changed before without being unplugged so I want to be sure it updates the symlink.

RTL433_PRODID="bda/2838/100"
SYMLINK="rtl433"
 
if [ "${PRODUCT}" = "${RTL433_PRODID}" ]; then
    if [ "${ACTION}" = "add" ]; then
          ln -s /dev/${DEVNAME} /dev/${SYMLINK}
          logger -t hotplug "Symlink from /dev/${DEVNAME} to /dev/${SYMLINK} created"
    fi
fi

if [ "${PRODUCT}" = "${RTL433_PRODID}" ]; then
    if [ "${ACTION}" = "change" ]; then
          ln -s /dev/${DEVNAME} /dev/${SYMLINK}
          logger -t hotplug "Device path changed to /dev/${DEVNAME} and symlink /dev/${SYMLINK} updated"
    fi
fi

if [ "${PRODUCT}" = "${RTL433_PRODID}" ]; then
    if [ "${ACTION}" = "remove" ]; then
        rm /dev/${SYMLINK}
        logger -t hotplug "Symlink /dev/${SYMLINK} removed"
    fi
fi

From the documentation usb can only have add and remove.

It's strange that the numbers change if it's plugged into the same port. Can you show examples of lsusb when they change?

Yes it would ideally be the same and the OS keeping track of device serial or similar to assign the same numbers every time. But every reboot it changes.

Now I rebooted and the device ID changed to 002. I have not unplugged the dongle.

lsusb
Bus 003 Device 002: ID 0bda:2838 Realtek RTL2838UHIDIR

Before it was:

Bus 003 Device 009: ID 0bda:2838 Realtek RTL2838UHIDIR

I can confirm the above script works and the symlink gets created at boot!

But the container can not read the symlink directly with the device tag.
docker run --device/dev/rtl433

I have to use the command:
docker run --device=$(readlink -f /dev/rtl433)

Then it works.

1 Like

The middle part of the script "change" can be removed, it doesn't do anything.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.