1

I have found a few related questions/articles but nothing about just simply moving an existing VM to a new directory on the same machine. That's what this question entails.

For this scenario, I need to move an existing virtual machine storage from /var/lib/libvirt/images/ to /var/lib/libvirt/new-dir/

Here is my theorized work plan:

  1. virsh shutdown my-vm-name

  2. rsync -a /var/lib/libvirt/images/my-vm-name /var/lib/libvirt/new-dir/

  3. virsh edit my-vm-name

    (within edit window)--> :s/\/var\/lib\/libvirt\/images/\/var\/lib\/libvirt\/new-dir/g
    
  4. virsh start my-vm-name

Will this work?

Is there a cleaner/better way to do it?

Rino Bino
  • 511
  • 5
  • 21

1 Answers1

1

In short, yes, the proposed work plan is correct. You can stop the VM, copy the files, edit the XML to point to the new location, and it will work.


However, please take into consideration that the VM may be using a storage pool, and by best practice you can use pools to hold/manage VM storage:

  1. Most probably you are using the default pool which will point to /var/lib/libvirt/images. You can verify this using virsh pool-list. So creating a new dir won't do anything as no storage pool is configured to point to the new dir yet.

  2. See how the default pool looks like using virsh vol-list default.

  3. Rather create a new pool to keep things clean.

Nou what you need to do is:

Create the pool

virsh pool-define-as new-dir dir - - - - "/var/lib/libvirt/new-dir"

Create the directory

mkdir -p "/var/lib/libvirt/new-dir"

Make sure the permissions are set correctly.

chown qemu:qemu "/var/lib/libvirt/new-dir"

If you run on RHEL based systems you need to run restorecon for the SELinux relabling

restorecon -vvRF /var/lib/libvirt/new-dir

Now let's build the pool

virsh pool-build new-dir

Start the pool

virsh pool-start new-dir

If you want the pool to autostart on the next reboot you'd need to run

virsh pool-autostart new-dir

Finally run

virsh pool-list && virsh pool-info new-dir

Migrate VM to the new pool

Copy the images to the new dir

cp -a /var/lib/libvirt/images/my-vm-name /var/lib/libvirt/new-dir/

Now dump my-vm-name definition to xml

virsh dumpxml my-vm-name > new-vm-name

Change the location (as you already mentioned in your question) to the new location

sed -i 's/images/new-dir/g;s/my-vm-name/new-vm-name/' new-vm-name

.. and finally define the new VM and start it

virsh define new-vm-name && virsh start new-vm-name
Rino Bino
  • 511
  • 5
  • 21
Valentin Bajrami
  • 4,045
  • 1
  • 18
  • 26
  • 1
    Thanks for the answer and extended details, however not using pools. The work plan in the original question worked fine. Essentially just stop, copy, edit, start is all that it needed and everything worked flawlessly. If you want to make a note of that within your answer I can accept this. – Rino Bino Jul 19 '22 at 21:07
  • You are welcome. Please feel free to edit the answer and add your own findings. – Valentin Bajrami Jul 20 '22 at 07:21