Rev 19 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/env bash## Copyright 1998-2008 VMware, Inc. All rights reserved.## This script manages the services needed to run VMware software.## Basic support for IRIX style chkconfig# chkconfig: 235 19 8# description: This service starts and stops VMware servicesETCDIR=/etc/vmware. $ETCDIR/bootstraplibdir="$LIBDIR"/vmware. "$libdir"/scripts/util.shload_settings "$libdir" || exit 1VNETLIB_LOG=/var/log/vnetlibPRODUCT_NAME="vmware-vmx"COMPONENT_NAME="vmware-vmx"# This comment is a hack to prevent RedHat distributions from outputing# "Starting <basename of this script>" when running this startup script.# We just need to write the word daemon followed by a space# This defines echo_success() and echo_failure() on RedHatif [ -r "$INITSCRIPTDIR"'/functions' ]; then. "$INITSCRIPTDIR"'/functions'fi# This defines $rc_done and $rc_failed on S.u.S.E.if [ -f /etc/rc.config ]; then# Don't include the entire file: there could be conflictsrc_done=`(. /etc/rc.config; echo "$rc_done")`rc_failed=`(. /etc/rc.config; echo "$rc_failed")`else# Make sure the ESC byte is literal: Ash does not support echo -erc_done='[71G done'rc_failed='[71Gfailed'fisubsys=vmwaredriver=vmmonvnet=vmnetvmblock=vmblockvmci=vmcivmci_alias='pci:v000015ADd00000740sv*sd*bc*sc*i*'vmhgfs=vmhgfsvsock=vsockvsock_alias=vmware_vsockvmciNode=vmcivsockNode=vsock# SHM settingsshmmaxPath=/proc/sys/kernel/shmmaxshmmaxMinValue=268435456 # 256MB## Are we running in a VM?#vmwareInVM() {"$BINDIR"/checkvm >/dev/null 2>&1}## Report a positive number if there are any VMs running.# May not be the actual vmmon reference count.#vmmonUseCount() {local count# Beware of module dependencies here. An exact match is importantcount=`/sbin/lsmod | awk 'BEGIN {n = 0} {if ($1 == "'"$driver"'") n = $3} END {print n}'`# If CONFIG_MODULE_UNLOAD is not set in the kernel, lsmod prints '-' instead of the# reference count, so ask vmrun, or if we don't have vmrun, look for running vmx processesif [ x${count} = "x-" ]thentype vmrun > /dev/null 2>&1if [ $? -eq 0 ]thencount=`vmrun list | awk 'BEGIN {n=0} /^Total running VMs:/ {n = $4} END {print n}'`elsecount=`ps -afe | grep "/bin/vmware-vmx" | grep -v grep | wc -l`fifiecho $count}# Is a given module loaded?isLoaded() {local module="$1"/sbin/lsmod | awk 'BEGIN {n = "no";} {if ($1 == "'"$module"'") n = "yes";} END {print n;}'}# Build a Linux kernel integer versionkernelVersionInteger() {echo $(((($1 * 256) + $2) * 256 + $3))}# Get the running kernel integer versiongetVersionInteger() {local version_utslocal v1local v2local v3version_uts=`uname -r`# There is no double quote around the back-quoted expression on purpose# There is no double quote around $version_uts on purposeset -- `IFS='.'; echo $version_uts`v1="$1"v2="$2"v3="$3"# There is no double quote around the back-quoted expression on purpose# There is no double quote around $v3 on purposeset -- `IFS='-ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'; echo $v3`v3="$1"kernelVersionInteger "$v1" "$v2" "$v3"}vmwareLoadModule() {/sbin/modprobe "$1" || exit 1}vmwareUnloadModule() {[ "`isLoaded "$1"`" = 'no' ] && return 0# if we ran depmod after removing the modules modprobe -r will not work:/sbin/modprobe -r "$1" || /sbin/rmmod $1 || exit 1}# Start the virtual machine monitor kernel servicevmwareStartVmmon() {vmwareLoadModule $driver}# Stop the virtual machine monitor kernel servicevmwareStopVmmon() {vmwareUnloadModule $driver}# Start the virtual ethernet kernel servicevmwareStartVmnet() {vmwareLoadModule $vnet"$BINDIR"/vmware-networks --start >> $VNETLIB_LOG 2>&1}# Stop the virtual ethernet kernel servicevmwareStopVmnet() {"$BINDIR"/vmware-networks --stop >> $VNETLIB_LOG 2>&1vmwareUnloadModule $vnet}# Returns 0 if networking is enabled, otherwise 1vmwareIsNetworkingEnabled() {[ "$vmdb_NETWORKING" = 'yes' ]return}vmwareRealModName() {# modprobe might be old and not understand the -R option, or# there might not be an alias. In both cases we assume# that the module is not upstreamed.mod=$1mod_alias=$2alias_file="/lib/modules/$(uname -r)/modules.alias"modname=$(/sbin/modprobe -R ${mod_alias} 2>/dev/null)if [ $? = 0 -a "$modname" != "" ] ; thenecho $modnameelif grep -F ${mod_alias} ${alias_file} >/dev/null 2>&1 ; thenecho $(grep -F ${mod_alias} ${alias_file} | awk '{print $3}')elseecho $modfi}# Start the virtual machine communication interface kernel servicevmwareStartVmci() {mod=$(vmwareRealModName $vmci $vmci_alias)# only load vmci if it's not already loadedif [ "`isLoaded "$mod"`" = 'no' ]; thenvmwareLoadModule "$mod"fivmware_rm_stale_node "$vmciNode"if [ ! -e "/dev/$vmciNode" ]; thenlocal minor=`cat /proc/misc | grep $vmciNode | awk '{print $1}'`mknod --mode=666 "/dev/$vmciNode" c 10 "$minor"elsechmod 666 "/dev/$vmciNode"fireturn 0}# Make sure the system has enough shared memory available to cover shmmaxMinValue.# To handle overflow/wrapping, check that shmmax is greater than 1 since any overflow# will make shmmax look negative. At least until shmmax or shmmaxMinValue wrap around# again.vmwareCheckSharedMemory() {if [ -f "$shmmaxPath" ]; thenshmmax=`cat $shmmaxPath`# Account for numbers that are too large that they wrap around and alias# to a smaller number or they are outright set to -1. If "1 < XXXX" fails# then the XXX value is # out of bounds. The only acceptable combo is that# both values satisfy that condition, else report that the max value the# system supports may not satisfy this programs requirements.if (( $shmmax < 1 )) || (( $shmmaxMinValue < 1 )) \|| (( $shmmax < $shmmaxMinValue )) ; thenecho "$shmmaxMinValue" > "$shmmaxPath"echo ""echo "Setting the max shared memory the system will allow to $shmmaxMinValue."echo ""fifireturn 0}# Stop the virtual machine communication interface kernel servicevmwareStopVmci() {# Hosted now has to interface with Tools. vmhgfs could possibly be loaded, which# will interfere with the removal of vmci. Only unload it if it's already# loaded.if [ "`isLoaded "$vmhgfs"`" = 'yes' ]; thenvmwareUnloadModule "$vmhgfs"fimod=$(vmwareRealModName $vmci $vmci_alias)# only unload vmci if it's already loadedif [ "`isLoaded "${mod}"`" = 'yes' ]; thenvmwareUnloadModule "${mod}"firm -f "/dev/$vmciNode"}isVmciNeeded() {if [ "$vmdb_VMCI_CONFED" = 'yes' ]; thenecho yeselseecho nofi}# starts after vmci is loadedvmwareStartVsock() {mod=$(vmwareRealModName $vsock $vsock_alias)# only load vsock if it's not already loadedif [ "`isLoaded "$mod"`" = 'no' ]; thenvmwareLoadModule "$mod"fivmware_rm_stale_node "$vsockNode"# Give udev 5 seconds to create our nodevmware_delay_for_node "/dev/$vsockNode" 5if [ ! -e "/dev/$vsockNode" ]; thenlocal minor=`cat /proc/misc | grep $vsockNode | awk '{print $1}'`mknod --mode=666 "/dev/$vsockNode" c 10 "$minor"elsechmod 666 "/dev/$vsockNode"fireturn 0}# unloads before vmcivmwareStopVsock() {mod=$(vmwareRealModName $vsock $vsock_alias)# only unload vsock if it's already loadedif [ "`isLoaded "$mod"`" = 'yes' ]; thenvmwareUnloadModule "$mod"firm -f /dev/vsock}isVsockNeeded() {if [ "$vmdb_VSOCK_CONFED" = 'yes' ]; thenecho yeselseecho nofi}vmware_start_authdlauncher() {vmware_bg_exec "`vmware_product_name` Authentication Daemon" \"$SBINDIR/vmware-authdlauncher"}vmware_stop_authdlauncher() {local launcherpid=`pidof vmware-authdlauncher`if [ -n "$launcherpid" ]; thenvmware_synchrone_kill $launcherpid "TERM"fi}vmwareService() {case "$1" instart)if vmwareInVM; then# Refuse to start services in a VM: they are uselessexit 1fiecho 'Starting VMware services:'exitcode='0'vmware_exec 'Virtual machine monitor' vmwareStartVmmonexitcode=$(($exitcode + $?))if [ "`isVmciNeeded`" = 'yes' ]; thenvmware_exec 'Virtual machine communication interface' vmwareStartVmciexitcode=$(($exitcode + $?))fi# vsock needs vmci started firstif [ "`isVsockNeeded`" = 'yes' ]; thenvmware_exec 'VM communication interface socket family' vmwareStartVsock# a vsock failure to load shouldn't cause the init to fail completely.fiif [ "`is_vmblock_needed`" = 'yes' ] ; thenvmware_exec 'Blocking file system' vmware_start_vmblockexitcode=$(($exitcode + $?))fi# Try to load parport_pc./sbin/modprobe parport_pc >/dev/null 2>&1if vmwareIsNetworkingEnabled; thenvmware_exec 'Virtual ethernet' vmwareStartVmnetexitcode=$(($exitcode + $?))fivmware_exec 'VMware Authentication Daemon' vmware_start_authdlauncherif [ "$exitcode" -gt 0 ]; thenexit 1fi[ -d /var/lock/subsys ] || mkdir -p /var/lock/subsystouch /var/lock/subsys/"$subsys"vmware_exec "Shared Memory Available" vmwareCheckSharedMemory;;stop)echo 'Stopping VMware services:'exitcode='0'vmware_exec 'VMware Authentication Daemon' vmware_stop_authdlauncher# If the 'K' version of this script is running, the system is# stoping services not because the user is running vmware-config.pl# or running the initscript directly but because the user wants to# shutdown. Suspend all VMs.if [ "`echo $BASENAME | sed -ne '/^K[0-9].vmware/p'`" ] ; thenif [ -x "$BINDIR"/vmrun ] ; thenfor i in `pidof vmware-vmx` ; do"$BINDIR"/vmrun suspend `ps -p $i -f | \sed -ne '/vmware/s/.* \(\/.*\.vmx\)/\1/p'` 2> /dev/nulldonefifiif [ "`vmmonUseCount`" -gt 0 ]; thenecho 'At least one instance of '"$PRODUCT_NAME"' is still running.' 1>&2echo 'Please stop all running instances of '"$PRODUCT_NAME"' first.' 1>&2echo " " >&2# Since we stopped authdlauncher to prevent new connections before disabling# any vmxs, need to restart it here to restore the environment back to# what it was before this init script ran.vmware_exec 'VMware Authentication Daemon' vmware_start_authdlauncher# The unconfigurator handle this exit code differentlyexit 2fi# vmci is used by vsock so the module can't unload until vsock does.if [ "`isVsockNeeded`" = 'yes' ]; thenvmware_exec 'VM communication interface socket family' vmwareStopVsockexitcode=$(($exitcode + $?))fiif [ "`isVmciNeeded`" = 'yes' ]; thenvmware_exec 'Virtual machine communication interface' vmwareStopVmciexitcode=$(($exitcode + $?))fivmware_exec 'Virtual machine monitor' vmwareStopVmmonexitcode=$(($exitcode + $?))if [ "`is_vmblock_needed`" = 'yes' ] ; thenvmware_exec 'Blocking file system' vmware_stop_vmblockexitcode=$(($exitcode + $?))fi# Try to unload parport_pc. Failure is allowed as it does not# exist on kernels 2.0, and some other process could be using# it./sbin/modprobe -r parport_pc >/dev/null 2>&1if vmwareIsNetworkingEnabled; thenvmwareStopVmnetfi# The vmware and vmware-tray processes don't terminate automatically# when the other services are shutdown. They persist after calling# 'init.d/vmware stop' and will happily keep going through an init# start command, continuing to minimally function, blissfully ignorant.# Time for a buzzkill.for i in `pidof vmware vmware-tray` ; dovmware_synchrone_kill $i "INT"doneif [ "$exitcode" -gt 0 ]; thenexit 1firm -f /var/lock/subsys/"$subsys";;status)if [ "`vmmonUseCount`" -gt 0 ]; thenecho 'At least one instance of '"$PRODUCT_NAME"' is still running.'echoif [ "$2" = "vmcount" ]; thenexit 2fifiif [ "$2" = "vmcount" ]; thenexit 0fiexitcode='0'echo -n "Module $driver "[ "`isLoaded "$driver"`" = 'yes' ] && echo loaded || echo "not loaded"if vmwareIsNetworkingEnabled; thenecho -n "Module $vnet "[ "`isLoaded "$vnet"`" = 'yes' ] && echo loaded || echo "not loaded"fiif [ "$exitcode" -gt 0 ]; thenexit 1fi;;restart)"$SCRIPTNAME" stop && "$SCRIPTNAME" start;;# Called to make sure script is in a runnable state.validate)exit 100;;stoppable)[ "`vmmonUseCount`" -lt 1 ]exit;;*)echo "Usage: "$BASENAME" {start|stop|status|restart|stoppable}"exit 1esac}SCRIPTNAME="$0"BASENAME=`basename "$SCRIPTNAME"`# Check permissionsif [ "`id -ur`" != '0' ]; thenecho 'Error: you must be root.'echoexit 1fivmwareService "$1"exit 0