Subversion Repositories configs

Rev

Rev 19 | Rev 175 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 - 1
#!/usr/bin/env bash
2
#
3
# Copyright 1998-2008 VMware, Inc.  All rights reserved.
4
#
5
# This script manages the services needed to run VMware software.
6
#
7
 
8
# Basic support for IRIX style chkconfig
9
# chkconfig: 235 19 8
10
# description: This service starts and stops VMware services
11
 
12
 
13
ETCDIR=/etc/vmware
14
 
15
. $ETCDIR/bootstrap
16
libdir="$LIBDIR"/vmware
17
 
18
. "$libdir"/scripts/util.sh
19
 
20
load_settings "$libdir" || exit 1
21
 
22
VNETLIB_LOG=/var/log/vnetlib
42 - 23
PRODUCT_NAME="vmware-vmx"
4 - 24
COMPONENT_NAME="vmware-vmx"
25
 
26
# This comment is a hack to prevent RedHat distributions from outputing
27
# "Starting <basename of this script>" when running this startup script.
28
# We just need to write the word daemon followed by a space
29
 
30
# This defines echo_success() and echo_failure() on RedHat
31
if [ -r "$INITSCRIPTDIR"'/functions' ]; then
32
   . "$INITSCRIPTDIR"'/functions'
33
fi
34
 
35
# This defines $rc_done and $rc_failed on S.u.S.E.
36
if [ -f /etc/rc.config ]; then
37
   # Don't include the entire file: there could be conflicts
38
   rc_done=`(. /etc/rc.config; echo "$rc_done")`
39
   rc_failed=`(. /etc/rc.config; echo "$rc_failed")`
40
else
41
   # Make sure the ESC byte is literal: Ash does not support echo -e
42
   rc_done=' done'
43
   rc_failed='failed'
44
fi
45
 
46
subsys=vmware
47
driver=vmmon
48
vnet=vmnet
49
vmblock=vmblock
50
vmci=vmci
51
vmci_alias='pci:v000015ADd00000740sv*sd*bc*sc*i*'
52
vmhgfs=vmhgfs
53
vsock=vsock
54
vsock_alias=vmware_vsock
55
 
56
vmciNode=vmci
57
vsockNode=vsock
58
 
59
# SHM settings
60
shmmaxPath=/proc/sys/kernel/shmmax
61
shmmaxMinValue=268435456 # 256MB
62
 
63
#
64
# Are we running in a VM?
65
#
66
vmwareInVM() {
67
   "$BINDIR"/checkvm >/dev/null 2>&1
68
}
69
 
70
#
71
# Report a positive number if there are any VMs running.
72
# May not be the actual vmmon reference count.
73
#
74
vmmonUseCount() {
75
   local count
76
   # Beware of module dependencies here. An exact match is important
77
   count=`/sbin/lsmod | awk 'BEGIN {n = 0} {if ($1 == "'"$driver"'") n = $3} END {print n}'`
78
   # If CONFIG_MODULE_UNLOAD is not set in the kernel, lsmod prints '-' instead of the
79
   # reference count, so ask vmrun, or if we don't have vmrun, look for running vmx processes
80
   if [ x${count} = "x-" ]
81
   then
82
      type vmrun > /dev/null 2>&1
83
      if [ $? -eq 0 ]
84
      then
85
         count=`vmrun list | awk 'BEGIN {n=0} /^Total running VMs:/ {n = $4} END {print n}'`
86
      else
87
         count=`ps -afe | grep "/bin/vmware-vmx" | grep -v grep | wc -l`
88
      fi
89
   fi
90
   echo $count
91
}
92
 
93
# Is a given module loaded?
94
isLoaded() {
95
   local module="$1"
96
 
97
   /sbin/lsmod | awk 'BEGIN {n = "no";} {if ($1 == "'"$module"'") n = "yes";} END {print n;}'
98
}
99
 
100
# Build a Linux kernel integer version
101
kernelVersionInteger() {
102
   echo $(((($1 * 256) + $2) * 256 + $3))
103
}
104
 
105
# Get the running kernel integer version
106
getVersionInteger() {
107
   local version_uts
108
   local v1
109
   local v2
110
   local v3
111
 
112
   version_uts=`uname -r`
113
 
114
   # There is no double quote around the back-quoted expression on purpose
115
   # There is no double quote around $version_uts on purpose
116
   set -- `IFS='.'; echo $version_uts`
117
   v1="$1"
118
   v2="$2"
119
   v3="$3"
120
   # There is no double quote around the back-quoted expression on purpose
121
   # There is no double quote around $v3 on purpose
122
   set -- `IFS='-ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'; echo $v3`
123
   v3="$1"
124
 
125
   kernelVersionInteger "$v1" "$v2" "$v3"
126
}
127
 
