Subversion Repositories configs

Rev

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