kvm跨版本迁移 machine问题

发布于 2022-03-23  199 次阅读


虚拟机跨版本迁移 machine问题

准备两台配置相同的宿主机:

主机名 ip kvm版本
kvm-test41 192.168.232.41 1.5.3
kvm-test42 192.168.232.42 2.12.0

低版本的kvm虚拟机向高版本的虚拟机进行迁移

使用对应命令在宿主机kvm-test41 创建名为test的虚拟机

virt-install  \
--name=test \
--memory=512 \
--vcpus=1 \
--os-type=linux \
--os-variant=rhel7 \
--location=/tmp/CentOS-7-x86_64-Minimal-2009.iso \
--disk path=/data/test.qcow2 \
--bridge=virbr0 \
--graphics=none \
--console=pty,target_type=serial  \
--extra-args="console=tty0 console=ttyS0"

查看支持的虚拟机版本类型,可以看到默认是 pc-i440fx-rhel7.0.0

[root@kvm-test41 qemu]# /usr/libexec/qemu-kvm -machine help 
Supported machines are:
none                 empty machine
pc                      RHEL 7.0.0 PC (i440FX + PIIX, 1996) (alias of pc-i440fx-rhel7.0.0)
pc-i440fx-rhel7.0.0  RHEL 7.0.0 PC (i440FX + PIIX, 1996) (default)
rhel6.6.0            RHEL 6.6.0 PC
rhel6.5.0            RHEL 6.5.0 PC
rhel6.4.0            RHEL 6.4.0 PC
rhel6.3.0            RHEL 6.3.0 PC
rhel6.2.0            RHEL 6.2.0 PC
rhel6.1.0            RHEL 6.1.0 PC
rhel6.0.0            RHEL 6.0.0 PC
[root@kvm-test41 qemu]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     test                           running
[root@kvm-test41 qemu]# virsh shutdown test
Domain test is being shutdown

[root@kvm-test41 qemu]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     test                           shut off

查看虚拟机文件路径

 [root@kvm-test41 qemu]# virsh domblklist  test
Target     Source
------------------------------------------------
vda        /data/test.qcow2
hda        -

虚拟机配置文件导出

[root@kvm-test41 qemu]# virsh dumpxml test>/tmp/test.xml

将宿主机kvm-test41上的虚拟机test迁移到kvm-test42宿主机下

[root@kvm-test41 qemu]# scp /tmp/test.xml root@192.168.232.42:/data/
[root@kvm-test41 qemu]# scp /data/test.qcow2 root@192.168.232.42:/data/

启动kvm-test42 上的迁移过来的虚拟机

##根据xml 文件注册虚拟机
[root@kvm-test42 tmp]# virsh define /data/test.xml
Domain test defined from /data/test.xml

[root@kvm-test42 tmp]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     test                        shut off

[root@kvm-test42 tmp]# virsh start test
Domain test started

[root@kvm-test42 tmp]#  virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     test                           running
###进入虚拟机检查是否正常
[root@kvm-test42 tmp]# virsh console test
Connected to domain test
Escape character is ^]
可以看出虚拟机可以正常进入通过ping baidu.com 也可以检测网络正常
 ##

测试虚拟机是否可以正常调整内存、CPU

[root@kvm-test42 ~]# virsh shutdown test
Domain test is being shutdown
#查看原有的配置
[root@kvm-test42 ~]# virsh dominfo test
Id:             2
Name:           test
UUID:           2caa8606-edb4-4284-b3ed-ac133404a297
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       73.8s
Max memory:     524288 KiB
Used memory:    524288 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c9,c649 (permissive)

[root@kvm-test42 ~]# virsh edit test
Domain test XML configuration edited.
修改对cpu 内存调整到2c1g

[root@kvm-test42 ~]# virsh start  test
Domain test started

