3
# VMware OCF resource agent
5
# Copyright (c) 2008 Apra Sistemi s.r.l.
8
# Description: Manages a VMware server 2.0 as a High-Availability
12
# Author: Cristian Mammoli <c.mammoli AT apra DOT it>
13
# License: GNU General Public License (GPL)
14
# Copyright: (C) 2008 Apra Sistemi s.r.l.
16
# See usage() function below for more details...
18
# OCF instance parameters:
19
# * OCF_RESKEY_VMXPATH (mandatory, full path to the virtual machine vmx file)
20
# * OCF_RESKEY_VIMSHBIN (mandatory, full path to th vmware-vim-cmd executable)
22
# Requirements/caveats:
23
# * vmware-server 2.0 RC1 installed and autostarted on all nodes
24
# * vmdk files must be in the same directory of the vmx file
25
# * vmx filenames must be unique, even if stored in different directories
26
# * Default_Action_Timeout stock value (20 sec) isn't enough if you are
27
# dealing with many virtual machines: raise it to something around 300 secs
28
# or use operation attributes with the proposed values
31
#################################################################
33
# Source ocf shell functions
34
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/resource.d/heartbeat}
35
. ${OCF_FUNCTIONS_DIR}/.ocf-shellfuncs
37
# Basic variables configuration
38
#################################################################
40
# Path to the virtual machine configuration file
41
VMXPATH="$OCF_RESKEY_vmxpath"
43
# Path to the vmware-vim-cmd executable
44
VIMSHBIN="$OCF_RESKEY_vimshbin"
53
# vmware-vim-cmd functions
54
#################################################################
56
# Get virtual machine vid
58
$VIMSHBIN vmsvc/getallvms 2>/dev/null \
59
| awk '/\/'"$1"'/ {print $1}'
62
# Is the vm waiting for input after a migration?
64
$VIMSHBIN vmsvc/message $1 2>/dev/null \
65
| awk /^msg.uuid.altered/
70
$VIMSHBIN vmsvc/message $1 2>/dev/null \
71
| awk '/^Virtual machine message/ {print $4}' \
72
| awk -F : '{print $1}'
77
$VIMSHBIN vmsvc/message $1 $2 $3 >/dev/null 2>&1
80
# Register a virtual machine
81
vmware_register_vm() {
82
$VIMSHBIN solo/registervm '"'$1'"' >/dev/null 2>&1
85
# Unregister a virtual machine
86
vmware_unregister_vm() {
87
$VIMSHBIN vmsvc/unregister $1 >/dev/null 2>&1
90
# Start a virtual machine
92
$VIMSHBIN vmsvc/power.on $1 >/dev/null 2>&1
95
# Suspend a virtual machine
97
$VIMSHBIN vmsvc/power.suspend $1 >/dev/null 2>&1
100
# Get virtual machine power state
101
vmware_get_status() {
102
$VIMSHBIN vmsvc/power.getstate $1 2>/dev/null \
103
| awk '/^Powered on/ || /^Powered off/ || /^Suspended/'
106
# Get vid of missing virtual machines
107
vmware_get_broken() {
108
$VIMSHBIN vmsvc/getallvm 2>&1 \
109
| awk -F "'" '/^Skipping/ {print $2}'
112
# Variables depending on the above functions
113
#################################################################
116
# Directory containing the virtual machine
117
VMXDIR="`dirname "$VMXPATH"`"
119
# Basename of the configuration file
120
RELVMXPATH="`basename "$VMXPATH"`"
122
# Vid of the virtual machine (can be empty if the vm is not registered)
123
VMID=`vmware_get_vid "$RELVMXPATH"`
125
# Virtual machine name
126
VM="`awk -F '"' '/^displayName/ {print $2}' "$VMXPATH"`"
128
# msg.autoAnswer value in config file
129
VMAUTOMSG="`awk -F '"' '/^msg.autoAnswer/ {print $2}' "$VMXPATH"`"
133
#################################################################
135
# Print usage summary
138
usage: $0 {start|stop|status|monitor|meta-data|validate-all}
140
Expects to have a fully populated OCF RA-compliant environment set.
144
# Check for dependencies
145
# Check for mandatory files presence and consistency
147
if [ -z "`pidof vmware-hostd`" ]; then
148
ocf_log warn "vmware-hostd is not running"
149
return $OCF_ERR_GENERIC
152
if [ ! -x "$VIMSHBIN" ]; then
153
ocf_log warn "vmware-vimsh executable missing or not in path ($VIMSHBIN)"
154
return $OCF_ERR_INSTALLED
157
if [ ! -f "$VMXPATH" ]; then
158
ocf_log warn "Specified vmx file ($VMXPATH) does not exist"
162
if [ -z "$VM" ]; then
163
ocf_log warn "Could not find out virtual machine name"
167
if [ "$VMAUTOMSG" != "TRUE" ]; then
168
ocf_log warn "Please set msg.autoAnswer = \"TRUE\" in your config file"
171
# $VMID is allowed to be empty in case we are validating a
172
# virtual machine which is not registered
177
# Start a virtual machine
179
# Don't start a VM if it's already running
180
if vmware_monitor; then
181
ocf_log info "Virtual machine $VM is already running"
184
# Removes stale lockfiles and missing virtual machines
185
# in case of a crash.
186
# Do not use with a clustered filesystem or you could
187
# end up starting the same VM in more than one node
188
ocf_log info "Removing stale lockfiles"
189
find "$VMXDIR" -name \*.lck -type f -exec rm "{}" \;
190
for BVM in `vmware_get_broken`; do
191
ocf_log info "Unregistering missing virtual machine $BVM"
192
vmware_unregister_vm $BVM
194
if [ -z "$VMID" ]; then
195
# VM is not registered, need to register
196
ocf_log info "Virtual machine $VM is not registered"
197
ocf_log info "Registering Virtual machine $VM"
198
vmware_register_vm "$VMXPATH"
199
VMID=`vmware_get_vid "$RELVMXPATH"`
200
if [ -z "$VMID" ]; then
201
ocf_log err "Could not register virtual machine $VM: aborting."
202
exit $OCF_ERR_GENERIC
204
ocf_log info "Virtual machine $VM registered with ID $VMID"
206
ocf_log info "Powering on virtual machine $VM"
207
vmware_poweron_vm $VMID
208
# Give the VM some time to initialize
211
if [ "$VMAUTOMSG" != "TRUE" ]; then
212
# msg.autoAnswer is not set: we try to deal with the
213
# most common question: msg.uuid.altered
214
ocf_log info "Checking msg.uuid.altered on VM $VM"
215
if [ -n "`vmware_uuid_alt $VMID`" ]; then
216
MSGID=`vmware_get_msgid $VMID`
217
vmware_answer_msg $VMID $MSGID 2
221
# Check if the VM is running. We don't bother
222
# with timeouts: we rely on the CRM for that.
224
vmware_monitor && break
225
ocf_log info "Virtual machine $VM is still stopped: delaying 10 seconds"
229
ocf_log info "Virtual machine $VM is running"
234
# Stop a virtual machine
236
# Don't stop a VM if it's not registered
237
if [ -z "$VMID" ]; then
238
ocf_log info "Virtual machine $VM is not registered"
241
# Don't stop a VM if it's already stopped
242
if vmware_monitor; then
243
# If the VM is running send a suspend signal and wait
244
# until it is off. We don't bother with timeouts: we
245
# rely on the CRM for that.
246
ocf_log info "Virtual machine $VM is running: suspending it"
247
vmware_suspend_vm $VMID
249
while vmware_monitor; do
250
ocf_log info "Virtual machine $VM is still running: delaying 10 seconds"
254
ocf_log info "Virtual machine $VM is already stopped"
256
ocf_log info "Unregistering virtual machine $VM"
257
vmware_unregister_vm $VMID
258
VMID=`vmware_get_vid "$RELVMXPATH"`
259
if [ -n "$VMID" ]; then
260
ocf_log err "Could not unregister virtual machine $VM: aborting."
261
exit $OCF_ERR_GENERIC
263
ocf_log info "Virtual machine $VM is stopped"
268
# Monitor a virtual machine
270
if [ "`vmware_get_status $VMID`" = "Powered on" ]; then
271
ocf_log debug "Virtual machine $VM (ID $VMID) is running..."
274
ocf_log debug "Virtual machine $VM is stopped/suspended/not registered"
275
return $OCF_NOT_RUNNING
279
# Print metadata informations
282
<?xml version="1.0"?>
283
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
284
<resource-agent name="vmware">
285
<version>0.1</version>
287
OCF compliant script to control vmware server 2.0 virtual machines.
289
<shortdesc lang="en">Manages VMWare Server 2.0 virtual machines</shortdesc>
292
<parameter name="vmxpath" unique="0" required="1">
294
VMX configuration file path
296
<shortdesc lang="en">VMX file path</shortdesc>
297
<content type="string"/>
300
<parameter name="vimshbin" unique="0" required="1">
302
vmware-vim-cmd executable path
304
<shortdesc lang="en">vmware-vimsh path</shortdesc>
305
<content type="string" default="/usr/bin/vmware-vim-cmd"/>
310
<action name="start" timeout="300" />
311
<action name="stop" timeout="300" />
312
<action name="monitor" timeout="30" interval="300" depth="0"
314
<action name="meta-data" timeout="5" />
320
# See how we were called
321
#################################################################
336
if [ $rc -ne 0 ]; then
338
stop) ocf_log info "validate failed, vmware considered stopped"
340
monitor) exit $OCF_NOT_RUNNING;;
341
status) exit $LSB_STATUS_STOPPED;;
342
*) # it's safe to bail out now
343
ocf_log err "vmware_validate failed"
347
elif [ "$1" = "validate-all" ]; then
367
exit $OCF_ERR_UNIMPLEMENTED