Error problem of QEMU-KVM Bridge network in Linux

Source: Internet
Author: User

To be exact, a KVM is a module of Linux. You can use Modprobe to load the KVM module. After you load the module, you can further create a virtual machine from another tool. However, only the KVM module is not enough, because the user can not directly control the kernel module to do things: there must be a user space tool to do. This user-space tool, the developer chooses the already-formed Open-source virtualization software QEMU. To say that QEMU is also a virtualization software. It is characterized by a virtual different CPU. For example, a power CPU can be virtual on a x86 CPU, and it can be used to compile programs that run on power. KVM uses a portion of QEMU and is slightly modified to become a user space tool that can control KVM. So you'll see that the official KVM download has two or three files, the KVM module, the Qemu tool, and the combination. That is, you can upgrade the KVM module only, or you can only upgrade the QEMU tool. This is the relationship between KVM and QEMU.

The RHCE virtual machine environment, which I had previously obtained, was successfully initiated through the native QEMU-KVM command after converting VMDK to QCOW2 format. There was a problem starting with the Virsh + XML configuration file. have been too lazy to see, no secondary use, direct QEMU-KVM start. However, every time through VNC to connect to feel more trouble, simply add a card to it, bridge the current physical network card, so you can pass SCRT direct connection. However, when the increase did appear the following error:

/etc/qemu-ifup:could Not launch network script
QEMU-KVM:-net tap,vlan=0,ifname=tap0:device ' tap ' could not to be initialized
Here is a record of the specific processing and resolution process.

First, the reference Virsh launched host

A virtual machine that was launched at the end of the Virsh command on the QEMU-KVM command, and when viewed through the PS Auxf|grep KVM, the network part parameters used when the other host was launched were as follows:

-netdev Tap,fd=22,id=hostnet0,vhost=on,vhostfd=23-device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:1f : 45:43,bus=pci.0,addr=0x3
According to gourd painting ladle also through the QEMU-KVM when starting to add such a bunch of things when. An error was found to start.

Second, view QEMU-KVM help

You can see the following network-related help information by viewing QEMU-KVM:

[Root@361way ~]#/USR/LIBEXEC/QEMU-KVM--help|grep-i Net
' Drives ': Floppy (a), hard disk (c), CD-ROM (d), network (n)
Network options:
-net Nic[,vlan=n][,macaddr=mac][,model=type][,name=str][,addr=str][,vectors=v]
Create a new network Interface card and connect it to VLAN ' n '
-net User[,vlan=n][,name=str][,net=addr[/mask]][,host=addr][,restrict=on|off]
Connect the user mode network stack to VLAN ' n ', configure its
-net tap[,vlan=n][,name=str][,fd=h][,fds=x:y:...:z][,ifname=name][,script=file][,downscript=dfile][,helper= helper][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h][,vhostfds=x:y:...:z][,vhostforce=on|off][ , Queues=n]
Connect the host TAP network interface to VLAN ' n '
Use network scripts ' file ' (default=/etc/qemu-ifup)
Use Network Helper ' helper ' (Default=/usr/libexec/qemu-bridge-helper) to
Use Vnet_hdr=off to avoid enabling the IFF_VNET_HDR tap flag
Use Vnet_hdr=on to make the lack of IFF_VNET_HDR support an error condition
Use ' vhostfd=h ' to connect to a already opened Vhost net device
Use ' vhostfds=x:y:...:z to connect to multiple already opened Vhost NET devices
-net Bridge[,vlan=n][,name=str][,br=bridge][,helper=helper]
Connects a host TAP network interface to a host bridge device ' BR '
-net Socket[,vlan=n][,name=str][,fd=h][,listen=[host]:p Ort][,connect=host:port]
-net Socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port[,localaddr=addr]]
-net Socket[,vlan=n][,name=str][,fd=h][,udp=host:port][,localaddr=host:port]
-net Dump[,vlan=n][,file=f][,len=n]
-net None use it alone to have zero network devices. If no-net option
is provided, the default is '-net nic-net user '
-netdev [user|tap|bridge|socket|hubport],id=str[,option][,option][,...]
[, Server] [, NoWait] [, Telnet] [, Mux=on|off] (TCP)
-chardev Socket,id=id,path=path[,server][,nowait][,telnet],[mux=on|off] (Unix)
Emulate a standard HCI in virtual scatternet ' n '
Add host computer to virtual scatternet ' n ' using VHCI
Emulate a Bluetooth device ' dev ' in scatternet ' n '
The relevant help for using the-net tap item is as follows:

