This file is indexed.

/usr/lib/ocf/resource.d/heartbeat/VirtualDomain is in cluster-agents 1:1.0.4-0ubuntu2.

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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/bin/sh
#
# Support:      linux-ha@lists.linux-ha.org
# License:      GNU General Public License (GPL)
#
#   Resource Agent for domains managed by the libvirt API.
#   Requires a running libvirt daemon (libvirtd).
#
#   (c) 2008-2010 Florian Haas, Dejan Muhamedagic,
#                 and Linux-HA contributors
#
#	usage: $0 {start|stop|status|monitor|migrate_to|migrate_from|meta-data|validate-all}
#
#######################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Defaults
OCF_RESKEY_force_stop_default=0
OCF_RESKEY_hypervisor_default="$(virsh --quiet uri)"

: ${OCF_RESKEY_force_stop=${OCF_RESKEY_force_stop_default}}
: ${OCF_RESKEY_hypervisor=${OCF_RESKEY_hypervisor_default}}
#######################################################################

## I'd very much suggest to make this RA use bash,
## and then use magic $SECONDS.
## But for now:
NOW=$(date +%s)

usage() {
  echo "usage: $0 {start|stop|status|monitor|migrate_to|migrate_from|meta-data|validate-all}"
}

meta_data() {
	cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="VirtualDomain">
<version>1.0</version>

<longdesc lang="en">
Resource agent for a virtual domain (a.k.a. domU, virtual machine,
virtual environment etc., depending on context) managed by libvirtd.
</longdesc>
<shortdesc lang="en">Manages virtual domains through the libvirt virtualization framework</shortdesc>

<parameters>

<parameter name="config" unique="1" required="1">
<longdesc lang="en">
Absolute path to the libvirt configuration file,
for this virtual domain.
</longdesc>
<shortdesc lang="en">Virtual domain configuration file</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="hypervisor" unique="0" required="0">
<longdesc lang="en">
Hypervisor URI to connect to. See the libvirt documentation for
details on supported URI formats. The default is system dependent.
</longdesc>
<shortdesc lang="en">Hypervisor URI</shortdesc>
<content type="string" default="${OCF_RESKEY_hypervisor_default}"/>
</parameter>

<parameter name="force_stop" unique="0" required="0">
<longdesc lang="en">
Always forcefully shut down ("destroy") the domain on stop. The default
behavior is to resort to a forceful shutdown only after a graceful
shutdown attempt has failed. You should only set this to true if
your virtual domain (or your virtualization backend) does not support
graceful shutdown.
</longdesc>
<shortdesc lang="en">Always force shutdown on stop</shortdesc>
<content type="boolean" default="${OCF_RESKEY_force_stop_default}" />
</parameter>

<parameter name="migration_transport" unique="0" required="0">
<longdesc lang="en">
Transport used to connect to the remote hypervisor while
migrating. Please refer to the libvirt documentation for details on
transports available. If this parameter is omitted, the resource will
use libvirt's default transport to connect to the remote hypervisor.
</longdesc>
<shortdesc lang="en">Remote hypervisor transport</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="monitor_scripts" unique="0" required="0">
<longdesc lang="en">
To additionally monitor services within the virtual domain, add this
parameter with a list of scripts to monitor.

Note: when monitor scripts are used, the start and migrate_from operations
will complete only when all monitor scripts have completed successfully.
Be sure to set the timeout of these operations to accommodate this delay.
</longdesc>
<shortdesc lang="en">space-separated list of monitor scripts</shortdesc>
<content type="string" default="" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="90" />
<action name="stop" timeout="90" />
<action name="status" depth="0" timeout="30" interval="10" />
<action name="monitor" depth="0" timeout="30" interval="10" />
<action name="migrate_from" timeout="60" />
<action name="migrate_to" timeout="120" />
<action name="meta-data" timeout="5" />
<action name="validate-all" timeout="5" />
</actions>
</resource-agent>
EOF
}

# Set options to be passed to virsh:
VIRSH_OPTIONS="--connect=${OCF_RESKEY_hypervisor} --quiet"

