[All In One] PVE 中 OpenWrt LXC 重启问题

从去年开始,我的 OpenWrt 是安装在 PVE 中的 LXC 容器中,但一直以来都有一个问题,OpenWrt 关机后就无法再次启动了。

我是将 enp6s0(有线网卡)、wlp2s0(无线网卡)硬件直通给 LXC 容器:

lxc.net.0.type: phys
lxc.net.0.link: enp6s0
lxc.net.0.flags: up
lxc.net.1.type: phys
lxc.net.1.link: wlp2s0
lxc.net.1.flags: up

查看 LXC 的启动日志发现网卡重命名失败:

faile to rename network device physNBNrWC to enp6s0
lxc_network_move_created_netdev_priv: 3549 invalid argument - Failed to move network device "wlp2s0" with ifindex 11 to network namespace 539922 and rename to physxAAvs5
lxc_spawn: 1840 failed to create the network

通过 dmesg 也是发现重命名失败的信息,尝试给 LXC 配置中网卡增加明确的命名 lxc.net.0.name: wan 后,有线网卡不再出现重命名失败。

但是无线网卡 PVE 依然无法操作成功。通过 ip link show 发现,LXC 关闭后,原有物理接口 wlp2s0 也消失了,网卡丢失一段时间,然后大概 5-10 分钟之后,接口重新出现,并且正常使用。怀疑是否是设备出现了问题

root@pve:~# lspci -k | grep -i wire -A 2
02:00.0 Network controller: MEDIATEK Corp. MT7922 802.11ax PCI Express Wireless Network Adapter
        Subsystem: Foxconn International, Inc. MT7922 802.11ax PCI Express Wireless Network Adapter
        Kernel driver in use: mt7921e
        Kernel modules: mt7921e

另外通过lsmod |grep 7921(网卡驱动),发现驱动加载正常,即便 modprobe mt7921e 重新加载驱动也没有效果。怀疑是否是电源管理导致设备进入了某种状态。在 PVE 主机和 OpenWrt 内部使用 iw 将无线网卡的电源管理禁用也没有效果。

使用 rfkill 查看是否有被 软/硬 禁用,也正常:

root@pve:~# rfkill list
0: hci0: Bluetooth
        Soft blocked: no
        Hard blocked: no
1: hci0: Wireless LAN
        Soft blocked: no
        Hard blocked: no

最终无奈之下尝试将设备重置,竟然成功了!

echo /sys/bus/pci/devices/02:00.0/remove # 设备号使用 lspci 获取
echo "1" > /sys/bus/pci/rescan

解决方案

LXC 容器的配置增加关机后置脚本:

root@pve:~# vi /usr/local/bin/pcie_hot_reset.sh # (脚本内容)
root@pve:~# chmod +x /usr/local/bin/pcie_hot_reset.sh

root@pve:~# tail /etc/pve/lxc/100.conf
lxc.hook.post-stop: /var/lib/lxc/openwrt_post_stop.sh

root@pve:~# cat /var/lib/lxc/openwrt_post_stop.sh
/usr/local/bin/pcie_hot_reset.sh 02:00.0

脚本参考@alex.forencich 的回答 https://unix.stackexchange.com/questions/73908/how-to-reset-cycle-power-to-a-pcie-device

Published At
This site is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at https://wayjam.me.