This configuration represents the TAP network interface attached to the host computer to the N VLAN and uses file and dfile two scripts to configure the network when the client is started and to cancel the network configuration when the client is shut down.

Tap parameters, indicating the use of tap equipment. Tap is a virtual network device that simulates a data link layer device (the second layer of an ISO seven-layer network structure), which is processed like an Ethernet data frame
Second-tier datagrams. And Tun is similar to tap, is also a kind of virtual network equipment, it is to network layer device simulation. Tap is used to create a network bridge, while Tun is associated with routing.

Vlan=n sets the device VLAN number, the default value is 0.

The Name=name setting name, which may be used in Qemu monior, is generally allocated automatically by the system.

Fd=h
A file descriptor connected to the tap interface that is now open, and in general does not set this option, but lets QEMU automatically create a tap interface. When the FD=H option is used
, ifname, script, Downscript, helper, VNET_HDR, etc. are not available (they cannot be present on the command line with the FD option).

Ifname=name sets the name of the tap virtual device (such as TAP1, TAP5, and so on) that is added to the host, and when this parameter is not set, QEMU produces the name of a tap interface based on the current situation in the system.

Script=file sets the network configuration script that the host computer performs automatically when the client is started. If not specified, the default value is "/etc/qemu-ifup", which specifies its own script path to replace the default value, or "Script=no" if no script is required.

Downscript=dfile sets up the network configuration script that the host computer automatically executes when the client shuts down. If not set, the default value is "/etc/qemu-ifdown", or "Downscript=no" if the host does not need to execute the script when the client shuts down.

Helper=helper set up a helper program that runs on the host when the client is started, including to build a tap virtual device with a default value of/usr/local/libexec/qemu-bridge-helper, usually without customization, with default values.

Sndbuf=nbytes limits the send buffer size of the tap device to n bytes, which can be set when traffic is required for traffic control. The default value is "Sndbuf=0", which does not limit the size of the send buffer.

Here again the following command launches the virtual machine:

/usr/libexec/qemu-kvm-m 8096-cpu qemu64,+vmx-net nic,vlan=0,macaddr=52:54:00:12:34:01-net tap,vlan=0,ifname=tap0- SMP 4,sockets=4,cores=1,threads=1-drive File=/data/img/rhce/jiaoshiji.qcow2-vnc 0.0.0.0:20 &
Or find the most mentioned error, see the following figure:

Qemu-ifup

Third, Network Bridge inspection

Through the Internet to check the relevant information to confirm the need for the default startup script file/etc/qemu-ifup, confirmed that this machine does not exist. Also need to install Bridge-utils and TUNCTL package, here in order to first bridge check, and then deal with the above error.

1. Confirm the package used by bridge

Bridge-utils and Tunctl, which provide the required brctl, Tunctl command line tools. It is necessary to use the bridge when the package, the machine used before, has been installed. Installations that are not installed can be installed in the following ways:

# yum Install bridge-utils Tunctl
2. Module Confirmation

# lsmod |grep Tun
Tun 27183 8 Vhost_net
If the Tun module is not loaded, run the Modprobe tun command to load; Of course, if you have already compiled Tun to the kernel (you can see if there is a "config_tun=y" option in the kernel CONFIG file), you do not need to load it.

3, authentication Authority

To check the permissions of the/dev/net/tun, you need to have read-writable permissions for the current user:

# Ll/dev/net/tun
Crw-rw-rw-1 root root 10, 200 October 20:34/dev/net/tun
4, bridging the physical network port

Set up a bridge and bind it to a functioning network interface, and make it the interface between the native and the external network. The main configuration commands are shown in the following command line.

# brctl ADDBR br0 #添加br0这个bridge
# brctl AddIf br0 eth0 #将br0与eth0绑定起来
# brctl STP br0 on #将br0加入到STP协议中
# dhclient Br0 #将br0的网络配置好
# route #参看路由表是否正常配置
Note: The above four steps need not be configured to skip directly because other virtual machines already in Virsh use the direct bridging physical network card configuration. Perform step 1-4 unless the QEMU-KVM is manually installed and you have never configured a bridge.

Iv. resolution of Qemu-ifup Error

