Changes between Version 2 and Version 3 of linux/persistent_device_naming


Ignore:
Timestamp:
06/05/2026 07:16:42 PM (8 days ago)
Author:
Tim Harvey
Comment:

add example of using udev rule to consistently name a webcam dev

Legend:

Unmodified
Added
Removed
Modified
  • linux/persistent_device_naming

    v2 v3  
    118118}}}
    119119
    120 == Audio
     120
     121[=#video]
     122== Video Devices (ie USB Webcams)
     123The Linux kernel does not guarantee the enumeration order of devices but at the user space level you can use systemd with udev rules to name devices in a persistent way. It can happen that devices are moving around or flip flopping between boots.
     124
     125Take for example video devices on a modern platform like Venice which has two onboard video codec devices. When you add a USB webcam it will add another device. If these are all kernel modules they could end up enumerating in random ways.
     126
     127You can use {{{v4l2-ctl --list-devices}}} to see them (or cat /sys/class/video4linux/video*/names):
     128{{{#!bash
     129root@venice:~# v4l2-ctl --list-devices
     130nxp,imx8mm-vpu-g1-dec (platform:38300000.video-codec):
     131        /dev/video2
     132        /dev/media1
     133
     134nxp,imx8mq-vpu-g2-dec (platform:38310000.video-codec):
     135        /dev/video3
     136        /dev/media2
     137
     138Logitech Webcam C930e (usb-xhci-hcd.1.auto-1):
     139        /dev/video0
     140        /dev/video1
     141        /dev/media0
     142}}}
     143 - above the webcam is on /dev/video0 and /dev/video1 but if you look at the devices capabilities via {{{v4l2-ctl --device /dev/video0 --all}}} you'll see that the first device is the capture device and the 2nd one is a UVC control device. On any other boot you may find the webcam is /dev/video2 and /dev/video3.
     144
     145Use {{{udevadm info}}} to get attributes that you can put in a udev rule:
     146{{{#!bash
     147root@venice:~# udevadm info -a -p $(udevadm info -q path -n /dev/video0) | grep -E "idVendor|idProduct|serial"
     148    ATTRS{idProduct}=="0843"
     149    ATTRS{idVendor}=="046d"
     150    ATTRS{serial}=="2FBAA8BE"
     151    ATTRS{idProduct}=="0002"
     152    ATTRS{idVendor}=="1d6b"
     153    ATTRS{serial}=="xhci-hcd.1.auto"
     154    ATTRS{dbc_idProduct}=="0010"
     155    ATTRS{dbc_idVendor}=="1d6b"
     156}}}
     157
     158Here we see that the webcam capture device /dev/video0 has a VID=046d and a PID=0843. Add a udev rule to symlink this to /dev/video_webcam:
     159{{{#!bash
     160# add udev rule for webcam
     161cat << EOF > /etc/udev/rules.d/99-webcam.rules
     162# External USB Webcam - Target only the video stream (index 0)
     163SUBSYSTEM=="video4linux", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="0843", ATTR{index}=="0", SYMLINK+="video_webcam"
     164EOF
     165# apply changes
     166sudo udevadm control --reload-rules
     167sudo udevadm trigger
     168}}}
     169 - note that we also filter on stream index as uvcvideo provides 2 devs and we want the first one with the video streams
     170
     171
     172[=#audio]
     173== Audio Devices
    121174
    122175The Linux kernel does not guarantee the enumeration order of devices but at the user space level you can use systemd with udev rules to name devices in a persistent way. It can happen that devices are moving around or flip flopping between boots.