From b5d3583d4c1f8b2b1d64f0570a6c8113473dd458 Mon Sep 17 00:00:00 2001 From: Fabian Arrotin Date: Apr 03 2025 09:57:22 +0000 Subject: Fixing kvm migration snippet with new variables Signed-off-by: Fabian Arrotin --- diff --git a/docs/tips/virtual_machines.md b/docs/tips/virtual_machines.md index 03eec1e..178a4de 100644 --- a/docs/tips/virtual_machines.md +++ b/docs/tips/virtual_machines.md @@ -20,7 +20,7 @@ We can then define some variables like : ``` # Defining some variables first -VM="kvm-guest1.dev.centos.org" +kvm_guest="kvm-guest1.dev.centos.org" hypervisor="kvm-hypervisor1.dev.centos.org" bastion_host="bastion.dev.centos.org" @@ -29,17 +29,19 @@ bastion_host="bastion.dev.centos.org" ip_addr=$(curl --silent -4 http://icanhazip.com) ssh -J ${bastion_host} ${hypervisor} "sudo iptables -I INPUT -p tcp --dport 22 --source ${ip_addr} -j ACCEPT" -# Ensuring our current user can read .qcow2 files on initial hypervisor -ssh $hypervisor "sudo setfacl -m u:$USER:rwx /var/lib/libvirt/images ; sudo virsh shutdown $VM ; sleep 15 ;sudo chown $USER /var/lib/libvirt/images/${VM}.qcow2" +# Shutting down machine and waiting until it's powered off +ssh $hypervisor "sudo setfacl -m u:$USER:rwx /var/lib/libvirt/images ; sudo virsh autostart --disable ${kvm_guest} ; sudo virsh shutdown ${kvm_guest} ; while true ; do echo "Waiting for [${kvm_guest}] VM to be powered off " ; sleep 2 ; sudo virsh list --name |grep -q ${kvm_guest} || break ; done" # Ensuring now that we can also write to local/target hypervisor and starting to rsync -S (sparsify) the VM +# Ensuring now that we can also write to local/target hypervisor and starting to rsync -S (sparsify) the VM sudo setfacl -m u:${USER}:rwx /var/lib/libvirt/images -disks=$(ssh $hypervisor "sudo virsh dumpxml ${VM}|grep '/var/lib/libvirt/images'|cut -f 2 -d '='|tr -d \"'\" | sed 's/index//g' ") -for disk in ${disks} ; do +disks=$(ssh $hypervisor "sudo virsh dumpxml ${kvm_guest}|grep '/var/lib/libvirt/images'|cut -f 2 -d '='|tr -d \"'\" | sed 's/index//g'|sed 's#.qcow2/>#.qcow2#g' ") +for disk in $disks ; do echo "Pulling disk ${disk}" - rsync -avS -4 --progress ${hypervisor}:${disk} /var/lib/libvirt/images/${disk} + ssh ${hypervisor} "sudo chown $USER ${disk}" + rsync -avS -4 --progress ${hypervisor}:${disk} ${disk} done -ssh $hypervisor "sudo virsh dumpxml $VM" > /var/lib/libvirt/images/${VM}.xml +ssh $hypervisor "sudo virsh dumpxml ${kvm_guest}" > /var/lib/libvirt/images/${kvm_guest}.xml ``` @@ -50,10 +52,10 @@ Once you have edited the .xml file to (eventually) match some settings that need ``` # then define/starts the VM -sudo virsh define /var/lib/libvirt/images/${VM}.xml -sudo virsh start ${VM} -sudo virsh console ${VM} -sudo virsh autostart --domain $VM +sudo virsh define /var/lib/libvirt/images/${kvm_guest}.xml +sudo virsh autostart --domain ${kvm_guest} +sudo virsh start ${kvm_guest} +sudo virsh console ${kvm_guest} ```