Subversion Repositories configs

Rev

Details | Last modification | View Log | RSS feed

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