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

#!/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
#!/bin/sh

switch=$(ip route list | awk '/^default / { print $NF }')
ifconfig $1 0.0.0.0 up
brctl addif ${switch} $1
-------------------------------------------------------------------------
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
_______________________________________________
kvm-devel mailing list
kvm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/kvm-devel

Reply via email to