Step-by-Step Guide: Automatically start a vApp on XenServer

Two years ago I wrote a quick blogpost on how to configure XenServer to automatically start a vApp. I noticed from the site statistics this post is still very popular. So I’ve decided to refresh the blogpost and ensure the script is up-to-date and a more flexible solution is provided. With the included script in this blogpost you are able to automatically start one or more vApps when a XenServer is booted by simple adding the description “autostart” to the vApp. This way you will not need to find the vApp GUID and add it manually to the rc.local bootfile on your XenServer hosts.

Due to an error in the script I have been using the manual solution of adding the xe command to start a vApp by GUID to the rc.local file. As I am running different server sets on my homelab, this was not a very flexible solution. So I finally got around to re-testing the script and fixing the errors. So here’s my tested solution that gives you more flexibility to automatically start one or more vApps when the XenServer boots.

To ensure the script is implemented in the correct way, I’ve written a step-by-step guide to configure a XenServer to automatically start a vApp after it is booted.

Configuring the vApp with the XenCenter GUI

The easiest way to configure a vApp is through XenCenter as it provides you with a GUI to quickly add and configure the VMs, boot order and start delays.

Screens Actions
  Start XenCenter and connect to the XenServer pool.
From the menu bar select Pool – Manage vApps…
Manage vApps Click on [New vApp…]
Manage vApps Enter a Name and Description and click on [Next]

Configure a distinctive description (like “autostart”) as it will be used by the script mentioned below to select the required vApp to automatically start the VMs when the XenServer is booted.

Manage vApps Select the VMs you want to add to the vApp and click on [Next]
Manage vApps Configure the Start order and Delay interval for each VM and click on [Next]
Manage vApps Review the settings and click on [Finish] to create the vApp
Manage vApps The vApp is created and will be automatically started when the startup script is added to the server.

 

Configuring the vApp with the XE CLI

You can use the following xe commands to create and configure a vApp in XenServer by using the command line interface:

# Create a vApp (and returns the UUID)
xe appliance-create name-label=test name-description=autostart

This function will return the uuid of the vApp ([vapp-uuid]) that is used by the other functions later on.

 

# Edit the description of an existing vApp
xe appliance-param-set uuid=[vapp-uuid] name-description="autostart"

Which uses the following syntax:

  • vapp-uuid: unique identifier for the specified vApp.

 

# Get the uuid of an existing vApp
xe appliance-list name-label="[vapp-name]"

Which uses the following syntax:

  • vapp-name: name of the specified vApp.

 

# Get the uuid of an existing VM
xe vm-list name-label=[vm-name]

Which uses the following syntax:

  • vm-name: name of the specified VM.

 

# Add a VM to the vApp and set the Start order and Start delay settings for that VM
xe vm-param-set uuid=[vm-uuid] appliance=[vapp-uuid] start-delay=25 order=1

Which uses the following syntax:

  • vm-uuid: unique identifier for the specified VM.
  • vapp-uuid: unique identifier for the specified vApp.

 

# Remove a VM from the vApp and reset the start order and delay settings
xe vm-param-set uuid=[vm-uuid] appliance="" start-delay=0 order=0

Which uses the following syntax:

  • vm-uuid: unique identifier for the specified VM.

 

# Remove the vApp
xe appliance-destroy uuid=[vapp-uuid]

Which uses the following syntax:

  • vapp-uuid: unique identifier for the specified vApp.

 

Edit files and scripts with WinSCP on the XenServer

In order to automatically start VMs on a XenServer, you’ll need to configure XenServer to run a script when it is booted. XenServer is based upon CentOS and runs the commands from rc.local (located at /etc/rc.d/rc.local) when it is booted. As I want XenServer to boot all vApps that have the autostart description, I’ll be using a script to check all configured vApps for the right description.

 

You can use a tool like WinSCP to remotely connect to a XenServer from a Windows client and edit or upload files.

 

I use winSCP to connect to the XenServer and easily create a new bash script file in the opt directory.
The following script will scan for vApps with the description “autostart” and automatically start them:

# AutoStart XenServer vApps with the tag autostart in their description
# Script originally created by Raido Consultants - http://www.raido.be
# Script updated and shared by E.Y. Barthel - https://www.virtues.it
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
	# debug info
	# echo "Esther's AutoStart : Checking vApp $VAPP"
	VAPP_TAGS="$(xe appliance-param-get uuid=$VAPP param-name=name-description)" 
	if [[ $VAPP_TAGS == *$TAG* ]]; then
		# action info:
		echo "starting vApp $VAPP";
		xe appliance-start uuid=$VAPP;
		sleep 20
	fi
done

 

By using this script, you are more flexible in configuring the auto-start option for you vApp.
You no longer need to keep track of the uuid for you vApp, but can easily turn the auto-start option on or off by adding or removing the custom “autostart” description to you vApp (or whatever description you want to set for the TAG variable. The vApp description is used, as vApps do not offer advanced attributes, like the VM tag attribute.

 

When the above script is created or uploaded to the opt directory on the XenServer, you’ll need to ensure execution rights are set for the script. If the rights are not set, the script cannot be run from the rc.local file and no actions will be started when a XenServer is booted.
You can use the following command to set the execution rights for the script:

chmod +x \opt\autostartvapps.sh

 

This command assumes the file is called autostartvapps.sh and is placed in the opt directory (located at /opt/autostartvapps.sh)

 

You can test the script, by running it manually with the following command

sh /opt/autostartvapps.sh

 

When the script is run without errors, you have one final step to perform. To automatically run the script when the XenServer is booted, ensure it is called from the rc.local file (located at /etc/rc.d/rc.local), by adding the following lines:

sleep 40
sh /opt/autostartvapps.sh

 

The sleep command is added to the rc.local file to ensure all processes are started when the XenServer is booted and the script can be executed. As I am using a whitebox XenServer in my Homelab, the 40 seconds delay (or sleep) is based upon a fair amount of boot time for the server. Make sure you test the minimal required time for your own environment when you are adjusting the sleep value in the rc.local file to meet your own needs.

 

 

Esther Barthel
Solutions Architect at cognition IT

Esther has been working in different roles and functions as an IT consultant ever since she finished her Masters degree in Computer Science in 1997. She has worked as a web developer, database administrator, and server administrator until she discovered how Server-Based Computing ( SBC ) combined servers, desktops, and user experience in one solution. Esther has been specializing in virtualization solutions such as SBC, VDI, application, and server virtualization for over eight years now and is currently working as a Senior Consultant at PepperByte, where she designs and implements Citrix® solutions for both small-business and large-enterprise infrastructures scaling from 100 to 15,000 users.
In january 2014 her first book Citrix XenApp 6.5 Expert Cookbook was published by Packt Publishing.

Esther is awarded as a Citrix Technology Professional (CTP) from 2015 - 2017.
Esther is awarded as a Microsoft Most Valuable Professional (MVP) in 2017.

Esther is a Citrix Certified Expert – Virtualization (CCE-V), Citrix Certified Professional – Mobility (CCP-M), Citrix Certified Professional – Networking (CCP-N) and RES Software Certified Professional (RCP).