#!/bin/sh
#
# Copyright 2005 VMware, Inc.  All rights reserved.
#

# Basic support for IRIX style chkconfig 
# chkconfig: 3 55 30
# description: Initialization for tools requiring later startup
# Must be run before xinetd starts up.

# load system helper functions.
. /etc/init.d/functions
. /etc/init.d/vmware-functions

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

# Start the VMware kernel's logger
start_vmkernel_logger() {
   killall vmklogger  > /dev/null 2>&1
   /usr/sbin/vmklogger &
}

# Inform the vmkernel of the hostname and ip address.
# Also load vmfs2, which depends on the service console ip addr.
vmkernel_late_config() {
  /etc/cron.hourly/updatevmkip
  service vmware late-vmk-config

  # Now that all FS drivers are loaded, rescan the storage fabric to
  # discover supported FS volumes.
  vmkfstools -V
}

start() {
   if ! isVMkernelLoaded ; then
      exit 0
   fi

   #Work around for bug 62830 (you really, don't want to know).
   pkill -f portResv902

   action "Starting VMware late init tools" /bin/true

   vmkernel_late_config

   # Logger can't be started immediately after vmkernel
   # is loaded because syslog isn't running yet.
   start_vmkernel_logger


   #remote NAS volumes
   action "   Restoring NAS volumes" /usr/sbin/esxcfg-nas -r

   #s/w based iscsi volumes
   action "   Restoring S/W iscsi volumes" /usr/sbin/esxcfg-swiscsi -r

   #restore s/w iscsi paths
   action "   Restoring S/W iscsi path configuration" /usr/sbin/esxcfg-mpath -r
   
   #mark installed updates as "rebooted"
   action "   Updating esxupdate database" /usr/sbin/esxupdate db addstate rebooted

   if isStateless ; then
      action "   Connecting to Virtual Center"  /bin/true
      LD_LIBRARY_PATH=/usr/lib/vmware/hostd /usr/lib/vmware/bin/connectToVC.py -w `esxcfg-advcfg -q -g /Misc/HostIPAddr` >  /dev/null 2>&1 &
   fi
   
   mkdir -p /var/log/oldconf
   cp -f /etc/vmware/esx.conf /var/log/oldconf/esx.conf.`date +"%F_%T"`
 
   # Work around for bug 145224
   # Touch the par ports so linux recognizes which ones are valid.
   LIMIT=7
   for ((a=0; a <=LIMIT; a++))
   do
     cat /dev/parport$a &> /dev/null
     cat /dev/par$a &> /dev/null
   done
   # Work around for bug 162942
   # lp must be unloaded so guests can get parallel port access
   modprobe -r lp > /dev/null 2>&1
   exit 0
}

stop() {
   action "Stopping VMware late init tools" /bin/true
}

# See how we were called.
case "$1" in
   start)
	start
        ;;

   stop)
	stop
        ;;

   status)
	echo "none"
	;;

   restart)
	"$0" stop && "$0" start
	;;
   *)
	echo "Usage: `basename "$0"` {start|stop|status|restart}"
	exit 1
esac

exit 0
