Statistics
| Branch: | Tag: | Revision:

mininet / util / install.sh @ 1c0b54e5

History | View | Annotate | Download (15.3 KB)

1
#!/usr/bin/env bash
2

    
3
# Mininet install script for Ubuntu (and Debian Lenny)
4
# Brandon Heller (brandonh@stanford.edu)
5

    
6
# Fail on error
7
set -e
8
 
9
# Fail on unset var usage
10
set -o nounset
11

    
12
# Location of CONFIG_NET_NS-enabled kernel(s)
13
KERNEL_LOC=http://www.openflow.org/downloads/mininet
14

    
15
# Attempt to identify Linux release
16

    
17
DIST=Unknown
18
RELEASE=Unknown
19
CODENAME=Unknown
20
ARCH=`uname -m`
21
if [ "$ARCH" = "x86_64" ]; then ARCH="amd64"; fi
22
if [ "$ARCH" = "i686" ]; then ARCH="i386"; fi
23

    
24
test -e /etc/debian_version && DIST="Debian"
25
grep Ubuntu /etc/lsb-release &> /dev/null && DIST="Ubuntu"
26
if [ "$DIST" = "Ubuntu" ] || [ "$DIST" = "Debian" ]; then
27
    install='sudo apt-get -y install'
28
    remove='sudo apt-get -y remove'
29
    pkginst='sudo dpkg -i'
30
    # Prereqs for this script
31
    if ! which lsb_release &> /dev/null; then
32
        $install lsb-release
33
    fi
34
    if ! which bc &> /dev/null; then
35
        $install bc
36
    fi 
37
fi
38
if which lsb_release &> /dev/null; then
39
    DIST=`lsb_release -is`
40
    RELEASE=`lsb_release -rs`
41
    CODENAME=`lsb_release -cs`
42
fi
43
echo "Detected Linux distribution: $DIST $RELEASE $CODENAME $ARCH"
44

    
45
# Kernel params
46

    
47
if [ "$DIST" = "Ubuntu" ]; then
48
    if [ "$RELEASE" = "10.04" ]; then
49
        KERNEL_NAME='3.0.0-15-generic'
50
    else
51
        KERNEL_NAME=`uname -r`
52
    fi
53
    KERNEL_HEADERS=linux-headers-${KERNEL_NAME}
54
elif [ "$DIST" = "Debian" ] && [ "$ARCH" = "i386" ] && [ "$CODENAME" = "lenny" ]; then
55
    KERNEL_NAME=2.6.33.1-mininet
56
    KERNEL_HEADERS=linux-headers-${KERNEL_NAME}_${KERNEL_NAME}-10.00.Custom_i386.deb
57
    KERNEL_IMAGE=linux-image-${KERNEL_NAME}_${KERNEL_NAME}-10.00.Custom_i386.deb
58
else
59
    echo "Install.sh currently only supports Ubuntu and Debian Lenny i386."
60
    exit 1