1. Add/etc/qemu-ifup Script

Since the internet has been found that there is no qemu-ifup script cause, here is a new qemu-ifup in/etc, which reads as follows:

#!/bin/bash
#This is a qemu-ifup script for bridging.
#You can use it when starting a KVM guest and bridge mode network.
#set Your bridge name
Switch=br0
If [-N "$"]; Then
#create a TAP interface; QEMU would handle it automatically.
#tunctl-U $ (whoami)-T $
#start up the TAP interface
IP Link Set up
Sleep 1
#add TAP interface to the bridge
Brctl addif ${switch} $
Exit 0
Else
echo "Error:no interface Specified"
Exit 1
Fi
And you need to use the root user to perform chmod u+x with executable permissions.

2, view Brctl Bridge Network

Before you manually start the virtual machine via QEMU-KVM, the following command is used to view the bridging network information as follows:

# Brctl Show Br0
Bridge name Bridge ID STP enabled interfaces
Br0 8000.7824af46ca60 No Enp3s0
Vnet0
Vnet1
Vnet2
And then manually started the virtual machine, no error found, and then look at the output of brctl, found that more automatically enabled a TAP0 network card, as follows:

# Brctl Show Br0
Bridge name Bridge ID STP enabled interfaces
Br0 8000.7824af46ca60 No Enp3s0
Tap0
Vnet0
Vnet1
Vnet2
After entering the virtual machine, through the configuration network, through the SECURECRT connection normal.

V. Steps to Backtrack

When you manually start a bridging network, you can set the script at startup to no, and the error in the screenshot above will not appear.

/usr/libexec/qemu-kvm-m 1024 \
-drive file=/data/images/centos6_4.qcow2,if=virtio \
-net nic,model=virtio-net tap,script=no-nographic-vnc:0
After you start using the-net Tap,script=no method, the system generates a TAPX virtual NIC, which defaults to the down state

# IP link Show Dev tap0
37:TAP0: <BROADCAST,MULTICAST> MTU 1500 Qdisc noop state down Qlen 500
Link/ether d2:b0:af:7b:23:0f BRD FF:FF:FF:FF:FF:FF
If you want to communicate with the outside world, you can do it manually, as shown below the current Br0 bridge, and there is no tap-related network card:

# Brctl Show Br0
Bridge name Bridge ID STP enabled interfaces
Br0 8000.7824af46ca60 No Enp3s0
Vnet0
Vnet1
Vnet2
When you do not use script, you need to manually restart the new network card, and the Tap0 also bridge to the br0 to communicate with the outside, the following methods

# IP Link Set tap0 up//Make tap0 State up
# brctl AddIf br0 tap0//bridge tap0 to Br0
# Brctl Show Br0
When implemented through scripting, the following:

/usr/libexec/qemu-kvm-m 1024 \
-drive file=/data/images/centos6_4.qcow2,if=virtio \
-net nic,model=virtio-net tap,script=/tmp/qemu-ifup.sh-nographic-vnc:0
The script is enabled by default, and the location is/etc/qemu-ifup, which can also be assigned to another path. TAP,SCRIPT=/TMP/QEMU-IFUP.SH specifies that the script network configuration is started before starting, and the script reads as follows:

# cat/tmp/qemu-ifup.sh
#!/bin/bash
# Bridging Network Equipment
Switch=br0
If [-N $]; Then//$1 for QEMU-KVM pass value, this is tap
IP Link Set up
Brctl addif ${switch} $
Exit 0
Else
echo "No interface!"
Exit 1
Fi
It's roughly the same as the script above, but it's a little bit simpler and looks more refreshing.

Note: Brctl delif br0 tap0 To remove the bridging network, the QEMU-KVM tool automatically unlocks the bridge bindings of the TAP device when the client shuts down, so this step is not required. correspond to Downscript if you want to specify the following:

#!/bin/bash
#This is a qemu-ifdown script for bridging.
#You can use it when starting a KVM guest and bridge mode network.
#Don ' t use this script in most cases; QEMU would handle it automatically.
#set Your bridge name
Switch=br0
If [-N "$"]; Then
# Delete The specified InterfaceName
Tunctl-d $
#release TAP interface from bridge
Brctl Delif ${switch} $
#shutdown the TAP interface
IP link set down
Exit 0
Else
echo "Error:no interface Specified"
Exit 1
Fi

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.