03.Rocky8的kvm创建虚拟主机和迁移主机

3.1 创建虚拟机

3.1.1 利用 qemu-img命令创建虚拟磁盘

注意: qemu-img create 一定要确认对应路径下没有此文件,如果存在将覆盖原文件

qemu-img create -f qcow2 /var/lib/libvirt/images/centos7-1.qcow2  30G

命令执行结果:

[root@rocky8 ~]# qemu-img create -f qcow2 /var/lib/libvirt/images/centos7-1.qcow2  30G
Formatting '/var/lib/libvirt/images/centos7-1.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=32212254720 lazy_refcounts=off refcount_bits=16

观察文件虚拟磁盘大小,比较用virt-manager创建的虚拟机磁盘文件大小
命令执行结果:

[root@rocky8 images]# ll -h /var/lib/libvirt/images/
total 196K
-rw-r--r-- 1 root root 193K Nov 16 00:56 centos7-1.qcow2

3.1.2 利用 osinfo-query命令查看支持的OS版本

查看支持的OS

osinfo-query os| grep centos

命令执行结果:

[root@rocky8 images]# osinfo-query os| grep centos
 centos-stream8       | CentOS Stream 8                                    | 8        | http://centos.org/centos-stream/8       
 centos-stream9       | CentOS Stream 9                                    | 9        | http://centos.org/centos-stream/9       
 centos5.0            | CentOS 5.0                                         | 5.0      | http://centos.org/centos/5.0            
 centos5.1            | CentOS 5.1                                         | 5.1      | http://centos.org/centos/5.1            
 centos5.10           | CentOS 5.10                                        | 5.10     | http://centos.org/centos/5.10           
 centos5.11           | CentOS 5.11                                        | 5.11     | http://centos.org/centos/5.11           
 centos5.2            | CentOS 5.2                                         | 5.2      | http://centos.org/centos/5.2            
 centos5.3            | CentOS 5.3                                         | 5.3      | http://centos.org/centos/5.3            
 centos5.4            | CentOS 5.4                                         | 5.4      | http://centos.org/centos/5.4            
 centos5.5            | CentOS 5.5                                         | 5.5      | http://centos.org/centos/5.5            
 centos5.6            | CentOS 5.6                                         | 5.6      | http://centos.org/centos/5.6            
 centos5.7            | CentOS 5.7                                         | 5.7      | http://centos.org/centos/5.7            
 centos5.8            | CentOS 5.8                                         | 5.8      | http://centos.org/centos/5.8            
 centos5.9            | CentOS 5.9                                         | 5.9      | http://centos.org/centos/5.9            
 centos6.0            | CentOS 6.0                                         | 6.0      | http://centos.org/centos/6.0            
 centos6.1            | CentOS 6.1                                         | 6.1      | http://centos.org/centos/6.1            
 centos6.10           | CentOS 6.10                                        | 6.10     | http://centos.org/centos/6.10           
 centos6.2            | CentOS 6.2                                         | 6.2      | http://centos.org/centos/6.2            
 centos6.3            | CentOS 6.3                                         | 6.3      | http://centos.org/centos/6.3            
 centos6.4            | CentOS 6.4                                         | 6.4      | http://centos.org/centos/6.4            
 centos6.5            | CentOS 6.5                                         | 6.5      | http://centos.org/centos/6.5            
 centos6.6            | CentOS 6.6                                         | 6.6      | http://centos.org/centos/6.6            
 centos6.7            | CentOS 6.7                                         | 6.7      | http://centos.org/centos/6.7            
 centos6.8            | CentOS 6.8                                         | 6.8      | http://centos.org/centos/6.8            
 centos6.9            | CentOS 6.9                                         | 6.9      | http://centos.org/centos/6.9            
 centos7.0            | CentOS 7                                           | 7        | http://centos.org/centos/7.0            
 centos8              | CentOS 8                                           | 8        | http://centos.org/centos/8

3.1.3 创建虚拟机使用光盘启动并手动安装

1)创建软链接
[root@rocky8 /]# mkdir data
[root@rocky8 /]# ln -s /isos/ /data/isos
[root@rocky8 /]# ll /data/isos/CentOS-7-x86_64-Minimal-2003.iso 
-rw-r--r--. 1 qemu qemu 1085276160 Nov 15 02:41 /data/isos/CentOS-7-x86_64-Minimal-2003.iso
2)virt-install安装