61
fi
62

    
63
# More distribution info
64
DIST_LC=`echo $DIST | tr [A-Z] [a-z]` # as lower case
65

    
66
# Kernel Deb pkg to be removed:
67
KERNEL_IMAGE_OLD=linux-image-2.6.26-2-686
68

    
69
DRIVERS_DIR=/lib/modules/${KERNEL_NAME}/kernel/drivers/net
70

    
71
OVS_RELEASE=1.4.0
72
OVS_PACKAGE_LOC=https://github.com/downloads/mininet/mininet
73
OVS_BUILDSUFFIX=-2
74
OVS_PACKAGE_NAME=ovs-$OVS_RELEASE-core-$DIST_LC-$RELEASE-$ARCH$OVS_BUILDSUFFIX.tar
75
OVS_SRC=~/openvswitch
76
OVS_TAG=v$OVS_RELEASE
77
OVS_BUILD=$OVS_SRC/build-$KERNEL_NAME
78
OVS_KMODS=($OVS_BUILD/datapath/linux/{openvswitch_mod.ko,brcompat_mod.ko})
79

    
80
function kernel {
81
    echo "Install Mininet-compatible kernel if necessary"
82
    sudo apt-get update
83
    if [ "$DIST" = "Ubuntu" ] &&  [ "$RELEASE" = "10.04" ]; then
84
        $install linux-image-$KERNEL_NAME
85
    elif [ "$DIST" = "Debian" ]; then
86
        # The easy approach: download pre-built linux-image and linux-headers packages:
87
        wget -c $KERNEL_LOC/$KERNEL_HEADERS
88
        wget -c $KERNEL_LOC/$KERNEL_IMAGE
89

    
90
        # Install custom linux headers and image:
91
        $pkginst $KERNEL_IMAGE $KERNEL_HEADERS
92

    
93
        # The next two steps are to work around a bug in newer versions of
94
        # kernel-package, which fails to add initrd images with the latest kernels.
95
        # See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=525032
96
        # Generate initrd image if the .deb didn't install it:
97
        if ! test -e /boot/initrd.img-${KERNEL_NAME}; then
98
            sudo update-initramfs -c -k ${KERNEL_NAME}
99
        fi
100
        
101
        # Ensure /boot/grub/menu.lst boots with initrd image:
102
        sudo update-grub
103

    
104
        # The default should be the new kernel. Otherwise, you may need to modify 
105
        # /boot/grub/menu.lst to set the default to the entry corresponding to the
106
        # kernel you just installed.
107
    fi
108
}
109

    
110
function kernel_clean {
111
    echo "Cleaning kernel..."
112

    
113
    # To save disk space, remove previous kernel
114
    $remove $KERNEL_IMAGE_OLD
115

    
116
    # Also remove downloaded packages:
117
    rm -f ~/linux-headers-* ~/linux-image-*
118
}
119

    
120
# Install Mininet deps
121
function mn_deps {
122
    echo "Installing Mininet dependencies"
123
    $install gcc make screen psmisc xterm ssh iperf iproute \
124
        python-setuptools python-networkx
125

    
126
    if [ "$DIST" = "Ubuntu" ] && [ "$RELEASE" = "10.04" ]; then
127
        echo "Upgrading networkx to avoid deprecation warning"
128
        sudo easy_install --upgrade networkx
129
    fi
130

    
131
    # Add sysctl parameters as noted in the INSTALL file to increase kernel 
132
    # limits to support larger setups:
133
    sudo su -c "cat $HOME/mininet/util/sysctl_addon >> /etc/sysctl.conf"
134

    
135
    # Load new sysctl settings:
136
    sudo sysctl -p
137
    
138
    echo "Installing Mininet core"
139
    pushd ~/mininet
140
    sudo make install
141
    popd
142
}
143

    
144
# The following will cause a full OF install, covering:
145
# -user switch
146
# -dissector
147
# The instructions below are an abbreviated version from
148
# http://www.openflowswitch.org/wk/index.php/Debian_Install
149
# ... modified to use Debian Lenny rather than unstable.
150
function of {
151
    echo "Installing OpenFlow and OpenFlow WireShark dissector..."
152
    cd ~/
153
    $install git-core autoconf automake autotools-dev pkg-config \
154
		make gcc libtool libc6-dev 
155
    git clone git://openflowswitch.org/openflow.git
156
    cd ~/openflow
157

    
158
    # Patch controller to handle more than 16 switches
159
    patch -p1 < ~/mininet/util/openflow-patches/controller.patch
160

    
161
    # Resume the install:
162
    ./boot.sh
163
    ./configure
164
    make
165
    sudo make install
166

    
167
    # Install dissector:
168
    $install wireshark libgtk2.0-dev
169
    cd ~/openflow/utilities/wireshark_dissectors/openflow
170
    make
171
    sudo make install
172

    
173
    # The OpenFlow wireshark plugin does not install to the correct dir.
174
    # The correct way would be to fix the install script.
175
    # For now, just copy it to the global WS plugin dir.
176
    # Tested on Ubuntu 11.04.
177
    if [ -e /var/packet-openflow.so ]; then
178
        WS_DIR=`ls -d /usr/lib/wireshark/libwireshark* | head -1`
179
        sudo cp /var/packet-openflow.so $WS_DIR/plugins/
180
    fi
181

    
182
    # Copy coloring rules: OF is white-on-blue:
183
    mkdir -p ~/.wireshark
184
    cp ~/mininet/util/colorfilters ~/.wireshark
185

    
186
    # Remove avahi-daemon, which may cause unwanted discovery packets to be 
187
    # sent during tests, near link status changes:
188
    $remove avahi-daemon
189

    
190
    # Disable IPv6.  Add to /etc/modprobe.d/blacklist:
191
    if [ "$DIST" = "Ubuntu" ]; then
192
        BLACKLIST=/etc/modprobe.d/blacklist.conf
193
    else
194
        BLACKLIST=/etc/modprobe.d/blacklist
195
    fi
196
    sudo sh -c "echo 'blacklist net-pf-10\nblacklist ipv6' >> $BLACKLIST"
197
    cd ~
198
}
199

    
200
# Install Open vSwitch
201
# Instructions derived from OVS INSTALL, INSTALL.OpenFlow and README files.
202
function ovs {
203
    echo "Installing Open vSwitch..."
204

    
205
    # Required for module build/dkms install
206
    $install $KERNEL_HEADERS
207

    
208
    # First see if we have packages
209
    # XXX wget -c seems to fail from github/amazon s3
210
    cd /tmp
211
    if wget $OVS_PACKAGE_LOC/$OVS_PACKAGE_NAME; then
212
	# Install dkms dependencies
213
	$install patch dkms fakeroot
214
        tar xf $OVS_PACKAGE_NAME
215
        orig=`tar tf $OVS_PACKAGE_NAME`
216
        # Now install packages in reasonable dependency order
217
        order='dkms common pki openvswitch-switch brcompat controller'
218
        pkgs=""
219
        for p in $order; do
220
            pkg=`echo "$orig" | grep $p`
221
	    # Annoyingly, things seem to be missing without this flag
222
            $pkginst --force-confmiss $pkg
223
        done
224
        # Switch can run on its own, but 
225
        # Mininet should control the controller
226
	if [ -e /etc/init.d/openvswitch-controller ]; then
227
            if sudo service openvswitch-controller stop; then
228
                echo "Stopped running controller"
229
            fi
230
            sudo update-rc.d openvswitch-controller disable
231
        fi
232
        echo "Done (hopefully) installing packages"
233
        cd ~
234
        return
235
    fi
236

    
237
    # Otherwise try distribution's OVS packages
238
    if [ "$DIST" = "Ubuntu" ] && [ `echo "$RELEASE >= 11.10" | bc` = 1 ]; then
239
	if $install openvswitch-switch openvswitch-controller; then
240
            return
241
        fi
242
    fi
243

    
244
    $install pkg-config gcc make python-dev libssl-dev libtool
245

    
246
    if [ "$DIST" = "Debian" ]; then
247
        if [ "$CODENAME" = "lenny" ]; then
248
            $install git-core
249
            # Install Autoconf 2.63+ backport from Debian Backports repo:
250
            # Instructions from http://backports.org/dokuwiki/doku.php?id=instructions
251
            sudo su -c "echo 'deb http://www.backports.org/debian lenny-backports main contrib non-free' >> /etc/apt/sources.list"
252
            sudo apt-get update
253
            sudo apt-get -y --force-yes install debian-backports-keyring
254
            sudo apt-get -y --force-yes -t lenny-backports install autoconf
255
        fi
256
    else
257
        $install git
258
    fi    
259

    
260
    # Install OVS from release
261
    cd ~/
262
    git clone git://openvswitch.org/openvswitch $OVS_SRC
263
    cd $OVS_SRC
264
    git checkout $OVS_TAG
265
    ./boot.sh
266
    BUILDDIR=/lib/modules/${KERNEL_NAME}/build
267
    if [ ! -e $BUILDDIR ]; then
268
        echo "Creating build sdirectory $BUILDDIR"
269
        sudo mkdir -p $BUILDDIR
270
    fi
271
    opts="--with-linux=$BUILDDIR"
272
    mkdir -p $OVS_BUILD
273
    cd $OVS_BUILD
274
    ../configure $opts
275
    make
276
    sudo make install
277

    
278
    modprobe
279
}
280

    
281
function remove_ovs {
282
    pkgs=`dpkg --get-selections | grep openvswitch | awk '{ print $1;}'`
283
    echo "Removing existing Open vSwitch packages:"
284
    echo $pkgs
285
    if ! $remove $pkgs; then
286
        echo "Not all packages removed correctly"
287
    fi
288
    # For some reason this doesn't happen
289
    if scripts=`ls /etc/init.d/*openvswitch* 2>/dev/null`; then
290
        echo $scripts
291
        for s in $scripts; do
292
            s=$(basename $s)
293
            echo SCRIPT $s
294
            sudo service $s stop
295
            sudo rm -f /etc/init.d/$s
296
            sudo update-rc.d -f $s remove
297
        done
298
    fi
299
    echo "Done removing OVS"
300
}
301

    
302
# Install NOX with tutorial files
303
function nox {
304
    echo "Installing NOX w/tutorial files..."
305

    
306
    # Install NOX deps:
307
    $install autoconf automake g++ libtool python python-twisted \
308
		swig libssl-dev make
309
    if [ "$DIST" = "Debian" ]; then
310
        $install libboost1.35-dev
311
    elif [ "$DIST" = "Ubuntu" ]; then
312
        $install python-dev libboost-dev 
313
        $install libboost-filesystem-dev
314
        $install libboost-test-dev
315
    fi
316
    # Install NOX optional deps:
317
    $install libsqlite3-dev python-simplejson
318

    
319
    # Fetch NOX destiny
320
    cd ~/
321
    git clone git://noxrepo.org/nox noxcore
322
    cd noxcore
323
    git checkout -b destiny remotes/origin/destiny
324

    
325
    # Apply patches
326
    git checkout -b tutorial-destiny
327
    git am ~/mininet/util/nox-patches/*.patch
328

    
329
    # Build
330
    ./boot.sh
331
    mkdir build
332
    cd build
333
    ../configure
334
    make -j3
335
    #make check
336

    
337
    # Add NOX_CORE_DIR env var:
338
    sed -i -e 's|# for examples$|&\nexport NOX_CORE_DIR=~/noxcore/build/src|' ~/.bashrc
339

    
340
    # To verify this install:
341
    #cd ~/noxcore/build/src
342
    #./nox_core -v -i ptcp:
343
}
344

    
345
# Install OFtest
346
function oftest {
347
    echo "Installing oftest..."
348

    
349
    # Install deps:
350
    $install tcpdump python-scapy
351

    
352
    # Install oftest:
353
    cd ~/
354
    git clone git://openflow.org/oftest
355
    cd oftest
356
    cd tools/munger
357
    sudo make install
358
}
359

    
360
# Install cbench
361
function cbench {
362
    echo "Installing cbench..."
363
    
364
    $install libsnmp-dev libpcap-dev
365
    cd ~/
366
    git clone git://openflow.org/oflops.git
367
    cd oflops
368
    sh boot.sh || true # possible error in autoreconf, so run twice
369
    sh boot.sh
370
    ./configure --with-openflow-src-dir=$HOME/openflow
371
    make
372
    sudo make install || true # make install fails; force past this
373
}
374

    
375
function other {
376
    echo "Doing other setup tasks..."
377

    
378
    # Enable command auto completion using sudo; modify ~/.bashrc:
379
    sed -i -e 's|# for examples$|&\ncomplete -cf sudo|' ~/.bashrc
380

    
381
    # Install tcpdump and tshark, cmd-line packet dump tools.  Also install gitk,
382
    # a graphical git history viewer.
383
    $install tcpdump tshark gitk
384

    
385
    # Install common text editors
386
    $install vim nano emacs
387

    
388
    # Install NTP
389
    $install ntp
390

    
391
    # Set git to colorize everything.
392
    git config --global color.diff auto
393
    git config --global color.status auto
394
    git config --global color.branch auto
395

    
396
    # Reduce boot screen opt-out delay. Modify timeout in /boot/grub/menu.lst to 1:
397
    if [ "$DIST" = "Debian" ]; then
398
        sudo sed -i -e 's/^timeout.*$/timeout         1/' /boot/grub/menu.lst
399
    fi
400

    
401
    # Clean unneeded debs:
402
    rm -f ~/linux-headers-* ~/linux-image-*
403
}
404

    
405
# Script to copy built OVS kernel module to where modprobe will
406
# find them automatically.  Removes the need to keep an environment variable
407
# for insmod usage, and works nicely with multiple kernel versions.
408
#
409
# The downside is that after each recompilation of OVS you'll need to
410
# re-run this script.  If you're using only one kernel version, then it may be
411
# a good idea to use a symbolic link in place of the copy below.
412
function modprobe {
413
    echo "Setting up modprobe for OVS kmod..."
414

    
415
    sudo cp $OVS_KMODS $DRIVERS_DIR
416
    sudo depmod -a ${KERNEL_NAME}
417
}
418

    
419
function all {
420
    echo "Running all commands..."
421
    kernel
422
    mn_deps
423
    of
424
    ovs
425
    nox
426
    oftest
427
    cbench
428
    other
429
    echo "Please reboot, then run ./mininet/util/install.sh -c to remove unneeded packages."
430
    echo "Enjoy Mininet!"
431
}
432

    
433
# Restore disk space and remove sensitive files before shipping a VM.
434
function vm_clean {
435
    echo "Cleaning VM..."
436
    sudo apt-get clean
437
    sudo rm -rf /tmp/*
438
    sudo rm -rf openvswitch*.tar.gz
439

    
440
    # Remove sensistive files
441
    history -c  # note this won't work if you have multiple bash sessions
442
    rm -f ~/.bash_history  # need to clear in memory and remove on disk
443
    rm -f ~/.ssh/id_rsa* ~/.ssh/known_hosts
444
    sudo rm -f ~/.ssh/authorized_keys*
445

    
446
    # Remove Mininet files
447
    #sudo rm -f /lib/modules/python2.5/site-packages/mininet*
448
    #sudo rm -f /usr/bin/mnexec
449

    
450
    # Clear optional dev script for SSH keychain load on boot
451
    rm -f ~/.bash_profile
452

    
453
    # Clear git changes
454
    git config --global user.name "None"
455
    git config --global user.email "None"
456

    
457
    # Remove mininet install script
458
    rm -f install-mininet.sh
459
}
460

    
461
function usage {
462
    printf 'Usage: %s [-acdfhkmntvxy]\n\n' $(basename $0) >&2
463
    
464
    printf 'This install script attempts to install useful packages\n' >&2
465
    printf 'for Mininet. It should (hopefully) work on Ubuntu 10.04, 11.10\n' >&2
466
    printf 'and Debian 5.0 (Lenny). If you run into trouble, try\n' >&2
467
    printf 'installing one thing at a time, and looking at the \n' >&2
468
    printf 'specific installation function in this script.\n\n' >&2
469

    
470
    printf 'options:\n' >&2
471
    printf -- ' -a: (default) install (A)ll packages - good luck!\n' >&2
472
    printf -- ' -b: install controller (B)enchmark (oflops)\n' >&2
473
    printf -- ' -c: (C)lean up after kernel install\n' >&2
474
    printf -- ' -d: (D)elete some sensitive files from a VM image\n' >&2    
475
    printf -- ' -f: install open(F)low\n' >&2
476
    printf -- ' -h: print this (H)elp message\n' >&2
477
    printf -- ' -k: install new (K)ernel\n' >&2
478
    printf -- ' -m: install Open vSwitch kernel (M)odule from source dir\n' >&2
479
    printf -- ' -n: install mini(N)et dependencies + core files\n' >&2
480
    printf -- ' -r: remove existing Open vSwitch packages\n' >&2
481
    printf -- ' -t: install o(T)her stuff\n' >&2
482
    printf -- ' -v: install open (V)switch\n' >&2
483
    printf -- ' -x: install NO(X) OpenFlow controller\n' >&2
484
    printf -- ' -y: install (A)ll packages\n' >&2    
485
    
486
    exit 2
487
}
488

    
489
if [ $# -eq 0 ]
490
then
491
    all
492
else
493
    while getopts 'abcdfhkmnrtvx' OPTION
494
    do
495
      case $OPTION in
496
      a)    all;;
497
      b)    cbench;;
498
      c)    kernel_clean;;
499
      d)    vm_clean;;
500
      f)    of;;
501
      h)    usage;;
502
      k)    kernel;;
503
      m)    modprobe;;
504
      n)    mn_deps;;
505
      r)    remove_ovs;;
506
      t)    other;;
507
      v)    ovs;;
508
      x)    nox;;
509
      ?)    usage;;
510
      esac
511
    done
512
    shift $(($OPTIND - 1))
513
fi