summaryrefslogtreecommitdiffstats
path: root/kernel/networking/openvswitch/br_to_vswitch/runtest.sh
blob: acd0b4669216e48ee06879d957740f1bd8d1945b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# vim: dict=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   runtest.sh of /kernel/networking/openvswitch/br_to_vswitch
#   Description: switch all bridge interfaces over to openvswitch
#   Author: Bill Peck <bpeck@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#   Copyright (c) 2012 Red Hat, Inc. All rights reserved.
#
#   This copyrighted material is made available to anyone wishing
#   to use, modify, copy, or redistribute it subject to the terms
#   and conditions of the GNU General Public License version 2.
#
#   This program is distributed in the hope that it will be
#   useful, but WITHOUT ANY WARRANTY; without even the implied
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#   PURPOSE. See the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Include Beaker environment
. /usr/bin/rhts-environment.sh || exit 1

PACKAGE="openvswitch"
BRIDGES=${BRIDGES:="$(brctl show | awk 'NR>1 && /^[^\t]/ {printf $1" "}')"}
count=0

convert_bridge()
{
bridge=$1
vswitch=$2

cat <<EOF >/etc/sysconfig/network-scripts/ifcfg-$vswitch
DEVICE=$vswitch
ONBOOT=yes
DEVICETYPE=ovs
TYPE=OVSBridge
HOTPLUG=no
EOF

bridgemac=$(cat /sys/class/net/$bridge/address)
for intf in $(ls /sys/class/net); do
    if [ "$intf" = "$bridge" ]; then 
        continue
    fi
    devicemac=$(cat /sys/class/net/$intf/address)
    if [ "$devicemac" = "$bridgemac" ]; then
        device=$intf
        break
    fi
done
if [ -n "$device" ]; then
cat <<EOF >>/etc/sysconfig/network-scripts/ifcfg-$vswitch
OVSBOOTPROTO="dhcp"
OVSDHCPINTERFACES="$device"
EOF
fi

# Remove the old bridge
if [ -e "/etc/sysconfig/network-scripts/ifcfg-$bridge" ]; then
    rm -f "/etc/sysconfig/network-scripts/ifcfg-$bridge"
fi
}

convert_libvirt()
{
# Convert libvirt guests using $bridge as well.
bridge=$1
vswitch=$2

for virt in $(ls /etc/libvirt/qemu/*.xml); do
    echo Converting $virt from bridge to openvswitch
    grep -w $vswitch $virt
    if [ $? -ne 0 ];then 
        sed  -ie "s/$bridge/$vswitch/; /$vswitch/ a \<virtualport type=\"openvswitch\"/>" $virt
        virsh define --file $virt
    fi
done
}

convert_port()
{
device=$1
vswitch=$2

# Don't attempt to convert ports which are dyamic
if [ -e "/etc/sysconfig/network-scripts/ifcfg-$device" ]; then
cat <<EOF >/etc/sysconfig/network-scripts/ifcfg-$device
DEVICE=$device
ONBOOT=yes
DEVICETYPE=ovs
TYPE=OVSPort
OVS_BRIDGE=$vswitch
BOOTPROTO=none
HOTPLUG=no
EOF
fi
}

# Install $PACKAGE if not installed and activate.
rpm -q $PACKAGE || fail "$PACKAGE not installed"
chkconfig --add $PACKAGE
service $PACKAGE start

for bridge in $BRIDGES; do
    vswitch="ovsbridge$count"
    count=$(($count + 1))

    # Convert /etc/sysconfig/network-scripts/ifcfg-br*
    convert_bridge $bridge $vswitch
    convert_libvirt $bridge $vswitch
    # Remove interfaces from br0 and add them to vlan-br
    while [ "$(brctl show $bridge | awk "/$bridge/ {print \$4}")" != "" ]; do
        intf=$(brctl show $bridge | awk "/$bridge/ {print \$4}")
        brctl delif $bridge $intf
        convert_port $intf $vswitch && report_result $TEST/$intf PASS 0 || report_result $TEST/$intf FAIL $?
    done
done

# Stop all virt guests
cmd=""
for virt in $(virsh list | awk 'NR>2 {print $2}'); do
    virsh destroy $virt
    # build delayed start command
    cmd="virsh start $virt; $cmd"
done

# Restart network using openvswitch
service network restart

# Restart virt guests
sh -c "$cmd"
ovs-vsctl show