# 建立 script (/etc/qemu-ifup) $ cat <<\EOF >/etc/qemu-ifup #!/bin/bash #This is a qemu-ifup script for bridging. #You can use it when starting a KVM guest with bridge mode network.
#set your bridge name switch=br0
if [ -n "$1" ]; then #start up the TAP interface ip linkset$1 up sleep 1 #add TAP interface to the bridge brctl addif ${switch}$1 exit 0 else echo “Error: no interface specified” exit 1 fi EOF
$ cat <<\EOF >/etc/qemu-ifdown #!/bin/bash #This is a qemu-ifdown script for bridging. #You can use it when starting a KVM guest with bridge mode network. #Don’t use this script in most cases; QEMU will handle it automatically.
#set your bridge name switch=br0
if [ -n "$1" ]; then # Delete the specified interfacename tunctl -d $1 #release TAP interface from bridge brctl delif ${switch}$1 #shutdown the TAP interface ip linkset$1 down exit 0 else echo “Error: no interface specified” exit 1 fi EOF
# 設定執行權限 $ chmod +x /etc/qemu-if{up,down}
若是目前使用者沒有 script 的 execute 權限,就無法新增 tap interface for bridge mode 喔!
3、啟動 bridge-mode virtual machine
當上述環境都準備好後,可使用以下指令啟動 virtual machine:
1 2 3 4 5 6 7 8 9 10 11 12
# -net nic -net tap => 指定 virtual machine 網卡為 bridge mode,建立 tap interface # script=/etc/qemu-ifup => 指定 script 配置網卡 # downscript=/etc/qemu-ifdown => 指定 script 移除網卡 (若設定為 no 則 QEMU 會自動協助處理) # --daemonize => QEMU 程序背景執行 $ kvm -vnc 0.0.0.0:1 -m 2048 /kvm/storage/vm_disks/ubnutu1604.img -net nic -net tap,script=/etc/qemu-ifup,downscript=/etc/qemu-ifdown --daemonize
# 查詢是否有啟動 tap interface 並與 bridge device 相連 $ brctl show bridge name bridge id STP enabled interfaces br0 8000.26503b3a5e74 yes ens1f0 tap0 virbr0 8000.5254001001dc yes virbr0-nic
[root@ocp-kvm-host ~]# cat /var/lib/libvirt/dnsmasq/default.conf ##WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE ##OVERWRITTEN AND LOST. Changes to this configuration should be made using: ## virsh net-edit default ## or other application using the libvirt API. ## ## dnsmasq conf file created by libvirt strict-order pid-file=/var/run/libvirt/network/default.pid except-interface=lo bind-dynamic interface=virbr0 dhcp-range=192.168.122.2,192.168.122.254 dhcp-no-override dhcp-lease-max=253 dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
# 配置 tap interface for NAT mode 的 script $ cat <<\EOF >/etc/qemu-ifup-NAT #!/bin/bash
# network information BRIDGE=virbr0 NETWORK=192.168.122.0 NETMASK=255.255.255.0 GATEWAY=192.168.122.1
if [ -n "$1" ]; then # 啟用 bridge device for NAT mode brctl stp ${BRIDGE} on ifconfig ${BRIDGE}${GATEWAY} netmask ${NETMASK} up
ifconfig "$1" 0.0.0.0 up brctl addif ${BRIDGE}"$1" exit 0 else echo"Error: no interface specified." exit 1 fi EOF
# 移除 tap interface for NAT mode 的 script $ cat <<\EOF >/etc/qemu-ifdown-NAT #!/bin/bash
# network information BRIDGE=virbr0
if [ -n "$1" ]; then echo"Tearing down network bridge for $1" ip linkset"$1" down brctl delif ${BRIDGE}"$1" exit 0 else echo"Error: no interface specified." exit 1 fi EOF
# 為目前的使用者加入 $ chmod +x /etc/qemu-if{up,down}-NAT
以上的 script 可能會因為自身的環境不同而有增減,需要特別注意一下
啟動 NAT-mode virtual machine
當環境都準備好後,可以使用以下指令啟動 virtual machine:
1 2 3 4 5 6 7 8 9 10
# -net nic -net tap => 設定 tap interface # script=/etc/qemu-ifup-NAT,downscript=/etc/qemu-ifdown-NAT => 使用指定的 script 配置網路 $ kvm -vnc 0.0.0.0:1 -m 2048 /kvm/storage/vm_disks/ubnutu1604.img -net nic -net tap,script=/etc/qemu-ifup-NAT,downscript=/etc/qemu-ifdown-NAT --daemonize
# 從下面結果可看出 tap0 是與 virbr0 這個 bridge device 相連 $ brctl show bridge name bridge id STP enabled interfaces br0 8000.2c600cb163d5 yes ens1f0 virbr0 8000.1a507abb7dff yes tap0 virbr0-nic