LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud
User Name
Password
Linux - Virtualization and Cloud This forum is for the discussion of all topics relating to Linux Virtualization and Linux Cloud platforms. Xen, KVM, OpenVZ, VirtualBox, VMware, Linux-VServer and all other Linux Virtualization platforms are welcome. OpenStack, CloudStack, ownCloud, Cloud Foundry, Eucalyptus, Nimbus, OpenNebula and all other Linux Cloud platforms are welcome. Note that questions relating solely to non-Linux OS's should be asked in the General forum.

Notices


Reply
  Search this Thread
Old 11-07-2012, 03:49 AM   #1
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Rep: Reputation: 103Reputation: 103
KVM Qemu instance bridged tap interface disconnects, cutting me off from the guest


Hi all

I'm setting up a tap0 interface for QEMU so I can run a KVM instance that can provide network services out from the host it runs on onto my network.

Here's how I set up the tap0 interface, according to http://en.wikibooks.org/wiki/QEMU/Networking:

Code:
#!/bin/sh 
# 
# script to bring up the tun device in QEMU in bridged mode 
# first parameter is name of tap device (e.g. tap0)
#
# some constants specific to the local host - change to suit your host
#
ETH0IP=172.16.1.1
GATEWAY=172.16.1.9
BROADCAST=172.16.255.255
#
# First take eth0 down, then bring it up with IP 0.0.0.0 
#
/sbin/ifdown eth0
/sbin/ifconfig eth0 0.0.0.0 promisc up
#
# Bring up the tap device (name specified as first argument, by QEMU)
#
/usr/local/sbin/openvpn --mktun --dev $1 --user `id -un`
/sbin/ifconfig $1 0.0.0.0 promisc up
#
# create the bridge between eth0 and the tap device
#
/usr/sbin/brctl addbr br0
/usr/sbin/brctl addif br0 eth0
/usr/sbin/brctl addif br0 $1
# 
# only a single bridge so loops are not possible, turn off spanning tree protocol
#
/usr/sbin/brctl stp br0 off 
# 
# Bring up the bridge with ETH0IP and add the default route 
#
/sbin/ifconfig br0 $ETH0IP netmask 255.255.255.0 broadcast $BROADCAST
/sbin/route add default gw $GATEWAY
The host's IP is 172.16.1.2 - the guest should answer on 172.16.1.1 after doing the above

The above works, but only for a few seconds. If I VNC to the 172.16.1.1 IP (which should be bridged into the KVM guest) I only get a few seconds of seeing the VNC screen, then I'm disconnected. Subsequent VNC attempts fail with "the server actively refused the connection" (in TightVNC in Windows). Many retries might sometimes allow VNC to reconnect to the running KVM instance.

What is wrong with my setup above? The tap0 interface is extremely unreliable, and disconnects a lot. If I do not start the VM, and leave the phyiscal machine's network alone, it is rock-solid...

Last edited by rylan76; 11-07-2012 at 06:38 AM.
 
Old 11-07-2012, 06:35 AM   #2
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Original Poster
Rep: Reputation: 103Reputation: 103
Hi guys

To all interested parties, I think I have solved this issue. In the tap0 setup script that sets up a bridged connection so the KVM VM can access the network, I made two mistakes.

The first mistake was to setup the bridge based on the wrong IP address. !

The second mistake was to use the wrong netmask for the Class B network I was on.

Once I corrected both of the above, everything started working.

First, the rationale. What I wanted was this:

172.16.1.1 - static IP of physical Centos 6 machine (host)
172.16.1.2 - static IP of virtual Centos 6 KVM qemu machine (e. g. the guest which runs "inside" a KVM on 172.16.1.1)

So here is my now fully working and correct qemu-ifup.sh for the above:

Code:
#!/bin/sh 
# 
# script to bring up the tun device in QEMU in bridged mode 
# first parameter is name of tap device (e.g. tap0)
#
# some constants specific to the local host - change to suit your host
#
ETH0IP=172.16.1.2
GATEWAY=172.16.1.9
BROADCAST=172.16.255.255
#
# First take eth0 down, then bring it up with IP 0.0.0.0 
#
/sbin/ifdown eth0
/sbin/ifconfig eth0 0.0.0.0 promisc up
#
# Bring up the tap device (name specified as first argument, by QEMU)
#
/usr/local/sbin/openvpn --mktun --dev $1 --user `id -un`
/sbin/ifconfig $1 0.0.0.0 promisc up
#
# create the bridge between eth0 and the tap device
#
/usr/sbin/brctl addbr br0
/usr/sbin/brctl addif br0 eth0
/usr/sbin/brctl addif br0 $1
# 
# only a single bridge so loops are not possible, turn off spanning tree protocol
#
/usr/sbin/brctl stp br0 off 
# 
# Bring up the bridge with ETH0IP and add the default route 
#
/sbin/ifconfig br0 $ETH0IP netmask 255.255.0.0 broadcast $BROADCAST
/sbin/route add default gw $GATEWAY
The problem line at the top was

