Linux: howto avoid video devices getting mixed up after reboot, using udev rules

This is part of my tutorial “How to stream analog tv to mobile android devices with Ubuntu, MythTV, xmltv and MythFrontEnd“.

In Linux video devices can be accessed through “/dev/video0”, “/dev/video1” and so on. The problem is that the device designations can change after a reboot, but there is a way to “fix” your video devices. You can do this by making “symbolic links” to your devices, based on device name, driver or any other unique identifier. You can then use this symbolic link instead of “/dev/video0” and your device order will hold up after every reboot. Since I also install ZoneMinder with 3 webcams, this step is a must for me.

This is a synopsis from the MythTV guide I followed:

– make sure your devices are connected and can be accessed as “/dev/video0“, “/dev/video1” and so on.

– in a terminal, type:
udevadm info -a -p $(udevadm info -q path -n /dev/video0)
– copy/paste the output to a text editor and save it

– now for device #2, #3 type:
udevadm info -a -p $(udevadm info -q path -n /dev/video1),
udevadm info -a -p $(udevadm info -q path -n /dev/video2)
and so on.

– copy/paste each output to a text editor and save it for later. Repeat this step for all your video devices.

The output will look something like this:

looking at device '/devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0/video4linux/video0':
KERNEL=="video0"
SUBSYSTEM=="video4linux"
DRIVER==""
ATTR{name}=="Vimicro USB 2.0 PC Camera (Venu"
ATTR{index}=="0"

looking at parent device '/devices/pci0000:00/0000:00:1d.7/usb1/1-3/1-3:1.0':
...

Now look for something that you can use to uniquely identify your video device.
In my case I used:
for “/dev/video0”: ATTR{name}=="Vimicro USB 2.0 PC Camera (Venu",
for “/dev/video1”: ATTR{name}=="UVC Camera (046d:0805)",
for “/dev/video2”: ATTR{name}=="DC10plus[0]" and
for “/dev/video3”: ATTR{name}=="ivtv0 encoder MPG",
because they contain the names of the devices and they are unique.

Ok, now we have our unique identifying attributes, now it’s time to make symbolic links using “udev“:

– in a terminal type:

sudo gedit /etc/udev/rules.d/99-server.rules

This will create a new file where we will define our rules.
Use a high number (99 in this case) to start the filename with, this will make sure udev overrides automatic settings. You can choose your own filename, just make it start with a high number.

– Now we define one rule per video device. On each boot udev will read these rules, and create a symbolic link for the device found based on this identifier.

– Here’s the contents of my /etc/udev/rules.d/99-server.rules:


KERNEL=="video[0-9]*", DRIVERS=="zr36067", SYMLINK+="video-DC10"
KERNEL=="video[0-9]*", ATTR{name}=="ivtv0 encoder MPG", SYMLINK+="video-PVR-150"
KERNEL=="video[0-9]*", ATTR{name}=="UVC Camera (046d:0805)", SYMLINK+="video-Logitech300"
KERNEL=="video[0-9]*", ATTR{name}=="Vimicro USB 2.0 PC Camera*", SYMLINK+="video-Vimicro"

Make sure you have 1 rule per line, and no empty lines. It could look different in the browser, but there’s 1 rule per line.

What these rules tell udev:

look for any video device between 0 and 9 that matches DRIVERS==”zr36067″, and create a link called “video-DC10“.
look for any video device between 0 and 9 that matches ATTR{name}==”ivtv0 encoder MPG”, and create a link called “video-PVR-150” and so on.

Reboot the system to see if the links are there. The easy way to check is to open the Thunar file manager, and browse to “/dev”:

Symbolic links created with udev rules

From now on you can use these fixed links and your video devices will no longer be mixed up!