128
vmwareLoadModule() {
129
   /sbin/modprobe "$1" || exit 1
130
}
131
 
132
vmwareUnloadModule() {
133
   [ "`isLoaded "$1"`" = 'no' ] && return 0
134
   # if we ran depmod after removing the modules modprobe -r will not work:
135
   /sbin/modprobe -r "$1" || /sbin/rmmod $1 || exit 1
136
}
137
 
138
# Start the virtual machine monitor kernel service
139
vmwareStartVmmon() {
140
   vmwareLoadModule $driver
141
}
142
 
143
# Stop the virtual machine monitor kernel service
144
vmwareStopVmmon() {
145
   vmwareUnloadModule $driver
146
}
147
 
148
# Start the virtual ethernet kernel service
149
vmwareStartVmnet() {
150
   vmwareLoadModule $vnet
151
   "$BINDIR"/vmware-networks --start >> $VNETLIB_LOG 2>&1
152
}
153
 
154
# Stop the virtual ethernet kernel service
155
vmwareStopVmnet() {
156
   "$BINDIR"/vmware-networks --stop >> $VNETLIB_LOG 2>&1
157
   vmwareUnloadModule $vnet
158
}
159
 
160
# Returns 0 if networking is enabled, otherwise 1
161
vmwareIsNetworkingEnabled() {
162
  [ "$vmdb_NETWORKING" = 'yes' ]
163
  return
164
}
165
 
166
vmwareRealModName() {
167
   # modprobe might be old and not understand the -R option, or
168
   # there might not be an alias. In both cases we assume
169
   # that the module is not upstreamed.
170
   mod=$1
171
   mod_alias=$2
19 - 172
   alias_file="/lib/modules/$(uname -r)/modules.alias"
4 - 173
 
174
   modname=$(/sbin/modprobe -R ${mod_alias} 2>/dev/null)
175
   if [ $? = 0 -a "$modname" != "" ] ; then
176
	echo $modname
19 - 177
   elif grep -F ${mod_alias} ${alias_file} >/dev/null 2>&1 ; then
178
	echo $(grep -F ${mod_alias} ${alias_file} | awk '{print $3}')
4 - 179
   else
180
	echo $mod
181
   fi
182
}
183
 
184
# Start the virtual machine communication interface kernel service
185
vmwareStartVmci() {
186
   mod=$(vmwareRealModName $vmci $vmci_alias)
187
 
188
   # only load vmci if it's not already loaded
189
   if [ "`isLoaded "$mod"`" = 'no' ]; then
190
      vmwareLoadModule "$mod"
191
   fi
192
   vmware_rm_stale_node "$vmciNode"
193
   if [ ! -e "/dev/$vmciNode" ]; then
194
      local minor=`cat /proc/misc | grep $vmciNode | awk '{print $1}'`
195
      mknod --mode=666 "/dev/$vmciNode" c 10 "$minor"
196
   else
197
      chmod 666 "/dev/$vmciNode"
198
   fi
199
 
200
   return 0
201
}
202
 
203
# Make sure the system has enough shared memory available to cover shmmaxMinValue.
204
# To handle overflow/wrapping, check that shmmax is greater than 1 since any overflow
205
# will make shmmax look negative.  At least until shmmax or shmmaxMinValue wrap around
206
# again.
207
vmwareCheckSharedMemory() {
208
   if [ -f "$shmmaxPath" ]; then
209
      shmmax=`cat $shmmaxPath`
210
      # Account for numbers that are too large that they wrap around and alias
211
      # to a smaller number or they are outright set to -1.  If "1 < XXXX" fails
212
      # then the XXX value is # out of bounds.  The only acceptable combo is that
213
      # both values satisfy that condition, else report that the max value the
214
      # system supports may not satisfy this programs requirements.
215
      if  ((  $shmmax < 1 )) || (( $shmmaxMinValue < 1 )) \
216
       || (( $shmmax < $shmmaxMinValue )) ; then
217
         echo "$shmmaxMinValue" > "$shmmaxPath"
218
         echo ""
219
         echo "Setting the max shared memory the system will allow to $shmmaxMinValue."
220
         echo ""
221
      fi
222
   fi
223
   return 0
224
}
225
 
226
 
227
# Stop the virtual machine communication interface kernel service
228
vmwareStopVmci() {
229
   # Hosted now has to interface with Tools.  vmhgfs could possibly be loaded, which
230
   # will interfere with the removal of vmci.  Only unload it if it's already
231
   # loaded.
232
   if [ "`isLoaded "$vmhgfs"`" = 'yes' ]; then
233
      vmwareUnloadModule "$vmhgfs"
234
   fi
235
 
236
   mod=$(vmwareRealModName $vmci $vmci_alias)
237
 
238
   # only unload vmci if it's already loaded
239
   if [ "`isLoaded "${mod}"`" = 'yes' ]; then
240
      vmwareUnloadModule "${mod}"
241
   fi
242
   rm -f "/dev/$vmciNode"
243
}
244
 
