OpenStack: Live migrate VM to another OpenStack hypervisor

show migrate state / log

nova migration-list --instance-uuid  ${SERVER_ID}
 
# show multiple migrations by date
nova migration-list | grep 2020-02-20 | awk -F"|" '{ print $8}' | sort | uniq -dc

CLI

VM=foo-u1804
 
# list all vms on a hypervisor
openstack server list --all --status ACTIVE --host com1.example.com
 
# get current hypervisor
openstack server show ${VM} -c OS-EXT-SRV-ATTR:host -f value
 
# list avaiable hypervisors
openstack host list -c "Host Name" -c Service -f value | grep compute | cut -d" " -f1
 
# live migrate VM to specific compute node
# deprected: openstack server migrate ${VM} --live ${TARGET_COMPUTE_NODE} --wait
openstack server migrate --live-migration --wait ${SERVER_ID}
 
# live migrate vm to specific host
openstack server migrate --os-compute-api-version 2.30 --live-migration --wait --host ${COMPUTE_NODE_ID} ${SERVER_ID}
 
# get state
openstack server show ${VM} -c name -c OS-EXT-SRV-ATTR:host -c status -f value | paste - - -
 
# migrate all VMs
openstack server list --all --host ${COMPUTE_NODE}
 
# migrae VMs
nova host-evacuate-live ${COMPUTE_NODE} [--max-servers 10]
 
# show remaining VMs
VMS=999
while [ ${VMS} -gt 0 ]; do
    echo ${VMS} VMs remaining on ${OS_NODE}
    VMS=$(openstack server list --all --status MIGRATING --host ${OS_NODE} -f value | wc -l)
    sleep 15
done
 
# migrae VMs
for VMID in $(openstack server list --all --status ACTIVE --host ${OS_NODE} -c ID -f value); do
    # start live migration
    date
    openstack server show ${VMID} -c id -c name -c OS-EXT-SRV-ATTR:host -c status -f value | paste - - - -
    nova live-migration ${VMID}
 
    # show migration status
    openstack server show ${VMID} -c id -c name -c OS-EXT-SRV-ATTR:host -c status -f value | paste - - - -
 
    # wait until migration has been completed
    while [ "$(openstack server show ${VMID} -c status -f value)" == "MIGRATING" ]; do sleep 1; done
 
    # show migration status
    openstack server show ${VMID} -c id -c name -c OS-EXT-SRV-ATTR:host -c status -f value | paste - - - -
    date
 
    echo
    sleep 10
done
 
 
# define VMs
VMIDS="
3fa95ff3-49b4-0000-1111-a0633720000
3fa95ff3-49b4-0000-1111-a0633720111
"
 
# all active VMs on specific host
VMIDS=$(openstack server list --all --status=ACTIVE -c ID -f value --host compute1-dev)
 
# live migrate VMs
for VMID in ${VMIDS}; do
    echo ${VMID}
    openstack server migrate ${VMID} --live compute2-prod --wait
    echo
done
 
# test
nova live-migration VM_ID
# nova host-evacuate-live
# --live-migration --wait
# nova server-migration-list

Cold migrate
https://docs.openstack.org/nova/rocky/admin/migration.html

openstack server list --all --status SHUTOFF --host ${COMPUTE_NODE}
 
# cold migrage VM to specific hypervisor
openstack server migrate --os-compute-api-version 2.56 --host com2-prod ${SERVER_ID}
 
# single VM
openstack server migrate ${SERVER_ID} 
openstack server resize confirm ${SERVER_ID}
 
# multiple VMs
VMIDS="
111111-22222-3333-0000-111111111111
111111-22222-3333-0000-222222222222
"
 
SERVER_IDS=$(openstack server list --all --status=SHUTOFF -c ID -f value --host compute1-dev)
for SERVER_ID in ${SERVER_IDS}; do
    openstack server migrate --wait ${SERVER_ID}
    openstack server resize confirm ${SERVER_ID}
done
 
openstack server list --all --status=VERIFY_RESIZE

qemu / libvirt

# fix DestinationDiskExists_Remote issue
docker exec -t -u root nova_libvirt ls -l  /var/lib/nova/instances/${SERVER_ID}

Show migrations

SERVER_IDS=$(openstack server list --all-projects -c ID -f value --name <VM_NAME(s)>)
for SERVER_ID in ${SERVER_IDS}; do
    nova migration-list --instance-uuid ${SERVER_ID}
done

Migration loop

while :
do
	openstack server migrate ${SERVER_ID} --live-migration --wait
	sleep $(( ( RANDOM % 1000 )  + 1 ))
done

Links
https://docs.openstack.org/nova/latest/admin/live-migration-usage.html
http://www.danplanet.com/blog/2016/03/03/evacuate-in-nova-one-command-to-confuse-us-all/