KVM network mode (1) - bridge

Network Configuration

Network modes supported by QEMU

qemu-kvm mainly provides 4 different modes of network to the client.
  1) Bridge-based virtual network card;
  2) NAT-based virtual network
  3) QEMU built-in user mode networking (user mode networking)
  4) Networks that directly assign network devices (including VT-d and SR-IOV)

1. Use bridge mode
In the qemu-kvm command line, the network parameters for bridge mode are as follows:
-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][ ,script=file][,downscript=dfile][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostforce=on|off]

1) Install bridge-utils and tunctl packages

# yum -y install bridge-utils tunctl

2) Check if the tun module is loaded

# lsmod | grep tun

If not loaded, run modprobe tun to load it. If tun has been compiled into the kernel (you can check whether there is a "CONFIG=y" option in the kernel config file), you don't need to load it. If the kernel does not configure the TUN module at all, you need to recompile the kernel.

# grep 'CONFIG_TUN' /boot/config-`uname -r`

3) Check the permissions of /dev/net/tun, the current user needs to have read and write permissions.

# ls -lh / dev / net / tun

4) Build a bridge, bind it to a working network interface, and let the bridge become the interface connecting the machine and the external network.

# brctl addr br0
# brctl addif br0 eth0
# brctl stp br0 on
# route
# ping 192.168.1.254

Or edit the configuration file to build a bridge

# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE="br0"
[root@kvm ~]# 
# cat /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
ONBOOT="yes"
TYPE="Bridge"
BOOTPROTO="none"
IPADDR="192.168.100.10"
NETMASK="255.255.255.0"
STP="on"
DELAY="0"

# service network restart
# dmesg
…………
device virbr0-nic entered promiscuous mode
br0: port 1(eth0) entering forwarding state 
…………

The state after the bridge is established is to put the network interface eth0 into promiscuous mode (promiscuous mode, receiving all data packets in the network), the bridge br0 enters the forwarding state, and has the same MAC address as eth0,

5) Prepare qemu-ifup and qemu-ifdown scripts

The script that the client will execute before starting the network is configured by the "script" option (defaults to /etc/qemu-ifup). Typically a TAP device is created on this script and bound to the bridge.

#!/bin/bash
#filename: /etc/qemu-ifup
switch=br0

if [ -n " ​​$1 " ]; then 
#tunctl -u $( whoami ) -t $ 1 # Some older versions do not automatically create TAP devices
 ifconfig $ 1 up
 sleep  0 .5s
brctl addif $switch $1
exit 0
else
echo 'Error: no specifed interface.'
exit 1
fi

Since the qemu-kvm tool will release the bridge binding of the TAP device when the client is shut down, it will also automatically delete the TAP device that is no longer in use, all the qemu-ifdown scripts are not necessary, it is best to set downscript=no (qemu The script parameter of -kvm is the script executed when the virtual machine starts, and the downscript parameter is the parameter executed when the virtual machine is shut down)

# cat /etc/qemu-ifdown 
#!/bin/bash

switch=br0

if [ -n $1 ];then
tunctl -d $1
brctl delif ${switch} $1
ip link set $1 down
exit 0
else
echo "Error: no interface specified"
exit 1
fi

6) Create a virtual machine

Create a thin-comb formatted disk

# dd if=/dev/zero of=/root/centos6.img bs=1M oflag=direct seek=4095 count=1

Install a centos6 minimal system using an image

# qemu-kvm -m 768 \
-smp 2 \
--boot order=cd \
--hda /root/centos6.img \
--cdrom /root/CentOS-6.9-x86_64-minimal.iso

 

7) Start the network in bridge mode with the qemu-kvm command.
In the host, start the client with the command line and check the status of the bridge, as follows:

# qemu-kvm /root/centos6.img \
-smp 2 \
-m 1024 \
-net nic \
-net tap,ifname=tap1,script=/etc/qemu-ifup,downscript=no \
-vnc :0 \
-daemonize

After the virtual machine is started, use the following command to see that TAP0 is created

# brctl show
# ls /sys/devices/virtual/net/
# vcnview :0

After shutting down the virtual machine, use the following to see that TAP0 is deleted

# brctl show
# ls /sys/devices/virtual/net/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324974536&siteId=291194637