2

My storage devices are mounted as follows:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/md2        193G   35G  148G  19% /
/dev/md4        492G   73M  466G   1% /mnt

My KVM image is located at /kvm/vhdd/ which is on partition /dev/md2

ls -l /kvm/vhdd/wwe.img
-rw------- 1 libvirt-qemu kvm 163611607040 Mar  9 09:12 /kvm/vhdd/wwe.img

I need to move this image partition /dev/md4, i.e. directory /mnt.

How to do this? I can stop my virtual machine. But what I should do after stopping? Should I just make cp? Or should I edit configs? I found

grep -R "/kvm" /etc/
/etc/libvirt/qemu.conf:#    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",
/etc/libvirt/storage/autostart/iso.xml:    <path>/kvm/iso</path>
/etc/libvirt/storage/autostart/vhdd.xml:    <path>/kvm/vhdd</path>
/etc/libvirt/storage/iso.xml:    <path>/kvm/iso</path>
/etc/libvirt/storage/vhdd.xml:    <path>/kvm/vhdd</path>
/etc/libvirt/qemu/wwe.xml:    <emulator>/usr/bin/kvm-spice</emulator>
/etc/libvirt/qemu/wwe.xml:      <source file='/kvm/vhdd/wwe.img'/>
/etc/libvirt/qemu/autostart/wwe.xml:    <emulator>/usr/bin/kvm-spice</emulator>
/etc/libvirt/qemu/autostart/wwe.xml:      <source file='/kvm/vhdd/wwe.img'/>
/etc/default/libvirtd:# Start libvirtd to handle qemu/kvm:
/etc/default/qemu-kvm:# /run/hugepages/kvm
/etc/apparmor.d/libvirt/libvirt-68d2ecf6-4569-49cc-871f-042a42ad1c03.files:  "/kvm/vhdd/wwe.img" rwk,

Should I replace this path /kvm/vhdd/ in my configs? Please help, I am afraid to crash service, thanks.

3 Answers 3

8

Backup Original VM:

  1. virsh shutdown wwe
  2. virsh list --all
  3. cp /etc/libvirt/qemu/wwe.xml /etc/libvirt/qemu/orig-wwe.xml
  4. cp /kvm/vhdd/wwe.img /kvm/vhdd/orig-wwe.img

Move VM image to partition /mnt/kvm:

  1. mkdir /mnt/kvm

  2. mv /kvm/vhdd/wwe.img /mnt/kvm/wwe.img

  3. Edit XML file: vim /etc/libvirt/qemu/wwe.xml

    From: <source file='/kvm/vhdd/wwe.img'/>
    
    To: <source file='/mnt/kvm/wwe.img'/>
    
  4. virsh start wwe

Note: You can restore to original state using orignal files at any time, however below is an alternate way used for cloning & moving VM in KVM Infra.



KVM - Clone Guests from available Image:

1- create a base VM and install OS, shut down the base VM

virsh shutdown basevm

2- dump its XML file to template.xml and copy its image to template.qcow2

virsh dumpxml basevm > /etc/libvirt/qemu/template.xml
cp /home/kvm/images/basevm.qcow2 /home/kvm/images/template.qcow2

3- in template.xml point the disk source file to template.qcow2

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2' cache='none'/>
  <source file='/home/kvm/images/template.qcow2'/>
  <target dev='vda' bus='virtio'/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</disk>

4- run virt-sysprep on template.qcow2. to reset the image, e.g. will remove SSH keys, create new MAC address, clean up log files, etc..

virt-sysprep -a /home/kvm/images/template.qcow2

5- after this point you don't need the base VM anymore. Delete it if not in use.

virsh undefine basevm
rm /home/kvm/images/basevm.qcow2

6- Cloning new VMs from Template: template.xml and template.qcow2

virt-clone --connect qemu:///system --original-xml /etc/libvirt/qemu/template.xml --name server --file /home/kvm/images/template.qcow2

KVM - Move Guest to Another Host:

1- After above steps transfer xml & image file to new Host KVM:

scp /etc/libvirt/qemu/template.xml kvm02:/etc/libvirt/qemu/template.xml
scp /home/kvm/images/template.qcow2 kvm02:/home/kvm/images/template.qcow2

virt-sysprep -a /home/kvm/images/template.qcow2
virsh define /etc/libvirt/qemu/template.xml
virsh start vm
4

If I understand, your need is to move /kvm/vhdd/wwe.img to somewhere in /mnt (let's say /mnt/kvm/vhdd/wwe.img ) ?

If so I would :

  • stop the virtual machine (using poweroff or other command in the VM itself), then wait for stopped state in virt-manager (or virsh)

  • move the image file to new location

  • change image location in VM definition :

  • either using virt-manager GUI (remove the hdd in VM definition and add again from new location),

  • or with virsh (assuming the VM name is 'wwe'), for example :

    ~# virsh dumpxml wwe > wwe.xml
    
    ~# sed "s[file='/kvm/vhdd/wwe.img'[file='/mnt/kvm/vhdd/wwe.img'[" wwe.xml > wwe_new.xml
    
    ~# diff wwe.xml wwe_new.xml
    

    (will show only the file location change as difference)

    ~# virsh undefine wwe
    
    ~# virsh define wwe_new.xml
    
  • restart the VM.

3

In addition to what has already been said I would recommend to use virsh edit instead of editing the xml file in /etc directly. The comments in this file say:

<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit vmname
or other application using the libvirt API.
-->

So the actual procedure is like this:

  1. virsh shutdown <vmname>
  2. mv oldpath/image.qcow newpath/image.qcow
  3. virsh edit <vmname> Search for tag tag <source file="..."> and change the path accordingly.
  4. virsh start <vmname>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .