openwrt 虚拟机直通

路由Openwrt设置

硬件背景

工控主机
CPU:Intel® N100
网卡: Intel Corporation Ethernet Controller I226-V

软件背景

1、ubuntu 24.04
2、qemu kvm
3、openwrt 镜像

需求

使用两张网卡直通方式,给ubuntu宿主机上安装虚拟机openwrt,需要使用vfio技术进行直通提高效率。

vifo配置

配置iOMMU

vim /etc/default/grub

添加如下:

GRUB_CMDLINE_LINUX_DEFAULT="intel_iommu=on"
grub-mkconfig -o /boot/efi/EFI/ubuntu/grub.cfg 

重启电脑

配置vfio网卡

lspci查看网卡pci号

lspci
-> 01:00.0 Ethernet controller: Intel Corporation Ethernet Controller I226-V (rev 04)
   02:00.0 Ethernet controller: Intel Corporation Ethernet Controller I226-V (rev 04)
   03:00.0 Ethernet controller: Intel Corporation Ethernet Controller I226-V (rev 04)
   04:00.0 Ethernet controller: Intel Corporation Ethernet Controller I226-V (rev 04)

网卡添加到vfio

sudo modprobe vfio
sudo modprobe vfio-pci
echo "0000:02:00.0" | sudo tee /sys/bus/pci/devices/0000:02:00.0/driver/unbind
echo "0000:03:00.0" | sudo tee /sys/bus/pci/devices/0000:03:00.0/driver/unbind
echo "vfio-pci" | sudo tee /sys/bus/pci/devices/0000:02:00.0/driver_override
echo "vfio-pci" | sudo tee /sys/bus/pci/devices/0000:03:00.0/driver_override
echo "0000:02:00.0" | sudo tee /sys/bus/pci/drivers/vfio-pci/bind
echo "0000:03:00.0" | sudo tee /sys/bus/pci/drivers/vfio-pci/bind

可以看到设备被绑定上目标驱动

ls /dev/vfio

制作openwrt虚拟机

为openwrt系统制作磁盘

qemu-img convert -p -f raw -O qcow2 openwrt-23.05.2-x86-generic-generic-ext4-combined.img openwrt-23.05.2-x86-generic-generic-ext4-combined.qcow2  
qemu-img resize -f raw openwrt-*.img 20G #扩容到20 G。

下载驱动

igc驱动适用于网卡 I226-V

virsh list可以查看pci设备编号

virsh nodedev-list --tree |grep pci

可以查看具体pci设备的domain bus slot 字段填写到xml中

virsh nodedev-dumpxml pci_0000_03_00_0

编写XML openwrt.xml

<domain type='kvm'>
  <name>my_virtual_machine</name>
  <memory unit='MiB'>512</memory>
  <currentMemory unit='MiB'>512</currentMemory>
  <vcpu>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-trusty'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <devices>
    <emulator>/usr/bin/kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='磁盘文件路径'/>
      <target dev='vda' bus='virtio'/>
    </disk>
  

  <hostdev mode='subsystem' type='pci' managed='yes'>
      <driver name='vfio'/>
      <source>
        <address domain='0x0000' bus='0x02' solt='0x00' function='0x0'/>
      </source>
  </hostdev>
  
   <hostdev mode='subsystem' type='pci' managed='yes'>
	      <driver name='vfio'/>
	      <source>
	        <address domain='0x0000' bus='0x03' solt='0x00' function='0x0'/>
	      </source>
  </hostdev>
  
    <controller type='usb' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' solt='0x02' function='0x0'/>
    </controller><input type='mouse' bus='ps2'/>

    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' solt='0x01' function='0x0'/>
    </controller>

    <graphics type='vnc' port='5970' autoport='no' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>

  </devices>
</domain>

拷贝文件到虚拟机

virt-copy-in  kmod-igc_5.15.137-1_i386_pentium4.ipk  -a openwrt-23.05.2-x86-generic-generic-ext4-combined.qcow2 /

在虚拟机中安装该驱动

opkg install  openwrt-23.05.2-x86-generic-generic-ext4-combined.qcow2

通过virsh 启动虚拟机

virsh create openwrt.xml

未完待续。。。。。

参考

【1】https://blog.csdn.net/hsyxxyg/article/details/136638643
【2】https://blog.csdn.net/jcf147/article/details/133351133

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值