Code:
ETH0IP=172.16.1.2
which I had as

Code:
ETH0IP=172.16.1.1
which was WRONG, since the Centos host's IP was 172.16.1.2

The problem line at the bottom was

Code:
/sbin/ifconfig br0 $ETH0IP netmask 255.255.0.0 broadcast $BROADCAST
which I had as

Code:
/sbin/ifconfig br0 $ETH0IP netmask 255.255.255.0 broadcast $BROADCAST
Once I fixed both of these everything started working - I can now ssh to 172.16.1.1 to get to the physical hosts's SSH daemon, and I can ssh to 172.16.1.2 to get to the KVM qemu guest's SSH daemon.

Also, both accesses are stable and I no longer get the SSH instances randomly disconnecting, they are now completely stable.

Clearly it was all my fault, I had let IPs clash in the bridging step and I was using the incorrect netmask for a class B network in the bridge setup script.

Once the above ifup script is done, I start my KVM Qemu instance - something like so:

Code:
sh -f /home/verisharepdc/Desktop/qemu-ifup.sh tap0

/usr/local/kvm/bin/qemu-system-x86_64 /home/verisharepdc/Desktop/vdisk.img -m 2048 -smp 2 -vnc 172.16.1.2:1 -net nic -net tap,ifname=tap0,script=no

sh -f /home/verisharepdc/Desktop/qemu-ifdown.sh tap0
in order to get it to use the tap0 device to be visible at 172.16.1.1 on the network.

Note that in the above, the VNC feed for the running QEmu KVM instance is available at 172.16.1.2:1 - if I pass this as is to TightVNC (for example) it works fine and I can see the XWindows instance running inside the qemu KVM instance.

For reference, here is my qemu-ifdown.sh:

Code:
#!/bin/sh 
# 
# Script to bring down and delete bridge br0 when QEMU exits 
# 
# Bring down eth0 and br0 
#
/sbin/ifdown eth0
/sbin/ifdown br0
/sbin/ifconfig br0 down 
# 
# Delete the bridge
#
/usr/sbin/brctl delbr br0 
# 
# bring up eth0 in "normal" mode 
#
/sbin/ifconfig eth0 -promisc
/sbin/ifup eth0 
#
# delete the tap device
#
/usr/local/sbin/openvpn --rmtun --dev $1
For reference as well, here is my physical machine's /etc/rc.local:

Code:
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/sbin/ifconfig eth0 172.16.1.2 netmask 255.255.0.0 broadcast 172.16.255.255 up
route add default gw 172.16.1.9 eth0
#service dhcpd start
#service named start
cp /etc/resolv.conf.bak /etc/resolv.conf
#smbd -D
#nmbd -D
modprobe kvm-intel
modprobe kvm
modprobe tun
sh -f /home/verisharepdc/Desktop/run_vm_unattended.sh &
- As you can see I assign it 172.16.1.2. Correlate with:

Code:
ETH0IP=172.16.1.2
in my qemu-ifup,.sh

Here's my Qemu KVM instance's /etc/rc.local:

Code:
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/sbin/ifconfig eth0 172.16.1.1 netmask 255.255.0.0 broadcast 172.16.255.255
route add default gw 172.16.1.6 eth0
#service dhcpd start
#service named start
cp /etc/resolv.conf.bak /etc/resolv.conf
#service nmb start
#service smb start
- As you can see I assign the VM 172.16.1.1

Hope this helps somebody...

Last edited by rylan76; 11-07-2012 at 06:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
no route to host on certain ports, kvm bridged guest el_tedward Linux - Networking 4 05-21-2012 01:11 AM
QEMU KVM bridged networking? iwanttolickazunyan Linux - Virtualization and Cloud 8 05-04-2012 11:21 AM
[SOLVED] Virtual XP guest shuts down when spice client disconnects from qemu-kvm allend Slackware 3 03-22-2012 09:34 AM
LXer: Set up RH VirtIO SCSI driver on Windows XP KVM at KVM-QEMU Instance on F14 LXer Syndicated Linux News 14 01-09-2011 12:21 PM
LXer: Install Oracle 11gR2 on SL6 KVM at KVM-QEMU Instance on SL 6 (alpha 3) LXer Syndicated Linux News 2 01-01-2011 06:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud

All times are GMT -5. The time now is 02:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration