QEMU创建桥接虚拟机

2019-05-16  本文已影响0人  Sigers

宿主机配置,尤其注意将br0配置到stp协议里去,改完重启宿主机

root@xiangxin-B250M-D2V:/home/xiangxin# vi /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback


#auto enp3s0
#iface enp3s0 inet static
#pre-up ifconfig enp3s0 hw ether e0:d5:5e:7f:31:97
#address 192.168.0.131
#netmask 255.255.255.0
#gateway 192.168.0.1

#dns-nameserver 114.114.114.114
#原网卡名为enp3s0,
auto br0
iface br0 inet static
address 192.168.0.131
netmask 255.255.255.0
gateway 192.168.0.1
bridge_ports enp3s0
bridge_stp on
bridge_fd 9
bridge_hello 2
bridge_maxage 12
dns-nameservers 114.114.114.114

#auto enp3s0
#iface enp3s0 inet manual

#auto tap0
#iface tap0 inet manual
#pre-up tunctl -t tap0 -u root
#pre-up ifconfig tap0 0.0.0.0 promisc up
#post-up brctl addif br0 tap0

启动网桥接口

tunctl -t tap0
brctl addif br0 tap0
ifconfig tap0 0.0.0.0 promisc up
brctl showstp br0

虚拟机启动命令

#创建虚拟磁盘
qemu-img create -f qcow2 ubuntu.img 10G
#安装系统
qemu-system-x86-64 -enable-kvm \
-hda ubuntu.img \
-boot d \
-cdrom ./ubuntu-16.04.6-desktop-amd64.iso \
-m 2048
#启动虚拟机
qemu-system-x86-64 -enable-kvm \
-boot -c \
-drive file=/wj/Qemu/ubuntu.img,if=virtio \
-m 2048 \
-smp 2 \
-netdev tap,ifname=tap0,script=no,downscript=no,id=net0 \
#-netdev tap,script=/wj/Qemu/qemu-ifup,downscript=no,id=net0 \
-device virtio-net-pci,netdev=ne0,mac=00:11:22:33:44:AB

参数及可加参数说明:
-daemonize表示在后台运行
-m 2048分配的内存

qemu-ifup脚本,可选,若使用上述命令启动了tap0,则不需要使用

root@xiangxin-B250M-D2V:/wj/Qemu# cat qemu-ifup 
#!/bin/bash

switch=br0
if [ -n "$1" ]; then
    ip link set $1 up
    sleep 1
    brctl addif ${switch} $1
    exit 0
else
    echo "Error: no interface specified"
    exit 1
fi

虚拟机配置,完了重启虚拟机

test@test-Standard-PC-i440FX-PIIX-1996:~$ vi /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto ens3
iface ens3 inet static
address 192.168.0.181
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 114.114.114.114
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp on
上一篇下一篇

猜你喜欢

热点阅读