Skip to content

Commit

Permalink
Merge pull request #440 from krig/xen-ra
Browse files Browse the repository at this point in the history
Update Xen RA to use xl instead of the deprecated xm
  • Loading branch information
dmuhamedagic committed Aug 14, 2014
2 parents 183567b + c4a3032 commit e6a5ab4
Showing 1 changed file with 54 additions and 41 deletions.
95 changes: 54 additions & 41 deletions heartbeat/Xen
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# Resource Agent for the Xen Hypervisor.
# Manages Xen virtual machine instances by
# mapping cluster resource start and stop,
# mapping cluster resource start and stop,
# to Xen create and shutdown, respectively.
#
# usage: $0 {start|stop|status|monitor|meta-data}
Expand All @@ -32,11 +32,10 @@

#######################################################################


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


Expand All @@ -45,6 +44,9 @@ usage() {
: ${OCF_RESKEY_allow_mem_management=0}
: ${OCF_RESKEY_reserved_Dom0_memory=512}

# prefer xl
xentool=$(which xl 2> /dev/null || which xm)

meta_data() {
cat <<END
<?xml version="1.0"?>
Expand Down Expand Up @@ -95,9 +97,9 @@ Name of the virtual machine.
</parameter>
<parameter name="shutdown_timeout">
<longdesc lang="en">
The Xen agent will first try an orderly shutdown using xm shutdown.
The Xen agent will first try an orderly shutdown using xl shutdown.
Should this not succeed within this timeout, the agent will escalate to
xm destroy, forcibly killing the node.
xl destroy, forcibly killing the node.
If this is not set, it will default to two-third of the stop action
timeout.
Expand All @@ -119,7 +121,7 @@ without installed PV drivers.
</parameter>
<parameter name="allow_mem_management" unique="0" required="0">
<longdesc lang="en">
This parameter enables dynamic adjustment of memory for start
This parameter enables dynamic adjustment of memory for start
and stop actions used for Dom0 and the DomUs. The default is
to not adjust memory dynamically.
</longdesc>
Expand Down Expand Up @@ -187,30 +189,38 @@ Xen_Status() {
return $OCF_SUCCESS
fi
fi
STATUS=`xm list --long $1 2>/dev/null | grep status 2>/dev/null`
if have_binary xenstore-ls; then
xenstore-ls -f /vm | grep -E "/vm.*name = \"$1\"" > /dev/null 2>&1
if [ $? -ne 0 ]; then
return $OCF_NOT_RUNNING
else
return $OCF_SUCCESS
fi
fi
STATUS=`$xentool list --long $1 2>/dev/null | grep status 2>/dev/null`
if [ "X${STATUS}" != "X" ]; then
# we have Xen 3.0.4 or higher
STATUS_NOSPACES=`echo "$STATUS" | awk '{ print $1,$2}'`
if [ "$STATUS_NOSPACES" = "(status 2)" -o "$STATUS_NOSPACES" = "(status 1)" ]; then
return $OCF_SUCCESS
else
else
return $OCF_NOT_RUNNING
fi
else
# we have Xen 3.0.3 or lower
STATUS=`xm list --long $1 2>/dev/null | grep state 2>/dev/null`
STATUS=`$xentool list --long $1 2>/dev/null | grep state 2>/dev/null`
echo "${STATUS}" | grep -qs "[-r][-b][-p]---"
if [ $? -ne 0 ]; then
return $OCF_NOT_RUNNING
else
return $OCF_SUCCESS
fi

fi
}

# If the guest is rebooting, it may completely disappear from the
# list of defined guests, thus xm/xen-list would return with not
# list of defined guests, thus xl/xen-list would return with not
# running; apparently, this period lasts only for a second or
# two
# If a status returns not running, then test status
Expand Down Expand Up @@ -251,16 +261,16 @@ Xen_Adjust_Memory() {
#NEWMEM=`echo "(${MAXMEM}-${OCF_RESKEY_reserved_Dom0_memory})/(${RUNCNT}+${CNTNEW})"|bc`
NEWMEM=$(( (${MAXMEM} - ${OCF_RESKEY_reserved_Dom0_memory}) / (${RUNCNT} + ${CNTNEW} ) ))
# do not rely on ballooning add dom0_mem=512 instead to force memory for dom0
#xm mem-set Domain-0 ${OCF_RESKEY_reserved_Dom0_memory}
#$xentool mem-set Domain-0 ${OCF_RESKEY_reserved_Dom0_memory}
for DOM in ${RUNNING}; do
xm mem-set ${DOM} ${NEWMEM}
$xentool mem-set ${DOM} ${NEWMEM}
done
ocf_log info "Adjusted memory to: $NEWMEM, for the following $RUNCNT domains: $RUNNING"
fi
}

Xen_List_all() {
xm list | grep -v -e "Name" -e "Domain-0" | awk '{print $1}'
$xentool list | grep -v -e "Name" -e "Domain-0" | awk '{print $1}'
}
Xen_List_running() {
ALL_DOMS=`Xen_List_all`
Expand Down Expand Up @@ -298,7 +308,7 @@ Xen_Monitor() {
}

Xen_Total_Memory() {
xm info | grep "^total_memory" | awk '{print $3}'
$xentool info | grep "^total_memory" | awk '{print $3}'
}

Xen_Start() {
Expand All @@ -316,16 +326,16 @@ Xen_Start() {
Xen_Adjust_Memory 1
ocf_log info "New memory for virtual domains: ${NEWMEM}"
sed -i -e "/^memory=/ s/^memory=.*/memory=${NEWMEM}/" ${OCF_RESKEY_xmfile}
xm mem-set ${DOMAIN_NAME} ${NEWMEM}
$xentool mem-set ${DOMAIN_NAME} ${NEWMEM}
fi

xm create ${OCF_RESKEY_xmfile} name=$DOMAIN_NAME
$xentool create ${OCF_RESKEY_xmfile} name=\"$DOMAIN_NAME\"
rc=$?
if [ $rc -ne 0 ]; then
return $OCF_ERR_GENERIC
else
else
if ocf_is_true "${OCF_RESKEY_allow_mem_management}"; then
xm mem-set ${DOMAIN_NAME} ${NEWMEM}
$xentool mem-set ${DOMAIN_NAME} ${NEWMEM}
fi
fi
while sleep 1; do
Expand All @@ -350,26 +360,26 @@ xen_domain_stop() {
if [ "$timeout" -gt 0 ]; then
ocf_log info "Xen domain $dom will be stopped (timeout: ${timeout}s)"
if ocf_is_true "${OCF_RESKEY_shutdown_acpi}"; then
xm trigger $dom power
$xentool trigger $dom power
else
xm shutdown $dom
$xentool shutdown $dom
fi

while Xen_Status $dom && [ "$timeout" -gt 0 ]; do
ocf_log debug "$dom still not stopped. Waiting..."
timeout=$((timeout-1))
sleep 1
done
fi

if [ "$timeout" -eq 0 ]; then
while Xen_Status $dom; do
ocf_log warn "Xen domain $dom will be destroyed!"
$xenkill $dom
sleep 1
done
# Note: This does not give up. stop isn't allowed to to fail.
# If xm destroy fails, stop will eventually timeout.
# If $xentool destroy fails, stop will eventually timeout.
# This is the correct behaviour.
fi

Expand All @@ -381,18 +391,18 @@ Xen_Stop() {
if Xen_Status_with_Retry ${DOMAIN_NAME}; then
vm=${DOMAIN_NAME}
elif Xen_Status migrating-${DOMAIN_NAME}; then
ocf_log info "Xen domain $DOMAIN_NAME is migrating"
ocf_log info "Xen domain $DOMAIN_NAME is migrating"
vm="migrating-${DOMAIN_NAME}"
else
ocf_log info "Xen domain $DOMAIN_NAME already stopped."
ocf_log info "Xen domain $DOMAIN_NAME already stopped."
fi

if [ "$vm" ]; then
xen_domain_stop $vm
else
# It is supposed to be gone, but there have been situations where xm
# list / xen-list showed it as stopped but it was still instantiated.
# Nuke it once more to make sure:
# It is supposed to be gone, but there have been situations where
# $xentool list / xen-list showed it as stopped but it was still
# instantiated. Nuke it once more to make sure:
$xenkill ${DOMAIN_NAME}
fi

Expand All @@ -404,10 +414,10 @@ Xen_Migrate_To() {
target_node="$OCF_RESKEY_CRM_meta_migrate_target"
target_attr="$OCF_RESKEY_node_ip_attribute"
target_addr="$target_node"

if Xen_Status ${DOMAIN_NAME}; then
ocf_log info "$DOMAIN_NAME: Starting xm migrate to $target_node"
ocf_log info "$DOMAIN_NAME: Starting $xentool migrate to $target_node"

if [ -n "$target_attr" ]; then
nodevalue=`crm_attribute --type nodes --node-uname $target_node --attr-name $target_attr --get-value -q`
if [ -n "${nodevalue}" -a "${nodevalue}" != "(null)" ]; then
Expand All @@ -416,15 +426,18 @@ Xen_Migrate_To() {
fi
fi

xm migrate --live $DOMAIN_NAME $target_addr

if [[ "$xentool" == *xm ]]; then
$xentool migrate --live $DOMAIN_NAME $target_addr
else
$xentool migrate $DOMAIN_NAME $target_addr
fi
rc=$?
if [ $rc -ne 0 ]; then
ocf_log err "$DOMAIN_NAME: xm migrate to $target_node failed: $rc"
ocf_log err "$DOMAIN_NAME: $xentool migrate to $target_node failed: $rc"
return $OCF_ERR_GENERIC
else
else
Xen_Adjust_Memory 0
ocf_log info "$DOMAIN_NAME: xm migrate to $target_node succeeded."
ocf_log info "$DOMAIN_NAME: $xentool migrate to $target_node succeeded."
return $OCF_SUCCESS
fi
else
Expand Down Expand Up @@ -495,19 +508,19 @@ else
DOMAIN_NAME=${DOMAIN_NAME:-${OCF_RESOURCE_INSTANCE}}
fi

for binary in xm sed awk; do
for binary in sed awk; do
check_binary $binary
done

if have_binary xen-destroy ; then
xenkill="xen-destroy"
else
xenkill="xm destroy"
xenkill="$xentool destroy"
fi

if [ -n "$OCF_RESKEY_shutdown_timeout" ]; then
ocf_is_decimal "$OCF_RESKEY_shutdown_timeout" || {
ocf_log err "shutdown_timeout must be a number"
ocf_log err "shutdown_timeout must be a number"
exit $OCF_ERR_CONFIGURED
}
fi
Expand Down

0 comments on commit e6a5ab4

Please sign in to comment.