Monday, August 26, 2013

Qemu tap network script

In the previous posts on Qemu networking, we have seen a manual procedure to setup up network access using Qemu tap device. Its a good way to get started but ideally we would like all these to be automated to ease our life. Here is a script that will automatically bring up the tap device and logically assign an IP address to that device. Name the script as qemu-ifup and place it under /etc directory. Qemu automatically picks up this script. Here is the qemu command line
$ qemu-system-x86_64 -hda cirros-0.3.0-x86_64-disk.img -netdev tap,id=net0,downscript=no -device e1000,netdev=net0

Script

$ cat /etc/qemu-ifup
#!/bin/bash                                                           
                                                                      
tap=$1
num=`echo $tap | sed 's/[a-zA-Z]*\([0-9]*\)/\1/'`                     

device_num=$(( $num + 1 ))

echo "tap = $tap, device_num = $device_num, num = $num"

#now generate a IP address
ip_addr=10.1.70.$device_num

ip link set $tap up


ip addr add $ip_addr/24 broadcast + dev $tap

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

iptables -I FORWARD 1 -i $tap -j ACCEPT

iptables -I FORWARD 1 -o $tap -m state --state RELATED,ESTABLISHED -j ACCEPT

[links]
http://tech-jnaan.blogspot.in/2013/08/configuring-qemu-tap-interface-for.html
* http://tech-jnaan.blogspot.in/2013/08/setting-up-qemu-with-nat.html

No comments:

Post a Comment