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 \
這樣啟動后的同一物理主機之間的兩個虛擬機是可直接通信的