This file is indexed.

/usr/lib/ocf/resource.d/heartbeat/vmware is in resource-agents 1:3.9.7-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/bin/sh
#
# VMware OCF resource agent
#
# Copyright (c) 2010 Apra Sistemi s.r.l.
#                    All Rights Reserved.
#
# Description:  Manages VMware server 2.0 virtual machines
#               as High-Availability resources
#
#
# Author:       Cristian Mammoli <c.mammoli AT apra DOT it>
# License:      GNU General Public License (GPL)
# Copyright:    (C) 2010 Apra Sistemi s.r.l.
#
# See usage() function below for more details...
#
# OCF instance parameters:
#  * OCF_RESKEY_vmxpath (mandatory: full path to the virtual machine vmx file)
#  * OCF_RESKEY_vimshbin (optional: full path to the vmware-vim-cmd executable,
#                        fallback to default location if not declared)
#
# Requirements/caveats:
#  * vmware-server 2.0 installed and autostarted on all nodes
#  * vmdk files must be in the same directory of the vmx file
#  * vmx filenames must be unique, even if stored in different directories
#  * Default_Action_Timeout stock value (20 sec) isn't enough if you are
#    dealing with many virtual machines: raise it to something around 600 secs
#    or use operation attributes with the proposed values
#  * Moving a vm among nodes will cause its mac address to change: if you need
#    to preserve the mac address set it manually in the nic options
#  * The script should be able to deal with paths and filenames with spaces,
#    anyway try to avoid it

# Initialization
#################################################################

# Source ocf shell functions
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Basic variables configuration
OCF_RESKEY_vimshbin_default="/usr/bin/vmware-vim-cmd"
: ${OCF_RESKEY_vimshbin=${OCF_RESKEY_vimshbin_default}}
#################################################################

# Path to the virtual machine configuration file
VMXPATH="$OCF_RESKEY_vmxpath"

# Path to the vmware-vim-cmd executable
VIMSHBIN="$OCF_RESKEY_vimshbin"

# Global variables
VMXDIR=
RELVMXPATH=
VMID=
VM=
VMAUTOMSG=

# vmware-vim-cmd functions
#################################################################

# Get virtual machine vid
vmware_get_vid() {
  $VIMSHBIN vmsvc/getallvms \
         | awk '/\/'"$1"'/ {print $1}'
}

# Is the vm waiting for input after a migration?
vmware_uuid_alt() {
  $VIMSHBIN vmsvc/message $1 \
         | awk /^msg.uuid.altered/
}

# Get message id
vmware_get_msgid() {
  $VIMSHBIN vmsvc/message $1 \
      | awk '/^Virtual machine message/ {print $4}' \
      | awk -F : '{print $1}'
}

# Answers message
vmware_answer_msg() {
  $VIMSHBIN vmsvc/message $1 $2 $3 >/dev/null
}

# Register a virtual machine
vmware_register_vm() {
  $VIMSHBIN solo/registervm '"'$1'"' >/dev/null
}

# Unregister a virtual machine
vmware_unregister_vm() {
  $VIMSHBIN  vmsvc/unregister $1 >/dev/null
}

# Start a virtual machine
vmware_poweron_vm() {
  $VIMSHBIN vmsvc/power.on $1 >/dev/null
}

# Suspend a virtual machine
vmware_suspend_vm() {
  $VIMSHBIN vmsvc/power.suspend $1 >/dev/null
}

# Get virtual machine power state
vmware_get_status() {
  $VIMSHBIN vmsvc/power.getstate $1 \
         | awk '/^Powered on/ || /^Powered off/ || /^Suspended/'
}

# Get vid of missing virtual machines
vmware_get_broken() {
  $VIMSHBIN vmsvc/getallvm 2>&1 \
         | awk -F \' '/^Skipping/ {print $2}'
}

# Variables depending on the above functions
#################################################################

