Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Udev: Device Manager for Linux Fundamentals     - Safety testing Unix and Linux server entry succinctly (Linux)

- Why you can have JavaScript string method (Programming)

- How to make a U disk to install Ubuntu (Linux)

- BusyBox build root file system (Linux)

- Copy Recovery using RMAN repository development environment (Database)

- Two network security scanning tools under ubuntu (Linux)

- MySQL5.6 based GTID master-slave replication (Database)

- CentOS 6.5 installation using a data recovery software extundelete (Linux)

- Windows7 system using Vagrant to build Linux virtualized development environment (Linux)

- How to use awk command in Linux (Linux)

- CentOS build JDK environment (Linux)

- MySQL / MariaDB database master-slave replication (Database)

- Linux using TCP-Wrapper Service Management (Linux)

- OpenSUSE 13.1 OpenSUSE 12.3 and how to install Cinnamon 2.2 Desktop (Linux)

- Ubuntu Linux Change the PATH (Linux)

- Linux performance optimization tools perf top (Linux)

- Oracle database, some basic grammatical structures (Database)

- Red Hat Linux mount U disk (Linux)

- Keepalived + Nginx Installation and Configuration (Server)

- Ubuntu Linux use ufw or iptables firewall configuration (Linux)

 
         
  Udev: Device Manager for Linux Fundamentals
     
  Add Date : 2017-08-31      
         
         
         
  1 Introduction to udev

-------------------------------------------------- ------------------------------
Udev is a feature in the Linux 2.6 kernel that replaces the original devfs and is the default Linux device management tool. Udev runs as a daemon and manages the device files in the / dev directory by listening to the uevent from the kernel. Unlike previous device management tools, udev runs in user space rather than in kernel space.

 
2 udev advantage

-------------------------------------------------- ------------------------------

2.1 dynamic management

-------------------------------------------------- ------------------------------
When a device is added / removed, the udev daemon listens on the uevent from the kernel to add or remove device files from / dev, so udev only generates device files for the connected devices, not / dev A lot of nothingness device files.


2.2 Custom naming rules


-------------------------------------------------- ------------------------------
Through the Linux default rules file, udev in / dev / for all devices defined kernel device name, such as / dev / sda, / dev / hda, / dev / fd and so on. Since udev is run in user space, Linux users can flexibly generate identifiable device file names, such as / dev / boot_disk, / dev / root_disk, / dev / color_printer, and more with custom rules files. .


2.3 Set the permissions of the device and the owner / group

-------------------------------------------------- ------------------------------
Udev can set the device file permissions and device file owner / groups according to certain conditions. In different versions of udev, the implementation of different methods.

Ps:
1. Device file: Since this article in a more popular way to explain udev, so the device file is generally referred to in / dev /, can be used by applications and device driver interaction files. Without specifically distinguishing device files, device nodes, or device special files.


2.sysfs: sysfs is a virtual file system (/ sys) in the Linux 2.6 kernel. It exports the device and driver information from the kernel's device module to the userspace. From this file system, Linux users can obtain the properties of many devices.


3.devpath: This article devpath refers to a device in the sysfs file system (/ sys) under the relative path, the path contains the device's properties file. Most of the commands in udev are for devpath operations. For example: sda devpath is / block / sda, sda2 devpath is / block / sda / sda2.


4. Kernel device name: The name of the device in sysfs, is udev default device file name.

3 Configuring and Using udev (CentOS 6.5)


-------------------------------------------------- ------------------------------
3.1 Check the version and running of udev in CentOS 6.5

 

-------------------------------------------------- ------------------------------
[Root @ rango ~] # rpm -qa | grep -i udev

Udev-147-2.51.el6.i686
Libudev-147-2.51.el6.i686
Libgudev1-147-2.51.el6.i686
System-config-printer-udev-1.1.16-23.el6.i686

 

[Root @ rango ~] # ps -ef | grep -i udev
Root 500 1 0 09:28? 00:00:00 / sbin / udevd -d
Root 3658 500 0 09:28? 00:00:00 / sbin / udevd -d
Root 3869 500 0 09:28? 00:00:00 / sbin / udevd -d


3.2 udev configuration file

