Solution to the problem of failure to rename the eth0 network card caused by the cloud-init component in the Galaxy Kirin server system

Foreword: Add biosdevname=0 net.ifnames=0 in grub and rename the network card ensxxx or enpxxx to eth0. The problem does not take effect after restarting the system. The following documents are analyzed and processed using the Galaxy Kirin V10 SP2 X86 architecture. This problem does not occur on a certain system version, it will occur as long as cloud-init is installed.

1. Basic information

1.1 System version
############## Kylin Linux Version #################
Release:
Kylin Linux Advanced Server release V10 (Sword)

Kernel:
4.19.90-25.31.v2101.ky10.x86_64

Build:
Kylin Linux Advanced Server
release V10 (SP2) /(Sword)-x86_64-Build09/20210524
#################################################
1.2 cloud-init version
rpm -qa | grep cloud-init
cloud-init-19.4-6.p05.ky10.noarch

2. Investigation process

2.1 Check messages logs and dmesg logs
(1)dmesg | grep eth0
[ 5.92788] virtio_net virtio0 enp1s0: renamed form eth0
#dmesg中发现网卡名已经将enp1s0改成了eth0了


(2) cat /var/log/messages | grep eth0
systend-udevd[346]: eth0: Network interface 2 is renamed fron 'eth0’ to 'enp1s0'
kernel: [3.651584] virtio_net virtio0 enp1s0: renaned from etho
systemd-udevd[332]: eth0: Network interface 2 is renaned fron 'etho’ to 'enp1s0'
kernel: [4.254616] virtio_net virtio0 enp1s0: renamed from eth0
systemd-udevd[318]: eth0: Network interface 2 is renamed from 'etho’ to 'enp1s0'
kernel: [2.446198] virtio_net virtio0 enp1s0: renaned fron eth0
systend-udevd[301]: eth0: Network interface 2 is renamed from 'eth0’ to 'enp1s0'
kernel: [5.927988] virtio_net virtio0 enp1s0: renaned from eth0
systend-udevd[5931: eth0: Network interface 2 is renamed from 'eth0' to "enp1s0'
#messages中systemd-udevd发现网卡名又将eth0改为enp1s0了
2.2 View udev rules

After reproducing the problem in many scenarios, I found that it was caused by udev rules in the system. The udev rule files were generated by themselves instead of manually created.

具体文件路径:
/etc/udev/rules.d/70-persistent-net.rules
2.3 View the udev files involved in the cloud-init component
[root@localhost ~]# grep -nr "70-persistent-net.rules" /usr/
匹配到二进制文件 /usr/lib/python3.7/site-packages/cloudinit/net/__pycache__/eni.cpython-37.opt-1.pyc
匹配到二进制文件 /usr/lib/python3.7/site-packages/cloudinit/net/__pycache__/eni.cpython-37.pyc
匹配到二进制文件 /usr/lib/python3.7/site-packages/cloudinit/net/__pycache__/sysconfig.cpython-37.opt-1.pyc
匹配到二进制文件 /usr/lib/python3.7/site-packages/cloudinit/net/__pycache__/sysconfig.cpython-37.pyc
/usr/lib/python3.7/site-packages/cloudinit/net/eni.py:347:            'netrules_path', 'etc/udev/rules.d/70-persistent-net.rules')
/usr/lib/python3.7/site-packages/cloudinit/net/sysconfig.py:312:            'netrules_path', 'etc/udev/rules.d/70-persistent-net.rules')
/usr/lib/dracut/modules.d/95udev-rules/module-setup.sh:59:    [[ $hostonly ]] && inst_rules 70-persistent-net.rules

The following are the files found involving 70-persistent-net.rules
/usr/lib/python3.7/site-packages/cloudinit/net/eni.py
/usr/lib/python3.7/site-packages/cloudinit/net/sysconfig.py

2.4 Solutions
(1) Add biosdevname=0 net.ifnames=0 parameter in grub boot

/etc/default/grub GRUB_COMLINE_LINUX tailing addition biosdevname=0 net.ifnames=0

(2) Update grub.cfg file
[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS
BIOS
#如果输出是UEFI则表示是UEFI启动
#如果输出是BIOS则表示是传统的Legacy BIOS启动

传统的Legacy BIOS启动文件路径:
/boot/grub2/grub.cfg
UEFI启动文件路径:
/boot/efi/EFI/kylin/grub.cfg

UEFI引导执行:grub2-mkconfig -o /boot/efi/EFI/kylin/grub.cfg
Legacy启动执行:grub2-mkconfig -o /boot/grub2/grub.cfg

The following is the modified grub file and the output of executing the grub2-mkconfig command:
Insert image description here

(3) Turn off cloud-init’s network takeover

​ Add the last line of the /etc/cloud/cloud.cfg configuration file:
​ network:
​ config: disabled
#There is an indentation problem, please pay attention when writing
The following is an example after the modification is completed:
Insert image description here

(4) Delete 70-persistent-net.rules
rm -f  /etc/udev/rules.d/70-persistent-net.rules
(5) Check whether there are 70-persistent-net.rules in the initramfs-xxx.img file

initramfs is the abbreviation of "initial ram file system". It is a temporary file system that is usually loaded into memory during the boot process to help the system initialize the hardware, load necessary drivers and other necessary functions so that the system can continue to load the real root file system.
This file is under /boot. xxx represents the specific kernel version. For example, the complete file name is: initramfs-4.19.90-25.31.v2101.ky10.aarch64.img

lsinitrd /boot/initramfs-$(uname -r).img | grep rules.d | grep 70-persistent-net.rules

If you can filter out the results by executing lsinitrd, you need to update initramfs-xxx.img. Make sure that the /etc/udev/rules.d/70-persistent-net.rules file is deleted before updating.
The update command is as follows:

dracut -f 
(6) Delete the network card configuration file

ifcfg-ens3 (the configuration file name is modified according to the actual situation, ens3 is just an example)

rm -f /etc/sysconfig/network-scripts/ifcfg-ens3
(7) Restart the test
reboot

Guess you like

Origin blog.csdn.net/weixin_45754407/article/details/134827432