<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andreas Schneider &#187; kvm</title>
	<atom:link href="http://blog.cryptomilk.org/tag/kvm/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cryptomilk.org</link>
	<description>a cosmological pedestrian</description>
	<lastBuildDate>Mon, 23 Jan 2012 09:21:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Qemu (kvm) internal network setup</title>
		<link>http://blog.cryptomilk.org/2007/07/12/qemu-kvm-internal-network-setup/</link>
		<comments>http://blog.cryptomilk.org/2007/07/12/qemu-kvm-internal-network-setup/#comments</comments>
		<pubDate>Thu, 12 Jul 2007 19:19:42 +0000</pubDate>
		<dc:creator>Andreas Schneider</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[SUSE]]></category>
		<category><![CDATA[kvm]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[qemu]]></category>

		<guid isPermaLink="false">http://blog.cynapses.org/2007/07/12/qemu-kvm-internal-network-setup/</guid>
		<description><![CDATA[I got a new notebook, a nice Thinkpad T61 with virtualization technology. I need to run some Windows system for development so I've decided to use the Kernel based Virtual Machine (KVM). The VMs should communicate over an internal network but should have access to the internet and I want access via network to ...]]></description>
			<content:encoded><![CDATA[<p>I got a new notebook, a nice Thinkpad T61 with virtualization technology. I need to run some Windows system for development so I&#8217;ve decided to use the <a href="http://kvm.qumranet.com/">Kernel based Virtual Machine</a> (KVM). The VMs should communicate over an internal network but should have access to the internet and I want access via network to them. So I setup a bridge with TUN/TAP devices masqueraded to my normal interface.</p>
<pre>

               HOST            QEMU GUEST1
        +---------------+   +--------------+
        | 10.10.5.158   |   |              |
 LAN ---+---- eth0      |   |              |
        |               |   |              |    QEMU GUEST2
        |   +------+ +--+---+---- nic0     |   +--------------+
        |   | tap0---+  |   |192.168.100.5 |   |              |
        |   | tap1---+  |   +--------------+   |              |
        |   +------+ |  |                      |              |
        |     br0    +--+----------------------+---- nic0     |
        |192.168.100.254|                      |192.168.100.1 |
        +---------------+                      +--------------+
</pre>
<h3>Needed packages:</h3>
<p>tunctl (uml-utilities)<br />
bridge-utilities<br />
kvm</p>
<h3>Setup the network:</h3>
<p>Create a file call kvm-network with the following content and make it executeable.</p>
<pre>
#!/bin/bash

KVMNET_UID=1000
KVMNET_GID=$(grep kvm /etc/group | cut -d ':' -f 3)

# number of TUN/TAP devices to setup
NUM_OF_DEVICES=3

case $1 in
        start)
                modprobe kvm
                modprobe kvm_intel

                modprobe tun
                echo "Setting up bridge device br0"
                brctl addbr br0
                ifconfig br0 192.168.100.254 netmask 255.255.255.0 up
                for ((i=0; i < NUM_OF_DEVICES ; i++)); do
                        echo -n "Setting up "
                        tunctl -b -g ${KVMNET_GID} -t kvmnet$i
                        #tunctl -b -u ${KVMNET_UID} -t kvmnet$i
                        brctl addif br0 kvmnet$i
                        ifconfig kvmnet$i up 0.0.0.0 promisc
                done
                SuSEfirewall2 stop
                SuSEfirewall2
        ;;
        stop)
                for ((i=0; i < NUM_OF_DEVICES ; i++)); do
                        ifconfig kvmnet$i down
                        brctl delif br0 kvmnet$i
                        tunctl -d kvmnet$i
                done
                ifconfig br0 down
                brctl delbr br0
                SuSEfirewall2 stop
                SuSEfirewall2

                rmmod kvm_intel
                rmmod kvm
        ;;
        *)
                echo "Usage: $(basename $0) (start|stop)"
        ;;
esac
</pre>
<p>br0 is the gateway to the external network.</p>
<h3>Setting up the firewall:</h3>
<p>Edit /etc/sysconfig/SuSEfirewall and set the following variables:</p>
<pre>
FW_DEV_INT="br0 qtap0 qtap1 qtap2 qtap3 qtap4"
FW_ROUTE="yes"
FW_MASQUERADE="yes"
FW_MASQ_NETS="192.168.100.0/24"
FW_PROTECT_FROM_INT="no"
</pre>
<p>If you don't run a SUSE system use the following lines to setup masquerading:</p>
<pre>
echo "1" > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
</pre>
<p>On the guest you have to set the default gateway to 192.168.100.254 which is the bridge br0 and take a look in /etc/resolv.conf to get the name servers. I run a Windows 2003 Server as a guest which is the dhcp and name server for the other guests (Vista, several Linux installations).</p>
<h3>Setting up qemu</h3>
<h4>Guest 1:</h4>
<pre>
#!/bin/bash

qemu-kvm /path/to/vm.img \
          -net nic,model=rtl8139,macaddr=52:54:00:12:34:56 \
          -net tap,ifname=qtap0,script=no \
          -m 256 \
          -smp 1 \
          -usb \
          -usbdevice tablet \
          -localtime
</pre>
<h4>Guest 2:</h4>
<pre>
#!/bin/bash

qemu-kvm /path/to/vm2.img \
          -net nic,model=rtl8139,macaddr=52:54:00:12:34:57 \
          -net tap,ifname=qtap1,script=no \
          -m 256 \
          -smp 1 \
          -usb \
          -usbdevice tablet \
          -localtime
</pre>
<p><strong>Note that the VMs have different MAC addresses</strong>. It took me a long time to find why I couldn't ping from one guest to another <img src='http://blog.cryptomilk.org/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  By the way, one of the guests is running <a href="http://blog.cynapses.org/wp-content/uploads/2007/07/vista.png">Vista</a>, which runs smoothly on my machine with KVM.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://blog.cryptomilk.org/2007/07/12/qemu-kvm-internal-network-setup/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>