# A state file where we record the domain name:
STATEFILE="${HA_RSCTMP}/VirtualDomain-${OCF_RESOURCE_INSTANCE}.state"

VirtualDomain_Define() {
    local virsh_output
    local domain_name
    # Note: passing in the domain name from outside the script is
    # intended for testing and debugging purposes only. Don't do this
    # in production, instead let the script figure out the domain name
    # from the config file. You have been warned.
    if [ -z "$DOMAIN_NAME" ]; then
	# Spin until we have a domain name
	while true; do
	    virsh_output=`virsh ${VIRSH_OPTIONS} define ${OCF_RESKEY_config}`
	    domain_name=`echo "$virsh_output" | sed -e 's/Domain \(.*\) defined from .*$/\1/'`
            if [ -n $domain_name ]; then
		break;
            fi
	    ocf_log debug "Domain not defined yet, probably unable to connect to hypervisor. Retrying."
            sleep 1
	done
	echo "$domain_name" > $STATEFILE
	ocf_log info "Domain name \"$domain_name\" saved to $STATEFILE."
    else
	ocf_log warn "Domain name ${DOMAIN_NAME} already defined, overriding configuration file ${OCF_RESKEY_config}. You should do this for testing only."
    fi
}

VirtualDomain_Cleanup_Statefile() {
    rm -f $STATEFILE || ocf_log warn "Failed to remove $STATEFILE during $__OCF_ACTION."
}

VirtualDomain_Status() {
    local try=0
    rc=$OCF_ERR_GENERIC
    status="no state"
    while [ "$status" = "no state" ]; do
	try=$(($try + 1 ))
        status="`virsh $VIRSH_OPTIONS domstate $DOMAIN_NAME`"
        case "$status" in
	    "shut off")
	        # shut off: domain is defined, but not started
		ocf_log debug "Virtual domain $DOMAIN_NAME is currently $status."
	        rc=$OCF_NOT_RUNNING
	        ;;
	    running|paused|idle|blocked)
		# running: domain is currently actively consuming cycles
		# paused: domain is paused (suspended)
		# idle: domain is running but idle
		# blocked: synonym for idle used by legacy Xen versions
		ocf_log debug "Virtual domain $DOMAIN_NAME is currently $status."
	        rc=$OCF_SUCCESS
	        ;;
            ""|"no state")
		# Empty string may be returned when virsh does not
		# receive a reply from libvirtd.
		# "no state" may occur when the domain is currently
		# being migrated (on the migration target only), or
		# whenever virsh can't reliably obtain the domain
		# state.
		status="no state"
		if [ "$__OCF_ACTION" = "stop" ] && [ $try -ge 3 ]; then
		    # During the stop operation, we want to bail out
		    # quickly, so as to be able to force-stop (destroy)
		    # the domain if necessary.
		    ocf_log error "Virtual domain $DOMAIN_NAME has no state during stop operation, bailing out."
		    return $OCF_ERR_GENERIC;
		else
		    # During all other actions, we just wait and try
		    # again, relying on the CRM/LRM to time us out if
		    # this takes too long.
		    ocf_log info "Virtual domain $DOMAIN_NAME currently has no state, retrying."
		    sleep 1
		fi
		;;
            *)
		# any other output is unexpected.
                ocf_log error "Virtual domain $DOMAIN_NAME has unknown status \"$status\"!"
                ;;
        esac
    done
    return $rc
}

VirtualDomain_Start() {
    if VirtualDomain_Status; then
	ocf_log info "Virtual domain $DOMAIN_NAME already running."
	return $OCF_SUCCESS
    fi

    virsh $VIRSH_OPTIONS start ${DOMAIN_NAME}
    rc=$?
    if [ $rc -ne 0 ]; then
	ocf_log error "Failed to start virtual domain ${DOMAIN_NAME}."
	return $OCF_ERR_GENERIC
    fi

    while ! VirtualDomain_Monitor; do
	sleep 1
    done
    return $OCF_SUCCESS
}

