Udev learning Summary

Source: Internet
Author: User
Tags symlink

Udev dynamic device management

 

After reading a lot of things summarized by our predecessors, it is time to make a summary to express the udev in my eyes.

What is udev?

Udev is a method for generating device file nodes. To replace the devfs generated before the 2.6 kernel, the biggest difference is that udev is a user space program, so

Hardware platform independent. When the system discovers a new hardware, udev generates corresponding device files for the hardware under/dev according to flexible udev rules.

 

What are the requirements of udev?

Udev requires a fixed hardware device name to facilitate user access. For example, there are two USB devices: USB storage and USB camera. Due to the kernel processing mechanism, if a USB storage device is first inserted, it is identified as SDA, and then the inserted USB camera is identified as SDB. If a USB camera is inserted first, it is recognized as SDA; therefore, users are confused and do not know which device is their own. After you use udev to specify rules, USB storage devices and cameras can be recognized as device nodes such as/dev/USB storage and/dev/USB camera no matter when they are inserted, it facilitates user identification.

 

Udev rules

The most important thing in udev is to build udev rules. It is precisely by specifying rules that devices can orderly appear under/dev;

 

The rule contains two elements: the matching item and the assignment item. The udev rule can be summarized in one sentence:

If (A & B & C...) Then (D & E & F ...)

Matching items include: kernel (the name recognized by the kernel by the device), Action (the device has a specific behavior), and so on. The matching items are specified by "=". The assigned values include: name (specify the name of the device under/Dev)

The best way to learn rules is through the example:

Kernel = "*", owner = "root" group = "root", mode = "0600"
# After loading any device in the kernel, set the root owner, root user group, and the access permission to 0600.

Kernel = "tty", name = "% K", group = "tty", mode = "0666", Options = "last_rule"

# Tty device, named % K (% kernel device in the kernel default name), set to TTY user group, 0666 access permission, and then all rules acting on TTY are invalid
Kernel = "SCD [0-9] *", symlink + = "CDROM-% K"

# Kernel = "SCD [0-9] *", represents the SCSI CD-ROM drive. It creates a pair of device symbolic connections: CDRom and CDROM-% K.
Kernel = "HD [A-Z]", bus = "ide", sysfs {removable} = "1 ", sysfs {Device/media }== "CDROM", symlink + = "CDROM-% K"

#The START kernel = "HD [A-Z]", indicating the ata cdrom Drive. This rule creates a symbolic connection with the above rule. The ata cdrom driver requires sysfs value to distinguish other ata devices, because scsi cdrom can be uniquely identified by the kernel.
Action = "add", subsystem = "scsi_device", run + = "/sbin/modprobe SG"

# Starting with action = "add", it tells udev to add/sbin/modprobe SG to the command list. When any SCSI device is added to the system, these commands will be executed. The result is that the computer should add the SG kernel module to detect new SCSI devices.

 

How to add udev rulesFor example:

The udev rule matches the information that uniquely identifies the target device. Because udev obtains the device information from sysfs, you can use the udevinfo command to query the feature information of loaded devices;

Generally, before creating a udev rule for device A, the device can be loaded to the system and then run

Udevinfo-a-p $ (udevinfo-Q path-N/dev/)

The feature information of device A and its parent device can be obtained;

Then, you can select information that uniquely identifies device A as a match;

To identify a hard disk SDA in the system,

Can run

Udevinfo-a-p $ (udevinfo-Q path-N/dev/SDA)

 

The result is as follows:

[Root @ localhost rules. d] # udevinfo-a-p $ (udevinfo-Q path-N/dev/SDA)

Udevinfo starts with the device specified by the devpath and then
Walks up the chain of parent devices. It prints for every device
Found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
And the attributes from one single parent device.

Looking at device '/block/SDA ':
Kernel = "SDA"
Subsystem = "Block"
Driver = ""
ATTR {capability} = "12"
ATTR {stat }= = "112812 30568 3171292 589470 137364 398132 4287050 2593137 0 567749 3182822"
ATTR {size }== "104857600"
ATTR {removable} = "0"
ATTR {range }= = "16"
ATTR {Dev }= = "8:0"

Looking at Parent device '/devices/pci0000: 00/0000: 00: 10.0/host0/target0: 0: 0/0: 0: 0: 0 ':
Kernels = "0: 0: 0: 0"
Subsystems = "SCSI"
Drivers = "SD"
Attrs {modalias} = "SCSI: t-0x00"
Attrs {ioerr_cnt} = "0xc"
Attrs {iodone_cnt} = "0x3d15f"
Attrs {iorequest_cnt} = "0x3d15f"
Attrs {iocounterbits }== "32"
Attrs {timeout} = "60"
Attrs {state} = "running"
Attrs {rev }= = "1.0"
Attrs {model }== "VMWare virtual S"
Attrs {vendor} = "VMWare ,"
Attrs {scsi_level }== "3"
Attrs {type }== "0"
Attrs {queue_type }== "simple"
Attrs {queue_depth} = "32"
Attrs {device_blocked} = "0"

Looking at Parent device '/devices/pci0000: 00/0000: 00: 10.0/host0/target0: 0: 0 ':
Kernels = "target0: 0: 0"
Subsystems = ""
Drivers = ""
Attrs {uevent} = ""

Looking at Parent device '/devices/pci0000: 00/0000: 00: 10.0/host0 ':
Kernels = "host0"
Subsystems = ""
Drivers = ""
Attrs {uevent} = ""

Looking at Parent device '/devices/pci0000: 00/0000: 00: 10.0 ':
Kernels = "bytes :00:10. 0"
Subsystems = "PCI"
Drivers = "mptspi"
Attrs {msi_bus }= ""
Attrs {broken_parity_status} = "0"
Attrs {enable} = "1"
Attrs {modalias }== "pci: v20171000d00000030sv00000000sd00000000bc01sc00i00"
Attrs {local_cpus} = "ffffffff"
Attrs {IRQ} = "19"
Attrs {class} = "0x010000"
Attrs {subsystem_device} = "0x0000"
Attrs {subsystem_vendor }== "0x0000"
Attrs {Device} = "0x0030"
Attrs {vendor} = "0x1000"

Looking at Parent device '/devices/pci0000: 00 ':
Kernels = "pci0000: 00"
Subsystems = ""
Drivers = ""
Attrs {uevent} = ""

 

 

 

 

 

 

How to add udev support to Embedded Systems

 

 

 

 

 

 

Official udev resources:

Man udev: it can be easily viewed in shell. In addition to no examples, it involves all aspects of udev and is suitable for querying when forgotten;

Udev Primer: It mainly includes how to guide your system to start udev and simple udev rules;

Udev official homepage: You can obtain the udev source code and related documents;

Writing udev rules: describes in detail how to build udev rules. For Chinese translation, see the blog post "udev Promotion (provided by professionals)".

Among them, A, B, and C are called matching items, that is, conditions; D, E, and F are called value assignment items, that is, after the conditions are met, corresponding operations are performed;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.