3.6 网络相关的参数
1.1. 1. -net nic 参数
-net nic[,macaddr=mac][,model=type][,name=str][,addr=str][,vectors=v]
为虚拟机创建网卡,可配置网卡 mac 地址,网卡类型等参数。
- 使用 “qemu-kvm -net nic,model=?” 查询 QEMU 实现了哪些网卡的模拟。
- 每一类 nic,model 都有一种 -device 与之对应。比如
-net nic,model=e1000
等价于-device e1000
。
1.2. 2. -net tap 和 -netdev tap 参数
-net tap[,vlan=n][,name=name][,fd=h][,ifname=name][,script=file][,downscript=dfile][,helper=helper]
-netdev tap,id=id[,fd=h][,ifname=name][,script=file][,downscript=dfile][,helper=helper]
这两个配置方法效果等效,但用于不同场合:
- -net tap[,...] 与 -net nic 参数配套使用。
- -netdev tap,id=id[,...] 与 -device 参数配套使用。
第一种是简化版,无需指定后端设备 id 。如果以 “-net nic[,...] 和 -netdev tap,id=id[,...]” 组合配置网卡,QEMU 会报以下错误:
(qemu) qemu-system-loongarch64: warning: hub 0 is not connected to host network
qemu-system-loongarch64: warning: netdev tap0 has no peer
两种方式都表示创建或者使用宿主机的 TAP 网络接口与 “-net nic” 模拟给虚拟机的前端网卡相连。
- tap 参数,表示使用 TAP 设备。
- vlan=n,设置该设备连入 VLAN n,默认值为0(即没有 VLAN)。
- name=name,设置名称,在 QEMU monitor 会用到,一般由系统自动分配。
- fd=h,连接到已经打开的 TAP 接口的文件描述符。fd 不能与 ifname、script、downscript、helper 等参数同时使用。
- ifname=name,设置在宿主机中添加的 TAP 虚拟设备的名称(如 tap0)。不设置时,QEMU 会自动生成一个 TAP 接口名称。
- script=file,设置宿主机在启动虚拟机时自动执行的网络配置脚本。如果不设置,默认值为 “/etc/qemu-ifup” 脚本。如果不需要执行脚本,则设置为 “script=no”。
- downscript=dfile,设置宿主机在关闭虚拟机时自动执行的网络配置脚本。如果不设置,默认值为 “/etc/qemu-ifdown” 脚本。如果不需要执行脚本,则设置为 “downscript=no”。
例如:
-net nic,macaddr=00:00:00:00:00:01,model=virtio-net-pci,addr=08 -net tap,ifname=tap0,script=no,downscript=no
等价于
-device virtio-net-pci,mac=00:00:00:00:00:01,netdev=tap0,addr=08 -netdev tap,id=tap0,script=no,downscript=no
1.3. 3. -net user 和 -netdev user 参数
-net [user|tap|bridge|vde|socket][,option][,option][,...]
old way to initialize a host network interface
(use the -netdev option if possible instead)
[,server][,nowait][,telnet][,websocket][,reconnect=seconds][,mux=on|off]
-netdev user,id=str[,ipv4[=on|off]][,net=addr[/mask]][,host=addr]
[,ipv6[=on|off]][,ipv6-net=addr[/int]][,ipv6-host=addr]
configure a user mode network backend with ID 'str',
让虚拟机使用不需要管理员权限的用户模式网络(user mode network),如 “-net nic -net user”
1.4. 4. -nic none 参数
当不需要配置任何网络设备时,需要使用 “-net none” 参数,因为如果不添加 “-net” 参数,则会被默认设置为 “-net nic -net user” 参数。