XenServer 6 Auto-Start VMs

Written on Saturday 7 January 2012 by Frank Vandebergh in category: Citrix

In XenServer 6, Citrix made the decision to not include the option anymore to auto-start VM's when the XenServer host boots up. The reason is that this feature interfered with the HA and Failover capabilities, but nonetheless it was a nice feature for situations where only one XenServer host is used.

In XenServer 6 it is possible to auto-start VM's but it requires enabling the auto-start feature at the pool level and on each individual VM by using the CLI which is not very user friendly. Therefore, we have created a script that runs during startup of the host and will start any VM that has been tagged with the "autostart" tag. This allows for easy administration using XenCenter.

In order to install the script,  copy the contents below to the end of the /etc/rc.d/rc.local file on the XenServer host, using for example WinSCP.  Afterwards just tag the VM's you would want to auto-start using the tag "autostart". The VM's will be started with a 20 second delay between each VM.

 

# AutoStart VM's that are tagged with autostart tag
# Script created by Raido Consultants - http://www.raido.be
TAG="autostart"
# helper function
function xe_param()
{
PARAM=$1
while read DATA; do
LINE=$(echo $DATA | egrep "$PARAM")
if [ $? -eq 0 ]; then
echo "$LINE" | awk 'BEGIN{FS=": "}{print $2}'
fi
done
}
# Get all VMs
sleep 20
VMS=$(xe vm-list is-control-domain=false | xe_param uuid)
for VM in $VMS; do
echo "Raido AutoStart Script : Checking VM $VM"
VM_TAGS="$(xe vm-param-get uuid=$VM param-name=tags)"
if [[ $VM_TAGS == *$TAG* ]]
then
echo "starting VM $VM"
sleep 20
xe vm-start uuid=$VM
fi
done



Update: This script will do the same with vApps. If "autostart" is in the description of a vApp it will start the vApp during boot. Within a vApp you can specify multiple VM's and their boot order.
# AutoStart Vapp's that have autostart in description
# Script created by Raido Consultants - http://www.raido.be 
TAG="autostart"
# helper function
function xe_param()
{
PARAM=$1
while read DATA; do
LINE=$(echo $DATA | egrep "$PARAM")
if [ $? -eq 0 ]; then
echo "$LINE" | awk 'BEGIN{FS=": "}{print $2}'
fi
done
}
# Get all Applicances
sleep 20
VAPPS=$(xe appliance-list | xe_param uuid)
for VAPP in $VAPPS; do
echo "Raido AutoStart : Checking vApp $VAPP"
VAPP_TAGS="$(xe appliance-param-get uuid=$VAPP param-name=name-description)"
if [[ $VAPP_TAGS == *$TAG* ]]
then
echo "starting vApp $VAPP"
xe appliance-start uuid=$VAPP
sleep 20
fi
done

 

 

Comments

Albert

Albert wrote 1 month ago:

Hi,

I want use your script but in the 15 line is some error, when I try run this script.

/opt/autostart.sh: line 15: syntax error near unexpected token `do'

/opt/autostart.sh: line 15: `VMS=$(xe vm-list is-control-domain=false | xe_param uuid) for VM in $VMS; do'

Albert

Albert wrote 1 month ago:

Ok, I founded where is the problem, you forgot put ;

VMS=$(xe vm-list is-control-domain=false | xe_param uuid);<--- for VM in $VMS; do

echo "Raido AutoStart Script : Checking VM $VM"

VM_TAGS="$(xe vm-param-get uuid=$VM param-name=tags)";<--- if [[ $VM_TAGS == *$TAG* ]];<---

then

Frank Vandebergh

Frank Vandebergh wrote 1 month ago:

Hi albert, you are right. Some line breaks are lost when posting it in this blog. the "Do" should start at a new line.

JayKudo

JayKudo wrote 1 month ago:

Doesnt the command '

xe vm-start tags=autostart --multiple

Do the same? Just put that in your rc.local

Nik Stapley wrote 1 month ago:

I can't believe that Citrix have removed such a fundamentally required feature! If anything, I've been hoping since the release of XenServer that Citrix would offer the same auto-start features which ESX have been offering since very early versions; i.e. Autostart these VMs in this order, and these in any order and with staggered pauses as the administrator dictates. Instead Citrix take the auto-start feature away all together, seriously what is going on? If HA really has an issue with it (which it should not) then offer the administrator the option as to whether they enable such a feature or not.

Comment