adding onos-build-vm.py and vm-setup.sh

Change-Id: I36de694aced53ba428352adeee9f1f18a2ecfa27
diff --git a/README b/README
index d7c128b..cf9d73f 100644
--- a/README
+++ b/README
@@ -1 +1,31 @@
-ONOS VM
+# ONOS VM Build Script
+
+### Setup
+First, install the build.py dependencies:
+
+$ ~/mininet/util/vm/build.py --depend
+
+Also, if you will be running this script in a VM, make sure that it supports nested virtualization (for KVM).
+In VMWare Fusion, this can be enabled in: Settings > Processors & Memory > Advanced Options > Enable hypervisor application in this virtual machine
+
+### Building a base image (Mininet VM)
+The first step is to build a base image. We will use a Mininet VM as a base image because it already has Mininet and a bunch of oother dependencies installed. We can do this using the following command:
+
+$ ~/mininet/util/vm/build.py raring64server
+
+Note: This will download the ISO, install Ubuntu, and install Mininet; it may take about 15 minutes on a Macbook Pro.
+
+The image will be placed in a timestamped folder in the current directory. (e.g. mn-raring64server-140606-19-21-35)
+
+### Building the ONOS VM
+The ONOS VM is built from an existing VM image (probably the base image that we just created).
+From the ONOS-VM directory:
+
+$ ./onos-build-vm.py -i mn-raring64server-*/mininet-vm-x86_64.vmdk -o onos-vm-v1
+Note: You should replace the image file with the correct directory.
+
+This script will boot the image and run the installONOS function (which just downloads a script and runs it).
+
+This will create onos-vm-v1.ovf and onos-vm-v1.vmdk
+
+To change what is installed on the VM, you should edit vm-setup.sh
diff --git a/onos-build-vm.py b/onos-build-vm.py
new file mode 100755
index 0000000..21e86d7
--- /dev/null
+++ b/onos-build-vm.py
@@ -0,0 +1,75 @@
+#!/usr/bin/python
+
+import os
+import sys
+import re
+import argparse
+from stat import ST_MODE, ST_SIZE
+from subprocess import check_output
+
+# Add the Mininet build script directory to the path, so we can import it
+#TODO do a better job finding Mininet
+path = os.path.join( '~', 'mininet', 'util', 'vm' )
+path = os.path.expanduser( path )
+if os.path.isdir( path ):
+    sys.path.append( path )
+else:
+    print 'Cannot find Mininet build script. Exiting...'
+    sys.exit( 1 )
+
+# Import required functions from the Mininet build script
+from build import bootAndRun, Prompt, OVFOSNameID, generateOVF, log, build, run
+
+def installONOS( vm, prompt=Prompt ):
+    # start with sendline
+    #TODO consider resizing the HDD
+    url = 'https://gerrit.onlab.us/gitweb?p=ONOS-VM.git;a=blob_plain;hb=refs/heads/master;f=vm-setup.sh'
+    vm.sendline( 'wget -O - %s | bash' % url )
+    # end with expect prompt
+    vm.expect( prompt )
+
+def vmdk2size( vmdk ):
+    "Return virtual disk size (in bytes) of vmdk image"
+    output = check_output( [ 'file', vmdk ] )
+    assert 'disk image' in output
+    return os.stat( vmdk )[ ST_SIZE ]
+
+def buildONOS( image, out, flavor='raring64server', installMemory=1024, ovfMemory=2048  ):
+    if not image:
+        # build VM from flavor
+        #build( flavor )
+        pass
+
+    bootAndRun( image, runFunction=installONOS, memory=installMemory, outputFile=out )
+
+    size = vmdk2size( out + '.vmdk' ) # get vmdk size
+    ovfname = out
+    osname, osid = OVFOSNameID( flavor )
+    print osname, osid
+
+    ovf = generateOVF( name=ovfname, osname=osname, osid=osid,
+                       diskname=out+'.vmdk', disksize=size, mem=2048, cpus=4,
+                       vmname='ONOS-VM', vminfo='An ONOS VM' )
+    log( '* Generated OVF descriptor file', ovf )
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser( description='ONOS VM build script' )
+    parser.add_argument( '-i', '--image', metavar='image', required=True,
+                         help='Build from an existing VM image' )
+    parser.add_argument( '-f', '--flavor', default='raring64server',
+                         help='VM flavor to build (e.g. raring64server)' )
+    parser.add_argument( 'scripts', nargs='*',
+                         help='VM flavor(s) to build (e.g. raring64server)' )
+    parser.add_argument( '-z', '--zip', action='store_true',
+                         help='archive .ovf and .vmdk into .zip file' )
+    parser.add_argument( '-o', '--out', required=True,
+                         help='output file for test image (vmdk)' )
+    args = parser.parse_args()
+
+    print args
+
+    buildONOS( args.image, args.out )
+
+    if args.zip:
+        log( '* Generating .zip file' )
+        run( 'zip %s-ovf.zip %s %s' % ( args.out, args.out + '.ovf', args.out + '.vmdk' ) )
diff --git a/vm-setup.sh b/vm-setup.sh
new file mode 100644
index 0000000..c70169f
--- /dev/null
+++ b/vm-setup.sh
@@ -0,0 +1,107 @@
+#/bin/bash
+# vm-setup.sh
+#  
+# This script installs ONOS dependencies and installs a desktop environment on a Miniet VM.
+
+ONOS_USER=mininet
+ONOS_USER_HOME=/home/${ONOS_USER}
+
+ECLIPSE_URL=http://ftp.osuosl.org/pub/eclipse//technology/epp/downloads/release/kepler/SR1/eclipse-java-kepler-SR1-linux-gtk-x86_64.tar.gz 
+
+sudo apt-get update
+
+# ------------- Install ONOS Dependencies -------------
+#sudo -u ${ONOS_USER} ./install_zookeeper.sh 
+#sudo -u ${ONOS_USER} ./install_cassandra.sh 
+#sudo -u ${ONOS_USER} ./install_maven.sh 
+sudo apt-get install -y maven
+
+# -------------- Setup Desktop ----------------
+sudo apt-get install -y xorg lxde
+
+#TODO: add backgrounds folder
+#TODO: change background
+# Remove wallpaper, change background color, and disable screensaver
+sudo sed -i 's/wallpaper_mode=1/wallpaper_mode=0/g' /usr/share/lxde/pcmanfm/LXDE.conf
+sudo sed -i 's/desktop_bg=#000000/desktop_bg=#104187/g' /usr/share/lxde/pcmanfm/LXDE.conf
+sudo sed -i '/screensaver/d' /etc/xdg/lxsession/LXDE/autostart
+
+DESKTOP=${ONOS_USER_HOME}/Desktop
+
+mkdir -p ${DESKTOP}
+
+cat > ${DESKTOP}/Eclipse << EOF
+[Desktop Entry]
+Encoding=UTF-8
+Type=Application
+Name=Eclipse
+Name[en_US]=Eclipse
+Icon=/opt/eclipse/icon.xpm
+Exec=/usr/bin/eclipse
+Comment[en_US]=
+EOF
+
+cat > ${DESKTOP}/Terminal << EOF
+[Desktop Entry]
+Encoding=UTF-8
+Type=Application
+Name=Terminal
+Name[en_US]=Terminal
+Icon=konsole
+Exec=/usr/bin/x-terminal-emulator
+Comment[en_US]=
+EOF
+
+cat > ${DESKTOP}/Gerrit << EOF
+[Desktop Entry]
+Encoding=UTF-8
+Type=Application
+Name=ONOS Gerrit
+Name[en_US]=ONOS Gerrit
+Icon=internet-web-browser
+Exec=/usr/bin/chromium-browser http://gerrit.onos.onlab.us
+Comment[en_US]=
+EOF
+
+cat > ${DESKTOP}/Docs << EOF
+[Desktop Entry]
+Encoding=UTF-8
+Type=Application
+Name=ONOS Docs
+Name[en_US]=ONOS Docs
+Icon=internet-web-browser
+Exec=/usr/bin/chromium-browser http://docs.onos.onlab.us
+Comment[en_US]=
+EOF
+
+cat > ${DESKTOP}/Wireshark << EOF
+[Desktop Entry]
+Encoding=UTF-8
+Type=Application
+Name=Wireshark
+Name[en_US]=Wireshark
+Icon=wireshark
+Exec=/usr/bin/wireshark
+Comment[en_US]=
+EOF
+
+# ------------- Install Other Tools ------------- 
+sudo apt-get install -y wireshark
+# Enable root-less wireshark and add mininet
+echo "Enabling wireshark group"
+echo "wireshark-common wireshark-common/install-setuid boolean true" | sudo debconf-set-selections 
+sudo dpkg-reconfigure -f noninteractive wireshark-common
+sudo usermod -a -G wireshark mininet
+
+sudo apt-get install -y git-review
+
+wget -c -N -O /tmp/eclipse.tar.gz ${ECLIPSE_URL}
+tar xzf /tmp/eclipse.tar.gz
+sudo mv eclipse /opt
+sudo ln -s /opt/eclipse/eclipse /usr/bin/eclipse
+
+# ------------ Change username and hostname ---------------
+echo 'mininet:onos' | sudo chpasswd
+sudo mv /home/mininet /home/onos
+sudo sed -i 's/mininet/onos/g' /etc/group /etc/gshadow /etc/hosts /etc/hostname /etc/passwd /etc/shadow /etc/sudoers
+