创建KVM虚拟机
复制KVM虚拟机
迁移KVM虚拟机
迁徙虚拟机的方法有很多,什么冷迁移(静态)、热迁移(动态)、完全迁移、部分迁移、挂存储迁移、离线迁移
归根结底 Linux中一些皆文件,迁移虚拟机就是挪动这两个文件
KVM虚拟机其实只有两个东东
- KVM配置文件
- KVM磁盘文件
本次实验以Linux尊崇的实用主义至上,详述一下生产中的实际KVM迁移过程
关机迁移
- 热迁移会造成数据丢失和未知以外,迁移尽量在关机的情况下做冷迁移
确保KVM环境可用
- 在新宿主机配置好KVM环境,确保KVM可正常运行
- 具体参见Linux单机使用本地镜像创建KVM虚拟机实例
拷贝KVM虚拟机配置文件
- 找到KVM虚拟主机配置文件并拷贝到新宿主机指定目录
scp -av /etc/libvirt/qemu/v1.xml root@10.0.0.150:/vms/vmhost
- 在新设备上可以将配置文件中的UUID上删除掉
拷贝KVM虚拟机硬盘文件
- 生产中一般KVM磁盘文件都存放在存储上,直接挂在新服务器上就完事了
- 磁盘文件拷贝还需要一段时间
scp -av /vms/vmdisk/v1.qcow2 root@10.0.0.150:/vms/vmdisk
认证虚拟主机
- 在迁移后的新宿主机上认证虚拟主机配置文件使系统识别迁移过来的虚拟主机
- 类似于导入配置文件,注意导入的虚拟机不能与已有虚拟机重名
virsh define /vms/vmhost/v1.xml
查看并启动迁移的虚拟主机
- 列出所有虚拟主机
virsh list --all
- 启动虚拟主机
virsh start v1
至此,迁移完成
虚拟主机配置文件
- 虚拟机配置文件在创建虚拟机时自动生成
- 手写的配置文件可以通过virsh define命令注册
虚拟机配置文件位置
[root@c7-199 ~]# find / -name 'v1*.xml'
/run/libvirt/qemu/v1.xml
/etc/libvirt/qemu/v1.xml
/etc/libvirt/qemu/autostart/v1.xml
虚拟机配置文件内容
[root@c7-199 ~]# ll /etc/libvirt/qemu/v1.xml
-rw------- 1 root root 3684 Jul 8 13:45 /etc/libvirt/qemu/v1.xml
[root@c7-199 ~]# ll /etc/libvirt/qemu/autostart/v1.xml
lrwxrwxrwx 1 root root 24 Jul 8 11:46 /etc/libvirt/qemu/autostart/v1.xml -> /etc/libvirt/qemu/v1.xml
[root@c7-199 ~]# cat /etc/libvirt/qemu/v1.xml
<!--
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 v1
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>v1</name>
<uuid>96922cf8-13de-4179-8d6a-316ada4a70fc</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Broadwell-noTSX-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/vms/vmdisk/v1.qcow2'/>
<target dev='hda' bus='ide'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/vms/vmiso/Rocky-8.4-x86_64-minimal.iso'/>
<target dev='hdb' bus='ide'/>
<readonly/>
<boot order='1'/>
<address type='drive' controller='0' bus='0' target='0' unit='1'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:61:20:f5'/>
<source bridge='br0'/>
<model type='rtl8139'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
</domain>
qemu-kvm部署文件
[root@c7-199 ~]# rpm -ql qemu-kvm
/etc/qemu-kvm/target-x86_64.conf
/usr/lib/udev/rules.d/80-kvm.rules
/usr/libexec/qemu-kvm
/usr/share/qemu-kvm/acpi-dsdt.aml
/usr/share/qemu-kvm/bios-256k.bin
/usr/share/qemu-kvm/bios.bin
/usr/share/qemu-kvm/dump-guest-memory.py
/usr/share/qemu-kvm/dump-guest-memory.pyc
/usr/share/qemu-kvm/dump-guest-memory.pyo
/usr/share/qemu-kvm/kvmvapic.bin
/usr/share/qemu-kvm/linuxboot.bin
/usr/share/qemu-kvm/multiboot.bin
/usr/share/qemu-kvm/pxe-e1000.rom
/usr/share/qemu-kvm/pxe-ne2k_pci.rom
/usr/share/qemu-kvm/pxe-pcnet.rom
/usr/share/qemu-kvm/pxe-rtl8139.rom
/usr/share/qemu-kvm/pxe-virtio.rom
/usr/share/qemu-kvm/q35-acpi-dsdt.aml
/usr/share/qemu-kvm/qemu-icon.bmp
/usr/share/qemu-kvm/rhel6-e1000.rom
/usr/share/qemu-kvm/rhel6-ne2k_pci.rom
/usr/share/qemu-kvm/rhel6-pcnet.rom
/usr/share/qemu-kvm/rhel6-rtl8139.rom
/usr/share/qemu-kvm/rhel6-virtio.rom
/usr/share/qemu-kvm/sgabios.bin
/usr/share/qemu-kvm/systemtap/conf.d/qemu_kvm.conf
/usr/share/qemu-kvm/systemtap/script.d/qemu_kvm.stp
/usr/share/qemu-kvm/trace-events
/usr/share/qemu-kvm/vgabios-cirrus.bin
/usr/share/qemu-kvm/vgabios-qxl.bin
/usr/share/qemu-kvm/vgabios-stdvga.bin
/usr/share/qemu-kvm/vgabios-vmware.bin
/usr/share/qemu-kvm/vgabios.bin
/usr/share/systemtap/tapset/qemu-kvm-simpletrace.stp
/usr/share/systemtap/tapset/qemu-kvm.stp