Subversion Repositories configs

Rev

Rev 19 | Go to most recent revision | Details | 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
23
PRODUCT_NAME="VMware VMX"
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
172
 
173
   modname=$(/sbin/modprobe -R ${mod_alias} 2>/dev/null)
174
   if [ $? = 0 -a "$modname" != "" ] ; then
175
	echo $modname
176
   else
177
	echo $mod
178
   fi
179
}
180
 
181
# Start the virtual machine communication interface kernel service
182
vmwareStartVmci() {
183
   mod=$(vmwareRealModName $vmci $vmci_alias)
184
 
185
   # only load vmci if it's not already loaded
186
   if [ "`isLoaded "$mod"`" = 'no' ]; then
187
      vmwareLoadModule "$mod"
188
   fi
189
   vmware_rm_stale_node "$vmciNode"
190
   if [ ! -e "/dev/$vmciNode" ]; then
191
      local minor=`cat /proc/misc | grep $vmciNode | awk '{print $1}'`
192
      mknod --mode=666 "/dev/$vmciNode" c 10 "$minor"
193
   else
194
      chmod 666 "/dev/$vmciNode"
195
   fi
196
 
197
   return 0
198
}
199
 
200
# Make sure the system has enough shared memory available to cover shmmaxMinValue.
201
# To handle overflow/wrapping, check that shmmax is greater than 1 since any overflow
202
# will make shmmax look negative.  At least until shmmax or shmmaxMinValue wrap around
203
# again.
204
vmwareCheckSharedMemory() {
205
   if [ -f "$shmmaxPath" ]; then
206
      shmmax=`cat $shmmaxPath`
207
      # Account for numbers that are too large that they wrap around and alias
208
      # to a smaller number or they are outright set to -1.  If "1 < XXXX" fails
209
      # then the XXX value is # out of bounds.  The only acceptable combo is that
210
      # both values satisfy that condition, else report that the max value the
211
      # system supports may not satisfy this programs requirements.
212
      if  ((  $shmmax < 1 )) || (( $shmmaxMinValue < 1 )) \
213
       || (( $shmmax < $shmmaxMinValue )) ; then
214
         echo "$shmmaxMinValue" > "$shmmaxPath"
215
         echo ""
216
         echo "Setting the max shared memory the system will allow to $shmmaxMinValue."
217
         echo ""
218
      fi
219
   fi
220
   return 0
221
}
222
 
223
 
224
# Stop the virtual machine communication interface kernel service
225
vmwareStopVmci() {
226
   # Hosted now has to interface with Tools.  vmhgfs could possibly be loaded, which
227
   # will interfere with the removal of vmci.  Only unload it if it's already
228
   # loaded.
229
   if [ "`isLoaded "$vmhgfs"`" = 'yes' ]; then
230
      vmwareUnloadModule "$vmhgfs"
231
   fi
232
 
233
   mod=$(vmwareRealModName $vmci $vmci_alias)
234
 
235
   # only unload vmci if it's already loaded
236
   if [ "`isLoaded "${mod}"`" = 'yes' ]; then
237
      vmwareUnloadModule "${mod}"
238
   fi
239
   rm -f "/dev/$vmciNode"
240
}
241
 
242
isVmciNeeded() {
243
   if [ "$vmdb_VMCI_CONFED" = 'yes' ]; then
244
      echo yes
245
   else
246
      echo no
247
   fi
248
}
249
 
250
# starts after vmci is loaded
251
vmwareStartVsock() {
252
   mod=$(vmwareRealModName $vsock $vsock_alias)
253
   # only load vsock if it's not already loaded
254
   if [ "`isLoaded "$mod"`" = 'no' ]; then
255
      vmwareLoadModule "$mod"
256
   fi
257
   vmware_rm_stale_node "$vsockNode"
258
   # Give udev 5 seconds to create our node
259
   vmware_delay_for_node "/dev/$vsockNode" 5
260
   if [ ! -e "/dev/$vsockNode" ]; then
261
      local minor=`cat /proc/misc | grep $vsockNode | awk '{print $1}'`
262
      mknod --mode=666 "/dev/$vsockNode" c 10 "$minor"
263
   else
264
      chmod 666 "/dev/$vsockNode"
265
   fi
266
 
267
   return 0
268
}
269
 
