大网数据广告活动
服务器运维问答

How to move KVM image to another directory/partition?

· 52CE 我爱测

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 …

问题原文

原帖链接 · Piduna · CC BY-SA 4.0

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.

已采纳解答

原帖链接 · Saqib Hashmi · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。