注意:
–name 安装的主机名字
–cdrom iso文件路径
–disk path 虚拟磁盘文件路径
–os-variant 操作系统
(下面这个命令就是创建一个叫centos7的主机)

virt-install --virt-type kvm --name centos7 --ram 1024 --vcpus 2 --cdrom=/data/isos/CentOS-7-x86_64-Minimal-2003.iso --disk path=/var/lib/libvirt/images/centos7-1.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-variant=centos7.0

命令执行结果:

[root@rocky8 /]# virt-install --virt-type kvm --name centos7 --ram 1024 --vcpus 2 --cdrom=/data/isos/CentOS-7-x86_64-Minimal-2003.iso --disk path=/var/lib/libvirt/images/centos7-1.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-variant=centos7.0

Starting install...

Domain is still running. Installation may be in progress.
You can reconnect to the console to complete the installation process.
3)手动安装系统

执行virt-manager,快看到有一天centos7的虚拟机

rocky安装redis rocky安装qemu_CentOS


然后手动安装该虚拟机,并做一些基本设置

1)查看iso文件路径是否正确

rocky安装redis rocky安装qemu_centos_02


2)修改BOOT options,勾选STAT CDROM1,不然会报错

rocky安装redis rocky安装qemu_centos_03


rocky安装redis rocky安装qemu_rocky安装redis_04


3)虚拟机基本设置

ssh 192.168.122.106
设置主机名
hostnamectl set-hostname centos7
安装vim
yum -y install vim

修改ssh连接慢

vim /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
重启ssh服务
systemctl restart sshd

修改selinux
vim /etc/selinux/config
SELINUX=disable
关闭防火墙
systemctl disable --now firewalld
setenforce 0

3.2利用virt-install实现

注意:kvm虚拟机内存不能大于宿主机!!!!!
安装命令:

virt-install --virt-type kvm --name centos7-2 --ram 1024 --vcpus 1 --disk bus=virtio,path=/var/lib/libvirt/images/centos7-2.qcow2 --network network=default,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole --autostart --boot hd

大于宿主机报错如下:

rocky安装redis rocky安装qemu_CentOS_05


修改一下内存大小

rocky安装redis rocky安装qemu_CentOS_06


运行成功。

运行virt-manager可以看到下面出现新的虚拟机

virt-manager

rocky安装redis rocky安装qemu_rocky安装redis_07

3.3 迁移虚拟机

A机10.0.0.152
B机10.0.0.155 (没有装虚拟机)
A机的centos7迁移到B机
迁移之前A机要迁移的虚拟机要关机

3.3.1 虚拟机状态检查

A机

[root@rocky8 qemu]# virsh list --all
 Id   Name        State
----------------------------
 -    centos7     shut off
 -    centos7-2   shut off

B机

[root@rockyb images]# virsh list --all
 Id   Name   State
--------------------

3.3.2 查看A机的磁盘文件和配置文件

磁盘文件迁移

A机

[root@rocky8 qemu]# virsh domblklist centos7
 Target   Source
---------------------------------------------------
 vda      /var/lib/libvirt/images/centos7-1.qcow2
 sda      /isos/CentOS-7-x86_64-Minimal-2003.iso

然后

scp /var/lib/libvirt/images/centos7-1.qcow2 10.0.0.155:/var/lib/libvirt/images/

rocky安装redis rocky安装qemu_centos_08


B机

rocky安装redis rocky安装qemu_linux_09

配置文件迁移
scp /etc/libvirt/qemu/centos7.xml  10.0.0.155:/etc/libvirt/qemu/

A机

rocky安装redis rocky安装qemu_linux_10

B机

rocky安装redis rocky安装qemu_rocky安装redis_11

B机定义注册虚拟机

查看B机虚拟机数量

[root@rockyb images]# virsh list --all
 Id   Name   State
--------------------

B机开始注册迁移的虚拟机

[root@rockyb images]# virsh define /etc/libvirt/qemu/centos7.xml
Domain 'centos7' defined from /etc/libvirt/qemu/centos7.xml

查看B机虚拟机数量

[root@rockyb images]# virsh list --all
 Id   Name      State
--------------------------
 -    centos7   shut off

virt-manager也能看到

rocky安装redis rocky安装qemu_rocky安装redis_12

迁移完成