Thicket data repository for the EEG
at main 4.1 kB view raw
1{ 2 "id": "https://www.tunbury.org/2025/06/04/libvirt-moves", 3 "title": "Moving libvirt machines", 4 "link": "https://www.tunbury.org/2025/06/04/libvirt-moves/", 5 "updated": "2025-06-04T00:00:00", 6 "published": "2025-06-04T00:00:00", 7 "summary": "I need to migrate some libvirt/qemu machines from one host to another. These workloads can easily be stopped for a few minutes while the move happens.", 8 "content": "<p>I need to migrate some libvirt/qemu machines from one host to another. These workloads can easily be stopped for a few minutes while the move happens.</p>\n\n<p>1. Identify the name of the VMs which are going to be moved. If the machines have already been shutdown, then adding <code>--all</code> will list them.</p>\n\n<div><div><pre><code><span># virsh list</span>\n</code></pre></div></div>\n\n<p>2. Shutdown the machine either by connecting to it and issuing a <code>poweroff</code> command or, by using sending the shutdown request via <code>virsh</code>. You can verify that it is powered off with <code>virsh domstate vm_name</code>.</p>\n\n<div><div><pre><code><span># virsh shutdown vm_name</span>\n</code></pre></div></div>\n\n<p>3. Export the configuration of the machine.</p>\n\n<div><div><pre><code><span># virsh dumpxml vm_name &gt; vm_name.xml</span>\n</code></pre></div></div>\n\n<p>4. List the block devices attached to the machine.</p>\n\n<div><div><pre><code><span># virsh domblklist vm_name</span>\n</code></pre></div></div>\n\n<p>Then for each block device check for any backing files using <code>qemu-img</code>. Backing files are caused by snapshots or building mulitple machines from a single master images.</p>\n\n<div><div><pre><code>qemu-img info image.qcow2\n</code></pre></div></div>\n\n<p>5. Transfer the files to be new machine. This could be done via <code>scp</code> but in my case I’m going to use <code>nc</code>. On the target machine I’ll run this (using literally port 5678).</p>\n\n<div><div><pre><code><span># nc -l 5678 | tar -xvf -</span>\n</code></pre></div></div>\n\n<p>And on the source machine, I’ll send the files to the target machine at IP 1.2.3.4 (replace with the actual IP) and using port 5678 (literally).</p>\n\n<div><div><pre><code><span># tar -xf - *.qcow2 *.xml | nc 1.2.3.4 5678</span>\n</code></pre></div></div>\n\n<p>6. On the target machine, the VM now needs to be <em>defined</em>. This is done by importing the XML file exported from the original machine. To keep things simple, my disk images are in the same paths on the source and target machines. If not, edit the XML file before the import to reflect the new disk locations.</p>\n\n<div><div><pre><code><span># virsh define vm_name.xml</span>\n</code></pre></div></div>\n\n<p>7. Start the VM.</p>\n\n<div><div><pre><code><span># virsh start vm_name</span>\n</code></pre></div></div>\n\n<p>8. Delete the source VM. On the <em>source</em> machine, run this command.</p>\n\n<div><div><pre><code><span># virsh undefine vm_name --remove-all-storage</span>\n</code></pre></div></div>\n\n<p>9. Open a remote console</p>\n\n<p>If things have gone wrong, it may be necessary to look at the console of the machine. If you are remote from both host machines this can be achieve using an <code>ssh</code> tunnel.</p>\n\n<p>Determine the VNC port number being used by your VM.</p>\n\n<div><div><pre><code><span># virsh vncdisplay vm_name</span>\n127.0.0.1:8\n</code></pre></div></div>\n\n<p>In the above output, <code>:8</code> tells us that the VNC port number is <code>5908</code>. Create the SSH tunnel like this:</p>\n\n<div><div><pre><code><span># ssh -L 5908:127.0.0.1:5908 fqdn.remote.host</span>\n</code></pre></div></div>\n\n<p>Once the <code>ssh</code> connection is established, open your favourite VNC viewer on your machine and connect to <code>127.0.0.5908</code>.</p>", 9 "content_type": "html", 10 "author": { 11 "name": "Mark Elvers", 12 "email": "mark.elvers@tunbury.org", 13 "uri": null 14 }, 15 "categories": [ 16 "libvirt,qemu", 17 "tunbury.org" 18 ], 19 "source": "https://www.tunbury.org/atom.xml" 20}