Udev

From Dikapedia
Jump to: navigation, search

Read more: https://dev.to/enbis/how-udev-rules-can-help-us-to-recognize-a-usb-to-serial-device-over-dev-tty-interface-pbk

Udev is the mechanism used to create and name /dev device nodes corresponding to the devices that are present in the system. Udev uses matching information provided by sysfs with rules provided by the user to dynamically add the required device nodes.

Udev rule files are kept in the /etc/udev/rules.d/ directory.

I.e:

$ cat /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="12:af:cc:bc:6e:e9", NAME="eth0"

$ ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
       inet 172.31.69.228  netmask 255.255.240.0  broadcast 172.31.79.255
       inet6 fe80::1495:34ff:fe57:6f13  prefixlen 64  scopeid 0x20<link>
       ether 16:95:34:57:6f:13  txqueuelen 1000  (Ethernet)
       RX packets 43073  bytes 61456011 (58.6 MiB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 7407  bytes 453917 (443.2 KiB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ ls -al /etc/sysconfig/network-scripts/ifcfg-eth0
-rw-r--r--. 1 root root 174 Jun  3 15:52 /etc/sysconfig/network-scripts/ifcfg-eth0

$ cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Created by cloud-init on instance boot automatically, do not edit.
#
BOOTPROTO=dhcp
DEVICE=eth0
HWADDR=12:14:7e:90:ad:77
ONBOOT=yes
STARTMODE=auto
TYPE=Ethernet
USERCTL=no

To manually change it to "ens5" you can do so by following the steps below:

$ sudo vi /etc/udev/rules.d/70-persistent-net.rules 

# change NAME="eth0" to NAME="ens5"

$ sudo cat /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="12:14:7e:90:ad:77", NAME="ens5"

# Then create the ifcfg-ens5 file:
$ sudo cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-ens5

$ sudo vi /etc/sysconfig/network-scripts/ifcfg-ens5

# contents should be:
BOOTPROTO=dhcp
DEVICE=ens5              <---------- change device=ens5
HWADDR=12:14:7e:90:ad:77
ONBOOT=yes
STARTMODE=auto
TYPE=Ethernet
USERCTL=no

$ sudo reboot

$ ifconfig -a
ens5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
       inet 172.31.84.21  netmask 255.255.240.0  broadcast 172.31.95.255
       inet6 fe80::1014:7eff:fe90:ad77  prefixlen 64  scopeid 0x20<link>
       ether 12:14:7e:90:ad:77  txqueuelen 1000  (Ethernet)
       RX packets 95  bytes 12184 (11.8 KiB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 89  bytes 10593 (10.3 KiB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


You can disable predictable network interface names by adding the net.ifnames=0 option to the GRUB_CMDLINE_LINUX line in /etc/default/grub file on your source machine. I have tested this in my lab and after making the required changes (adding net.ifnames=0) on my source machine, the target machine(instance type M5) got launched with one network interface file only i.e. ifcfg-eth0.

Below are the steps to disable predictable network interface.

Note: Before making any changes on your source, I would recommend you take backup of /etc/default/grub file (maybe in your home directory).

1. Login to your source machine and disable predictable network interface names by adding the net.ifnames=0 option to the GRUB_CMDLINE_LINUX line in /etc/default/grub file, by using below command

$ sudo sed -i '/^GRUB\_CMDLINE\_LINUX/s/\"$/\ net\.ifnames\=0\"/' /etc/default/grub

2. Rebuild the Grub configuration:

$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg


Removing Mac address in udev



When you create an AMI from an existing instance, and then launch a new instance from said AMI, udev will have an existing rule for eth0. When a new instance is launched, it launches with an ENI that has a different MAC address. Because of this, the new network interface will not match against the existing udev rule. Hence your Instance status checks were failing.

For future references you can comment out the HWADDR line from etc/network-scripts/icfg-eth0 and also comment out the below line from /etc/udev/rules.d/70-persistent-net.rules before taking an AMI which would not hardcode the HWADDR for the instances you launch using the AMI. You can read more about the udev 70-persistent-net rules here[1][2].

*** /etc/udev/rules.d/70-persistent-net.rules ***
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}==“<HW-ADDR>“, NAME="eth0"    // Needs to be commented