#查看调整配置后的虚拟机配置信息
[root@kvm-test42 ~]# virsh dominfo test
Id:             3
Name:           test
UUID:           2caa8606-edb4-4284-b3ed-ac133404a297
OS Type:        hvm
State:          running
CPU(s):         2
CPU time:       6.7s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c578,c1008 (permissive)

[root@kvm-test42 ~]# virsh console test
Connected to domain test
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64

localhost login: root
密码:
Last login: Thu Mar 24 21:40:34 on ttyS0
[root@localhost ~]# top
top - 16:09:24 up 0 min,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:  94 total,   1 running,  93 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.4 sy,  0.0 ni, 99.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1014632 total,   778508 free,   140564 used,    95560 buff/cache

测试虚拟机是否可以正常扩容磁盘

#查看虚拟机磁盘位置
[root@kvm-test42 ~]# virsh domblklist test
Target     Source
------------------------------------------------
vda        /data/test.qcow2
hda        -

[root@kvm-test42 ~]# virsh shutdown test
Domain test is being shutdown

#通过以下命令可以看到我们磁盘大小为10GB
[root@kvm-test42 ~]# qemu-img info /data/test.qcow2
image: /data/test.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

#增加5GB磁盘空间
[root@kvm-test42 ~]# qemu-img resize /data/test.qcow2 +5G
Image resized.
[root@kvm-test42 ~]# qemu-img info /data/test.qcow2
image: /data/test.qcow2
file format: qcow2
virtual size: 15G (16106127360 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

#进入虚拟机进行lvm 扩容
[root@kvm-test42 ~]# virsh start  test
Domain test started
[root@kvm-test42 ~]# virsh console test

虚拟机 lvm 扩容

#虚拟机test lvm 扩容

[root@test ~]# fdisk /dev/vda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):
Using default response p
分区号 (3,4,默认 3):3
起始 扇区 (20971520-31457279,默认为 20971520):
将使用默认值 20971520
Last 扇区, +扇区 or +size{K,M,G} (20971520-31457279,默认为 31457279):
将使用默认值 31457279
分区 3 已设置为 Linux 类型,大小设为 5 GiB

命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。

#创建pv
[root@test ~]# pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created.

#将/dev/vda3 扩展至 centos 卷组
[root@test ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   1   2   0 wz--n- <9.00g    0
[root@test ~]# vgextend centos /dev/vda3
  Volume group "centos" successfully extended

#将全部空间扩展至/dev/mapper/centos-root
lvextend -l +100%FREE /dev/mapper/centos-root

#查看虚拟机文件系统
[root@test ~]# lsblk -f
NAME            FSTYPE   LABEL UUID                                   MOUNTPOINT
sr0
vda
├─vda1          xfs            5060a41a-8ec3-4a9a-a225-935fc459cde5   /boot
├─vda2          LVM2_mem       N1JzNZ-nb7z-oDPX-fdwP-QcKG-8YZ6-aG1S7R
│ ├─centos-root xfs            5608e21c-24be-40a8-9113-c74c8dab6710   /
│ └─centos-swap swap           6b8d73b6-e722-4087-a284-01e36f605ca0   [SWAP]
└─vda3          LVM2_mem       hKUmEg-1iOb-qDkd-8L9f-ylH3-uRWa-19sZz4
  └─centos-root xfs            5608e21c-24be-40a8-9113-c74c8dab6710   /

#使扩容生效 xfs_growfs /dev/mapper/centos-root
[root@test ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=524032 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2096128, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2096128 to 3405824

[root@test ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 232M     0  232M    0% /dev
tmpfs                    244M     0  244M    0% /dev/shm
tmpfs                    244M  4.5M  240M    2% /run
tmpfs                    244M     0  244M    0% /sys/fs/cgroup
/dev/mapper/centos-root   13G  1.3G   12G   10% /
/dev/vda1               1014M  137M  878M   14% /boot
tmpfs                     49M     0   49M    0% /run/user/0
此时磁盘空间增加了5GB

这里要注意的是宿主机kvm-test42 支持的machine 版本类型包含 pc-i440fx-rhel7.0.0 的

[root@kvm-test42 tmp]# /usr/libexec/qemu-kvm -machine help
Supported machines are:
pc                   RHEL 7.6.0 PC (i440FX + PIIX, 1996) (alias of pc-i440fx-rhel7.6.0)
pc-i440fx-rhel7.6.0  RHEL 7.6.0 PC (i440FX + PIIX, 1996) (default)
pc-i440fx-rhel7.5.0  RHEL 7.5.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.4.0  RHEL 7.4.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.3.0  RHEL 7.3.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.2.0  RHEL 7.2.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.1.0  RHEL 7.1.0 PC (i440FX + PIIX, 1996)
pc-i440fx-rhel7.0.0  RHEL 7.0.0 PC (i440FX + PIIX, 1996)
rhel6.6.0            RHEL 6.6.0 PC
rhel6.5.0            RHEL 6.5.0 PC
rhel6.4.0            RHEL 6.4.0 PC
rhel6.3.0            RHEL 6.3.0 PC
rhel6.2.0            RHEL 6.2.0 PC
rhel6.1.0            RHEL 6.1.0 PC
rhel6.0.0            RHEL 6.0.0 PC
q35                  RHEL-7.6.0 PC (Q35 + ICH9, 2009) (alias of pc-q35-rhel7.6.0)
pc-q35-rhel7.6.0     RHEL-7.6.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel7.5.0     RHEL-7.5.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel7.4.0     RHEL-7.4.0 PC (Q35 + ICH9, 2009)
pc-q35-rhel7.3.0     RHEL-7.3.0 PC (Q35 + ICH9, 2009)
none                 empty machine

高版本的kvm虚拟机向低版本的虚拟机进行迁移

使用对应命令在宿主机kvm-test42 创建名为test-ev的虚拟机

virt-install  \
--name=test-ev \
--memory=512 \
--vcpus=1 \
--os-type=linux \
--os-variant=rhel7 \
--location=/tmp/CentOS-7-x86_64-Minimal-2009.iso \
--disk path=/data/test-ev.qcow2 \
--bridge=virbr0 \
--graphics=none \
--console=pty,target_type=serial  \
--extra-args="console=tty0 console=ttyS0"
[root@kvm-test42 tmp]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     test-ev                           running
[root@kvm-test42 tmp]# virsh shutdown test-ev
Domain test is being shutdown

[root@kvm-test42 tmp]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     test-ev                           shut off

查看虚拟机文件路径

 [root@kvm-test42 tmp]# virsh domblklist  test-ev
Target     Source
------------------------------------------------
vda        /data/test-ev.qcow2
hda        -

虚拟机配置文件导出

[root@kvm-test42 tmp]# virsh dumpxml test-ev>/tmp/test-ev.xml

将宿主机kvm-test42上的虚拟机test-ev迁移到kvm-test41宿主机下

[root@kvm-test42 tmp]# scp /tmp/test-ev.xml root@192.168.232.41:/data/
[root@kvm-test42 tmp]# scp /data/test-ev.qcow2 root@192.168.232.41:/data/

启动迁移到kvm-test41宿主机上的虚拟机

##注册虚拟机
[root@kvm-test41 qemu]# virsh define /data/test-ev.xml
Domain test-ev defined from /data/test-ev.xml
##直接启动虚拟机会出现,machine 类型不支持
[root@kvm-test41 qemu]# virsh start test-ev
error: Failed to start domain test-ev
error: internal error: process exited while connecting to monitor: qemu-kvm: -machine pc-i440fx-rhel7.6.0,accel=kvm,usb=off,dump-guest-core=off: Unsupported machine type
Use -machine help to list supported machines!

通过修改virsh edit test-ev
hvm
修改为:hvm
再次启动虚拟机,虚拟机就可以正常启动

[root@kvm-test41 qemu]# virsh start test-ev
Domain test-ev started

测试迁移后的虚拟机是否可以正常调整配置

将原有1c512mb 修改为2c 1g

#关闭虚拟机
[root@kvm-test41 data]# virsh shutdown test-ev
Domain test-ev is being shutdown
#查看test-ev 配置信息
[root@kvm-test41 data]#  virsh dominfo test-ev
Id:             -
Name:           test-ev
UUID:           b869836a-9b5e-45b9-9679-82847533293e
OS Type:        hvm
State:          shut off
CPU(s):         1
Max memory:     524288 KiB
Used memory:    524288 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0

#将配置改成2c1G
[root@kvm-test41 data]# virsh edit test-ev
修改内容如下
<memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>2</vcpu>
  #查看改配后test-ev 配置信息
 [root@kvm-test41 data]# virsh dominfo test-ev
Id:             -
Name:           test-ev
UUID:           b869836a-9b5e-45b9-9679-82847533293e
OS Type:        hvm
State:          shut off
CPU(s):         2
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0

启动虚拟机
[root@kvm-test41 data]# virsh start  test-ev
Domain test-ev started
进去虚拟机
[root@kvm-test41 data]# virsh console test-ev
Connected to domain test-ev
Escape character is ^]

#使用top 可以看目前已经是2C1G 的配置
top - 18:59:03 up 1 min,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:  99 total,   1 running,  98 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni, 99.7 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1014628 total,   775132 free,   140416 used,    99080 buff/cache

检查迁移后虚拟机test-ev 是否可以正常扩容空间

增加test-ev.qcow2 10G 空间
[root@kvm-test41 data]# qemu-img resize /data/test-ev.qcow2 +10G
Image resized.

#进行lvm扩容
[root@kvm-test41 data]# virsh console test-ev
Connected to domain test-ev
Escape character is ^]

#在test-ev 虚拟机做lvm 扩容
[root@test-ev ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 484M     0  484M   0% /dev
tmpfs                    496M     0  496M   0% /dev/shm
tmpfs                    496M  6.7M  489M   2% /run
tmpfs                    496M     0  496M   0% /sys/fs/cgroup
/dev/mapper/centos-root  8.0G  1.3G  6.8G  16% /
/dev/vda1               1014M  137M  878M  14% /boot
tmpfs                    100M     0  100M   0% /run/user/0
[root@test-ev ~]# fdisk /dev/vda
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):
First sector (20971520-41943039, default 20971520):
Using default value 20971520
Last sector, +sectors or +size{K,M,G} (20971520-41943039, default 41943039):
Using default value 41943039
Partition 3 of type Linux and of size 10 GiB is set
Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e

#通知操作系统分区表的更改,重启系统也可以刷新分区表
[root@test-ev ~]# partprobe /dev/vda

新加的分区/dev/vda3 创建为物理卷
[root@test-ev ~]# pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created.

 #物理卷 /dev/vda3 扩展至 centos卷组
[root@test-ev ~]# vgextend centos /dev/vda3
  Volume group "centos" successfully extended

 #将全部空间扩展至/dev/mapper/centos-root
[root@test-ev ~]# lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from <8.00 GiB (2047 extents) to 17.99 GiB (4606 extents).
  Logical volume centos/root successfully resized.

#可以看到这个10GB 空间已经扩容到系统盘了
[root@test-ev ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom
vda             252:0    0   20G  0 disk
├─vda1          252:1    0    1G  0 part /boot
├─vda2          252:2    0    9G  0 part
│ ├─centos-root 253:0    0   18G  0 lvm  /
│ └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
└─vda3          252:3    0   10G  0 part
  └─centos-root 253:0    0   18G  0 lvm  /
该虚拟机可以正常扩容

这里需要注意不管高版本迁移低版本,还是低版本迁移高版本,都需要注意 machine 支持的版本以及类型。