Jump to content

udevilicious


Recommended Posts

!--=[ udevilicios

!--=[ by znx

 

Original the /dev directory was full to the gills of every possible

combination, this made for a huge amount of wasted space. Lots of devices

that you would never need nor require and the possibility that the device

doesn't exist at all. Now due to the fact that ever increasing amounts of

hardware were getting supported (yes Linux is growing ;)) the /dev

directory was growing to near ridiculous levels.

 

Enter devfs. This was the first attempt at a solution to the problem,

created by Richard Gooch, devfs allowed the device entries to be made on

the fly. Connecting a device would then create the device node inside the

/dev directory, disconnecting would then remove the entry. This mean that

the huge difficultly with over population of the directory. So you think

why udev? Well unfortunately devfs had some problems along the way (nothing

that I ever encountered) but bugs that couldn't get solved meant that

another solution was required. So this is where udev steps in, written by

Greg Kroah-Hartman, it was a user space solution removing the need for more

code inside the kernel (hence user dev!).

 

One of the great features of udev is something called persistent naming.

Normally devices are named in the order that they are connected in. If you

use any USB device you'll already know this, the first time you find you

camera as /dev/sda the next as /dev/sdb. This makes it a difficult to

handle these devices via /etc/fstab. Udev solves all this by allowing the

user to specify a device name. This means you can name your camera

/dev/camera, no more hunting.

 

All of the rules that we will create will be made in the file

/etc/udev/rules.d/10-udev.rules. All the default rules are in

/etc/udev/rules.d/50-udev.rules, don't add any here, they'll get

overwritten.

 

OK so lets find our device. I'm going to use my 256Mb USB stick in this

example. So stick the drive into the port and lets look throw the output of

dmesg.

 

# dmesg
...
usb 1-1: new full speed USB device using uhci_hcd and address 2
SCSI subsystem initialized
Initializing USB Mass Storage driver...
scsi0 : SCSI emulation for USB Mass Storage devices
usbcore: registered new driver usb-storage
USB Mass Storage support registered.
usb-storage: device found at 2
usb-storage: waiting for device to settle before scanning
 Vendor: USB       Model: Flash Drive       Rev: 1.12
 Type:   Direct-Access                      ANSI SCSI revision: 00
usb-storage: device scan complete
SCSI device sda: 507901 512-byte hdwr sectors (260 MB)
sda: Write Protect is off
sda: Mode Sense: 37 00 00 00
sda: assuming drive cache: write through
SCSI device sda: 507901 512-byte hdwr sectors (260 MB)
sda: Write Protect is off
sda: Mode Sense: 37 00 00 00
sda: assuming drive cache: write through
/dev/scsi/host0/bus0/target0/lun0: unknown partition table
Attached scsi removable disk sda at scsi0, channel 0, id 0, lun 0
#

 

I can see that it has mounted this device under /dev/sda. You can also see

that the drive hasn't had any partitions detected (else we would have sda1,

sda2 etc). So now we need to discover where the information is stored

inside the /sys directory. After that we can read through the information

inside the /sys directory.

 

# udevinfo -q path -n /dev/sda
/block/sda
# udevinfo -a -p /block/sda | less
device '/sys/block/sda' has major:minor 8:0
 looking at class device '/sys/block/sda':
   SYSFS{dev}="8:0"
   SYSFS{range}="16"
   SYSFS{removable}="1"
   SYSFS{size}="507901"
...

 

There is a lot of data in there and what we need to do now is pick the

correct keys to identify our device uniquely. So on with the rule (remember

put this into /etc/udev/rules.d/10-udev.rules):

 

BUS="usb",KERNEL="sd[a-z]",SYSFS[product]="USB Flash Drive",NAME="%k",SYMLINK="flash"

 

Let's walk through this:

 

BUS="usb"                        - Connecting on the USB bus
KERNEL="sd[a-z]"                 - How the kernel sees your device
SYSFS[product]="USB Flash Drive" - From the output of of udevinfo
NAME="%k"                        - Continue to create the default device name
SYMLINK="flash"                  - Link to /dev/flash

 

You can add lots of other SYSFS entries if you need to tighten up the

description of the device. Using "USB Flash Device" or something equally as

generic can lead to more than device matching the rule. Instead look for

information on the particular manufacture and model.

 

Now when the device is connected the symlink will magically appear. And

then you can use it. The entry can be made in /etc/fstab allowing quick and

easy access to your device. A really nice rule to show you the power of

udev is this:

 

KERNEL="eth*",SYSFS[address]="00:d0:b7:c5:48:30",NAME="inet"
KERNEL="eth*",SYSFS[address]="00:d0:b7:c5:48:31",NAME="lan"

 

When the interface is configured you will get /dev/lan and /dev/inet. Just

think of your firewall rules:

 

iptables -A input -i lan -p tcp -j accept
iptables -A input -i inet -p tcp -j drop

 

This is far easier to understand which interface you are configuring.

 

Well thats it.. have fun!

Link to post
Share on other sites
  • 1 year later...

A question came from Fujita as to how you can obtain information about network interfaces, so:

 

udevinfo -a -p /sys/class/net/eth0/

 

^_^ .. keeping the udev alive!!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...