Post

Some basic XenServer VM management from the CLI / command line

List some VMs we care about:

xe vm-list | grep na | grep -v Xen | awk ‘{print $4}’ | sort -n

Take snapshot of the VMs

for i in xe vm-list | grep na | grep -v Xen | awk ‘{print $4}’ | sort -n; do echo “Snapshotting $i”; xe vm-snapshot new-name-label=”BASE vGPU IMAGE” vm=$i; done

Start the VMs

for i in xe vm-list | grep na | grep -v Xen | awk ‘{print $4}’ | sort -n; do echo $i; xe vm-start vm=$i; done

Stop the VMs

for i in xe vm-list | grep na | grep -v Xen | awk ‘{print $4}’ | sort -n; do echo $i; xe vm-shutdown vm=$i; done

Shutdown – Take snapshot – Start sequence:

On one line:
for i in xe vm-list | grep na | grep -v Xen | awk ‘{print $4}’ | sort -n; do xe vm-shutdown vm=$i; echo “Snapshotting $i”; xe vm-snapshot new-name-label=”BASE vGPU IMAGE” vm=$i; xe vm-start vm=$i; done

For use in a short shell script (prettier formatting):
#!/bin/sh

for i in xe vm-list | grep na | grep -v Xen | awk ‘{print $4}’ | sort -n
do
xe vm-shutdown vm=$i
echo “Snapshotting $i”
xe vm-snapshot new-name-label=”BASE vGPU IMAGE” vm=$i
xe vm-start vm=$i
done

Import VMs from directory:

for i in *.bkp; do xe vm-import filename=$i sr-uuid=pvscan | grep Local | awk ‘{print $4}’ | sed ‘s/-/ /’ | awk ‘{print $2}’ preserve=true; done

Export VMs to current directory:

for i in xe vm-list | grep na | grep -v Xen | awk ‘{print $4}’ | sort -n;do echo $i; xe vm-export filename=$i.bkp vm=$i; done

This post is licensed under CC BY 4.0 by the author.