qemu 对于外部网卡的配置方式: 比如基于 linux bridge 或者 ovs bridge(或者 macvtap sriov 等)
qemu 设计了一种基于(启动)脚本和(清理)脚本的方式来处理外部网卡的情况
bash
root@debian:~
▶ qemu-system-x86_64 --help | grep -A 30 "netdev tap"
-netdev tap,id=str[,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,downscript=dfile]
[,br=bridge][,helper=helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off]
[,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][,queues=n]
[,poll-us=n]
configure a host TAP network backend with ID 'str'
connected to a bridge (default=br0)
use network scripts 'file' (default=/etc/qemu-ifup)
to configure it and 'dfile' (default=/etc/qemu-ifdown)
to deconfigure it
use '[down]script=no' to disable script execution
use network helper 'helper' (default=/usr/lib/qemu/qemu-bridge-helper) to
configure it
use 'fd=h' to connect to an already opened TAP interface
use 'fds=x:y:...:z' to connect to already opened multiqueue capable TAP interfaces
use 'sndbuf=nbytes' to limit the size of the send buffer (the
default is disabled 'sndbuf=0' to enable flow control set 'sndbuf=1048576')
use vnet_hdr=off to avoid enabling the IFF_VNET_HDR tap flag
use vnet_hdr=on to make the lack of IFF_VNET_HDR support an error condition
use vhost=on to enable experimental in kernel accelerator
(only has effect for virtio guests which use MSIX)
use vhostforce=on to force vhost on for non-MSIX virtio guests
use 'vhostfd=h' to connect to an already opened vhost net device
use 'vhostfds=x:y:...:z to connect to multiple already opened vhost net devices
use 'queues=n' to specify the number of queues to be created for multiqueue TAP
use 'poll-us=n' to specify the maximum number of microseconds that could be
spent on busy polling for vhost net
-netdev bridge,id=str[,br=bridge][,helper=helper]
configure a host TAP network backend with ID 'str' that is
connected to a bridge (default=br0)
using the program 'helper (default=/usr/lib/qemu/qemu-bridge-helper)
在 QEMU 的网络配置中,script
和 downscript
选项允许用户指定在虚拟机网络接口创建和销毁时执行的脚本。通过这些选项,用户可以实现更复杂和灵活的网络配置,特别是在使用虚拟化和云环境时。
设计思路
- 自动化网络配置 :
script=ovs-ifup
和downscript=ovs-ifdown
的使用使得虚拟机的网络接口在启动和停止时可以自动配置。通过脚本,用户可以设置虚拟网络接口的属性,绑定到 Open vSwitch(OVS)桥接,以及其他网络设置。 - 灵活性与可扩展性: 使用脚本提供了高度的灵活性,用户可以根据自己的需求编写脚本来实现特定的网络配置。例如,可以添加 VLAN 标签、设置 QoS 策略,或动态调整网络参数。
- 集成现有网络管理工具 : 使用
ovs-ifup
和ovs-ifdown
脚本可以直接与 Open vSwitch 集成,使 QEMU 虚拟机能够无缝地融入现有的网络架构中。这种集成有助于在虚拟化环境中保持网络策略的一致性。
使用场景
- 云服务提供商: 在公有云或私有云环境中,为每个虚拟机创建和管理网络接口是常见需求。使用上述脚本,云服务商可以确保每个虚拟机在启动时能够自动获得正确的网络配置。
- 实验和测试环境 : 开发人员可以使用
script
和downscript
来快速创建和销毁网络接口。这在测试网络应用程序或网络配置时非常有用。 - 大规模部署: 在需要快速部署多个虚拟机并确保它们所有网络接口都符合一定配置标准的场合,使用脚本可以显著减少手动配置的工作量,从而提高效率。
示例
在一个典型的使用场景中,用户可能会编写如下的 ovs-ifup
和 ovs-ifdown
脚本:
-
ovs-ifup
脚本:bash#!/bin/bash # Configure the interface for Open vSwitch OVS_BRIDGE="br0" sudo ip link set "$1" up sudo ovs-vsctl add-port "$OVS_BRIDGE" "$1"
-
ovs-ifdown
脚本:bash#!/bin/bash # Remove the interface from Open vSwitch OVS_BRIDGE="br0" sudo ovs-vsctl del-port "$OVS_BRIDGE" "$1" sudo ip link set "$1" down
这个设计思路和使用场景展示了如何通过 QEMU 的网络脚本增强虚拟化环境的网络管理。
在 QEMU 的网络配置中,当你使用 script
和 downscript
选项时,默认情况下,QEMU 会查找这些脚本:
-
默认路径:
script
: 默认位置是/etc/qemu-ifup
downscript
: 默认位置是/etc/qemu-ifdown
如果你希望使用自定义脚本,必须在 -netdev
选项中显式指定脚本的路径。例如:
javascript
-netdev tap,id=str,script=/path/to/your/ovs-ifup,downscript=/path/to/your/ovs-ifdown
自定义路径的最佳实践
- 确保脚本具有执行权限 (
chmod +x script_path
)。 - 在脚本中使用绝对路径,以确保 QEMU 能够找到依赖的命令和资源。
- 可以将脚本放置在任何合适的目录中,只要在
-netdev
选项中指定正确的路径。
这种灵活性允许你根据系统环境和管理策略来组织你的脚本。