XenServer Script: Get VM Running Status for a Storage Resource

I needed to find the state of VMs that were on a XenServer storage volume to "do other stuff" with. If you consider the XenServer 6.0 Command Reference there isn't a direct command for this action (because Virtual Machines have Virtual Block Devices which in turn reference the Virtual Disk Images .. which are what are stored on the disk.  

You could just simply try and disconnect the PBD and catch what comes back.. but that didn't feel a) pretty b) clever. 

So I wrote this script - which queries a XenServer SR (by name or by UUID) and then finds all the VMs' running states and tells you about it. The output (per VM) is :

UUID, vm-name, power-state  

Which you can then manipulate as required.

Comments and feedback welcome.   

====================================================

#!/bin/bash

#--------------------------------
# This email address is being protected from spambots. You need JavaScript enabled to view it.
# 4 April 2016
# script to determine state of VMs on an XenServer SR
#--------------------------------

#set -x
isactive="hosting"
isinactive="idle"

usage()
{
cat << EOF
usage: $0 options

This script shows the running state of VMs on a given XenServer SR. Minimal option will simply state
if the volume is hosting or idle. OPTIONS: -h Show this message -n value name of the SR -u value UUID of the SR -m minimal output ("'$isactive' if at least 1 VM running/suspended or '$isinactive' if all halted ) EOF } reportstate() { #$1 full text, $2 min text, $3 exit state if $showminimal -eq "true";then echo $2 else echo $1 fi exit $3 } showminimal="false" while getopts “hn::u::m” OPTION do case $OPTION in h) usage exit 1 ;; n) srname="$OPTARG" ;; u) sruuid=${OPTARG} ;; m) showminimal="true" ;; ?) usage exit ;; esac done if [[ -z "$srname" ]] && [[ -z "$sruuid" ]];then usage exit 1 fi #check volume exists if [ -z "$srname" ];then mysruuid=`xe sr-list uuid=$sruuid --minimal` else mysruuid="$(u=`xe sr-list name-label=$srname --minimal`;xe sr-list uuid=$u --minimal)" fi if [ -z "$mysruuid" ];then reportstate "Unknown SR information given: $srname $sruuid" "" 1 fi myvdilist=`xe vdi-list sr-uuid=$mysruuid --minimal` if [ -z "$myvdilist" ];then reportstate "No VDI instances found. No VMs deployed with storage on: $srname $sruuid" "empty" 0 else for vdiuuid in ${myvdilist//,/ } do #Suspended VMs create a suspended file which does not #create a vmuuid myvmuuid=`xe vbd-list vdi-uuid=$vdiuuid params=vm-uuid --minimal` if [ ! -z "$myvmuuid" ]; then myvmstate="$(myvmuuid=`xe vm-param-get uuid=$myvmuuid param-name=uuid`;myvmname=`xe vm-param-get uuid=$myvmuuid param-name=name-label`;myvmpwrstate=`xe vm-param-get uuid=$myvmuuid param-name=power-state`;echo $myvmuuid,${myvmname},$myvmpwrstate)" if [ "$showminimal" == "false" ]; then echo $myvmstate else myvminfo=(${myvmstate//,/ }) if [ "${myvminfo[2]}" == "running" ] || [ "${myvminfo[2]}" == "suspended" ]; then echo $isactive exit 0 fi fi fi done if [ "$showminimal" == "true" ]; then reportstate "All VMs in halted state on: $srname $sruuid" $isinactive 0 fi fi

====================================================

 Download GetSRVMRunningState.zip

Print