Udev 란

Udev 는 리눅스 커널을 위한 장치 관리자이다. devfsd와 hotplug를 계승하는 udev는 주로 /dev 디렉터리의 장치 노드를 관리한다.
CentOS/RHEL/OL/Rocky 에서 systemd-udevd daemon 으로 동작된다.

장치 정보 상세 보기

Udev Rule 을 사용하기 위해선 udevadm 명령을 통해 장치의 상세 정보를 확인해야된다.

아래는 장치의 기본 정보를 확인하는 방법이다.

# udevadm info /dev/sdb
P: /devices/pci0000:80/0000:80:08.3/0000:83:00.0/ata2/host1/target1:0:0/1:0:0:0/block/sdb
N: sdb
S: disk/by-id/ata-Samsung_SSD_860_PRO_2TB_S5Gqwerasdfzxfc
S: disk/by-id/wwn-0x500123456789012
S: disk/by-path/pci-0000:83:00.0-ata-2.0
E: DEVLINKS=/dev/disk/by-id/ata-Samsung_SSD_860_PRO_2TB_S5Gqwerasdfzxfc /dev/disk/by-id/wwn-0x500123456789012 /dev/disk/by-path/pci-0000:83:00.0-ata-2.0
E: DEVNAME=/dev/sdb
E: DEVPATH=/devices/pci0000:80/0000:80:08.3/0000:83:00.0/ata2/host1/target1:0:0/1:0:0:0/block/sdb
E: DEVTYPE=disk
...생략

P 는 DEVPATH,
N 는 NAME,
S 는 SUBSYSTEM,
E 는 ENV 를 의미한다.

  • 참고: man udev
       # man udev
       ... 생략
         SUBSYSTEM
             Match the subsystem of the event device.
         DEVPATH
             Match the devpath of the event device.
         ENV{key}
             Match against a device property value.
         ATTR{filename}
             Match sysfs attribute values of the event device. Trailing whitespace in the attribute values is ignored unless the specified match value itself contains trailing whitespace.
         NAME
             Match the name of a network interface. It can be used once the NAME key has been set in one of the preceding rules.
         KERNEL
             Match the name of the event device.
    

아래는 장치의 상세 정보 확인 방법이다.

# udevadm info -a /dev/sdb
 
Udevadm info 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 '/devices/pci0000:80/0000:80:08.3/0000:83:00.0/ata2/host1/target1:0:0/1:0:0:0/block/sdb':
    KERNEL=="sdb"
    SUBSYSTEM=="block"
    DRIVER==""

...생략

  looking at parent device '/devices/pci0000:80/0000:80:08.3/0000:83:00.0/ata2/host1/target1:0:0/1:0:0:0':
    KERNELS=="1:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{rev}=="2B6Q"
    ATTRS{type}=="0"
    ATTRS{wwid}=="naa.500123456789012"
    ATTRS{scsi_level}=="6"
    ATTRS{model}=="Samsung SSD 860 "
    ATTRS{state}=="running"
    ATTRS{queue_depth}=="31"

...생략

Udev Rule 작성

장치 상세 정보를 이용하여 아래와 같은 Udev Rule 작성 할 수 있다.

DISK 예제

아래 내용은 Udev rule 을 이용하여 DISK 속성을 설정하는 방법이다.

# vi /etc/udev/rules.d/99-ssd.rules
ACTION=="add|change", SUBSYSTEM=="block", ATTRS{model}=="Samsung SSD*", ATTR{queue/read_ahead_kb}="2048"
ACTION=="add|change", SUBSYSTEM=="block", ATTRS{model}=="Samsung SSD*", RUN+="/sbin/hdparm -W 0 /dev/%k"

위와 같은 방법으로 read_ahead_kb 와 같은 장치의 Parameter 및 명령어를 통한 Write Cache OFF 와 같은 설정을 적용 할 수 있다.
참고로 %k 는 장치의 Kernel name 을 말하는 것이다.

Ethernet Interface 예제

아래 내용은 Udev rule 을 이용하여 Ethernet Interface 이름을 변경하는 방법이다.

# vi /etc/udev/rules.d/70-persistent-network.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="11:22:33:44:55:66", KERNEL=="ens*", NAME="eth0"

Mac Address 가 11:22:33:44:55:66 이고 장치의 Kernel name 이 ens* 인 장치를 add 하는 ACTION 이 발생될 때, NAME 을 eth0 로 변경하는 Rule 이다.

위와 같은 예제는 기본적으로 udevadm 으로 확인한 정보 기반으로 작성하는 것을 권고한다.

Udev Rule 이 적용이 안되는 경우

Udev Rule 이 정상적으로 적용이 안되는 경우, systemd-udevd 의 로그를 확인하는 것이 좋다.

아래는 Ethernet Interface 변경 할 때, 제일 많이 발생되는 오류이며 systemd-udevd 로그를 통해 확인이 가능하다.

Jan 01 09:00:00 test-node1 systemd-udevd: Error changing net interface name 'eth2' to 'eth1': File exists
Jan 01 09:00:00 test-node1 systemd-udevd: could not rename interface '8' from 'eth2' to 'eth1': File exists

특정 Interface 를 eth1로 이름을 변경하려고 했으나 eth1 의 이름을 Interface ‘8’ 이 사용중이라 변경이 불가능한 이슈이다.
이런 이슈는 eth1 이름을 사용하는 Interface ‘8’ 을 다른 이름으로 변경하고(eth1 이 아닌 다른 이름) eth1 로 변경하고 싶은 Interface 를 변경하면 된다.

쉽게 정리하면 replace 는 불가능하며 name 은 unique 해야된다.

Udev Reload

아래 명령어를 통해 Udev Rule 를 Reload 할 수 있습니다.

# udevadm control --reload

Udev Trigger

아래 명령어를 통해 Udev Rule 를 적용 할 수 있습니다.

# udevadm trigger --action=add 
  or
# udevadm trigger --action=change

참고 자료

chhanz's profile image

chhanz

2022-09-19

Read more posts by this author