VirtualDomain_Stop() {
    local i
    local status
    local shutdown_timeout
    local out ex

    VirtualDomain_Status
    status=$?

    case $status in
	$OCF_SUCCESS)
	    if ! ocf_is_true $OCF_RESKEY_force_stop; then
 		# Issue a graceful shutdown request
		ocf_log info "Issuing graceful shutdown request for domain ${DOMAIN_NAME}."
		virsh $VIRSH_OPTIONS shutdown ${DOMAIN_NAME}
	        # The "shutdown_timeout" we use here is the operation
		# timeout specified in the CIB, minus 5 seconds
		shutdown_timeout=$(( $NOW + ($OCF_RESKEY_CRM_meta_timeout/1000) -5 ))
		# Loop on status until we reach $shutdown_timeout
		while [ $NOW -lt $shutdown_timeout ]; do
		    VirtualDomain_Status
		    status=$?
		    case $status in
			$OCF_NOT_RUNNING)
			    # This was a graceful shutdown. Clean
			    # up and return.
			    VirtualDomain_Cleanup_Statefile
			    return $OCF_SUCCESS
			    ;;
			$OCF_SUCCESS)
			    # Domain is still running, keep
			    # waiting (until shutdown_timeout
			    # expires)
			    sleep 1
			    ;;
			*)
			    # Something went wrong. Bail out and
			    # resort to forced stop (destroy).
			    break;
		    esac
		    NOW=$(date +%s)
		done
	    fi
	    ;;
	$OCF_NOT_RUNNING)
	    ocf_log info "Domain $DOMAIN_NAME already stopped."
	    return $OCF_SUCCESS
    esac
    # OK. Now if the above graceful shutdown hasn't worked, kill
    # off the domain with destroy. If that too does not work,
    # have the LRM time us out.
    ocf_log info "Issuing forced shutdown (destroy) request for domain ${DOMAIN_NAME}."
    out=$(virsh $VIRSH_OPTIONS destroy ${DOMAIN_NAME} 2>&1)
    ex=$?
    echo >&2 "$out"
    # unconditionally clean up.
    VirtualDomain_Cleanup_Statefile
    case $ex$out in
	*"error: Requested operation is not valid: domain is not running"*)	
	    : ;; # unexpected path to the intended outcome, all is well
	[!0]*)
	    return $OCF_ERR_GENERIC ;;
	0*)
	    while [ $status != $OCF_NOT_RUNNING ]; do
		VirtualDomain_Status
		status=$?
	    done ;;
    esac
    return $OCF_SUCCESS
}

VirtualDomain_Migrate_To() {
    local target_node
    local remoteuri
    local transport_suffix

    target_node="$OCF_RESKEY_CRM_meta_migrate_target"

    if VirtualDomain_Status; then
        # Find out the remote hypervisor to connect to. That is, turn
        # something like "qemu://foo:9999/system" into
        # "qemu+tcp://bar:9999/system"
	if [ -n "${OCF_RESKEY_migration_transport}" ]; then
	    transport_suffix="+${OCF_RESKEY_migration_transport}"
	fi
        # Scared of that sed expression? So am I. :-)
	remoteuri=$(echo ${OCF_RESKEY_hypervisor} | sed -e "s,\(.*\)://[^/:]*\(:\?[0-9]*\)/\(.*\),\1${transport_suffix}://${target_node}\2/\3,")

	# OK, we know where to connect to. Now do the actual migration.
	ocf_log info "$DOMAIN_NAME: Starting live migration to ${target_node} (using remote hypervisor URI ${remoteuri})."
	virsh ${VIRSH_OPTIONS} migrate --live $DOMAIN_NAME ${remoteuri}
	rc=$?
	if [ $rc -ne 0 ]; then
	    ocf_log err "$DOMAIN_NAME: live migration to ${remoteuri} failed: $rc"
	    return $OCF_ERR_GENERIC
	else
	    ocf_log info "$DOMAIN_NAME: live migration to ${target_node} succeeded."
	    VirtualDomain_Cleanup_Statefile
	    return $OCF_SUCCESS
	fi
    else
	ocf_log err "$DOMAIN_NAME: migrate_to: Not active locally!"
	return $OCF_ERR_GENERIC
    fi
}

