---
title: "OpenFlow学习之Mininet安装"
author: "witsky"
date: "2013-12-03T13:43:11Z"
tags: []
categories: ["互联网"]
slug: "openflow-mininet"
draft: false
lastmod: "2013-12-03T13:43:11Z"
id: 1984264
url: "https://witsky.iteye.com/blog/1984264"
---

摘要：  
       这里给出了Mininet在ubuntu 12.04 KVM (qemu 1.0)的环境下测试

提前准备：宿主机ubuntu + 虚拟机KVM（官方给出的镜像是vmdk vmware格式，教程中给出了vmdk到KVM镜像qcow2装换）  
操作系统：ubuntu server amd64

安装完成后设置宿主机网络为  
[ubuntu@node2:~$](mailto:ubuntu@node2:~%24) cat /etc/network/interfaces  
\# This file describes the network interfaces available on your system  
\# and how to activate them. For more information, see interfaces(5).

\# The loopback network interface  
auto lo  
iface lo inet loopback

auto eth0  
iface eth0 inet static  
address 192.168.1.12  
gateway 192.168.1.1  
netmask 255.255.255.0

auto eth1  
iface eth1 inet manual

auto br0  
iface br0 inet static  
      address 192.168.1.18  
      gateway 192.168.1.1  
      netmask 255.255.255.0  
      bridge\_ports eth1  
      bridge\_stp on  
      bridge\_maxwait 0

重启网络服务，出现 br0网桥，这里假设有双网卡。单网卡也可以见后文。  
然后安装  KVM    
apt-get install -y kvm libvirt-bin  
添加用户到libvirtd组  
useradd $USER libvirtd

virsh net-destroy virtbr0 && virsh net-undefine virbr0  
然后重启libvirt-bin  
service libvirt-bin restart

 

对于单网卡使用KVM桥接方式的的用户

brctl addbr br0   
ifup br0  
ifconfig br0 promisc  
ifconfig br0 192.168.1.100 netmask 255.55.255.0 dev br0  
   
就可以使用KVM桥接方式网桥br0

 

下载 mininet vmware镜像  
   
