All of lore.kernel.org
 help / color / mirror / Atom feed
From: Avi Kivity <avi-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
To: Daniel Qarras <dqarras-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
Cc: kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
Subject: Re: Step by step instructions for KVM networking
Date: Sun, 25 Feb 2007 10:31:09 +0200	[thread overview]
Message-ID: <45E1494D.6010907@qumranet.com> (raw)
In-Reply-To: <20070222174843.13805.qmail-xjuEXE6AJAOvuULXzWHTWIglqE1Y4D90QQ4Iyu8u01E@public.gmane.org>

[-- Attachment #1: Type: text/plain, Size: 1330 bytes --]

Daniel Qarras wrote:
> Hi!
>
>   
>> In general the setup is exactly like qemu, so any howto for qemu is 
>> applicable.
>>
>> Here's what I do:
>>
>> - copy scripts/kvm from the kvm tarball to /etc/init.d, chkconfig and
>> start the kvm service. that sets up a bridge
>> - copy scripts/qemu-ifup into a new directory /etc/kvm.  make sure it
>> is executable.
>> - run qemu like this:
>>
>>    qemu-system-x86_64 -net nic -net tap,script=/etc/kvm/qemu-ifup
>> [other options]...
>>     
>
> Thanks for your kind answer but unfortunately I cannot find the scripts
> you mentioned from the kvm tar ball:
>
> sh-3.1$ find kvm-14 | grep scripts
> kvm-14/scripts
> kvm-14/scripts/run_img
> kvm-14/scripts/65-kvm.rules
> kvm-14/scripts/mkbootdisk
> sh-3.1$ 
>
> There is kvm-14/kvm script but that doesn't look at all like a Red Hat
> init script, it a Python script. I googled for qemu-ifup and found
> about dozen different kind of versions so I'm not sure which one I
> should use. But definetely the scripts you mentioned would be just what
> I was looking for!
>
>   

Sorry, the scripts didn't make it into the release.  I'm attaching them 
here.

If you're running Fedora, it looks like there's some bridging support in 
initscripts (I haven't tried it though).


-- 
error compiling committee.c: too many arguments to function


[-- Attachment #2: kvm --]
[-- Type: text/plain, Size: 4510 bytes --]

#!/bin/sh
# kvm init script              Takes care for all VMM tasks
#
# chkconfig: - 99 01
# description: The KVM is a kernel level Virtual Machine Monitor.  \
#              Currently it starts a bridge and attached eth0 for it

dir=$(dirname "$0")

ifnum=${ifnum:-$(ip route list | awk '/^default / { print $NF }' | sed 's/^[^0-9]*//')}
ifnum=${ifnum:-0}
switch=${sw0:-sw${ifnum}}
pif=${pif:-eth${ifnum}}
antispoof=${antispoof:-no}
command=$1

if [ -f /etc/sysconfig/network-scripts/ifcfg-${pif} ]; then
    . /etc/sysconfig/network-scripts/ifcfg-${pif}
fi

get_ip_info() {
    addr=`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'`
    gateway=$(ip route list | awk '/^default / { print $3 }')
}
    
do_ifup() {
     if [ ${addr} ] ; then
        ip addr flush $1 
        ip addr add ${addr} dev $1 
        ip link set dev $1 up 
     fi
}

link_exists()
{
    if ip link show "$1" >/dev/null 2>/dev/null
    then
        return 0
    else
        return 1
    fi
}

create_switch () {
    local switch=$1

    if [ ! -e "/sys/class/net/${switch}/bridge" ]; then
	brctl addbr ${switch} >/dev/null 2>&1
	brctl stp ${switch} off >/dev/null 2>&1
	brctl setfd ${switch} 0.1 >/dev/null 2>&1
    fi
    ip link set ${switch} up >/dev/null 2>&1
}

add_to_switch () {
    local switch=$1
    local dev=$2

    if [ -e "/sys/class/net/${switch}/brif/${dev}" ]; then
	ip link set ${dev} up >/dev/null 2>&1 || true
	return
    fi
    brctl addif ${switch} ${dev} >/dev/null 2>&1
    ip link set ${dev} up >/dev/null 2>&1
}

#taken from Xen
transfer_routes () {
    local src=$1
    local dst=$2
    # List all routes and grep the ones with $src in.
    # Stick 'ip route del' on the front to delete.
    # Change $src to $dst and use 'ip route add' to add.
    ip route list | sed -ne "
/dev ${src}\( \|$\)/ {
  h
  s/^/ip route del /
  P
  g
  s/${src}/${dst}/
  s/^/ip route add /
  P
  d
}" | sh -e
}


change_ips() {
    local src=$1
    local dst=$2

    #take care also for case we do not have /etc/sysconfig data (the switch as a src case)
    if [ -x $BOOTPROTO ]; then
        if [ -x $(pgrep dhclient) ];then
           BOOTPROTO="null"
        else
            BOOTPROTO="dhcp"
        fi
    fi

    if [ $BOOTPROTO = "dhcp" ]; then
        ifdown ${src} >/dev/null 2>&1 || true
        ip link set ${src} up >/dev/null 2>&1
        pkill dhclient >/dev/null 2>&1
	for ((i=0;i<3;i++)); do
	    pgrep dhclient >/dev/null 2>&1 || i=4	
   	    sleep 1
	done
        dhclient ${dst} >/dev/null 2>&1
    else
        get_ip_info ${src}
        ifdown ${src} >/dev/null 2>&1 || true
        ip link set ${src} up >/dev/null 2>&1
        do_ifup ${dst}
        transfer_routes ${src} ${dst}
        ip route add default via ${gateway} dev ${dst}
    fi
}
               
antispoofing () {
    iptables -P FORWARD DROP >/dev/null 2>&1
    iptables -F FORWARD >/dev/null 2>&1
    iptables -A FORWARD -m physdev --physdev-in ${dev} -j ACCEPT >/dev/null 2>&1
}

status () {
    local dev=$1
    local sw=$2
    
    echo '============================================================'
    ip addr show ${dev}
    ip addr show ${sw}
    echo ' '
    brctl show ${sw}
    echo ' '
    ip route list
    echo ' '
    route -n
    echo '============================================================'
    gateway=$(ip route list | awk '/^default / { print $3 }')
    ping -c 1 ${gateway} || true
    echo '============================================================'
}

start () {
    if [ "${switch}" = "null" ] ; then
	return
    fi

    create_switch ${switch}
    add_to_switch ${switch} ${pif}
    change_ips ${pif} ${switch}

    if [ ${antispoof} = 'yes' ] ; then
	antispoofing
    fi

    grep -q GenuineIntel /proc/cpuinfo && /sbin/modprobe kvm-intel
    grep -q AuthenticAMD /proc/cpuinfo && /sbin/modprobe kvm-amd
}

stop () {
    if [ "${switch}" = "null" ]; then
	return
    fi
    if ! link_exists "$switch"; then
	return
    fi

    change_ips ${switch} ${pif}
    ip link set ${switch} down
    brctl delbr ${switch}

    grep -q GenuineIntel /proc/cpuinfo && /sbin/modprobe -r kvm-intel
    grep -q AuthenticAMD /proc/cpuinfo && /sbin/modprobe -r kvm-amd
    /sbin/modprobe -r kvm
}


case "$command" in
    start)
	echo -n $"Starting KVM: "
	start
	echo
	;;
    
    stop)
	echo -n $"Shutting down KVM: "
	stop
	echo
	;;

    status)
	status ${pif} ${switch}
	;;

    *)
	echo "Unknown command: $command" >&2
	echo 'Valid commands are: start, stop, status' >&2
	exit 1