-------------------------------------------------- ------------------------------
[Root @ rango ~] # cat /etc/udev/udev.conf
#The initial syslog (3) priority: "err", "info", "debug" or its
#numerical equivalent. For runtime debugging, the daemons internal
#state can be changed with: "udevadm control - log-priority = < value>".
Udev_log = "err"
Udev_log: syslog Log level of logging, the default is err. If you change to info or debug, there will be verbose udev log is recorded.
In fact, in CentOS, in addition to the configuration file listed in the parameters udev_log, Linux users can also modify the parameters udev_root and udev_rules, but these two parameters are not recommended to modify, so not shown in udev.conf Lane. Syslog default udev log will be recorded, Linux users can only modify the log level (err, info, degub, etc.); device permissions can not be set in udev.conf, but in the rules file (*. set.


3.3 through udev set the device file permissions


-------------------------------------------------- ------------------------------
In CentOS 6.5 udev, there is no permission file, all the permissions are set by the rules file (* .rules), in the following rules file configuration process will be introduced to.


3.4 udev rules and rules files

-------------------------------------------------- ------------------------------
Rules file is the most important part of udev, the default is stored in / etc / udev / rules.d / under. All rules files must have the suffix ".rules". CentOS has default rules files that generate not only kernel device names for devices, but also symbolic links with strong identities. E.g:
[Root @ rango ~] # ls / dev / disk / by-uuid /
0E29-04AB 624d6d56-8613-4a27-ad6a-50eab26868a9
0F15181B0F15181B 9017-7DE0
1C58125758122FCE f139256a-b396-48e5-ba9c-6c13f4f8923b
281892191891E5DA
However, these link names are long and difficult to invoke, so custom rule files are often required to produce easy-to-use, well-identifiable device files or symbolic links.
Udev queries all rule files in alphabetical order for rule file names, and then manages their device files or file links for devices that match rules. Although udev does not parse subsequent rule files because a device matches a rule, the order of parsing is still important. Normally, it is recommended that you want the first rule file to be parsed. For example, create a file named /etc/udev/rules.d/10-myrule.rules and write your rules to the file, so udev will parse the system's default rules file before parsing it into your file .
In the rules file, all non-blank lines are treated as a rule, except for lines beginning with "#", but a rule can not be extended to more than one line. Rules are composed of multiple key-valuepairs separated by commas. Key-value pairs can be classified into condition-matching key-value pairs (hereinafter referred to as "matching keys") and assignment key-value pairs (hereinafter referred to as " Assignment key "), a rule can have multiple matching keys and multiple assignment keys. A match key is a condition that matches all the attributes of a device. When a device's attributes match all the matching keys in the rule, it is assumed that the rule takes effect and then the assignment of the rule is performed according to the contents of the assignment key. Here is a simple rule:
KERNEL == "sda", NAME = "my_root_disk", MODE = "0660"
KERNEL is the match key, NAME and MODE are assignment keys. The meaning of this rule is: If there is a device kernel device name sda, the conditions take effect, the implementation of the following assignment: / dev under a device named my_root_disk file, and set the device file permissions to 0660 .
It is a match key only if the operator is "==" or "! =", And is an assignment key for other operators.


All operators for udev rules

-------------------------------------------------- ------------------------------
"==": compare key, value, if equal to, then the conditions are met;
"! =": Comparison key, the value, if not equal to, then the conditions are met;
"=": Assign a value to a key;
"+ =": Assigns a key to a number of entries.
": =": Assign a value to a key, and reject all subsequent changes to that key. The purpose is to prevent the subsequent rule file assignments to the key.


Match key for udev rule

-------------------------------------------------- ------------------------------
ACTION: Event (uevent) behavior, such as: add (add equipment), remove (remove equipment).
KERNEL: Kernel device name, for example: sda, cdrom.
DEVPATH: devpath of the device.
SUBSYSTEM: The subsystem name of the device, for example: sda The subsystem is block.
BUS: The device name in the devpath, for example: usb.
DRIVER: The name of the device driver in devpath, for example: ide-cdrom.
ID: The ID of the device in devpath.
SYSFS {filename}: The contents of the file "filename" in the device's devpath.
For example: SYSFS {model} == "ST936701SS" means: If the device model is ST936701SS, the device matches the match key.
In a rule, you can set up to five SYSFS matching keys.
ENV {key}: environment variable. In a rule, you can set up matching keys for up to five environment variables.
PROGRAM: Calls an external command.
RESULT: The result of the external command PROGRAM. E.g:
 PROGRAM == "/ lib / udev / scsi_id -g -s $ devpath", RESULT == "35000c50000a7ef67" Calling the external command / lib / udev / scsi_id to query the SCSI ID of the device. If the result is 35000c50000a7ef67, the device matches Match key.


Udev important assignment key

-------------------------------------------------- ------------------------------
NAME: Device file name generated under / dev. Only the first assignment to a device will take effect, and then the matching rule will ignore the NAME assignment for that device. If there is no rule to assign values to the device NAME, udev will use the kernel device name to generate the device file.
SYMLINK: Generates symbolic links for device files under / dev /. Since udev can only generate a device file for a device, it is recommended to use a symbolic link in order not to overwrite the files generated by the system's default udev rule.
OWNER, GROUP, MODE: Set the permissions for the device.
ENV {key}: Imports an environment variable.
The value of udev and the callable substitution operator:
The keys and operators in the key-value pair are introduced, and finally the value. Linux users can freely customize the value of the udev rules file. For example: my_root_disk, my_printer. You can also reference the following substitution operators:
$ Kernel,% k: The kernel device name of the device, for example: sda, cdrom.
$ Number,% n: the kernel number of the device, for example: sda3 kernel number is 3.
$ Devpath,% p: The devpath of the device.
$ Id,% b: The ID number of the device in devpath.
$ Sysfs {file},% s {file}: The contents of the file sysfs in the device. In fact, the property value of equipment.
For example: $ sysfs {size} represents the size of the device (disk).
$ Env {key},% E {key}: The value of an environment variable.
$ Major,% M: major number of the device.
$ Minor% m: minor number of the device.
$ Result,% c: The result returned by PROGRAM.
$ Parent,% P: The device file name of the parent device.
$ Root,% r: udev_root value, the default is / dev /.
$ Tempnode,% N: The temporary device name.
%%: The symbol% itself.
$$: The symbol $ itself.
 KERNEL == "sd *", PROGRAM = "/ lib / udev / scsi_id -g -s% p", \ RESULT == "35000c50000a7ef67", SYMLINK = "% k_% c" Execution of the rule: If there is a kernel The device name starts with sd and the SCSI ID is 35000c50000a7ef67, and a symbolic link "sda_35000c50000a7ef67" is generated for the device file.

4 Develop udev rules and query device information examples

-------------------------------------------------- ------------------------------
4.1 Find the information (attributes) of the device to make udev rules

-------------------------------------------------- ------------------------------
When we set the rules for the specified device, we first need to know the properties of the device, such as the serial number of the device, disk size, vendor ID, device path and so on. Usually we can get through the following methods:
Query the sysfs file system:
As mentioned earlier, sysfs contains a lot of equipment and driver information.
For example, the SYSFS {size} of the device sda can be found in cat / sys / block / sda / size; SYSFS {model} information can be found in cat / sys / block / sda / device / model.
Udevadm info Command: (CentOS 5.3 is udevinfo command)
Udevadm info You can query the device information in the udev database. For example, use udevadm info to query model and size information for device sda:
[Root @ rango ~] # udevadm info -a -p / sys / block / sda | egrep "model | size"
  ATTR {size} == "976773168"
  ATTRS {model} == "ST500DM002-1BD14


4.2 Simple rules for udev

-------------------------------------------------- ------------------------------

Rules for generating NIC device files

-------------------------------------------------- ------------------------------
 This rule means that if the subsystem of the device is net, and the address (MAC address) is equal to "net", SYSFS {address} == "AA: BB: CC: DD: EE: FF", NAME = "public_NIC" ) To "AA: BB: CC: DD: EE: FF" to generate a device file named public_NIC for the device.