245
isVmciNeeded() {
246
   if [ "$vmdb_VMCI_CONFED" = 'yes' ]; then
247
      echo yes
248
   else
249
      echo no
250
   fi
251
}
252
 
253
# starts after vmci is loaded
254
vmwareStartVsock() {
255
   mod=$(vmwareRealModName $vsock $vsock_alias)
256
   # only load vsock if it's not already loaded
257
   if [ "`isLoaded "$mod"`" = 'no' ]; then
258
      vmwareLoadModule "$mod"
259
   fi
260
   vmware_rm_stale_node "$vsockNode"
261
   # Give udev 5 seconds to create our node
262
   vmware_delay_for_node "/dev/$vsockNode" 5
263
   if [ ! -e "/dev/$vsockNode" ]; then
264
      local minor=`cat /proc/misc | grep $vsockNode | awk '{print $1}'`
265
      mknod --mode=666 "/dev/$vsockNode" c 10 "$minor"
266
   else
267
      chmod 666 "/dev/$vsockNode"
268
   fi
269
 
270
   return 0
271
}
272
 
273
# unloads before vmci
274
vmwareStopVsock() {
275
   mod=$(vmwareRealModName $vsock $vsock_alias)
276
   # only unload vsock if it's already loaded
277
   if [ "`isLoaded "$mod"`" = 'yes' ]; then
278
     vmwareUnloadModule "$mod"
279
   fi
280
   rm -f /dev/vsock
281
}
282
 
283
isVsockNeeded() {
284
   if [ "$vmdb_VSOCK_CONFED" = 'yes' ]; then
285
      echo yes
286
   else
287
      echo no
288
   fi
289
}
290
 
291
vmware_start_authdlauncher() {
292
   vmware_bg_exec "`vmware_product_name` Authentication Daemon" \
293
      "$SBINDIR/vmware-authdlauncher"
294
}
295
 
296
vmware_stop_authdlauncher() {
297
   local launcherpid=`pidof vmware-authdlauncher`
298
   if [ -n "$launcherpid" ]; then
299
      vmware_synchrone_kill $launcherpid "TERM"
300
   fi
301
}
302
 
303
vmwareService() {
304
   case "$1" in
305
      start)
306
         if vmwareInVM; then
307
            # Refuse to start services in a VM: they are useless
308
            exit 1
309
         fi
310
 
311
         echo 'Starting VMware services:'
312
         exitcode='0'
313
 
314
         vmware_exec 'Virtual machine monitor' vmwareStartVmmon
315
         exitcode=$(($exitcode + $?))
316
 
317
         if [ "`isVmciNeeded`" = 'yes' ]; then
318
            vmware_exec 'Virtual machine communication interface' vmwareStartVmci
319
            exitcode=$(($exitcode + $?))
320
         fi
321
 
322
         # vsock needs vmci started first
323
         if [ "`isVsockNeeded`" = 'yes' ]; then
324
            vmware_exec 'VM communication interface socket family' vmwareStartVsock
325
            # a vsock failure to load shouldn't cause the init to fail completely.
326
         fi
327
 
328
         if [ "`is_vmblock_needed`" = 'yes' ] ; then
329
            vmware_exec 'Blocking file system' vmware_start_vmblock
330
            exitcode=$(($exitcode + $?))
331
         fi
332
 
333
         # Try to load parport_pc.
334
         /sbin/modprobe parport_pc >/dev/null 2>&1
335
 
336
         if vmwareIsNetworkingEnabled; then
337
            vmware_exec 'Virtual ethernet' vmwareStartVmnet
338
            exitcode=$(($exitcode + $?))
339
         fi
340
 
341
         vmware_exec 'VMware Authentication Daemon' vmware_start_authdlauncher
342
 
343
         if [ "$exitcode" -gt 0 ]; then
344
            exit 1
345
         fi
346
 
347
         [ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
348
         touch /var/lock/subsys/"$subsys"
349
 
350
         vmware_exec "Shared Memory Available"  vmwareCheckSharedMemory
351
      ;;
352
 
353
      stop)
354
         echo 'Stopping VMware services:'
355
         exitcode='0'
356
 
357
         vmware_exec 'VMware Authentication Daemon' vmware_stop_authdlauncher
358
 
359
         # If the 'K' version of this script is running, the system is
360
         # stoping services not because the user is running vmware-config.pl
361
         # or running the initscript directly but because the user wants to
362
         # shutdown.  Suspend all VMs.
363
         if [ "`echo $BASENAME | sed -ne '/^K[0-9].vmware/p'`" ] ; then
