Showing posts with label computers. Show all posts
Showing posts with label computers. Show all posts

Wednesday, December 30, 2009

Creating udev rules on Linux

I spent some time figuring out how to customize behaviour when hot pluging devices on linux, so I might as well post my notes in case I forget. :)

You can use udev to trigger events, to write custom linux scripts for hot plugged device.

Here's one of the more clever ideas with custom hotplug scripts using a standard usb stick as a key to unlock the computer. This will secure a workstation to only the person with the key:

http://raftaman.net/?p=300

Here's a simple example of writing a custom image processor for a camera (most cameras are already supported, but this is an example from the ground up).

Basically what is needed is:

a. figuring out the name of the device on the system
b. figuring out the full signature of the device
c. writing a rule to match the signature
d. writing a program that will be triggered

First you have to figure out the name of the device on the system. When the device is unplugged, take a snapshot of all the device on the system. IMO an easy way is to look at the /sys/ directory. It will contain nodes for all hotplugged devices. First take a snapshot when the device is unplugged.

find /sys > /tmp/dev_out.txt

Then plug in the device, and take another snapshot:

find /sys > /tmp/dev_in.txt

Now you can compare the differences in the two snapshots. Note that one physical device can actually be several devices (for example if it has multiple partitions). You will want a short name.

diff /tmp/dev_out.txt /tmp/dev_in.txt

An alternate way to find the name of device nodes, run this, and plug in device.

udevadm monitor

The name might look like /sys/path/to/device. Then get complete signature of the device:

udevadm info -a -p /sys/path/to/device

Udevadm info starts with the device specified by the devpath and then walks up the chain of parent devices... The complete signature of the device looks like a bunch of gibberish, but it's just key/value pairs:


looking at device '/devices/pci0000:00/0000:00:1f.4/usb2/2-2/2-2:1.0/host13/target13:0:0/13:0:0:0/block/sdc':
KERNEL=="sdc"
SUBSYSTEM=="block"
DRIVER==""
ATTR{range}=="16"
ATTR{ext_range}=="256"
ATTR{removable}=="1"
ATTR{ro}=="0"
ATTR{size}=="429569"
ATTR{capability}=="53"
ATTR{stat}==" 87 996 1083 1820 0 0 0 0 0 1016 1820"

looking at parent device '/devices/pci0000:00/0000:00:1f.4/usb2/2-2/2-2:1.0/host13/target13:0:0/13:0:0:0':
KERNELS=="13:0:0:0"
SUBSYSTEMS=="scsi"
DRIVERS=="sd"
ATTRS{device_blocked}=="0"
ATTRS{type}=="0"
ATTRS{scsi_level}=="0"
ATTRS{vendor}=="VTech "
ATTRS{model}=="Kidizoom "
ATTRS{rev}=="1.21"
ATTRS{state}=="running"
ATTRS{timeout}=="30"
ATTRS{iocounterbits}=="32"
ATTRS{iorequest_cnt}=="0x28a"
ATTRS{iodone_cnt}=="0x28a"
ATTRS{ioerr_cnt}=="0x8"
ATTRS{modalias}=="scsi:t-0x00"
ATTRS{evt_media_change}=="0"
ATTRS{queue_depth}=="1"
ATTRS{queue_type}=="none"
ATTRS{max_sectors}=="240"

looking at parent device '/devices/pci0000:00/0000:00:1f.4/usb2/2-2/2-2:1.0/host13/target13:0:0':
KERNELS=="target13:0:0"
SUBSYSTEMS=="scsi"
DRIVERS==""

...

These two lines should basically describe the device:

ATTRS{vendor}=="VTech "
ATTRS{model}=="Kidizoom "

Forming a rule is basically a matter of picking out a few lines from the complete signature and putting them all on one line seperated with commas. Files go under

/etc/udev/rules.d/

The name of the rule starts with a number (order executed) and ends with .rules
There can only be at most one parent in matching rules.
RUN will trigger a program to execute after device is mounted
ACTION can be "add" or "remove"

# EXAMPLE:
# FILE /etc/udev/rules.d/99-camera-copy.rules
# This will trigger /path/to/your/program
# when the device is plugged in
ACTION=="add", SUBSYSTEM=="block", ATTRS{vendor}=="VTech ", ATTRS{model}=="Kidizoom ", ATTRS{rev}=="1.21", RUN+="/path/to/your/program"

Also, udev will pause until the program called is finished. You can also run the program in the background to return immediately (recommended for longer processes). To detach bash script, enclose it in braces and end with &:


#!/bin/bash
{
echo "hello there" >> /tmp/udev_test.log
} &

All programs will be run as root user (full permissions). To lauch graphical programs as a regular user:

#!/bin/bash
{
# what program to run?
run=picasa
user=`who grep :0\) cut -f 1 -d ' '`
export DISPLAY=:0.0
su $user -c "xhost local:$user; $run"
} &


For more information, here's an older writeup (the tool "udevinfo" has changed to "udevadm info")

http://www.reactivated.net/writing_udev_rules.html


Also, thank you Rcspam for an example of using the kidizoom on Ubuntu 10.04+:
http://rcspamsblogen.blogspot.com/2011/01/kidizoom-and-ubuntu-maverick.html
(English)

Monday, November 30, 2009

Microsoft Patches -- Conflict of Interest

My wife's mother was having trouble with Windows Vista (64 bit). She said that  Windows installed a couple system updates, then stopped working. Sure enough, I restored the computer to a previous configuration, got everything working. Then Microsoft installed ten updates again, which completely broke the system. It wouldn't even boot. This took several hours to fix.

Seems like this was a widespread problem:

http://www.pcworld.com/article/183335/latest_microsoft_patches_cause_black_screen_of_death.html

I think it's questionable on Microsoft's part, why this broke. Especially since it occurred during the start of the biggest spending week of the year, right before Black Friday. 

Coincidentally, Microsoft just introduced a new product they are wanting to sell: Windows 7. It certainly would be a clever way to drive up sales of new PC's during the spending spree. If they break Vista and XP, a lot of people would just buy a new computer rather than paying the cost of fixing an old one.

Or maybe they just don't know what they are doing.   Neither explanation is good.  :-)

At the very least, there's an inherent conflict of interest in that Microsoft is able to modify software that someone purchased, at the same time they want people to stop using it and buy something else. Since like any company, they assign priorities in terms of what makes the most money.  :-)