1. Installer les paquets
apt-get install vde
2. lancer la commande suivante pour que tun apparaisse dans la liste des interfaces lors d'un ifconfig
vde_switch -tap tun -daemon
3. avoir le bon fichier /etc/qemu-ifup (je ne sais plus qui est l'auteur original de ce script, probablement Olivier mais ce n'est pas certain)
#!/bin/bash
#
###########################################################################
# Configuration of the tunN devices for usage with QEMU.
#
# Based on the tunconfig script from MOL.
#
# This script should be named /etc/qemu-ifup-sudo
#
# To use this script you must have...
#
# 0) kernel support for TUN devices
#
# 1) the device node /dev/net/tun with read/write permissions
#
# 2) invoked qemu with TUN support (this is the default)
#
# 3) configured /etc/sudoers to let you run this script
#
# An example /etc/sudoers entry for (3) would be:
#
# myself ALL=/etc/qemu-ifup-sudo
#
# If your linux box is configured as a firewall or a DHCP server or a
# router then you don't want to be running this script.
#
# Fabrice is working on a user-net implementation that will eventually
# obsolete this script.
#
#############################################################################
PROVIDE_DHCP=yes
DNS_REDIRECT=yes
IFCONFIG=/sbin/ifconfig
IPTABLES=/sbin/iptables
DHCPD=/usr/sbin/dhcpd
# uncomment to override nameserver autodetection
# NAMESERVER=10.0.0.1
####################################################################
TUN_DEV=${1:-tun}
ACTION=${2:-up}
TUN_NUM=0
NET_NUM=`expr 40 + $TUN_NUM`
TUN_NET=192.168.$NET_NUM.0
TUN_HOST=192.168.$NET_NUM.1
#########################################################
# Misc Checks
#########################################################
-x $IFCONFIG || {
echo "---> fatal: $IFCONFIG not installed" 1>&2
exit 1
}
-x $DHCPD || {
echo "---> warning: $DHCPD not installed" 1>&2
PROVIDE_DHCP=no
}
-x $IPTABLES || {
echo "---> fatal: $IPTABLES not installed" 1>&2
exit 1
}
$IPTABLES -L -n -t nat > /dev/null || {
echo "---> fatal: $IPTABLES indicates no NAT support" 1>&2
exit 1
}
-e /proc/sys/net/ipv4/ip_forward || {
echo "---> fatal: you don't have /proc/sys/net/ipv4/ip_forward" 1>&2
exit 1
}
#########################################################
# Remove old (possibly stale) ruleset
#########################################################
{
$IPTABLES -t nat -D POSTROUTING -s $TUN_NET/24 -d ! $TUN_NET/24 -j MASQUERADE
$IPTABLES -t nat -D PREROUTING -p tcp -i $TUN_DEV -d $TUN_HOST --dport 53 -j qemu-dns
$IPTABLES -t nat -D PREROUTING -p udp -i $TUN_DEV -d $TUN_HOST --dport 53 -j qemu-dns
$IPTABLES -t nat -F qemu-dns
} >& /dev/null
#########################################################
# Bring down interface
#########################################################
"$ACTION" = down && {
$IFCONFIG $TUN_DEV down
}
#########################################################
# Configure interface
#########################################################
"$ACTION" = up && {
# the dhcpd server can get stuck if the QEMU side of the
# tun device is shutdown uncleanly
-f /var/run/dhcpd.pid -a "$PROVIDE_... && {
echo "kill -9 stale dhcpd server"
kill -9 `cat /var/run/dhcpd.pid` > /dev/null 2>&1
}
# configure the interface
$IFCONFIG $TUN_DEV $TUN_HOST
# masquerade the tun network
$IPTABLES -t nat -A POSTROUTING -s $TUN_NET/24 -d ! $TUN_NET/24 -j MASQUERADE
# DNS redirection
"$DNS_REDIRECT" = yes && {
! "$NAMESERVER" && {
NAMESERVER=`grep ^nameserver /etc/resolv.conf | awk -- '{ print $2 ; exit 0; }'`
! "$NAMESERVER" && {
echo "Could not determine the nameserver (localhost is used)."
NAMESERVER=$TUN_HOST
}
}
# create a table for DNS redirection
$IPTABLES -t nat -N qemu-dns 2> /dev/null
$IPTABLES -t nat -A qemu-dns -j DNAT --to $NAMESERVER
# redirect tcp/udp port 53 (nameserver queries)
$IPTABLES -t nat -A PREROUTING -p tcp -i $TUN_DEV -d $TUN_HOST --dport 53 -j qemu-dns
$IPTABLES -t nat -A PREROUTING -p udp -i $TUN_DEV -d $TUN_HOST --dport 53 -j qemu-dns
}
}
#########################################################
# Generate DHCP configuration file
#########################################################
for N in `seq 40 50` ; do
T_NET=192.168.$N.0
T_HOST=192.168.$N.1
T_RANGE="192.168.$N.2 192.168.$N.100"
echo "subnet $T_NET netmask 255.255.255.0 {"
echo " option domain-name-servers $T_HOST;"
echo " option routers $T_HOST;"
echo " range $T_RANGE;"
echo "}"
done > /tmp/qemu-dhcpd-$$.conf
#########################################################
# Start the DHCP and IP forwarding
#########################################################
IFACES=`netstat -i | sed -n -e 's/^\(tun0-9\).*/\1/gp'`
if "$IFACES" ; then
"$PROVIDE_DHCP" = yes && $DHCPD -q -cf /tmp/qemu-dhcpd-$$.conf $IFACES
echo 1 > /proc/sys/net/ipv4/ip_forward
else
$IPTABLES -t nat -X qemu-dns >& /dev/null
echo 0 > /proc/sys/net/ipv4/ip_forward
fi
exit 0