Udev usage-create a device node in linux

Source: Internet
Author: User
Tags symlink
In Linux, all exist in the form of device files. In earlier Linux versions, the dev directory contains all possible device files. However, it is difficult for Linux users to find matching device files in these large numbers of device files. Now udev only generates device files for devices connected to the Linux operating system. And ude

In Linux, all exist in the form of device files. In earlier Linux versions, the/dev directory contains all possible device files. However, it is difficult for Linux users to find matching device files in these large numbers of device files. Now udev only generates device files for devices connected to the Linux operating system. In addition, udev can define a udev rule (rule) to generate device files that match device properties, these device attributes can be the kernel device name, bus path, vendor name, model, serial number, or disk size.

Dynamically and automatically manage device information: When a device is added/deleted, the udev daemon listens to uevent events from the kernel to add or delete Device Files under/dev, therefore, udev can only generate device files for connected devices, instead of generating a large number of Device Files under/dev like the 2.4 kernel. In addition, you can use this function to run external programs when a device is added, such as automatically disabling the touchpad when the mouse is added.
Use custom naming and device management: Using the Udev rule file, udev defines kernel device names for all devices in/dev/, such as/dev/sda,/dev/hda, And/dev/fd. Because udev runs in user space, Linux users can perform subsequent operations on this information. For example, they can generate a human device identifier through a custom rule file, for example,/dev/my_disk and/dev/nameusb can also modify the user group permissions of parameter members.

First, you need to know
• Sysfs: sysfs is a Virtual File System (/sys) in the Linux 2.6 kernel ). It exports device and driver information from the kernel device module to the user space (userspace ). In this file system, Linux users can obtain the attributes of many devices.
• Devpath: The devpath in this document refers to the relative path of a device under the sysfs File System (/sys), which contains the property file of the device. Majority in udevCommandAll operations on devpath. For example, the devpath of sda is/block/sda, And the devpath of sda2 is/block/sda/sda2.
• Kernel device name: the name of the device in sysfs, which is the default device file name used by udev.

Udev main configuration file

The main udev configuration file is/etc/udev. conf. This file is usually very short. It may only contain comments starting with "#" and there are several options:

Udev_root =/dev/# sets the absolute path, which is equivalent to creating the root of the chroot. Udev_rules =/etc/udev/rules. d/# rule storage address udev_log = err # log input level

 

Udev rule configuration file instance
 

The default rule configuration file is stored in/etc/udev/rules. d /.EdBy default, Hat has established some rules for devices and rules written by some hardware companies.
Go to the directory. You can see the configuration file prefixed with two digits. You can use vi to enter the configuration file and view it. One row is a rule. The default value is from small to large, these indicate the order in which the values take effect.

Before using udev to write rules, let's look at an example.

KERNEL = sd *, PROGRAM =/lib/udev/scsi _Id-G-s % p, RESULT = 123456, SYMLINK = % k _ % c

 

Execution of this rule: if a kernel device name starts with sd and the scsi id is 123456, a symbolic link "sda_123456" is generated for the device file ". % p % k % c please refer to"Udev value and callable replacement Operator"
 

 

Udev Rule Configuration File

In the rule file, except for the rows (comments) starting with "#", all non-empty rows are regarded as one rule, but one rule cannot be extended to multiple rows. The rules are composed of multiple key-value pairs (pairs) separated by commas (,). key-value pairs can be divided into condition matching key-value pairs (hereinafter referred to as "Matching key ") A rule can have multiple matching keys and multiple value assignment keys. A matching key is a condition that matches all the properties of a device. When a property of a device matches all the matching keys in the rule, the rule takes effect and then follows the value assignment key, assign a value to execute this rule.
The rules in the Rule file consist of a series of key/value pairs, which are separated by commas.

These configurations can also be seen in the above example, but I think you may wonder why the KERNEL is the matching key and the NAME and MODE are the value assignment keys? This is determined by the operator in the middle.

Only when the operator is "=" or "! = ", It is the matching key; if it is another operator, it is the value assignment key.

The following table describes the matching key and value assignment key operators:

Operator matching or value assignment t Interpretation
----------------------------------------
= Matching equal comparison
! = Unequal matching comparison
= Assign a specific value to the key, which can overwrite the previous value.
+ = Assign a value to append a specific value to an existing key.
: = Assign a specific value to the key, and the subsequent rules cannot overwrite it.

Udev rule matching key:

Key meaning ---------------------------------------- ACTION of the ACTION event (uevent), for example:Dd(Add device), remove (delete device ). The name of the device seen by the KERNEL in the KERNEL. For example, sd * indicates that any SCSI disk device devpath kernel device is recorded, for example,/devices/* SUBSYSTEM name. For example, the sda SUBSYSTEM is block. The name of the BUS, such as the name of the IDE or USB driver, such as the name of the ide-CdRomID is independent of the property value of SYSFS {value} sysfs, which can represent any ENV {key} environment variable and any PROGRAM executable, if the program returns 0, the key is considered true (TrUe) the standard output returned by a PROGRAM call on RESULT. NAME of the device file created according to this rule. Note: Only the NAME description in the first line is valid and is ignored later. If you want to use more than two names to access a device, consider the SYMLINK key. SYMLINK is a symbolic link generated for device files under/dev. Because udev can only generate one device file for a device, we recommend that you use Symbolic Links to avoid overwriting the files generated by the system's default udev rules. OWNER: the GROUP where the device file belongs to the GROUP. The permission of the MODE device file. The program list LABEL executed by RUN for the device in octal MODE is the name LABEL used for internal control in the configuration file (the following GOTO Service) GOTO jump to the matching rule (identified by LABEL ), similar to GOTOIMPORT {type} in programming language, it imports the Rule Set generated after a file or program is executed to the current file WAIT_FOR_SYSFS and waits for the creation of a specific device file. It is mainly used for timing and dependency issues. PTIONS-specific options: last_rule executes the terminal rule for such devices; ignore_device ignores the current rule; ignore_remove ignores the next and removes the request. All_partitions creates device files for all disk partitions.

 

Udev value and callable replacement Operator

The key-value pairs and operators are all described, and finally the value ). Linux users can customize the value of the udev rule file at will. For example, my_root_disk and my_printer. You can also reference the following replacement operators: ------------------------------------------ $ kernel, % k: the device's kernel device name, such as: sda, cdrom. $ Number, % n: the kernel number of the device. For example, the kernel number of sda3 is 3. $ Devpath, % p: devpath of the device. $ Id, % B: ID of the device in devpath. $ Sysfs {File}, % S {file}: file Content in the sysfs of the device. It is actually the property value of the device. For example, $ sysfs {size} indicates the size of the device (Disk. $ Env {key}, % E {key}: the value of an environment variable. $ Major, % M: The major number of the device. $ Minor % m: the minor number of the device. $ ReSuLt, % c: Result $ parent returned by PROGRAM, % P: Device File Name of the parent device. $ Root, % r: udev_root value. The default value is/dev /. $ Tempnode, % N: temporary device name. %: Symbol % itself. $: Symbol $ itself.

 

Query of information required by udev rules

Common commands for checking the above key information

Udevinfo-a-p $ (udevinfo-q path-n/dev/sda1) the preceding command uses udevinfo twice: the first time is to return the sysfs device path (which is usually different from the path of the Linux Device File name --/dev/hda -- we see). The second time is to query the device path, the result is that udevinfo-a-p/sys/class/net/eth0scsi_id-g-s/block/sdascsi_id-g-x-s/block/sda/ sda3ata_id/dev/hda

Related Article

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.