270
# unloads before vmci
271
vmwareStopVsock() {
272
   mod=$(vmwareRealModName $vsock $vsock_alias)
273
   # only unload vsock if it's already loaded
274
   if [ "`isLoaded "$mod"`" = 'yes' ]; then
275
     vmwareUnloadModule "$mod"
276
   fi
277
   rm -f /dev/vsock
278
}
279
 
280
isVsockNeeded() {
281
   if [ "$vmdb_VSOCK_CONFED" = 'yes' ]; then
282
      echo yes
283
   else
284
      echo no
285
   fi
286
}
287
 
288
vmware_start_authdlauncher() {
289
   vmware_bg_exec "`vmware_product_name` Authentication Daemon" \
290
      "$SBINDIR/vmware-authdlauncher"
291
}
292
 
293
vmware_stop_authdlauncher() {
294
   local launcherpid=`pidof vmware-authdlauncher`
295
   if [ -n "$launcherpid" ]; then
296
      vmware_synchrone_kill $launcherpid "TERM"
297
   fi
298
}
299
 
300
vmwareService() {
301
   case "$1" in
302
      start)
303
         if vmwareInVM; then
304
            # Refuse to start services in a VM: they are useless
305
            exit 1
306
         fi
307
 
308
         echo 'Starting VMware services:'
309
         exitcode='0'
310
 
311
         vmware_exec 'Virtual machine monitor' vmwareStartVmmon
312
         exitcode=$(($exitcode + $?))
313
 
314
         if [ "`isVmciNeeded`" = 'yes' ]; then
315
            vmware_exec 'Virtual machine communication interface' vmwareStartVmci
316
            exitcode=$(($exitcode + $?))
317
         fi
318
 
319
         # vsock needs vmci started first
320
         if [ "`isVsockNeeded`" = 'yes' ]; then
321
            vmware_exec 'VM communication interface socket family' vmwareStartVsock
322
            # a vsock failure to load shouldn't cause the init to fail completely.
323
         fi
324
 
325
         if [ "`is_vmblock_needed`" = 'yes' ] ; then
326
            vmware_exec 'Blocking file system' vmware_start_vmblock
327
            exitcode=$(($exitcode + $?))
328
         fi
329
 
330
         # Try to load parport_pc.
331
         /sbin/modprobe parport_pc >/dev/null 2>&1
332
 
333
         if vmwareIsNetworkingEnabled; then
334
            vmware_exec 'Virtual ethernet' vmwareStartVmnet
335
            exitcode=$(($exitcode + $?))
336
         fi
337
 
338
         vmware_exec 'VMware Authentication Daemon' vmware_start_authdlauncher
339
 
340
         if [ "$exitcode" -gt 0 ]; then
341
            exit 1
342
         fi
343
 
344
         [ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
345
         touch /var/lock/subsys/"$subsys"
346
 
347
         vmware_exec "Shared Memory Available"  vmwareCheckSharedMemory
348
      ;;
349
 
350
      stop)
351
         echo 'Stopping VMware services:'
352
         exitcode='0'
353
 
354
         vmware_exec 'VMware Authentication Daemon' vmware_stop_authdlauncher
355
 
356
         # If the 'K' version of this script is running, the system is
357
         # stoping services not because the user is running vmware-config.pl
358
         # or running the initscript directly but because the user wants to
359
         # shutdown.  Suspend all VMs.
360
         if [ "`echo $BASENAME | sed -ne '/^K[0-9].vmware/p'`" ] ; then
361
            if [ -x "$BINDIR"/vmrun ] ; then
362
               for i in `pidof vmware-vmx` ; do
363
                  "$BINDIR"/vmrun suspend `ps -p $i -f | \
364
                       sed -ne '/vmware/s/.* \(\/.*\.vmx\)/\1/p'` 2> /dev/null
365
               done
366
            fi
367
 
368
         fi
369
 
370
         if [ "`vmmonUseCount`" -gt 0 ]; then
371
            echo 'At least one instance of '"$PRODUCT_NAME"' is still running.' 1>&2