364
            if [ -x "$BINDIR"/vmrun ] ; then
365
               for i in `pidof vmware-vmx` ; do
366
                  "$BINDIR"/vmrun suspend `ps -p $i -f | \
367
                       sed -ne '/vmware/s/.* \(\/.*\.vmx\)/\1/p'` 2> /dev/null
368
               done
369
            fi
370
 
371
         fi
372
 
373
         if [ "`vmmonUseCount`" -gt 0 ]; then
374
            echo 'At least one instance of '"$PRODUCT_NAME"' is still running.' 1>&2
375
            echo 'Please stop all running instances of '"$PRODUCT_NAME"' first.' 1>&2
376
            echo " " >&2
377
 
378
            # Since we stopped authdlauncher to prevent new connections before disabling
379
            # any vmxs, need to restart it here to restore the environment back to
380
            # what it was before this init script ran.
381
            vmware_exec 'VMware Authentication Daemon' vmware_start_authdlauncher
382
 
383
            # The unconfigurator handle this exit code differently
384
            exit 2
385
         fi
386
 
387
         # vmci is used by vsock so the module can't unload until vsock does.
388
         if [ "`isVsockNeeded`" = 'yes' ]; then
389
            vmware_exec 'VM communication interface socket family' vmwareStopVsock
390
            exitcode=$(($exitcode + $?))
391
         fi
392
 
393
         if [ "`isVmciNeeded`" = 'yes' ]; then
394
            vmware_exec 'Virtual machine communication interface' vmwareStopVmci
395
            exitcode=$(($exitcode + $?))
396
         fi
397
 
398
         vmware_exec 'Virtual machine monitor' vmwareStopVmmon
399
         exitcode=$(($exitcode + $?))
400
 
401
         if [ "`is_vmblock_needed`" = 'yes' ] ; then
402
            vmware_exec 'Blocking file system' vmware_stop_vmblock
403
            exitcode=$(($exitcode + $?))
404
         fi
405
 
406
         # Try to unload parport_pc. Failure is allowed as it does not
407
         # exist on kernels 2.0, and some other process could be using
408
         # it.
409
         /sbin/modprobe -r parport_pc >/dev/null 2>&1
410
 
411
         if vmwareIsNetworkingEnabled; then
412
	    vmwareStopVmnet
413
         fi
414
 
415
         # The vmware and vmware-tray processes don't terminate automatically
416
         # when the other services are shutdown.  They persist after calling
417
         # 'init.d/vmware stop' and will happily keep going through an init
418
         # start command, continuing to minimally function, blissfully ignorant.
419
         # Time for a buzzkill.
420
         for i in `pidof vmware vmware-tray` ; do
421
            vmware_synchrone_kill $i "INT"
422
         done
423
 
424
         if [ "$exitcode" -gt 0 ]; then
425
            exit 1
426
         fi
427
 
428
         rm -f /var/lock/subsys/"$subsys"
429
      ;;
430
 
431
      status)
432
         if [ "`vmmonUseCount`" -gt 0 ]; then
433
            echo 'At least one instance of '"$PRODUCT_NAME"' is still running.'
434
            echo
435
            if [ "$2" = "vmcount" ]; then
436
               exit 2
437
            fi
438
         fi
439
         if [ "$2" = "vmcount" ]; then
440
               exit 0
441
         fi
442
 
443
         exitcode='0'
444
 
445
         echo -n "Module $driver "
446
         [ "`isLoaded "$driver"`" = 'yes' ] && echo loaded || echo "not loaded"
447
         if vmwareIsNetworkingEnabled; then
448
            echo -n "Module $vnet "
449
            [ "`isLoaded "$vnet"`" = 'yes' ] && echo loaded || echo "not loaded"
450
         fi
451
 
452
         if [ "$exitcode" -gt 0 ]; then
453
            exit 1
454
         fi
455
      ;;
456
 
457
      restart)
458
         "$SCRIPTNAME" stop && "$SCRIPTNAME" start
459
      ;;
460
 
461
      # Called to make sure script is in a runnable state.
462
      validate)
463
         exit 100
464
      ;;
465
 
466
      stoppable)
467
	 [ "`vmmonUseCount`" -lt 1 ]
468
	 exit
469
      ;;
470
 
471
      *)
472
         echo "Usage: "$BASENAME" {start|stop|status|restart|stoppable}"
473
         exit 1
474
   esac
475
}
476
 
477
SCRIPTNAME="$0"
478
BASENAME=`basename "$SCRIPTNAME"`
479
 
480
# Check permissions
481
if [ "`id -ur`" != '0' ]; then
482
   echo 'Error: you must be root.'
483
   echo
484
   exit 1
485
fi
486
 
487
vmwareService "$1"
488
 
489
exit 0