i have this structure of space of hdd-s:

df -h
Filesystem      Size  Used Avail Use% Mounted on
udev             16G     0   16G   0% /dev
tmpfs           3.2G  8.9M  3.2G   1% /run
/dev/md2        193G   35G  148G  19% /
tmpfs            16G     0   16G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs            16G     0   16G   0% /sys/fs/cgroup
/dev/md4        492G   73M  466G   1% /mnt

Now, my kvm-image is on /dev/md2 on /. ls -l /kvm/vhdd/wwe.img -rw------- 1 libvirt-qemu kvm 163611607040 Mar 9 09:12 /kvm/vhdd/wwe.img

I need to replace this image to /dev/md4 to /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.

up vote 4 down vote accepted

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

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.