372
            echo 'Please stop all running instances of '"$PRODUCT_NAME"' first.' 1>&2
373
            echo " " >&2
374
 
375
            # Since we stopped authdlauncher to prevent new connections before disabling
376
            # any vmxs, need to restart it here to restore the environment back to
377
            # what it was before this init script ran.
378
            vmware_exec 'VMware Authentication Daemon' vmware_start_authdlauncher
379
 
380
            # The unconfigurator handle this exit code differently
381
            exit 2
382
         fi
383
 
384
         # vmci is used by vsock so the module can't unload until vsock does.
385
         if [ "`isVsockNeeded`" = 'yes' ]; then
386
            vmware_exec 'VM communication interface socket family' vmwareStopVsock
387
            exitcode=$(($exitcode + $?))
388
         fi
389
 
390
         if [ "`isVmciNeeded`" = 'yes' ]; then
391
            vmware_exec 'Virtual machine communication interface' vmwareStopVmci
392
            exitcode=$(($exitcode + $?))
393
         fi
394
 
395
         vmware_exec 'Virtual machine monitor' vmwareStopVmmon
396
         exitcode=$(($exitcode + $?))
397
 
398
         if [ "`is_vmblock_needed`" = 'yes' ] ; then
399
            vmware_exec 'Blocking file system' vmware_stop_vmblock
400
            exitcode=$(($exitcode + $?))
401
         fi
402
 
403
         # Try to unload parport_pc. Failure is allowed as it does not
404
         # exist on kernels 2.0, and some other process could be using
405
         # it.
406
         /sbin/modprobe -r parport_pc >/dev/null 2>&1
407
 
408
         if vmwareIsNetworkingEnabled; then
409
	    vmwareStopVmnet
410
         fi
411
 
412
         # The vmware and vmware-tray processes don't terminate automatically
413
         # when the other services are shutdown.  They persist after calling
414
         # 'init.d/vmware stop' and will happily keep going through an init
415
         # start command, continuing to minimally function, blissfully ignorant.
416
         # Time for a buzzkill.
417
         for i in `pidof vmware vmware-tray` ; do
418
            vmware_synchrone_kill $i "INT"
419
         done
420
 
421
         if [ "$exitcode" -gt 0 ]; then
422
            exit 1
423
         fi
424
 
425
         rm -f /var/lock/subsys/"$subsys"
426
      ;;
427
 
428
      status)
429
         if [ "`vmmonUseCount`" -gt 0 ]; then
430
            echo 'At least one instance of '"$PRODUCT_NAME"' is still running.'
431
            echo
432
            if [ "$2" = "vmcount" ]; then
433
               exit 2
434
            fi
435
         fi
436
         if [ "$2" = "vmcount" ]; then
437
               exit 0
438
         fi
439
 
440
         exitcode='0'
441
 
442
         echo -n "Module $driver "
443
         [ "`isLoaded "$driver"`" = 'yes' ] && echo loaded || echo "not loaded"
444
         if vmwareIsNetworkingEnabled; then
445
            echo -n "Module $vnet "
446
            [ "`isLoaded "$vnet"`" = 'yes' ] && echo loaded || echo "not loaded"
447
         fi
448
 
449
         if [ "$exitcode" -gt 0 ]; then
450
            exit 1
451
         fi
452
      ;;
453
 
454
      restart)
455
         "$SCRIPTNAME" stop && "$SCRIPTNAME" start
456
      ;;
457
 
458
      # Called to make sure script is in a runnable state.
459
      validate)
460
         exit 100
461
      ;;
462
 
463
      stoppable)
464
	 [ "`vmmonUseCount`" -lt 1 ]
465
	 exit
466
      ;;
467
 
468
      *)
469
         echo "Usage: "$BASENAME" {start|stop|status|restart|stoppable}"
470
         exit 1
471
   esac
472
}
473
 
474
SCRIPTNAME="$0"
475
BASENAME=`basename "$SCRIPTNAME"`
476
 
477
# Check permissions
478
if [ "`id -ur`" != '0' ]; then
479
   echo 'Error: you must be root.'
480
   echo
481
   exit 1
482
fi
483
 
484
vmwareService "$1"
485
 
486
exit 0