vmware_set_env() {
	# Directory containing the virtual machine
	VMXDIR="`dirname "$VMXPATH"`"

	# Basename of the configuration file
	RELVMXPATH="`basename "$VMXPATH"`"

	# Vid of the virtual machine (can be empty if the vm is not registered)
	VMID=`vmware_get_vid "$RELVMXPATH"`

	# Virtual machine name
	VM="`awk -F '"' '/^displayName/ {print $2}' "$VMXPATH"`"

	# msg.autoAnswer value in config file
	VMAUTOMSG="`awk -F '"' '/^msg.autoAnswer/ {print toupper($2)}' "$VMXPATH"`"
}

# Main functions
#################################################################

# Print usage summary
vmware_usage() {
    cat <<END
usage: $0 {start|stop|status|monitor|meta-data|validate-all}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

# Check for mandatory files presence and consistency
vmware_validate() {
  if [ -z "`pidof vmware-hostd`" ]; then
    ocf_log err "vmware-hostd is not running"
    exit $OCF_ERR_GENERIC
  fi

  if [ ! -x "$VIMSHBIN" ]; then
    ocf_log err "vmware-vim-cmd executable missing or not in path ($VIMSHBIN)"
    exit $OCF_ERR_ARGS
  fi

  if [ ! -f "$VMXPATH" ]; then
    ocf_log err "Specified vmx file ($VMXPATH) does not exist"
    exit $OCF_ERR_ARGS
  fi

  # Now we can safely setup variables...
  vmware_set_env

  # ... and verify them
  if [ -z "$VM" ]; then
    ocf_log err "Could not find out virtual machine name"
    exit $OCF_ERR_ARGS
  fi

  if [ "$VMAUTOMSG" != "TRUE" ]; then
    ocf_log warn "Please set msg.autoAnswer = \"TRUE\" in your config file"
  fi

  # $VMID is allowed to be empty in case we are validating a
  # virtual machine which is not registered

  return $OCF_SUCCESS
}

# More relaxed checking in case of probes
vmware_validate_probe() {
  if [ ! -x "$VIMSHBIN" ]; then
    ocf_log warn "vmware-vim-cmd executable missing or not in path ($VIMSHBIN)"
    exit $OCF_NOT_RUNNING
  fi

  if [ ! -f "$VMXPATH" ]; then
    ocf_log warn "Specified vmx file ($VMXPATH) does not exist"
    exit $OCF_NOT_RUNNING
  fi

  # Now we can safely setup variables...
  vmware_set_env
}

# Start a virtual machine
vmware_start() {
  # Don't start a VM if it's already running
  if vmware_monitor; then
    ocf_log info "Virtual machine $VM is already running"
    return $OCF_SUCCESS
  else
    # Removes stale lockfiles and missing virtual machines
    # in case of a crash.
    # Do not use with a clustered filesystem or you could
    # end up starting the same VM in more than one node
    ocf_log info "Removing stale lockfiles"
    find "$VMXDIR" -name \*.lck -type f -exec rm "{}" \;
    for BVM in `vmware_get_broken`; do
      ocf_log info "Unregistering missing virtual machine $BVM"
      vmware_unregister_vm $BVM
    done
    if [ -z "$VMID" ]; then
      # VM is not registered, need to register
      ocf_log info "Virtual machine $VM is not registered"
      ocf_log info "Registering Virtual machine $VM"
      vmware_register_vm "$VMXPATH"
      VMID=`vmware_get_vid "$RELVMXPATH"`
      if [ -z "$VMID" ]; then
        ocf_log err "Could not register virtual machine $VM"
        exit $OCF_ERR_GENERIC
      fi
      ocf_log info "Virtual machine $VM registered with ID $VMID"
    fi
    ocf_log info "Powering on virtual machine $VM"
    vmware_poweron_vm $VMID
    # Give the VM some time to initialize
    sleep 10

    if [ "$VMAUTOMSG" != "TRUE" ]; then
      # msg.autoAnswer is not set: we try to deal with the
      # most common question: msg.uuid.altered
      ocf_log info  "Checking msg.uuid.altered on VM $VM"
      if [ -n "`vmware_uuid_alt $VMID`" ]; then
        MSGID=`vmware_get_msgid $VMID`
        vmware_answer_msg $VMID $MSGID 2
      fi
    fi

    # Check if the VM is running. We don't bother
    # with timeouts: we rely on the CRM for that.
    while :; do
      vmware_monitor && break
      ocf_log info "Virtual machine $VM is still stopped: delaying 10 seconds"
      sleep 10
    done

    ocf_log info "Virtual machine $VM is running"
    return $OCF_SUCCESS
  fi
}

# Stop a virtual machine
vmware_stop() {
  # Don't stop a VM if it's not registered
  if [ -z "$VMID" ]; then
    ocf_log info "Virtual machine $VM is not registered"
    return $OCF_SUCCESS
  else
    # Don't stop a VM if it's already stopped
    if vmware_monitor; then
      # If the VM is running send a suspend signal and wait
      # until it is off. We don't bother with timeouts: we
      # rely on the CRM for that.
      ocf_log info "Virtual machine $VM is running: suspending it"
      vmware_suspend_vm $VMID
      sleep 5
      while vmware_monitor; do
        ocf_log info "Virtual machine $VM is still running: delaying 10 seconds"
        sleep 10
      done
    else
      ocf_log info "Virtual machine $VM is already stopped"
    fi
    # VMware randomly fails to unregister VMs,
    # so we loop until we have success or timeout
    ocf_log info "Unregistering virtual machine $VM"
    vmware_unregister_vm $VMID
    VMID=`vmware_get_vid "$RELVMXPATH"`
    while [ -n "$VMID" ]; do
      ocf_log warn "Could not unregister virtual machine $VM: retrying."
      sleep 10
      vmware_unregister_vm $VMID
      VMID=`vmware_get_vid "$RELVMXPATH"`
    done
    ocf_log info "Virtual machine $VM is stopped"
    return $OCF_SUCCESS
  fi
}

# Monitor a virtual machine
vmware_monitor() {
  if [ -n "$VMID" ] && [ "`vmware_get_status $VMID`" = "Powered on" ]; then
    ocf_log debug "Virtual machine $VM (ID $VMID) is running..."
    return $OCF_SUCCESS
  else
    ocf_log debug "Virtual machine $VM is stopped/suspended/not registered"
    return $OCF_NOT_RUNNING
  fi
}

# Print metadata informations
meta_data() {
  cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="vmware">
<version>0.2</version>
<longdesc lang="en">
OCF compliant script to control vmware server 2.0 virtual machines.
</longdesc>
<shortdesc lang="en">Manages VMWare Server 2.0 virtual machines</shortdesc>

<parameters>
<parameter name="vmxpath" unique="0" required="1">
<longdesc lang="en">
VMX configuration file path
</longdesc>
<shortdesc lang="en">VMX file path</shortdesc>
<content type="string"/>
</parameter>

<parameter name="vimshbin" unique="0" required="0">
<longdesc lang="en">
vmware-vim-cmd executable path
</longdesc>
<shortdesc lang="en">vmware-vim-cmd path</shortdesc>
<content type="string" default="${OCF_RESKEY_vimshbin_default}"/>
</parameter>
</parameters>

<actions>
<action name="start"        timeout="600" />
<action name="stop"         timeout="600" />
<action name="monitor"      timeout="30" interval="300" depth="0"/>
<action name="meta-data"    timeout="5" />
</actions>
</resource-agent>
END
}

# See how we were called
#################################################################

case $1 in
meta-data)
  meta_data
  exit $OCF_SUCCESS
  ;;

start)
  vmware_validate
  vmware_start
  ;;

stop)
  vmware_validate
  vmware_stop
  ;;

status|monitor)
  if ocf_is_probe; then
    vmware_validate_probe
  else
    vmware_validate
  fi
  vmware_monitor
  ;;

usage|help)
  vmware_usage
  exit $OCF_SUCCESS
  ;;

validate-all)
  vmware_validate
  ;;

*)
  vmware_usage
  exit $OCF_ERR_UNIMPLEMENTED
  ;;

esac

exit $?