#!/bin/bash
#
# Copyright (c) 2009, Microsoft Corporation - All rights reserved.
#
# This software is available to you under a choice of one of two
# licenses.  You may choose to be licensed under the terms of the GNU
# General Public License (GPL) Version 2, available from the file
# LICENSE-GPL in the main directory of this source tree, or the
# BSD license (http://opensource.org/licenses/bsd-license.php).
#
#     Redistribution and use in source and binary forms, with or
#     without modification, are permitted provided that the following
#     conditions are met:
#
#      - Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      - Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials
#        provided with the distribution.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Authors:
#   Haiyang Zhang <haiyangz@microsoft.com>
#   Hank Janssen  <hjanssen@microsoft.com>
#
### BEGIN INIT INFO
# Provides:             vmbus
# Required-Start:       $null
# Should-Start:         $null
# Required-Stop:        $local_fs
# Should-Stop:          network
# Default-Start:        1 2 3 5
# Default-Stop:         0 6
# Short-Description:    script to optionally load vmbus during system startup
# Description:          script to optionally load vmbus during system startup
### END INIT INFO
#
# vmbus         
#
# chkconfig: 345 09 80
# description: Synthetic bus driver for Microsoft Hyper-V 2008 and 2008 R2


# Source function library.
if [ -f /etc/SuSE-release ]; then
. /etc/rc.status 
rc_reset
else
. /etc/rc.d/init.d/functions
fi

# vmbus configuration file
#. /etc/sysconfig/vmbus

status_update ()
{
    last_status=$?
    if [ -f /etc/SuSE-release ]; then
        rc_status -v
    else
        if [ $last_status -eq 0 ]; then
            success
        else
            failure
        fi
    fi
}

start () 
{
    loaded=`lsmod | grep vmbus`
    if [ -z "${loaded}" ]; then
        echo -n $"Starting vmbus: "
        /sbin/modprobe vmbus 2>/dev/null
        status_update
    fi
    
    loaded=`lsmod | grep netvsc`
    if [ -n "${loaded}" ]; then
		echo $"Checking for new synthetic nics..."
		check_new_nic
    fi
}

stop () 
{
    echo -n $"Shutting down vmbus: "
    
    loaded=`lsmod | grep netvsc`
    if [ -n "${loaded}" ]; then
        rmmod netvsc
        if [ $? -ne 0 ]; then
            return $?   
        fi
    fi
    
    loaded=`lsmod | grep vmbus`
    if [ -n "${loaded}" ]; then
        rmmod vmbus
        status_update
    fi
}

status() 
{
	loaded=`/sbin/lsmod | grep vmbus`
	if [ -n "$loaded" ]; then
		echo "vmbus is loaded"
	else
		echo "vmbus is not loaded"
	fi
}

#
# This is to workaround an issue when a new nic is
# added during booting and somehow we didnt get the uevent.
# Without this uevent, the netvsc.uevent script does not get
# kick off.
#
check_new_nic()
{
	nics=`find /sys/class/net -name seth*`
	for nic in $nics
	do
		echo "add" > ${nic}/uevent
	done
}

# See how we were called.
case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  status)
	status
        ;;    
  *)
        echo $"Usage: $0 {start|stop|status}"
        exit 1
esac

exit 0
