Skip to content

Instantly share code, notes, and snippets.

@metalrufflez
Created November 21, 2011 14:05
Show Gist options
  • Save metalrufflez/1382703 to your computer and use it in GitHub Desktop.
Save metalrufflez/1382703 to your computer and use it in GitHub Desktop.
Migrate Xen 5.* VMs using an external disk on the newhost
#!/bin/bash
# Uncomment the lines below to print the commands instead of executing them
# DEBUG refers to exporting from external dom0
# DEBUG2 refers to importing the vm to local dom0
#DEBUG="echo"
#DEBUG2="echo"
# You HAVE to inform at lease vm and host or else, usage in your face
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <vm-name> <host> [new_host]"
exit 1
fi
# User readable variables from parameters
VM=$1
HOST=$2
NEWHOST=$3
# Edit here your external USB Device and USB Mount point
USBDEV="/dev/sda1"
USBDIR="/mnt/usb"
# Edit here your user and password for the external Xen Host
USER="user"
PWD="password"
# Edit your Storage Repository Name and Network Name you wish to import you VM to
SRNAME="storage"
NETNAME="network"
# Here it'll use the above vars to get the UUIDs the commands require
SRUUID=$(xe sr-list name-label="$SRNAME" --minimal)
NETUUID=$(xe network-list name-label="$NETNAME" --minimal)
# In case of failure of any "important" command, restart the vm on the original dom0
failure() {
echo "Restarting $VM on $HOST"
$DEBUG xe -h $HOST -u $USER -pw $PWD vm-start vm=$VM
exit 1
}
# This does all the magic, export from external dom0 and import to local one
exportimport() {
echo "Exporting $VM"
$DEBUG xe vm-export -h $HOST -u $USER -pw $PWD vm=$VM filename=$USBDIR/$VM.xva || failure
echo "Importing $VM"
$DEBUG2 xe vm-import sr-uuid=$SRUUID filename=$USBDIR/$VM.xva || failure
}
# Check if the dom0 provided really exists
if ! xe vm-list -h $HOST -u $USER -pw $PWD > /dev/null 2>&1; then
echo "There is no Xen host named $HOST"
exit 1
fi
# Check if there is already a vm with the name of the vm you're trying to import
if xe vm-list | grep -q $VM; then
echo "There is already a $VM in this pool"
exit 1
fi
# Check if there is the VM you informed in the external dom0
if ! xe -h $HOST -u $USER -pw $PWD vm-list | grep $VM > /dev/null 2>&1; then
echo "There is no $VM in $HOST"
exit 1
fi
# This if is just useful if you use a third parameter when calling the script
# You also need to edit the case parameters and the name-label to use this script in other dom0
if [ ! -z "$NEWHOST" ]; then
case $NEWHOST in
host01) NEWHOSTID=$(xe host-list name-label=host01 --minimal);;
host02) NEWHOSTID=$(xe host-list name-label=host02 --minimal);;
*) echo "Please choose a valid host to start $VM"
exit 1;;
esac
NEWHOSTNAME=$(xe host-list uuid=$NEWHOSTID params=name-label --minimal)
fi
# Mounts the USB external device if not already mounted
if ! mount | grep -q usb; then
echo "Mounting external disk"
mount $USBDEV $USBDIR || exit 1
fi
# Shuts down the VM on the remote host
echo "Shutting Down $VM"
if ! xe -h $HOST -u $USER -pw $PWD vm-list name-label=$VM | grep -q halted; then
$DEBUG xe -h $HOST -u $USER -pw $PWD vm-shutdown vm=$VM || exit 1
fi
# Checks if there's already a xva with the provided name in the USB device
# If exists, import it without exporting
# If it returns some error, delete the existing xva and do the export/import
# If it doesn't exists, do the default export/import
if [ -a $USBDIR/$VM.xva ]; then
echo "There's already a $USBDIR/$VM.xva in this server, will try to import it"
$DEBUG2 xe vm-import sr-uuid=$SRUUID filename=$USBDIR/$VM.xva
if [ "$?" -ne "0" ]; then
echo "Error importing $VM, will try to export and import again"
$DEBUG2 rm -f $USBDIR/$VM.xva
exportimport
fi
else
exportimport
fi
# Gets the UUID of the old vif, mac address and the new vm uuid to fix the network before trying to start it
OLDVIF="$(xe vm-vif-list name-label=$VM --minimal)"
VMUUID="$(xe vm-list name-label=$VM --minimal)"
MAC="$(xe -h $HOST -u $USER -pw $PWD vm-vif-list name-label=$VM params=MAC --minimal)"
# Delete the existing and non-working vif
echo "Deleting old VIF"
$DEBUG2 xe vif-destroy uuid=$OLDVIF || failure
# Create a new vif in the network configured before and with the old mac (so you dont need to edit the ifcfg)
echo "Create new VIF"
$DEBUG2 xe vif-create vm-uuid=$VMUUID device=0 network-uuid=$NETUUID mac=$MAC || failure
# Start VM in the informed host. If not informed will do a round robin
if [ -z $NEWHOST ]; then
echo "Starting $VM"
$DEBUG2 xe vm-start vm=$VM || failure
else
echo "Starting $VM on $NEWHOSTNAME"
$DEBUG2 xe vm-start vm=$VM on=$NEWHOSTID || failure
fi
# Cleanup and remove the exported xva
echo "Removing XVA"
$DEBUG2 rm -f $USBDIR/$VM.xva
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment