Categories
Computers

USB Camera Automounting

Another reference post for me about Linux configuration stuff. Details after the jump, proceed at your own peril.

The goal here is to use udev to create a consistent device name which automount can use to mount the device. It’s possible to use the device node the kernel creates when the USB device is plugged in, but it restrictive. What if the camera is plugged into a different USB port? What if another USB device is already using that device node?

First, use dmesg to find out what device node was created when the USB cable was plugged in. Just scan the output for the USB related output, then look for a SCSI device such as sdc or sdd. Since all of this has to be done as root anyway, the tail command could also be used for the same ends:

tail -f /var/log/syslog  --> or wherever your syslog happens to be

From here on out, assume everything is done as the root user. Now use the following command:

udevadm info --name=/dev/sdc1 --attribute-walk

The value for the name option should be the path to the device node created for the USB camera. I’m using /dev/sdc1 for the example because that’s what was created in my case just now. There will be a lot of seemingly indecipherable output.

Now, it’s time to create a udev rule to detect the USB camera and create a device symlink. I’m not going to get into udev rule details. But what needs to be done is find some unique attributes that udev can use to uniquely identify the camera when it is plugged into the USB port. In my case, the camera is a Lumix DMC-LX5 and that information is part of the device information in the attribute-walk we just did. Here’s the rule I’ve created:

KERNEL=="sd?1", SUBSYSTEMS=="usb", ATTRS{product}=="DMC-LX5", SYMLINK+="camera"

Place the rule in the file /etc/udev/rules.d/10-local.rules. Then restart udev. On my debian/testing box that’s done quite simply:

/etc/init.d/udev restart

Unfortunately, the camera connection needs to be cycled. Probably best to just cycle the power. Once the camera indicates a connection, check the device directory to make sure the rule is working properly. The ls command can be used for this:

$ ls -l /dev/ca*
lrwxrwxrwx 1 root root 4 Apr  3 20:39 /dev/camera -> sdc1

All right, that’s the first half of the problem. Now it’s time to configure automount. I’m assuming it’s already installed. Start by adding the following line to /etc/auto.master:

/-  /etc/auto.removable     --timeout=30

Now, either create or edit the file /etc/auto.removable and add the following entry:

/media/camera       -fstype=auto,gid=plugdev        :/dev/camera

The 1000 foot view of what we’ve just done is told automount to create a directory /media/camera and when it’s accessed to mount the device /dev/camera to it. See the man pages for mount and auto.master for more details about the setup. I can say that there’s nothing particularly flashy here. Alternately, the group ID could be floppy; just make sure that the group exists and it’s one normal users are part of before using it. Otherwise, only the root user will be able to access the device.

Finally, restart autofs like so:

/etc/init.d/autofs restart

That’s it. The camera should now be accessible in the /media/camera directory. The nice thing about this is the basic technique can be applied to just about any kind of USB device. Another nicety about automount is it takes care of unmounting the device when it’s turned off or unplugged.

Leave a Reply

Your email address will not be published. Required fields are marked *