esac

[-- Attachment #3: qemu-ifup --]
[-- Type: text/plain, Size: 117 bytes --]

#!/bin/sh

switch=$(ip route list | awk '/^default / { print $NF }')
ifconfig $1 0.0.0.0 up
brctl addif ${switch} $1

[-- Attachment #4: Type: text/plain, Size: 345 bytes --]

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

[-- Attachment #5: Type: text/plain, Size: 186 bytes --]

_______________________________________________
kvm-devel mailing list
kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/kvm-devel

  parent reply	other threads:[~2007-02-25  8:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-21 18:57 Step by step instructions for KVM networking Daniel Qarras
     [not found] ` <786483.53715.qm-uAqfo9yIfQuvuULXzWHTWIglqE1Y4D90QQ4Iyu8u01E@public.gmane.org>
2007-02-22  9:17   ` Avi Kivity
     [not found]     ` <45DD5FA9.2010602-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-02-22 17:48       ` Daniel Qarras
     [not found]         ` <20070222174843.13805.qmail-xjuEXE6AJAOvuULXzWHTWIglqE1Y4D90QQ4Iyu8u01E@public.gmane.org>
2007-02-25  8:31           ` Avi Kivity [this message]
     [not found]             ` <45E1494D.6010907-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-02-25  8:50               ` Avi Kivity
     [not found]                 ` <45E14DE5.7050401-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2007-02-26 18:11                   ` Daniel Qarras
2007-03-25 21:10                     ` Steve Kwee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45E1494D.6010907@qumranet.com \
    --to=avi-atkuwr5tajbwk0htik3j/w@public.gmane.org \
    --cc=dqarras-/E1597aS9LQAvxtiuMwx3w@public.gmane.org \
    --cc=kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.