Generates a symbolic link rule for a disk of a specified size

-------------------------------------------------- ------------------------------
This rule means that if a device's subsystem is a block and its size is 71096640 (block), then the device file for the device is the same as the device's file size. If this is the case, the device's file size is 71096640 (block), and the device's file name is: SUBSYSTEM == "block" SYSFS {size} == "71096640" SYMLINK = "my_disk" Name produces a symbolic link named my_disk.
Rules for generating device files for disks with the specified serial number through external commands

-------------------------------------------------- ------------------------------
 KERNEL == "sd * [0-9]", PROGRAM == "/ lib / udev / scsi_id -g -s% p", \ RESULT == "35000c50000a7ef67", NAME + = "root_disk% n" : If the kernel device name of the device is beginning with sd (disk device), ending with a number (disk partition) and querying the SCSI_ID number of the device with an external command is "35000c50000a7ef67", a kernel number beginning with root_disk, ending with the kernel number The device file, and replace the original device file (if it exists). For example: Generate the device name / dev / root_disk2, replace the original device name / dev / sda2.
With this rule, you can keep the consistency of the system partition name in / etc / fstab without being affected by the load order or by the destruction of the disk label, which causes the system partition to not be found when the operating system starts.


4.3 Other commonly used udev command

-------------------------------------------------- ------------------------------
Udevadm test (udevadm subcommand): For a device that simulates the execution of udev once without triggering uevent and outputs the process of querying the rules file, the behavior being executed, and the result of the rule file execution.
Simulate a udev event run for the given device, and print debugoutput


The start_udev: start_dev command restarts the udev daemon and re-queries all devices for all rule files in the rule directory and then performs the behavior in the matching rules. This command is usually used to make the new rule file effective immediately:
[Root @ rango ~] # start_udev
Starting udev: [OK]
Start_udev generally no standard output, all the udev information in accordance with the configuration file (udev.conf) parameters set by the syslog records.
     
         
         
         
  More:      
 
- VMware ghost Linux card error (Linux)
- Oracle 12C with multi-column index (Database)
- Use GLOBK batch command network adjustment (Linux)
- Math objects easily overlooked but very convenient method --JavaScript (Programming)
- Using IntelliJ IDEA Import Spark Spark latest source code and compile the source code (Linux)
- CentOS6 installation configuration rsh (Server)
- How to handle special characters in JSON (Programming)
- Nginx configuration support f4v video format player (Server)
- Logging information through the web GUI (LogAnalyzer) (Server)
- Ubuntu Froxlor Server Administration panel installation (Server)
- How to Disable Linux IPv6 (Linux)
- To install Jetty server configuration in detail (Server)
- Installation and use the telnet command under CentOS 6.x (Linux)
- MongoDB learning Notes (2) basic connection example of -Nodejs and MongoDB (Database)
- Ubuntu batch scp to copy files without password (Linux)
- Android WebView use layman (Programming)
- Linux Change ssh port and disable remote root login at (Linux)
- Linux ACL permissions (Linux)
- Python 2 Chinese garbage problem solved (Linux)
- Tip: Use Cryptsetup U disk encryption (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.