9

Sticking a USB-microcontroller on the port (similar to Arduino) creates these two files:

/dev/serial/by-id/usb-MBED_MBED_CMSIS-DAP_10105a42e87da33c103dccfb6bc235360a97-if01
/dev/disk/by-id/usb-MBED_microcontrolleur_10105a42e87da33c103dccfb6bc235360a97-0:0

Are the ids in this case generated on the fly by a program or are they hardcoded somewhere on the device itself?

If it is created on the fly:

  1. How excactly is it created?
  2. Will it exist only on Linux machines?
Pithikos
  • 1,564
  • 2
  • 15
  • 20

2 Answers2

7

The file name is generated by udev, but the id inside it comes from the device itself:

if you run lsusb -v and search for it, you'll find it's the serial id of the device:

...
iManufacturer        MBED
iProduct             microcontrolleur
iSerial              10105a42e87da33c103dccfb6bc235360a97

or something similar.


How does this happen ?

Let's say the usb stick gets mounted on /mnt/tmp. On my system i get:

$ udevadm info --device-id-of-file /mnt/tmp
8:17

Now run this and search for 8:17:

$ udevadm info --export-db | less
...
P: /devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1
N: sdb1
W: 61
S: block/8:17
S: disk/by-id/usb-pqi_IntelligentStick_AA04212900042956-0:0-part1

This gives us the device path to use with udevadm. Run this and search for for your device id (AA042... in my case)

$ udevadm test /devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0/host4/target4:0:0/4:0:0:0/block/sdb/sdb1 2>&1 | less
...
udev_rules_apply_to_event: LINK 'disk/by-id/usb-pqi_IntelligentStick_AA04212900042956-0:0-part1' /lib/udev/rules.d/60-persistent-storage.rules:31

That gives us all the udev rules + line numbers applied (60-persistent-storage.rules:31 etc)

And here's where the file name comes from:

$ /lib/udev/usb_id /devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0/host4/target4:0:0/4:0:0:0/block/sdb
pqi_IntelligentStick_AA04212900042956-0:0
lemonsqueeze
  • 1,260
  • 1
  • 9
  • 19
  • The question is then how lsusb gets this info.. because it might use `udev` like `udisks` does. – Pithikos Nov 14 '14 at 17:58
  • You can check the serial number really comes from the device itself by running ChipGenius under windows. – lemonsqueeze Nov 14 '14 at 18:30
  • as alex showed, `udev` just concatenates the serial id from the kernel into a larger string. `lsusb` or `lsblk` use the same serial id. there's no treachery going on. feel free to trust built-in `ls` commands; otherwise, what can you trust? – underscore_d Oct 21 '15 at 19:55
5

by-id symlinks are created by udev rules (and are specific to OS'es using udev)

You can find the specific rules in /lib/udev/rules.d:

for serial devices in 60-persistent-serial.rules:

ENV{.ID_PORT}=="", SYMLINK+="serial/by-id/$env{ID_BUS}-$env{ID_SERIAL}-if$env{ID_USB_INTERFACE_NUM}"
ENV{.ID_PORT}=="?*", SYMLINK+="serial/by-id/$env{ID_BUS}-$env{ID_SERIAL}-if$env{ID_USB_INTERFACE_NUM}-port$env{.ID_PORT}"

and for the disk devices in 60-persistent-storage.rules:

KERNEL=="sd*|sr*|cciss*", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL}=="?*", SYMLINK+="disk/by-id/$env{ID_BUS}-$env{ID_SERIAL}"
Alex P.
  • 2,713
  • 12
  • 15