Archive | June 2013

KVM virtualization with Debian GNU/Linux in 7 steps

1. Install required packages

# apt-get install bridge-utils kvm

2. Create an empty kvm virtual machine image:

# kvm-img create vm.qcow2 -f qcow2 20G

3. Prepare a bridge so you can attach to it the net interface of your vm.

3a. Setup your eth0 interface to capture all the traffic:

# ifconfig eth0 promisc up

3b. Create the bridge interface:

# brctl addbr br0

3c. Put your eth0 interface in the bridge (so it captures all the wire traffic and sends it to all the others interfaces in the bridge and vice-versa):

# brctl addif br0 eth0

4. Restore your network connection by the br0 interface (optional)

4a. Bring up your bridge interface and give to it an address (so you can use it as your ip address):

# ifconfig br0 <your_ip> <your_netmask> up

4b. Remember to restore your default gateway:

# ip route add default via <gateway_ip>

5. Start your virtual machine (-boot d to install Debian from the ~/iso/debian.iso image, optional)

# kvm -hda vm.qcow2 -cdrom ~/iso/debian.iso -boot d -net nic,vlan=0 -net tap,vlan=0,ifname=tapvm

6. Add tap interface of vm to your bridge

# brctl addif br0 tapvm

7. Enable forwarding and tell to iptables to allow tapvm traffic to flow through your pc

# sysctl -w net.ipv4.ip_forward=1
# iptables -A FORWARD -m physdev --physdev-out tapvm -j ACCEPT
# iptables -A FORWARD -m physdev --physdev-in tapvm -j ACCEPT