wget  [https://github.com/downloads/mininet/mininet/mininet-2.0.0-113012-amd64-ovf.zip](https://github.com/downloads/mininet/mininet/mininet-2.0.0-113012-amd64-ovf.zip)

转化镜像 vmware 格式到 kvm支持的格式   
   
如果使用的是vmware  workstation 略过

使用KVM 并且  qemu-img 版本1.0 报错

qemu-img convert -O qcow2 mininet-vm-disk1.vmdk mininet-vm-disk1.qcow2  
报错

[ubuntu@node2:~/vmimg/mininet-ovf$](mailto:ubuntu@node2:~/vmimg/mininet-ovf%24) qemu-img convert -O qcow2 mininet-vm-disk1.vmdk mininet-vm-disk1.qcow2  
qemu-img: error while reading sector 131072: Invalid argument  
[ubuntu@node2:~/vmimg/mininet-ovf$](mailto:ubuntu@node2:~/vmimg/mininet-ovf%24) qemu-img convert -f vmdk -O qcow2 mininet-vm-disk1.vmdk mininet-vm-disk1.qcow2  
qemu-img: error while reading sector 131072: Invalid argument  
[ubuntu@node2:~/vmimg/mininet-ovf$](mailto:ubuntu@node2:~/vmimg/mininet-ovf%24)

conversion of a VMDK image with qemu-img failed with “error while reading sector 131072: Invalid argument”解决办法[http://serverfault.com/questions/429478/conversion-of-a-vmdk-image-with-qemu-img-failed-with-error-while-reading-sector](http://serverfault.com/questions/429478/conversion-of-a-vmdk-image-with-qemu-img-failed-with-error-while-reading-sector)  
原因是qemu的版本太低需要大于1. x

 

cd /tmp  
apt-get install apt-get install libglib2.0-dev  
wget [http://wiki.qemu.org/download/qemu-1.2.0.tar.bz2](http://wiki.qemu.org/download/qemu-1.2.0.tar.bz2)  
tar xf qemu-1.2.0.tar.bz2

cd qemu-1.2.0 && ./configure && make -j16 （时间较长）

在/etc/libvirt/qemu/下创建mininet.xml(xml文件格式定义KVM镜像配置具体google "qemu syntax")

sudo cat mininet.xml   
\[sudo] password for ubuntu:   
&lt;!--  
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE   
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:  
  virsh edit mininet  
or other application using the libvirt API.  
\--&gt;

&lt;domain type='kvm'&gt;  
  &lt;name&gt;mininet&lt;/name&gt;  
  &lt;uuid&gt;3245e642-05dc-bd23-6d0e-e7582dc00660&lt;/uuid&gt;  
  &lt;memory&gt;4194304&lt;/memory&gt;  
  &lt;currentMemory&gt;4194304&lt;/currentMemory&gt;  
  &lt;vcpu cpuset='1,3,5,7,9,11,13,15'&gt;4&lt;/vcpu&gt;  
  &lt;os&gt;  
    &lt;type arch='x86\_64' machine='pc-1.0'&gt;hvm&lt;/type&gt;  
    &lt;boot dev='hd'/&gt;  
  &lt;/os&gt;  
  &lt;features&gt;  
    &lt;acpi/&gt;  
    &lt;apic/&gt;  
    &lt;pae/&gt;  
  &lt;/features&gt;  
  &lt;cpu match='exact'&gt;  
    &lt;model&gt;Westmere&lt;/model&gt;  
    &lt;vendor&gt;Intel&lt;/vendor&gt;  
    &lt;feature policy='require' name='tm2'/&gt;  
    &lt;feature policy='require' name='est'/&gt;  
    &lt;feature policy='require' name='vmx'/&gt;  
    &lt;feature policy='require' name='ds'/&gt;  
    &lt;feature policy='require' name='ss'/&gt;  
    &lt;feature policy='require' name='vme'/&gt;  
    &lt;feature policy='require' name='rdtscp'/&gt;  
    &lt;feature policy='require' name='ht'/&gt;  
    &lt;feature policy='require' name='dca'/&gt;  
    &lt;feature policy='require' name='pbe'/&gt;  
    &lt;feature policy='require' name='tm'/&gt;  
    &lt;feature policy='require' name='pdpe1gb'/&gt;  
    &lt;feature policy='require' name='ds\_cpl'/&gt;  
    &lt;feature policy='require' name='xtpr'/&gt;  
    &lt;feature policy='require' name='acpi'/&gt;  
    &lt;feature policy='require' name='monitor'/&gt;  
  &lt;/cpu&gt;  
  &lt;clock offset='utc'/&gt;  
  &lt;on\_poweroff&gt;destroy&lt;/on\_poweroff&gt;  
  &lt;on\_reboot&gt;restart&lt;/on\_reboot&gt;  
  &lt;on\_crash&gt;restart&lt;/on\_crash&gt;  
  &lt;devices&gt;  
    &lt;emulator&gt;/usr/bin/kvm&lt;/emulator&gt;  
    &lt;disk type='file' device='disk'&gt;  
      &lt;driver name='qemu' type='qcow2'/&gt;  
      &lt;source file='/var/lib/libvirt/images/mininet-vm-disk1.qcow2'/&gt;  
      &lt;target dev='vda' bus='virtio'/&gt;  
      &lt;address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/&gt;  
    &lt;/disk&gt;  
    &lt;interface type='bridge'&gt;  
      &lt;mac address='52:54:00:31:05:76'/&gt;  
      &lt;source bridge='br0'/&gt;  
      &lt;model type='virtio'/&gt;  
      &lt;address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/&gt;  
    &lt;/interface&gt;  
    &lt;serial type='pty'&gt;  
      &lt;target port='0'/&gt;  
    &lt;/serial&gt;  
    &lt;console type='pty'&gt;  
      &lt;target type='serial' port='0'/&gt;  
    &lt;/console&gt;  
    &lt;input type='mouse' bus='ps2'/&gt;  
    &lt;graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/&gt;  
    &lt;sound model='ich6'&gt;  
      &lt;address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/&gt;  
    &lt;/sound&gt;  
    &lt;video&gt;  
      &lt;model type='cirrus' vram='9216' heads='1'/&gt;  
      &lt;address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/&gt;  
    &lt;/video&gt;  
    &lt;memballoon model='virtio'&gt;  
      &lt;address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/&gt;  
    &lt;/memballoon&gt;  
  &lt;/devices&gt;  
&lt;/domain&gt;

virsh create /etc/libvirt/qemu/mininet.xml

virsh consloe mininet

使用mininet:mininet

 

登陆修改

sudo ifconfig eth0 192.168.1.19 netmask 255.255.255.0 up

sudo route add default gw 192.168.1.1

sudo echo "nameserver 61.139.2.69" &gt; /etc/resolv.conf

sudo echo "nameserver 61.139.2.69" &gt; /etc/resolvconf/resolv.conf.d/base

ssh [mininet@192.168.1.19](mailto:mininet@192.168.1.19) 

账户 mininet

密码 mininet

配置静态IP

[root@mininet-vm](mailto:root@mininet-vm):~# cat /etc/network/interfaces  
\# This file describes the network interfaces available on your system  
\# and how to activate them. For more information, see interfaces(5).

\# The loopback network interface  
auto lo  
iface lo inet loopback

\# The primary network interface  
auto eth0  
iface eth0 inet static  
address 192.168.1.19  
gateway 192.168.1.1  
netmask 255.255.255.0  
[root@mininet-vm](mailto:root@mininet-vm):~#

 

参考：

- [http://mininet.github.com/vm-setup-notes/](http://mininet.github.com/vm-setup-notes/)
- [https://github.com/mininet/mininet/downloads/](https://github.com/mininet/mininet/downloads/)
- [http://serverfault.com/questions/429478/conversion-of-a-vmdk-image-with-qemu-img-failed-with-error-while-reading-sector](http://serverfault.com/questions/429478/conversion-of-a-vmdk-image-with-qemu-img-failed-with-error-while-reading-sector)

 

***注意这里给出的是ubuntu12.04版本的安装教程，在ubuntu 12.10 中qemu已经是1.2版本了，不存在qemu-img convert问题。***