Skip to content

Instantly share code, notes, and snippets.

@d6e
Last active December 17, 2015 21:09
Show Gist options
  • Save d6e/5672324 to your computer and use it in GitHub Desktop.
Save d6e/5672324 to your computer and use it in GitHub Desktop.
For migrating vm's from one hypervisor to the next.
#!/bin/bash
if test "$1" == "" ; then
echo "### Usage ###"
echo "migrate.sh <domain> <user@remotehost>"
exit
fi
if [[ $(virsh list | egrep -o $1) != "" ]]
then
echo "The domain is currently running, attempting to shutdown"
virsh shutdown $1
sleep 10
# If domain doesn't shutdown, force it
if [[ $(virsh list | egrep -o $1) != "" ]]
then
echo "The domain is being stubborn so I'm killing it"
virsh destroy $1
fi
fi
rhost=$(echo $2 | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
rsync -az --sparse --progress /var/lib/libvirt/images/$1.img $2:/var/lib/libvirt/images/
virsh dumpxml $1 > $1.xml
virsh -c qemu+ssh://$rhost/system define $1.xml
rm $1.xml
echo "$1 has been migrated to $rhost"
echo "starting domain on remote host... "
virsh -c qemu+ssh://$rhost/system start $1
echo "removing old domain..."
virsh undefine $1
rm /var/lib/libvirt/images/$1.img
echo "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment