XenServer VM Management via Command Line
This guide provides useful commands for managing XenServer VMs from the command line, which is particularly helpful for batch operations and automation.
Listing VMs
To list specific VMs (filtering for “na” in the name but excluding those with “Xen”):
1
| xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n
|
Taking Snapshots
Create snapshots of multiple VMs in a single operation:
1
2
3
4
5
6
| # For loop with proper formatting
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
|
Starting VMs
Start multiple VMs in sequence:
1
2
3
4
5
6
| # For loop with proper formatting
for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`
do
echo "Starting $i"
xe vm-start vm=$i
done
|
Stopping VMs
Shut down multiple VMs in sequence:
1
2
3
4
5
6
| # For loop with proper formatting
for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`
do
echo "Shutting down $i"
xe vm-shutdown vm=$i
done
|
Combined Operations: Shutdown, Snapshot, Start
Execute a complete workflow to shut down VMs, take snapshots, and restart them:
One-line Version (Not Recommended for Readability)
1
| 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
|
Shell Script Version (Recommended)
1
2
3
4
5
6
7
8
9
| #!/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
|
Importing VMs
Import multiple VM backup files to a storage repository:
1
2
3
4
5
| # For loop with proper formatting
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
|
Exporting VMs
Export multiple VMs to backup files in the current directory:
1
2
3
4
5
6
| # For loop with proper formatting
for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`
do
echo "Exporting $i"
xe vm-export filename=$i.bkp vm=$i
done
|
Additional Useful Commands
Get VM Details
1
2
3
4
5
| # Get detailed information about a specific VM
xe vm-param-list uuid=<vm-uuid>
# List all parameters for a VM
xe vm-param-list uuid=<vm-uuid> | less
|
VM Power State Management
1
2
3
4
5
6
7
8
| # Force shutdown a VM that won't respond
xe vm-shutdown vm=<vm-name-or-uuid> --force
# Reboot a VM
xe vm-reboot vm=<vm-name-or-uuid>
# Suspend a VM
xe vm-suspend vm=<vm-name-or-uuid>
|
VM Memory Management
1
2
| # Set VM memory (in bytes)
xe vm-memory-limits-set vm=<vm-name-or-uuid> static-min=1GiB static-max=4GiB dynamic-min=1GiB dynamic-max=4GiB
|
VM Network Management
1
2
3
4
5
| # List VM network interfaces
xe vm-vif-list vm=<vm-name-or-uuid>
# Create a new network interface for a VM
xe vif-create vm-uuid=<vm-uuid> network-uuid=<network-uuid> mac=random device=0
|