#!/bin/bash
#
#   ConVirt   -  Copyright (c) 2008 Convirture Corp.
#   ======
#
# ConVirt is a Virtualization management tool with a graphical user
# interface that allows for performing the standard set of VM operations
# (start, stop, pause, kill, shutdown, reboot, snapshot, etc...). It
# also attempts to simplify various aspects of VM lifecycle management.
#
#
# This software is subject to the GNU General Public License, Version 2 (GPLv2)
# and for details, please consult it at:
#
#    http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
# 
#
# author : Jd <jd_jedi@users.sourceforge.net>
#

# Distribution and version specific code can go here.
# Assume that common functions are included
# Assme the DIST and the VER to be set here.
open_ports()
{
    ports="$*"
    echo "open_ports : not implemented for ${DIST} distribution. "
    echo "If you are using firewall, please open $ports."
    echo "These are required for ConVirt to connect succesfully"
    return 0
}
# Restart the network
restart_network()
{
    /etc/init.d/networking restart
    return 0
}
# update the network service
setup_nw_svc()
{
    # default dnsmasq service is causing problems. disable it.
    echo "Disabling default dnsmasq service."
    /etc/init.d/dnsmasq stop
    update-rc.d -f dnsmasq remove
    update-rc.d convirt-nw defaults
    return 0
}

verify_bridge(){
  match_found=`grep "^auto $bridgeName" -c /etc/network/interfaces`
  if [  $match_found -gt 0 ]; then
    return 1
  fi
  return 0
}

# Milind : Implementation for Debian/Ubuntu
# Bridge parameters from 
# https://help.ubuntu.com/community/KVM/Networking
#
setup_public_bridge_for_kvm() { 
  # create a backup file so people can restore easily
  NW_FILE=/etc/network/interfaces
  BACKUP_FILE=$NW_FILE.`date +"%Y%m%d.%H%M%S"`
  cp $NW_FILE $BACKUP_FILE
  CHANGED="F" 
  # Iterate through interfaces
  PHY_IFACES=`get_candidates_for_bridging`
  for iface in $PHY_IFACES
  do 
    index=`echo $iface | sed 's/^[^0-9]*//'`
    bridgeName=br$index

    # check if the interface is not already part of some other 
    # bridge
    grep "bridge_ports .*$iface" $NW_FILE >/dev/null 2>/dev/null
    if [ $? != 1 ]; then
       echo "interface $iface already part of bridge. skiping it"

       mkdir -p /etc/kvm
       sed  "{s/SWITCH_NAME/$bridgeName/} " < ${common_scripts}/qemubridge-ifup > /etc/kvm/qemu-ifup-${bridgeName}
       chmod 744 /etc/kvm/qemu-ifup-${bridgeName}

       continue
    fi

    verify_bridge
    if [ "$?" == "0" ]; then
      CHANGED="T"
      # look for interface entry in NW_FILE, if not, add it.
      grep "iface .*$iface inet" $NW_FILE >/dev/null 2>/dev/null
      if [ $? == 1 ]; then
         echo "" >> $NW_FILE
         echo "auto $iface" >> $NW_FILE  
         echo "iface $iface inet manual" >> $NW_FILE  
      fi
      echo ""  >> $NW_FILE
      echo "auto $bridgeName"  >> $NW_FILE
      echo "iface $bridgeName inet dhcp" >> $NW_FILE
      echo "  bridge_ports $iface" >> $NW_FILE
      echo "  bridge_fd 0" >> $NW_FILE
      echo "  bridge_stp off" >> $NW_FILE
      echo "  bridge_maxwait 0" >> $NW_FILE
      echo ""  >> $NW_FILE

      mkdir -p /etc/kvm
      sed  "{s/SWITCH_NAME/$bridgeName/} " < ${common_scripts}/qemubridge-ifup > /etc/kvm/qemu-ifup-${bridgeName}
      chmod 744 /etc/kvm/qemu-ifup-${bridgeName}
     
      # switch the interface to manual
      sed -i "s/iface $iface inet.*/iface $iface inet manual/" $NW_FILE 
      
    fi
  done
  if [ "$CHANGED" == "T" ]; then
     enable_ip_forward
     restart_network
  else
     rm -f $BACKUP_FILE
  fi
}
#install convirt tool dependencies
install_dependencies(){
   $SUDO apt-get -y install ssh socat dnsmasq uml-utilities lvm2 expect python-pexpect kpartx
}