VirtualDomain_Migrate_From() {
    while ! VirtualDomain_Monitor; do
	sleep 1
    done
    ocf_log info "$DOMAIN_NAME: live migration from ${OCF_RESKEY_CRM_meta_migrate_source} succeeded."
    return $OCF_SUCCESS
}

VirtualDomain_Monitor() {
    # First, check the domain status. If that returns anything other
    # than $OCF_SUCCESS, something is definitely wrong.
    VirtualDomain_Status
    rc=$?
    if [ ${rc} -eq ${OCF_SUCCESS} ]; then
	# OK, the generic status check turned out fine.  Now, if we
	# have monitor scripts defined, run them one after another.
	for script in ${OCF_RESKEY_monitor_scripts}; do
	    script_output="$($script 2>&1)"
	    script_rc=$?
	    if [ ${script_rc} -ne ${OCF_SUCCESS} ]; then
		# A monitor script returned a non-success exit
		# code. Stop iterating over the list of scripts, log a
		# warning message, and propagate $OCF_ERR_GENERIC.
		ocf_log warn "Monitor command \"${script}\" for domain ${DOMAIN_NAME} returned ${script_rc} with output: ${script_output}"
		rc=$OCF_ERR_GENERIC
		break
	    else
		ocf_log debug "Monitor command \"${script}\" for domain ${DOMAIN_NAME} completed successfully with output: ${script_output}"
	    fi
	done
    fi
    return ${rc}
}

VirtualDomain_Validate_All() {
    # Required binaries:
    for binary in virsh sed; do
        check_binary $binary
    done

    if [ -z $OCF_RESKEY_config ]; then
	ocf_log error "Missing configuration parameter \"config\"."
	return $OCF_ERR_CONFIGURED
    fi

    # check if we can read the config file (otherwise we're unable to
    # deduce $DOMAIN_NAME from it, see below)
    if [ ! -r $OCF_RESKEY_config ]; then
	if ocf_is_probe; then
	    ocf_log info "Configuration file $OCF_RESKEY_config not readable during probe."
	else
	    ocf_log error "Configuration file $OCF_RESKEY_config does not exist or is not readable."
	    return $OCF_ERR_INSTALLED
	fi
    fi
}

if [ $# -ne 1 ]; then
  usage
  exit $OCF_ERR_ARGS
fi

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

# Everything except usage and meta-data must pass the validate test
VirtualDomain_Validate_All || exit $?

# During a probe, it is permissible for the config file to not be
# readable (it might be on shared storage not available during the
# probe). In that case, VirtualDomain_Define can't work and we're
# unable to get the domain name. Thus, we also can't check whether the
# domain is running. The only thing we can do here is to assume that
# it is not running.
if ocf_is_probe && [ ! -r $OCF_RESKEY_config ]; then
    exit $OCF_NOT_RUNNING
fi

# Define the domain on startup, and re-define whenever someone deleted
# the state file, or touched the config.
if [ ! -e $STATEFILE ] || [ $OCF_RESKEY_config -nt $STATEFILE ]; then
    VirtualDomain_Define 
fi
# By now, we should definitely be able to read from the state file.
# If not, something went wrong.
if [ ! -r $STATEFILE ]; then
    ocf_log err "$STATEFILE not found or unreadable. This is unexpected. Cannot determine domain name."
    exit $OCF_ERR_GENERIC
fi
# Finally, retrieve the domain name from the state file.
DOMAIN_NAME=`cat $STATEFILE 2>/dev/null`
if [ -z $DOMAIN_NAME ]; then
    ocf_log err "$STATEFILE is empty. This is unexpected. Cannot determine domain name."
    exit $OCF_ERR_GENERIC
fi

case $1 in
    start)
	VirtualDomain_Start
	;;
    stop)
	VirtualDomain_Stop
	;;
    migrate_to)
	VirtualDomain_Migrate_To
	;;
    migrate_from)
	VirtualDomain_Migrate_From
	;;
    status)
	VirtualDomain_Status
	;;
    monitor)
	VirtualDomain_Monitor
        ;;
    validate-all)		
	;;
    *)	
	usage
	exit $OCF_ERR_UNIMPLEMENTED
	;;
esac
exit $?