1.2. qemu命令起虚拟机增加网络配置
1. 网络配置
常见的网络模式分为tap网络和基础网络模式两种。
1.1. TAP网络(桥接模式)
虚拟机直接接入宿主机物理网络,获得独立IP
1.1.1. 使用tap方式起虚拟机网络
-netdev tap,id=hostnet0,ifname=tap0 \-device virtio-net-pci,netdev=hostnet0 \
其中,-netdev中的id要和-device中的netdev值保持一致。自定义值
1.1.2. qemu启动示例
/usr/bin/qemu-system-loongarch64 \-name guest=vm1 \-machine virt,accel=kvm \-nodefaults \-m 2048 \-smp 2,maxcpus=4,cores=4,threads=1,sockets=1 \-cpu 'la464-loongarch-cpu' \-bios /usr/share/edk2/loongarch64/QEMU_EFI.fd \-drive file=/home/OpenCloudOS-9.4.qcow2,if=virtio \-nographic \-serial stdio \-netdev tap,id=hostnet0,ifname=tap0 \-device virtio-net-pci,netdev=hostnet0 \-monitor telnet:localhost:4445,server,nowait \-msg timestamp=on
使用上述qemu命令启动虚拟机会提示缺少/etc/qemu-ifup文件,创建该文件并编写脚本
1.1.3. /etc/qemu-ifup文件编写
[root@bogon ~]# ls -lah /etc/qemu-ifup
-rwxr-xr-x 1 root root 1.2K 8月 6日 10:11 /etc/qemu-ifup[root@bogon ~]# cat /etc/qemu-ifup
#! /bin/sh
# Script to bring a network (tap) device for qemu up.
# The idea is to add the tap device to the same bridge
# as we have default routing to.# in order to be able to find brctl
PATH=$PATH:/sbin:/usr/sbin
ip=$(which ip)if [ -n "$ip" ]; thenip link set "$1" up
elsebrctl=$(which brctl)if [ ! "$ip" -o ! "$brctl" ]; thenecho "W: $0: not doing any bridge processing: neither ip nor brctl utility not found" >&2exit 0fiifconfig "$1" 0.0.0.0 up
fi#switch=$(ip route ls | \
# awk '/^default / {
# for(i=0;i<NF;i++) { if ($i == "dev") { print $(i+1); next; } }
# }'
# )
switch=virbr0# only add the interface to default-route bridge if we
# have such interface (with default route) and if that
# interface is actually a bridge.
# It is possible to have several default routes too
for br in $switch; doif [ -d /sys/class/net/$br/bridge/. ]; thenif [ -n "$ip" ]; thenip link set "$1" master "$br"elsebrctl addif $br "$1"fiexit # exit with status of the previous commandfi
doneecho "W: $0: no bridge for guest interface found" >&2
1.2. 基础网络模式(NAT模式)
适用于简单联网需求,虚拟机通过宿主机 NAT 访问外网。
1.2.1. 使用user模式起虚拟机
-netdev user,id=net,hostfwd=tcp::2222-:22 \-device virtio-net-pci,netdev=net \
其中,-netdev中的id要和-device中的netdev值保持一致。自定义值
1.2.2. 完整的qemu启动命令
/usr/bin/qemu-system-loongarch64 \-name guest=vm1 \-machine virt,accel=kvm \-nodefaults \-m 2048 \-smp 2,maxcpus=4,cores=4,threads=1,sockets=1 \-cpu 'la464-loongarch-cpu' \-bios /usr/share/edk2/loongarch64/QEMU_EFI.fd \-drive file=/home/OpenCloudOS-9.4-test.qcow2,if=virtio \-nographic \-serial stdio \-netdev user,id=net1,hostfwd=tcp::2222-:22 \-device virtio-net-pci,netdev=net1 \-monitor telnet:localhost:4448,server,nowait \-msg timestamp=on
hostfwd=tcp::2222-:22 解析:将宿主机的2222端口转发到虚拟机的22端口(SSH)
1.2.3. 自定义局域网网段
自定义虚拟机网络为192.168.200.XX网段
-netdev user,id=net1,net=192.168.200.0/24,hostfwd=tcp::2223-:22 \-device virtio-net-pci,netdev=net1 \
2. 问题记录
问题描述:使用上述两种方式在同一物理机上创建启动两个虚拟机,发现两个虚拟机中的ip一致
问题分析:分析问题发现,新创建启动的两个虚拟机中的MAC地址一样,故IP一致
解决方法:自定义虚拟机的MAC地址
TAP网络:
-netdev tap,id=hostnet0,ifname=tap0 \-device virtio-net-pci,netdev=hostnet0,mac=52:54:00:12:34:54 \ -netdev tap,id=hostnet1,ifname=tap1 \-device virtio-net-pci,netdev=hostnet1,mac=52:54:00:12:34:56 \
基础网络:
-netdev user,id=net1,net=192.168.200.0/24,hostfwd=tcp::2223-:22 \-device virtio-net-pci,netdev=net1,mac=52:54:00:12:34:54 \-netdev user,id=net1,net=192.168.200.0/24,hostfwd=tcp::2223-:22 \-device virtio-net-pci,netdev=net1,mac=52:54:00:12:34:56 \
这样启动后的同一物理主机之间的两个虚拟机是可直接通信的