目前服务器领域centos7基本是主流的操作系统,而linux相对于Windows来说,软硬件方面的支持是差很多的,在硬件方面来说,以一个免驱的网卡为例,window xp可能不会自动识别到,但Windows10基本都会自动识别到,简简单单的即插即用。根本原因在Windows随着版本的升级,内置的各类网卡驱动更多,也因此Windows系统越来越大。
tips:什么是驱动?
驱动指的是操作系统层面的一组程序,该程序告诉操作系统的内核如何使用硬件,例如,网卡的启停,网卡的IP绑定,网卡的路由路径这些都是网卡驱动通知内核调用相关的程序,例如,ifconfig程序,做相应的动作来完成的。
而linux操作系统的内核一般是不集成过多的网卡的,主要在于linux一般是服务器用途,内核追求轻量化,因此,我们可以看到,linux 6.X的内核更新介绍里说某些网卡驱动已经集成到了内核中,也就是某些型号的网卡可以做到即插即用,而低版本的内核通常支持的硬件是比较少的,因此,某些情况下,某个硬件我们想使用的时候就必须升级内核了,简单来说,内核版本越高,那么,它可以识别的硬件种类越多,硬件识别之后,我们就可以安装相应的驱动来使用这些硬件了。通常我们是采用源码编译的方式安装驱动。
下面我将就在一台物理机通过编译方式安装水星的网卡驱动做一个介绍,本次实践基于虚拟机安装网卡驱动,见我的博客(本次相关文件都在下面这个文章内的网盘链接):
Linux|centos7操作系统|VMware虚拟机安装水星免驱USB网卡8188gu记录-CSDN博客
该服务器是一台比较老旧的服务器,硬件配置如下:
CPU: E52660 16核心
硬盘:259G 金士顿
主板:华南金牌770X
内存:16G
操作系统:centos7.5
网卡:主板网卡 芯片型号是r8168? USB网卡芯片型号是8188gu
操作系统的安装就不在这里废话了
内核版本选择的是5,4,266版本,方式是rpm包直接安装
通常,内核的安装有两种方式,第一种是网上的各种deb,rpm包,该方式比较迅速,方便,基本不会有错误,第二种方式是手动编译方式,需要对内核比较熟悉,需要更多的定制功能,比如,linux系统对NTFS文件系统的支持,NET网咯的扩展性功能,EXT2文件系统的支持特殊的需求,以及一些自定义的自己编写的模块需要编译进内核。
本次实践选择的是使用现成的rpm包,rpm包下载地址是:
centos7的默认编译器版本是4.8版本,随着内核升级到了5.X版本,这个时候内核和编译器已经是不匹配了,因此,需要使用高版本的gcc编译器,本例中使用devtools版本的gcc9,将此gcc加入环境变量:
在/etc/profile文件末尾添加:
source /opt/rh/devtools-set/enable
添加后,执行命令? source /etc/profile?
如果内核和gcc编译器版本不匹配会有什么样的后果呢?
基本上编译是不会成功的,一开始编译就一大堆报错,例如下面这样:
make ARCH=x86_64 CROSS_COMPILE= -C /lib/modules/3.10.0-862.14.1.0.h209.eulerosv2r7.x86_64/build M=/opt/rtl8188gu-master modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-862.14.1.0.h209.eulerosv2r7.x86_64'
CC [M] /opt/rtl8188gu-master/core/rtw_cmd.o
In file included from include/linux/kernel.h:11,
from include/linux/cache.h:4,
from include/linux/time.h:4,
from include/linux/stat.h:18,
from include/linux/module.h:10,
from /opt/rtl8188gu-master/include/basic_types.h:76,
from /opt/rtl8188gu-master/include/drv_types.h:26,
from /opt/rtl8188gu-master/core/rtw_cmd.c:17:
include/linux/log2.h:22:1: warning: ignoring attribute ‘noreturn’ because it conflicts with attribute ‘const’ [-Wattributes]
22 | int ____ilog2_NaN(void);
| ^~~
/opt/rtl8188gu-master/core/rtw_cmd.c: In function ‘rtw_joinbss_cmd’:
/opt/rtl8188gu-master/core/rtw_cmd.c:1477:38: warning: taking address of packed member of ‘struct _WLAN_BSSID_EX’ may result in an unaligned pointer value [-Waddress-of-packed-member]
1477 | pnetwork->network.IELength - 12, &psecnetwork->IELength,
| ^~~~~~~~~~~~~~~~~~~~~~
/opt/rtl8188gu-master/core/rtw_cmd.c:1490:32: warning: taking address of packed member of ‘struct _WLAN_BSSID_EX’ may result in an unaligned pointer value [-Waddress-of-packed-member]
1490 | pnetwork->network.IELength, &psecnetwork->IELength);
| ^~~~~~~~~~~~~~~~~~~~~~
/opt/rtl8188gu-master/core/rtw_cmd.c:1494:57: warning: taking address of packed member of ‘struct _WLAN_BSSID_EX’ may result in an unaligned pointer value [-Waddress-of-packed-member]
1494 | rtw_append_exented_cap(padapter, &psecnetwork->IEs[0], &psecnetwork->IELength);
| ^~~~~~~~~~~~~~~~~~~~~~
CC [M] /opt/rtl8188gu-master/core/rtw_security.o
In file included from include/linux/kernel.h:11,
from include/linux/cache.h:4,
from include/linux/time.h:4,
from include/linux/stat.h:18,
from include/linux/module.h:10,
from /opt/rtl8188gu-master/include/basic_types.h:76,
from /opt/rtl8188gu-master/include/drv_types.h:26,
from /opt/rtl8188gu-master/core/rtw_security.c:17:
include/linux/log2.h:22:1: warning: ignoring attribute ‘noreturn’ because it conflicts with attribute ‘const’ [-Wattributes]
22 | int ____ilog2_NaN(void);
| ^~~
CC [M] /opt/rtl8188gu-master/core/rtw_debug.o
In file included from include/linux/kernel.h:11,
from include/linux/cache.h:4,
from include/linux/time.h:4,
类似上面的gcc编译器问题,随着内核版本的升级,很多原内核的模块比如cfg80211也需要跟随升级,如果不升级,那么在加载新网卡模块的时候,可能会报错,如下图:
关键报错信息:cfg88211:failed to load regulatory.db
那么,我们的固件也就是firmware应该怎么更新呢?
其实比较的简单,/lib/firmware目录下就是centos7所使用的固件的存放目录,当然了,这个报错其实并不太影响网卡的驱动安装,因为,我在把网卡驱动安装完毕后,仍然有这个报错,但网卡是正常工作的,如果不希望看到这个报错,处理方案为:
手动从 https://mirrors.edge.kernel.org/pub/software/network/wireless-regdb/ 下载最新的包,解压缩后需要将文件 regulatory.db 和 regulatory.db.p7s 复制到/lib/firmware中?
最终系统日志里在没有看到这个报错了(我下载的是wireless-regdb-2023.09.01.tar.gz)
[root@localhost firmware]# pwd
/lib/firmware
[root@localhost firmware]# ls -al reg*
-rw-r--r-- 1 root root 4896 Jan 18 23:16 regulatory.db
-rw-r--r-- 1 root root 1182 Jan 18 23:16 regulatory.db.p7s
[root@localhost firmware]# ls -al /opt/test/wireless-regdb-2023.09.01/
total 192
drwxrwxr-x. 4 root root 4096 Sep 2 03:58 .
drwxr-xr-x. 7 root root 4096 Jan 18 07:12 ..
-rw-rw-r--. 1 root root 1871 Sep 2 03:58 CONTRIBUTING
-rwxrwxr-x. 1 root root 3672 Sep 2 03:58 db2bin.py
-rwxrwxr-x. 1 root root 4181 Sep 2 03:58 db2fw.py
-rwxrwxr-x. 1 root root 17490 Sep 2 03:58 dbparse.py
-rw-rw-r--. 1 root root 82408 Sep 2 03:58 db.txt
drwxrwxr-x. 2 root root 94 Sep 2 03:58 debian-example
-rwxrwxr-x. 1 root root 363 Sep 2 03:58 gen-pubcert.sh
-rw-rw-r--. 1 root root 25 Sep 2 03:58 .gitignore
-rw-rw-r--. 1 root root 880 Sep 2 03:58 LICENSE
-rw-rw-r--. 1 root root 3989 Sep 2 03:58 Makefile
-rw-rw-r--. 1 root root 1448 Sep 2 03:58 README
-rw-rw-r--. 1 root root 6668 Sep 2 03:58 regulatory.bin
-rw-rw-r--. 1 root root 1266 Sep 2 03:58 regulatory.bin.5
-rw-rw-r--. 1 root root 4896 Sep 2 03:58 regulatory.db
-rw-rw-r--. 1 root root 29 Sep 2 03:58 regulatory.db.5
-rw-rw-r--. 1 root root 1182 Sep 2 03:58 regulatory.db.p7s
-rw-rw-r--. 1 root root 451 Sep 2 03:58 sforshee.key.pub.pem
-rw-rw-r--. 1 root root 977 Sep 2 03:58 sforshee.x509.pem
-rw-rw-r--. 1 root root 49 Sep 2 03:58 sha1sum.txt
drwxrwxr-x. 2 root root 27 Sep 2 03:58 web
-rw-rw-r--. 1 root root 972 Sep 2 03:58 wireless-regdb.spec
?我这里强调cfg80211这个模块是由于该模块是8188gu这个模块的依赖,相关证据如下:
[root@localhost firmware]# modinfo 8188gu |grep dep
depends: cfg80211
[root@localhost firmware]# lsmod |grep 8188gu
8188gu 1007616 0
cfg80211 704512 1 8188gu
因此,保证cfg80211模块正常运行是非常必要的
由于操作系统的内核升级导致usb_modeswitch?切换U盘报错,报错如下:
tips:主要报错信息是Error:Sierra?conrol?message?failed (error -7)
?
这个错误导致以下命令执行出错:
[root@localhost firmware]# usb_modeswitch -KW -v06bd -p 1a2b
Take all parameters from the command line
* usb_modeswitch: handle USB devices with multiple modes
* Version 2.4.0 (C) Josua Dietze 2016
* Based on libusb1/libusbx
! PLEASE REPORT NEW CONFIGURATIONS !
DefaultVendor= 0x06bd
DefaultProduct= 0x1a2b
StandardEject=1
Look for default devices ...
found USB ID 1d6b:0003
found USB ID 2109:3431
found USB ID 1d6b:0002
found USB ID 0bda:1a2b
found USB ID 8087:0024
found USB ID 1d6b:0002
No devices in default mode found. Nothing to do. Bye!
?两种解决方案,第一个是更新usb相关的模块,第二个是更新固件
usb相关模块有usb-storage,scsi-mod等等好几个模块,更新比较困难,因此,选择第二个方法:更新固件,更新固件后可以摆脱这个usb相关module的依赖,也就是说不需要管这些模块是否加载了
更新固件的方法为:从 https://mirrors.edge.kernel.org/pub/linux/kernel/firmware/下载后的最新 firmware 包存放在当前目录的lib文件夹下?
我下载的是这个固件包:
[root@localhost firmware]# ls -al /opt/test/linux-firmware-20240115.tar.gz
-rwxr-xr-x. 1 root root 553601923 Jan 17 09:27 /opt/test/linux-firmware-20240115.tar.gz
解压上面的压缩包后,备份原/lib/firmware目录,删除原firmware目录下的所有内容,将解压出来的内容放置到原/lib/firmware目录下,无需重启服务器,即时生效?
以上两个报错解决后,发现板载网卡也就是r8186在新内核下看不到了,翻遍日志也找不到任何有用的信息,而在旧内核下是可以看到这个网卡的
那么,如何知道板载网卡的芯片型号呢?
以旧内核进入系统后,使用ethtool?查看网卡信息就可以确定板载网卡的芯片信息了:
[root@localhost firmware]# ethtool -i enp5s0
driver: r8168
version: 8.047.05-NAPI
firmware-version:
expansion-rom-version:
bus-info: 0000:05:00.0
supports-statistics: yes
supports-test: no
supports-eeprom-access: no
supports-register-dump: yes
supports-priv-flags: no
以新内核进入系统后,开始make?素质三连,发现报错如下:
关键报错:fatal error:linux/pci-aspm.h:No such file or directory
打开报错的位置:
[root@localhost firmware]# sed -n '65,70p' /opt/test/Realtek-PCIe-GBE-NIC-Driver-master/r8168-8.047.04/src/r8168_n.c
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
#define dev_printk(A,B,fmt,args...) printk(A fmt,##args)
#else
#include <linux/dma-mapping.h>
#include <linux/moduleparam.h>
[root@localhost firmware]# sed -n '55,65p' /opt/test/Realtek-PCIe-GBE-NIC-Driver-master/r8168-8.047.04/src/r8168_n.c
#include <linux/init.h>
#include <linux/rtnetlink.h>
#include <linux/completion.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
#include <linux/pci-aspm.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,37)
#include <linux/prefetch.h>
#endif
?经判断,这里是内核版本检查,可以删除,因此,删除这三行后,可以正常编译了:
删除的三行内容如下
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
#include <linux/pci-aspm.h>
#endif
?剩下基本就是一路绿灯了,按照上面提到的虚拟机内安装网卡驱动的文章,一路畅通无阻的安装完毕了
编辑/etc/udev/rules.d/70-persistent-ipoib.rules,该文件末尾添加三行内容如下:
[root@localhost r8168-8.047.04]# cat /etc/udev/rules.d/70-persistent-ipoib.rules
# This is a sample udev rules file that demonstrates how to get udev to
# set the name of IPoIB interfaces to whatever you wish. There is a
# 16 character limit on network device names though, so don't go too nuts
#
# Important items to note: ATTR{type}=="32" is IPoIB interfaces, and the
# ATTR{address} match must start with ?* and only reference the last 8
# bytes of the address or else the address might not match on any given
# start of the IPoIB stack
#
# Note: as of rhel7, udev is case sensitive on the address field match
# and all addresses need to be in lower case.
#
# ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="32", ATTR{address}=="?*00:02:c9:03:00:31:78:f2", NAME="mlx4_ib3"
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="1a2b", RUN+="/usr/sbin/usb_modeswitch -K -v 0bda -p 1a2b"
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="1a2b", RUN+="/bin/bash -c 'modprobe 8188gu && echo 0bda 1a2b > /sys/bus/usb-serial/drivers/option1/new_id'"
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="1a2b", RUN+="/bin/bash -c 'modprobe 8188gu && echo 0bda 1a2b > /sys/bus/usb-serial/drivers/option1/new_id'"
idVendor和idProduct?是根据下面这个命令的倒数第三行得出的,因为是安装的8188gu这个驱动,因此modprobe 8188gu
[root@localhost r8168-8.047.04]# lsusb
Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 002 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0bda:1a2b Realtek Semiconductor Corp.
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
重启服务器后可以看到非常快的就自动识别到了USB并自动挂载了8188gu?这个驱动
相关自动挂载的日志在/var/log/messages?文件内,内容如下:
Jan 18 23:17:50 localhost kernel: usb 1-1.1: New USB device found, idVendor=0bda, idProduct=b711, bcdDevice= 2.00
Jan 18 23:17:50 localhost kernel: usb 1-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jan 18 23:17:50 localhost kernel: usb 1-1.1: Product: 802.11n WLAN Adapter
Jan 18 23:17:50 localhost kernel: usb 1-1.1: Manufacturer: Realtek
Jan 18 23:17:50 localhost kernel: usb 1-1.1: SerialNumber: 00E04CB82101
Jan 18 23:17:50 localhost kernel: sdb: sdb1 sdb2 < sdb5 >
?
?
WiFi连接:
WiFi连接有很多种方法,这里使用nmcli的方式来连接WiFi:
扫描WiFi热点:
[root@localhost r8168-8.047.04]# nmcli device wifi lis
* SSID MODE CHAN RATE SIGNAL BARS SECURITY
mywifi-WiFi5 Infra 5 54 Mbit/s 100 ▂▄▆█ WPA2
-- Infra 5 54 Mbit/s 100 ▂▄▆█ WPA2
* mywifi Infra 5 54 Mbit/s 90 ▂▄▆█ WPA2
ChinaNet-gTUm Infra 5 54 Mbit/s 80 ▂▄▆_ WPA1 WPA2
MIQI Infra 11 54 Mbit/s 55 ▂▄__ WPA1 WPA2
-- Infra 11 54 Mbit/s 55 ▂▄__ WPA1 WPA2
-- Infra 8 54 Mbit/s 50 ▂▄__ WPA2
zhwww Infra 8 54 Mbit/s 49 ▂▄__ WPA2
ChinaNet-6b4R Infra 2 54 Mbit/s 42 ▂▄__ WPA1
ChinaNet-wfJ9 Infra 10 54 Mbit/s 24 ▂___ WPA1 WPA2
nmcli直接连接:
mywifi是我的WiFi,可以看到网卡wlp0s26u1u1?成功连接名为mywifi的这个热点
[root@localhost r8168-8.047.04]# nmcli device wifi connect mywifi password 密码
Device 'wlp0s26u1u1' successfully activated with '00efc1c4-b849-4821-8f75-c5957cb6defd'.
未完待续!!!