KVM-虚拟机【安装与基础使用】

目录

【1】kvm-01:安装-KVM管理工具

【2】创建新的虚拟机

【3】kvm常用管理命令

【4】console控制台登录

【5】修改虚拟机的密码

【6】KVM虚拟机的虚拟磁盘管理和快照管理

【7】将raw格式的磁盘挂载到宿主机

【8】磁盘的管理命令

【9】创建快照-内部快照

【10】查看虚拟机中的快照

【11】创建快照并起名称

【12】删除快照

【13】还原快照

【14】查看快照中的配置情况

【15】快照在宿主机的存放路径

【16】编辑、修改快照中的配置

【17】克隆虚拟机 - 完整克隆

【18】克隆虚拟机 - 连接克隆

【19】创建桥接网卡

【20】创建新的虚拟机的网络模式使用桥接模式

【21】将现有的虚拟机更改为桥接模式 

【22】宿主机的网卡

【23】为什么创建的虚拟机,默认的网络是nat,网段会是122呢?

【24】冷添加网卡 - 备份配置文件或者 edit

【25】冷添加硬盘

【26】热添加硬盘

【27】硬盘格式化,挂载使用

【28】磁盘扩容

【29】热添加网卡

【30】热调整内存大小

【31】热添加CPU

【32】冷迁移技术

【33】热迁移技术


kvm-01

10.0.0.31

4G

cpu开启vt虚拟化

kvm-02

10.0.0.32

2G

cpu开启vt虚拟化

KVM-虚拟机【安装与基础使用】_第1张图片

【1】kvm-01:安装-KVM管理工具

[root@kvm-01 ~]# lsmod | grep kvm
kvm_intel             170181  0 
kvm                   554609  1 kvm_intel
irqbypass              13503  1 kvm

[root@kvm-01 ~]# yum -y install libvirt qemu-kvm virt-install.noarch openssl
[root@kvm-01 ~]# systemctl start libvirtd.service 
[root@kvm-01 ~]# systemctl enable libvirtd.service

https://mirror.tuna.tsinghua.edu.cn/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-Minimal-1810.iso

### 把下载的镜像拉到虚拟机 ### 放到opt下

【2】创建新的虚拟机

  • 建议创建的虚拟机的宿主机内存不要低于2G。否则会特别慢
  • 下面使用命令的方式创建虚拟机,搭配vnc得到图形界面
[root@kvm-01 ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7-test --memory 1024 --vcpus 1 --disk /opt/centos7-test.raw,format=raw,size=10 --cdrom /opt/CentOS-7-x86_64-Minimal-1810.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

Starting install...
Allocating 'centos2.raw'                                                                  |  10 GB  00:00:00     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

# --virt-type kvm      :虚拟化类型kvm、qemu
# --os-type=linux      :系统类型
# --os-variant rhel7   :系统版本
# --name centos7-test  :虚拟机名称(唯一)
# --memory 1024        :虚拟机内存
# --vcpus 1            :CPU核数
# --disk /opt/centos2.raw,format=raw,size=10     :硬盘位置、格式、容量(唯一)
# --cdrom /opt/CentOS-7-x86_64-Minimal-1810.iso  :使用的镜像文件
# --network network=default                      :默认网络
# --graphics vnc,listen=0.0.0.0                  :vnc显示输出
# --noautoconsole

[root@kvm-01 ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 1     centos7-test                   running

KVM-虚拟机【安装与基础使用】_第2张图片

KVM-虚拟机【安装与基础使用】_第3张图片

KVM-虚拟机【安装与基础使用】_第4张图片

KVM-虚拟机【安装与基础使用】_第5张图片

KVM-虚拟机【安装与基础使用】_第6张图片

KVM-虚拟机【安装与基础使用】_第7张图片

KVM-虚拟机【安装与基础使用】_第8张图片 KVM-虚拟机【安装与基础使用】_第9张图片

KVM-虚拟机【安装与基础使用】_第10张图片 KVM-虚拟机【安装与基础使用】_第11张图片

KVM-虚拟机【安装与基础使用】_第12张图片

KVM-虚拟机【安装与基础使用】_第13张图片

【3】kvm常用管理命令

# 查看运行中的虚拟机
[root@kvm-01 ~]# virsh list 
 Id    Name                           State
----------------------------------------------------


# 查看所有存在的虚拟机
[root@kvm-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test                   shut off

# 启动虚拟机
[root@kvm-01 ~]# virsh start centos7-test 
Domain centos7-test started

[root@kvm-01 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     centos7-test                   running

KVM-虚拟机【安装与基础使用】_第14张图片

KVM-虚拟机【安装与基础使用】_第15张图片

# 关机虚拟机
[root@kvm-01 ~]# virsh shutdown centos7-test 
Domain centos7-test is being shutdown

# 拔电源关机
[root@kvm-01 ~]# virsh destroy centos7-test

# 重启
[root@kvm-01 ~]# virsh reboot centos7-test

# 磁盘文件占用空间,创建的时候分配了10G,查看目前已使用
[root@kvm-01 ~]# du -smh /opt/*
1.4G    /opt/centos2.raw
918M    /opt/CentOS-7-x86_64-Minimal-1810.iso

# 磁盘空间分配的大小
[root@kvm-01 ~]# ll -h /opt/
total 2.3G
-rw------- 1 qemu qemu  10G Jul 14 00:44 centos2.raw
-rw-r--r-- 1 qemu qemu 918M Jul 13 08:42 CentOS-7-x86_64-Minimal-1810.iso

# 备份磁盘文件
[root@kvm-01 ~]# cp /opt/centos2.raw /backup/

# 配置文件备份
[root@kvm-01 ~]# virsh dumpxml centos7-test > kvm_centos7-test.xml

# 删除虚拟机,先关机在删除,删除后磁盘文件是不会被删除的注意
[root@kvm-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     centos7-test                   running

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

[root@kvm-01 ~]# virsh undefine centos7-test 
Domain centos7-test has been undefined

[root@kvm-01 ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------

# 上面备份的配置文件,根据配置文件导入虚拟机
[root@kvm-01 ~]# ls
anaconda-ks.cfg  kvm_centos7-test.xml
[root@kvm-01 ~]# virsh define kvm_centos7-test.xml 
Domain centos7-test defined from kvm_centos7-test.xml

[root@kvm-01 ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 -     centos7-test                   shut off

# 修改配置文件edit,如果修改了磁盘文件的名称,但是没有修改配置文件,启动虚拟机时他会找不到磁盘文件而报错
[root@kvm-01 ~]# virsh edit centos7-test
[root@kvm-01 opt]# mv centos7-test.raw centos7-test-7-14.raw 
[root@kvm-01 opt]# virsh start centos7-test 
error: Failed to start domain centos7-test
error: Cannot access storage file '/opt/centos7-test.raw': No such file or directory

[root@kvm-01 opt]# virsh edit centos7-test
      
      
[root@kvm-01 opt]# virsh start centos7-test 
Domain centos7-test started

# 修改cpu的核数,很多时候修改了配置需要重启生效,reboot不管用
[root@kvm-01 opt]# virsh edit centos7-test
  2
  
[root@kvm-01 opt]# virsh shutdown centos7-test 
Domain centos7-test is being shutdown

[root@kvm-01 opt]# virsh start centos7-test 
Domain centos7-test started

KVM-虚拟机【安装与基础使用】_第16张图片

# 重命名,不能处于运行期间修改
[root@kvm-01 opt]# virsh shutdown centos7-test 
Domain centos7-test is being shutdown

[root@kvm-01 opt]# virsh domrename centos7-test centos7-test-7-14
Domain successfully renamed

[root@kvm-01 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off

[root@kvm-01 opt]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

# 挂起处于运行的虚拟机
[root@kvm-01 opt]# virsh suspend centos7-test-7-14 
Domain centos7-test-7-14 suspended

[root@kvm-01 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 8     centos7-test-7-14              paused

# 恢复被挂起的虚拟机,恢复后虚拟机的时间可能会落后
[root@kvm-01 opt]# virsh resume centos7-test-7-14 
Domain centos7-test-7-14 resumed

[root@kvm-01 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 8     centos7-test-7-14              running

# 建议虚拟机下载时间同步服务
[root@localhost ~]# yum -y instarll chrony

# 查询vnc端口号
[root@kvm-01 opt]# virsh vncdisplay centos7-test-7-14 
:0
# 一个虚拟机相当于一个qemu进程
[root@kvm-01 opt]# ps -ef | grep qemu

# KVM 宿主机开机自动启动虚拟机,宿主机开机时只要启动了libvirtd服务,就会拉起虚拟机
[root@kvm-01 opt]# virsh autostart centos7-test-7-14 
Domain centos7-test-7-14 marked as autostarted

[root@kvm-01 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off

[root@kvm-01 opt]# systemctl restart libvirtd.service
 
[root@kvm-01 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     centos7-test-7-14              running

# 查看虚拟机是否被设置开机自启,如果我们删除了autostart目录下的软连接就相当与关闭了开机自启
[root@kvm-01 opt]# ll -h /etc/libvirt/qemu/autostart/
total 0
lrwxrwxrwx 1 root root 39 Jul 14 02:14 centos7-test-7-14.xml -> /etc/libvirt/qemu/centos7-test-7-14.xml

[root@kvm-01 opt]# rm -rf /etc/libvirt/qemu/autostart/centos7-test-7-14.xml 
[root@kvm-01 opt]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off

[root@kvm-01 opt]# systemctl restart libvirtd.service 
[root@kvm-01 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off

[root@kvm-01 opt]# ln -s /etc/libvirt/qemu/centos7-test-7-14.xml /etc/libvirt/qemu/autostart/
[root@kvm-01 opt]# ll /etc/libvirt/qemu/autostart/
total 0
lrwxrwxrwx 1 root root 39 Jul 14 02:23 centos7-test-7-14.xml -> /etc/libvirt/qemu/centos7-test-7-14.xml

[root@kvm-01 opt]# systemctl restart libvirtd.service 
[root@kvm-01 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     centos7-test-7-14              running
 
 # 第二种方式查看虚拟机是否被设置开机自启
 [root@kvm-01 opt]# virsh list --autostart --all 
 Id    Name                           State
----------------------------------------------------
 1     centos7-test-7-14              running
 
 # 取消开机自启、1:删除软连接 2:命令方式
[root@kvm-01 opt]# virsh autostart --disable centos7-test-7-14 
Domain centos7-test-7-14 unmarked as autostarted

[root@kvm-01 opt]# virsh list --autostart --all 
 Id    Name                           State
----------------------------------------------------

【4】console控制台登录

  • console:命令版的控制台
  • vnc:图形版的控制台
# 7系统的方式,在虚拟机中配置
[root@kvm-01 opt]# ssh [email protected]
[email protected]'s password: 
Last login: Fri Jul 14 13:57:14 2023 from gateway

[root@localhost ~]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
[root@localhost ~]# reboot
Connection to 192.168.122.75 closed by remote host.
Connection to 192.168.122.75 closed.
[root@kvm-01 opt]# 

[root@kvm-01 opt]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 1     centos7-test-7-14              running

[root@kvm-01 opt]# virsh console centos7-test-7-14 
Connected to domain centos7-test-7-14
Escape character is ^]
ctrl+] 退出 、回车进入

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

localhost login: root
Password: 
Last login: Fri Jul 14 14:33:14 from gateway
[root@localhost ~]#
[root@localhost ~]# ctrl + ] 退出 ,返回到宿主机界面
[root@kvm-01 opt]# 

【5】修改虚拟机的密码

  • 虚拟机中需要安装qemu-guest-agent
[root@kvm-01 opt]# ssh [email protected]
[email protected]'s password: 
Last login: Fri Jul 14 14:39:36 2023

[root@localhost ~]# ps -ef | grep  qemu.guest_agent
root      3177  3104  0 14:53 pts/0    00:00:00 grep --color=auto qemu.guest_agent

[root@localhost ~]# yum -y install qemu-guest-agent
[root@localhost ~]# systemctl start qemu-guest-agent

[root@localhost ~]# systemctl enable qemu-guest-agent

[root@localhost ~]# ps -ef | grep  qemu.guest_agent
root      3185     1  0 14:54 ?        00:00:00 /usr/bin/qemu-ga --method=virtio-serial --path=/dev/virtio-ports/org.qemu.guest_agent.0 --blacklist=guest-file-open,guest-file-close,guest-file-read,guest-file-write,guest-file-seek,guest-file-flush,guest-exec,guest-exec-status -F/etc/qemu-ga/fsfreeze-hook
root      3187  3104  0 14:54 pts/0    00:00:00 grep --color=auto qemu.guest_agent

[root@localhost ~]# login
localhost login: Connection to 192.168.122.75 closed.

[root@kvm-01 opt]# virsh set-user-password --user root --password 123 centos7-test-7-14 
Password set successfully for root in centos7-test-7-14

## 验证登录
[root@kvm-01 opt]# ssh [email protected]
[email protected]'s password: 
Last login: Fri Jul 14 14:49:24 2023 from gateway

【6】KVM虚拟机的虚拟磁盘管理和快照管理

  • raw:裸格式,没有经过任何压缩,原始数据,占用空间大,不支持快照功能,不方便传输,读写性能好,可以直接挂载到宿主机,总50G,但是我们使用了1G,传输到新机器时也是全部空间传输50G
  • qcow2:copy on write (写时复制)占用空间小,支持快照功能,性能比raw差一点,方便传输,总50G,但是我们使用了1G,传输到新机器时只传输1G
# 创建一台磁盘为qcow2格式的虚拟机
[root@kvm-01 opt]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7-test-qcow2 --memory 1024 --vcpus 1 --disk /opt/centos7-test-qcow2.qcow2,format=qcow2,size=10 --cdrom /opt/CentOS-7-x86_64-Minimal-1810.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

Starting install...
Allocating 'centos7-test-qcow2.qcow2'                        |  10 GB  00:00:00     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

【7】将raw格式的磁盘挂载到宿主机

# raw 可以直接挂载到宿主机,先关机,查询磁盘的位置偏移量
[root@kvm-01 opt]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 opt]# fdisk /opt/centos7-test-7-14.raw
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p       

Disk /opt/centos7-test-7-14.raw: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b436c

                     Device Boot      Start         End      Blocks   Id  System
/opt/centos7-test-7-14.raw1   *        2048    20971519    10484736   83  Linux

Command (m for help): q 

[root@kvm-01 opt]# echo $((2048*512))
1048576

[root@kvm-01 opt]# mkdir /images
[root@kvm-01 opt]# mount -o loop,offset=1048576 /opt/centos7-test-7-14.raw /images/
[root@kvm-01 opt]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   17G  5.1G   12G  30% /
devtmpfs             1.9G     0  1.9G   0% /dev
tmpfs                1.9G     0  1.9G   0% /dev/shm
tmpfs                1.9G  8.7M  1.9G   1% /run
tmpfs                1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1           1014M  139M  876M  14% /boot
tmpfs                378M     0  378M   0% /run/user/0
/dev/loop0            10G  1.3G  8.8G  13% /images

[root@kvm-01 opt]# ls /images/
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

[root@kvm-01 opt]# du -smh /images/
1.2G    /images/

# 挂载之后我们可以管理文件,创建文件,或者直接修改虚拟机的密码
# 修改密码方式一
[root@kvm-01 opt]# chroot /images/
[root@kvm-01 /]# passwd 
Changing password for user root.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@kvm-01 /]# exit
exit

[root@kvm-01 opt]# umount /images 
[root@kvm-01 opt]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

[root@kvm-01 opt]# ssh [email protected]
[email protected]'s password: 
Last login: Fri Jul 14 14:57:42 2023 from gateway   

# 修改密码方式二
[root@kvm-01 opt]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 opt]# mount -o loop,offset=1048576 /opt/centos7-test-7-14.raw /images/
[root@kvm-01 opt]# ls /images/
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

[root@kvm-01 ~]# chroot /images/
[root@kvm-01 /]# echo 123|passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.
[root@kvm-01 /]# exit
exit

[root@kvm-01 ~]# umount /images 

[root@kvm-01 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

[root@kvm-01 ~]# ssh [email protected]
[email protected]'s password: 
Last login: Fri Jul 14 15:29:35 2023 from gateway

【8】磁盘的管理命令

# 查看磁盘真实使用情况 info 
[root@kvm-01 ~]# qemu-img info /opt/centos7-test-7-14.raw
image: /opt/centos7-test-7-14.raw
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.3G

# 创建一块 qcow2 的 磁盘
[root@kvm-01 ~]# qemu-img create -f qcow2 /opt/test-qcow2 1G
Formatting '/opt/test-qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@kvm-01 ~]# ls /opt/
centos7-test-7-14.raw     CentOS-7-x86_64-Minimal-1810.iso
centos7-test-qcow2.qcow2  test-qcow2

[root@kvm-01 ~]# qemu-img info /opt/test-qcow2
image: /opt/test-qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    
# 创建一块 raw 的 磁盘
[root@kvm-01 ~]# qemu-img create -f raw /opt/test-raw 1G
Formatting '/opt/test-raw', fmt=raw size=1073741824 

[root@kvm-01 ~]# qemu-img info /opt/test-raw
image: /opt/test-raw
file format: raw
virtual size: 1.0G (1073741824 bytes)
disk size: 0

# qcow2 磁盘扩容
[root@kvm-01 ~]# qemu-img resize /opt/test-qcow2 +2G
Image resized.
[root@kvm-01 ~]# qemu-img info /opt/test-qcow2 
image: /opt/test-qcow2
file format: qcow2
virtual size: 3.0G (3221225472 bytes)
disk size: 260K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    
[root@kvm-01 ~]# qemu-img resize /opt/test-qcow2 10G
Image resized.
[root@kvm-01 ~]# qemu-img info /opt/test-qcow2 
image: /opt/test-qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 200K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    
# raw 磁盘扩容
[root@kvm-01 ~]# qemu-img resize /opt/test-raw 10G
Image resized.
[root@kvm-01 ~]# qemu-img info /opt/test-raw 
image: /opt/test-raw
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 0

# convert 转换磁盘格式,将raw格式转为为qcow2格式,先关闭虚拟机
[root@kvm-01 ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 4     centos7-test-7-14              running
 -     centos7-test-qcow2             shut off

[root@kvm-01 ~]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off
 -     centos7-test-qcow2             shut off

[root@kvm-01 ~]# time qemu-img convert -f raw -O qcow2 /opt/centos7-test-7-14.raw /opt/centos7-test-7-14.raw-qcow2

real    0m12.599s
user    0m0.064s
sys    0m1.897s

# 如果想使用转为后的磁盘,修改虚拟机配置文件
[root@kvm-01 ~]# virsh edit centos7-test-7-14
      
      

[root@kvm-01 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

[root@kvm-01 ~]# ll -h /opt/
total 4.9G
-rw------- 1 root root  10G Jul 14 04:02 centos7-test-7-14.raw
-rw-r--r-- 1 qemu qemu 1.4G Jul 14 04:09 centos7-test-7-14.raw-qcow2

[root@kvm-01 ~]# virsh dumpxml centos7-test-7-14 | grep  qcow2
      
      

【9】创建快照-内部快照

  • raw格式不支持快照
  • 创建的快照都在磁盘文件中,属于内部快照
[root@kvm-01 ~]# virsh snapshot-create centos7-test-7-14 
Domain snapshot 1689322674 created

[root@kvm-01 ~]# qemu-img info /opt/centos7-test-7-14.raw-qcow2 
image: /opt/centos7-test-7-14.raw-qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 3.3G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         1689322674             222M 2023-07-14 04:17:54   00:09:03.995
Format specific information:
    compat: 1.1
    lazy refcounts: false

【10】查看虚拟机中的快照

[root@kvm-01 ~]# virsh snapshot-list centos7-test-7-14 
 Name                 Creation Time             State
------------------------------------------------------------
 1689322674           2023-07-14 04:17:54 -0400 running

【11】创建快照并起名称

[root@kvm-01 ~]# virsh snapshot-create-as centos7-test-7-14 --name initialize
Domain snapshot initialize created
[root@kvm-01 ~]# virsh snapshot-list centos7-test-7-14 
 Name                 Creation Time             State
------------------------------------------------------------
 1689322674           2023-07-14 04:17:54 -0400 running
 initialize           2023-07-14 04:26:37 -0400 running

【12】删除快照

[root@kvm-01 ~]# virsh snapshot-delete centos7-test-7-14 --snapshotname initialize
Domain snapshot initialize deleted

[root@kvm-01 ~]# virsh snapshot-list centos7-test-7-14 
 Name                 Creation Time             State
------------------------------------------------------------
 1689322674           2023-07-14 04:17:54 -0400 running

【13】还原快照

[root@kvm-01 ~]# virsh console centos7-test-7-14 
Connected to domain centos7-test-7-14
Escape character is ^]

[root@localhost ~]# ls /tmp/
ks-script-GjIS1d
systemd-private-ac9445ab0ec04d66b6b67d4c2828d3f6-chronyd.service-ZFYI3F
yum.log
[root@localhost ~]# rm -rf /tmp/*

[root@kvm-01 ~]# virsh snapshot-revert centos7-test-7-14 --snapshotname 1689322674

[root@kvm-01 ~]# virsh console centos7-test-7-14 
Connected to domain centos7-test-7-14
Escape character is ^]

[root@localhost ~]# ls /tmp/
ks-script-GjIS1d
systemd-private-ac9445ab0ec04d66b6b67d4c2828d3f6-chronyd.service-ZFYI3F
yum.log

【14】查看快照中的配置情况

[root@kvm-01 ~]# virsh snapshot-current centos7-test-7-14

  1689322674
  running
  1689322674
  
  
    
    
  
  ..........

【15】快照在宿主机的存放路径

[root@kvm-01 ~]# ls /var/lib/libvirt/qemu/snapshot/centos7-test-7-14/
1689322674.xml

【16】编辑、修改快照中的配置

[root@kvm-01 ~]# virsh snapshot-edit centos7-test-7-14 1689322674
Snapshot 1689322674 XML configuration not changed.

【17】克隆虚拟机 - 完整克隆

  • 自动克隆
## 第一种,不指定磁盘文件的存放路径,自动
[root@kvm-01 ~]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 ~]# virt-clone -o centos7-test-7-14 -n centos7-test-7-14-clone --auto-clone
Allocating 'centos7-test-7-14.raw-qcow2-clone'                                                      |  10 GB  00:00:10     

Clone 'centos7-test-7-14-clone' created successfully.

## --auto 表示克隆的虚拟机磁盘文件会和源虚拟机的磁盘文件存放相同路径
[root@kvm-01 ~]# ls /opt/
centos7-test-7-14.raw        centos7-test-7-14.raw-qcow2-clone  CentOS-7-x86_64-Minimal-1810.iso  test-raw
centos7-test-7-14.raw-qcow2  centos7-test-qcow2.qcow2           test-qcow2                        test-raw-qcow2
[root@kvm-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off
 -     centos7-test-7-14-clone        shut off
 -     centos7-test-qcow2             shut off

## 第二种,指定磁盘文件的存放路径
[root@kvm-01 ~]# virt-clone -o centos7-test-7-14 -n centos7-test-7-14-clone --file /opt/centos7-test-7-14-clone-qcow2
Allocating 'centos7-test-7-14-clone-qcow2'                                                            |  10 GB  00:00:04     

Clone 'centos7-test-7-14-clone' created successfully.
  • 手动克隆
[root@kvm-01 ~]# qemu-img convert -f qcow2 -O qcow2 -c /opt/centos7-test-7-14.raw-qcow2 /opt/centos7-test-7-14.raw-qcow2-02

# 生成配置文件
[root@kvm-01 ~]# virsh dumpxml centos7-test-7-14 > centos7-test-7-14-clone.xml

  centos7-test-7-14-clone
  ## 删除UUID,会自动生成
  1048576
  ## 删除mac地址
    
  
# 导入
[root@kvm-01 ~]# virsh define centos7-test-7-14-clone.xml 
Domain centos7-test-7-14-clone defined from centos7-test-7-14-clone.xml

[root@kvm-01 ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off
 -     centos7-test-7-14-clone        shut off

【18】克隆虚拟机 - 连接克隆

[root@kvm-01 ~]# qemu-img create -f qcow2 -b /opt/centos7-test-7-14.raw-qcow2 /opt/centos7-test-7-14.raw-qcow2-clone
Formatting '/opt/centos7-test-7-14.raw-qcow2-clone', fmt=qcow2 size=10737418240 backing_file='/opt/centos7-test-7-14.raw-qcow2' encryption=off cluster_size=65536 lazy_refcounts=off

[root@kvm-01 ~]# ll -h /opt/
total 2.5G
-rw-r--r-- 1 root root 1.8G Jul 14 05:21 centos7-test-7-14.raw-qcow2
-rw-r--r-- 1 root root 193K Jul 14 05:32 centos7-test-7-14.raw-qcow2-clone
-rw-r--r-- 1 qemu qemu 918M Jul 13 08:42 CentOS-7-x86_64-Minimal-1810.iso

[root@kvm-01 ~]# qemu-img info /opt/centos7-test-7-14.raw-qcow2-clone 
image: /opt/centos7-test-7-14.raw-qcow2-clone
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
backing file: /opt/centos7-test-7-14.raw-qcow2  # 引用的源磁盘文件
Format specific information:
    compat: 1.1
    lazy refcounts: false

# 生成配置文件
[root@kvm-01 ~]# virsh dumpxml centos7-test-7-14 > centos7-test-7-14-clone.xml

  centos7-test-7-14-clone
  ## 删除UUID,会自动生成
  1048576
  ## 删除mac地址
    
  
# 导入
[root@kvm-01 ~]# vim centos7-test-7-14-clone.xml 
[root@kvm-01 ~]# virsh define centos7-test-7-14-clone.xml 
Domain centos7-test-7-14-clone defined from centos7-test-7-14-clone.xml

[root@kvm-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off
 -     centos7-test-7-14-clone        shut off

【19】创建桥接网卡

[root@kvm-01 ~]# virsh start centos7-test-7-14
Domain centos7-test-7-14 started

[root@kvm-01 ~]# virsh start centos7-test-7-14-clone 
Domain centos7-test-7-14-clone started

[root@kvm-01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.31
NETMASK=255.255.255.0
GATEWAY=10.0.0.254
DNS1=114.114.114.114
DNS2=202.106.0.10

[root@kvm-01 ~]# systemctl stop NetworkManager
[root@kvm-01 ~]# systemctl disable NetworkManager

## 创建桥接网卡的命令,会多出一个br0的网卡
[root@kvm-01 ~]# virsh iface-bridge eth0 br0
Created bridge br0 with attached device eth0
Bridge interface br0 started

## 取消桥接网卡的命令 
[root@kvm-01 ~]# virsh iface-unbridge br0
Device eth0 un-attached from bridge br0
Interface eth0 started

【20】创建新的虚拟机的网络模式使用桥接模式

[root@kvm-01 ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7-bridge-qcow2 --memory 1024 --vcpus 1 --disk /opt/centos7-bridge-qcow2,format=qcow2,size=10 --cdrom /opt/CentOS-7-x86_64-Minimal-1810.iso --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

Starting install...
Allocating 'centos7-bridge-qcow2'                                                                              |  10 GB  00:00:00     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

【21】将现有的虚拟机更改为桥接模式 

  • 先将虚拟机关机
[root@kvm-01 ~]# virsh shutdown centos7-test-7-14
Domain centos7-test-7-14 is being shutdown

 [root@kvm-01 ~]# virsh edit centos7-test-7-14
    
      
      

 KVM-虚拟机【安装与基础使用】_第17张图片

# 启动测试、验证
[root@kvm-01 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

[root@kvm-01 ~]# virsh console centos7-test-7-14
Connected to domain centos7-test-7-14
Escape character is ^]

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

localhost login: root
Password: 
Last login: Fri Jul 14 19:25:30 on ttyS0
[root@localhost ~]# ip a

KVM-虚拟机【安装与基础使用】_第18张图片KVM-虚拟机【安装与基础使用】_第19张图片

KVM-虚拟机【安装与基础使用】_第20张图片

KVM-虚拟机【安装与基础使用】_第21张图片

【22】宿主机的网卡

[root@kvm-01 ~]# brctl show
bridge name    bridge id        STP enabled    interfaces
br0        8000.000c29f65db5    yes        eth0
                            vnet0
                            vnet1
virbr0        8000.525400ae7a5b    yes        virbr0-nic

# nat模式
[root@kvm-01 ~]# virsh dumpxml centos7-test-7-14 | grep -i vnet
      

【23】为什么创建的虚拟机,默认的网络是nat,网段会是122呢?

  • 系统默认定义的网络default
[root@kvm-01 ~]# virsh net-list 
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

[root@kvm-01 ~]# virsh net-edit default

KVM-虚拟机【安装与基础使用】_第22张图片

【24】冷添加网卡 - 备份配置文件或者 edit

[root@kvm-01 ~]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 ~]# virsh dumpxml centos7-test-7-14 > centos7-test-7-14-br0-net.xml
[root@kvm-01 ~]# vim centos7-test-7-14-br0-net.xml 
....添加
    
      
      
    
    
[root@kvm-01 ~]# virsh snapshot-delete centos7-test-7-14 --snapshotname 1689322674
Domain snapshot 1689322674 deleted

# 有快照先删快照
[root@kvm-01 ~]# virsh undefine centos7-test-7-14 
Domain centos7-test-7-14 has been undefined

[root@kvm-01 ~]# virsh define centos7-test-7-14-br0-net.xml 
Domain centos7-test-7-14 defined from centos7-test-7-14-br0-net.xml

[root@kvm-01 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

[root@kvm-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 17    centos7-test-7-14              running
 -     centos7-bridge-qcow2           shut off

# 查看虚拟机的网卡
[root@kvm-01 ~]# virsh domiflist centos7-test-7-14 
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:a5:7f:b9
vnet1      network    default    virtio      52:54:00:b7:1d:f3

KVM-虚拟机【安装与基础使用】_第23张图片

【25】冷添加硬盘

[root@kvm-01 ~]# qemu-img create -f qcow2 /opt/test.qcow2 1G
Formatting '/opt/test-qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off 

[root@kvm-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 17    centos7-test-7-14              running
 -     centos7-bridge-qcow2           shut off
 
[root@kvm-01 ~]# virsh edit centos7-test-7-14
...添加
    
      
      
      
    

# 运行中的虚拟机不会直接生效,重启
[root@kvm-01 ~]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

# 查看是否成功
[root@kvm-01 ~]# virsh domblklist centos7-test-7-14 
Target     Source
------------------------------------------------
vda        /opt/centos7-test-7-14.raw-qcow2
vdb        /opt/test.qcow2
hda        -

[root@localhost ~]# fdisk -l
  

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b436c

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20971519    10484736   83  Linux

Disk /dev/vdb: 1073 MB, 1073741824 bytes, 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

【26】热添加硬盘

  • 热添加的硬盘,虚拟机重启后,就没有,需要配置永久生效
[root@kvm-01 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

# 查看虚拟机有几块网卡 
[root@kvm-01 ~]# virsh domiflist centos7-test-7-14 
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:a5:7f:b9

# 查看虚拟机有几块硬盘
[root@kvm-01 ~]# virsh domblklist centos7-test-7-14 
Target     Source
------------------------------------------------
vda        /opt/centos7-test-7-14.raw-qcow2
hda        -

# 创建硬盘
[root@kvm-01 ~]# qemu-img create -f qcow2 /opt/add_centos7-test-7-14.qcow2 1G
Formatting '/opt/add_centos7-test-7-14.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off 

# 热添加硬盘,这种方式不是永久生效,重启后硬盘就掉了
[root@kvm-01 ~]# virsh attach-disk centos7-test-7-14 /opt/add_centos7-test-7-14.qcow2 vdb --subdriver qcow2
Disk attached successfully

[root@kvm-01 ~]# virsh domblklist centos7-test-7-14 
Target     Source
------------------------------------------------
vda        /opt/centos7-test-7-14.raw-qcow2
vdb        /opt/add_centos7-test-7-14.qcow2
hda        -

# 卸载硬盘
[root@kvm-01 ~]# virsh detach-disk centos7-test-7-14 vdb
Disk detached successfully

[root@kvm-01 ~]# virsh domblklist centos7-test-7-14 
Target     Source
------------------------------------------------
vda        /opt/centos7-test-7-14.raw-qcow2

hda        -

# 热添加硬盘,永久生效,先添加,在配置永久生效
[root@kvm-01 ~]# virsh attach-disk centos7-test-7-14 /opt/add_centos7-test-7-14.qcow2 vdb --subdriver qcow2
Disk attached successfully

[root@kvm-01 ~]# virsh attach-disk centos7-test-7-14 /opt/add_centos7-test-7-14.qcow2 vdb --subdriver qcow2 --config 
Disk attached successfully

[root@kvm-01 ~]# virsh domblklist centos7-test-7-14 
Target     Source
------------------------------------------------
vda        /opt/centos7-test-7-14.raw-qcow2
vdb        /opt/add_centos7-test-7-14.qcow2
hda        -

[root@kvm-01 ~]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

[root@kvm-01 ~]# virsh domblklist centos7-test-7-14 
Target     Source
------------------------------------------------
vda        /opt/centos7-test-7-14.raw-qcow2
vdb        /opt/add_centos7-test-7-14.qcow2
hda        -

【27】硬盘格式化,挂载使用

[root@10 ~]# mkfs.xfs /dev/vdb 
meta-data=/dev/vdb               isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@10 ~]# mount /dev/vdb /mnt/
[root@10 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  1.3G  8.8G  13% /
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  6.7M  489M   2% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb       1014M   33M  982M   4% /mnt

[root@10 ~]# dd if=/dev/zero bs=5M count=10 of=/mnt/test
10+0 records in
10+0 records out
52428800 bytes (52 MB) copied, 0.0274432 s, 1.9 GB/s
[root@10 ~]# du -smh /mnt/test 
50M    /mnt/test

【28】磁盘扩容

# 虚拟机中卸载挂载点
[root@10 ~]# umount /mnt/

# 宿主机中卸载硬盘
[root@kvm-01 ~]# virsh detach-disk centos7-test-7-14 vdb
Disk detached successfully

# 扩容
[root@kvm-01 ~]# qemu-img resize /opt/add_centos7-test-7-14.qcow2 5G
Image resized.
[root@kvm-01 ~]# qemu-img info /opt/add_centos7-test-7-14.qcow2 
image: /opt/add_centos7-test-7-14.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 993M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

# 不需要再执行永久生效,因为之前的配置文件已经修改过了        
[root@kvm-01 ~]# virsh attach-disk centos7-test-7-14 /opt/add_centos7-test-7-14.qcow2 vdb --subdriver qcow2
Disk attached successfully

[root@kvm-01 ~]# virsh domblklist centos7-test-7-14 
Target     Source
------------------------------------------------
vda        /opt/centos7-test-7-14.raw-qcow2
vdb        /opt/add_centos7-test-7-14.qcow2
hda        -

[root@kvm-01 ~]# virsh console centos7-test-7-14 
Connected to domain centos7-test-7-14
Escape character is ^]

[root@10 ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b436c

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20971519    10484736   83  Linux

Disk /dev/vdb: 5368 MB, 5368709120 bytes, 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

# 挂载后,我们发现磁盘空间并没有改变,是应为我们还没有刷新磁盘的inode
[root@10 ~]# mount /dev/vdb /mnt/
[root@10 ~]# ls /mnt/
test
[root@10 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        10G  1.3G  8.8G  13% /
devtmpfs        485M     0  485M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M  6.7M  489M   2% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/0
/dev/vdb       1014M   83M  932M   9% /mnt

KVM-虚拟机【安装与基础使用】_第24张图片

# 刷新inode
[root@10 ~]# xfs_growfs /dev/vdb   # 如果文件系统是ext2\ext3\ext4的使用的是resize2fx /dev/vdb
meta-data=/dev/vdb               isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=262144, 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 262144 to 1310720

KVM-虚拟机【安装与基础使用】_第25张图片

【29】热添加网卡

# 查看虚拟机有几块网卡
[root@kvm-01 ~]# virsh domiflist centos7-test-7-14 
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:a5:7f:b9

# 添加网卡
[root@kvm-01 ~]# virsh attach-interface centos7-test-7-14 bridge br0
Interface attached successfully

[root@kvm-01 ~]# virsh domiflist centos7-test-7-14 
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:a5:7f:b9
vnet1      bridge     br0        rtl8139     52:54:00:7b:d5:f4

KVM-虚拟机【安装与基础使用】_第26张图片

KVM-虚拟机【安装与基础使用】_第27张图片

# 指定添加的网卡类型
[root@kvm-01 ~]# virsh attach-interface centos7-test-7-14 bridge br0 --model virtio
Interface attached successfully

# 永久生效
[root@kvm-01 ~]# virsh attach-interface centos7-test-7-14 bridge br0 --model virtio --config 
Interface attached successfully

 
[root@kvm-01 ~]# virsh domiflist centos7-test-7-14 
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:a5:7f:b9
vnet1      bridge     br0        rtl8139     52:54:00:7b:d5:f4
vnet2      bridge     br0        virtio      52:54:00:91:f8:2c

KVM-虚拟机【安装与基础使用】_第28张图片

KVM-虚拟机【安装与基础使用】_第29张图片

# 卸载网卡
[root@kvm-01 ~]# virsh detach-interface centos7-test-7-14 --type bridge --mac 52:54:00:4e:2f:03 --config 
Interface detached successfully


[root@kvm-01 ~]# virsh domiflist centos7-test-7-14 
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:a5:7f:b9

【30】热调整内存大小

KVM-虚拟机【安装与基础使用】_第30张图片

[root@kvm-01 ~]# virsh setmem centos7-test-7-14 512M

[root@kvm-01 ~]# virsh setmem centos7-test-7-14 1G

KVM-虚拟机【安装与基础使用】_第31张图片

## 调整最大内存值,先关机
[root@kvm-01 ~]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 ~]# virsh list --all 
 Id    Name                           State
----------------------------------------------------
 -     centos7-bridge-qcow2           shut off
 -     centos7-test-7-14              shut off

[root@kvm-01 ~]# virsh setmaxmem centos7-test-7-14 2048M

KVM-虚拟机【安装与基础使用】_第32张图片

[root@kvm-01 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

KVM-虚拟机【安装与基础使用】_第33张图片

[root@kvm-01 ~]# virsh setmem centos7-test-7-14 1G

KVM-虚拟机【安装与基础使用】_第34张图片

  • 这样调整内存并不是永久生效的,如果我们在关机,就会还原

KVM-虚拟机【安装与基础使用】_第35张图片

  • 永久生效 --config
[root@kvm-01 ~]# virsh setmem centos7-test-7-14 1G --config 

KVM-虚拟机【安装与基础使用】_第36张图片

【31】热添加CPU

KVM-虚拟机【安装与基础使用】_第37张图片

# 调整虚拟机的CUP最大值,因为我们创建虚拟机的时候,并没指定,默认会和CUP的数量相同
[root@kvm-01 ~]# virsh shutdown centos7-test-7-14 
Domain centos7-test-7-14 is being shutdown

[root@kvm-01 ~]# virsh setvcpus centos7-test-7-14 --maximum 10 --config 

KVM-虚拟机【安装与基础使用】_第38张图片

## 开机,查看CPU
[root@kvm-01 ~]# virsh setvcpus centos7-test-7-14 4

KVM-虚拟机【安装与基础使用】_第39张图片

【32】冷迁移技术

  • kvm-01操作
[root@kvm-01 ~]# virsh shutdown centos7-test-7-14

# 查看磁盘文件位置
[root@kvm-01 ~]# virsh domblklist centos7-test-7-14 
Target     Source
------------------------------------------------
vda        /opt/centos7-test-7-14.raw-qcow2
hda        -

# 将centos7-test-7-14 迁移到 kvm-02中
[root@kvm-01 ~]# scp -rp /opt/centos7-test-7-14.raw-qcow2 [email protected]:/opt

[root@kvm-01 ~]# virsh dumpxml centos7-test-7-14 > centos7-test-7-14.xml
[root@kvm-01 ~]# scp -rp centos7-test-7-14.xml [email protected]:/opt
  • kvm-02操作
[root@kvm-02 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.32
NETMASK=255.255.255.0
GATEWAY=10.0.0.254
DNS1=114.114.114.114
DNS2=202.106.0.10

[root@kvm-02 ~]# ls /opt/
centos7-test-7-14.raw-qcow2  centos7-test-7-14.xml

# 导入
[root@kvm-02 ~]# virsh define /opt/centos7-test-7-14.xml 
Domain centos7-test-7-14 defined from /opt/centos7-test-7-14.xml

[root@kvm-02 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off

# 创建br0网卡,虚拟机配置的是br0
[root@kvm-02 ~]# virsh iface-bridge eth0 br0
Created bridge br0 with attached device eth0
Bridge interface br0 started

# 启动,如果迁移的是链接克隆的虚拟机还需要将母体迁移过来
[root@kvm-02 ~]# virsh start centos7-test-7-14 
Domain centos7-test-7-14 started

[root@kvm-02 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     centos7-test-7-14              running

[root@kvm-02 ~]# virsh domiflist centos7-test-7-14 
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      bridge     br0        virtio      52:54:00:a5:7f:b9
  • 注:如果迁移的宿主机的CPU跟源宿主机的CPU不相同,虚拟机会起不来,报CPU特性不相同

【33】热迁移技术

kvm-01

10.0.0.31

2G

创建br0

kvm、nfs

开启虚拟化

kvm-02

10.0.0.32

2G

创建br0

kvm、nfs

开启虚拟化

nfs-01

10.0.0.41

1G

nfs

nfs-01 

[root@nfs-01 ~]# yum -y install nfs-utils.x86_64 rpcbind.x86_64

[root@nfs-01 ~]# systemctl start nfs

[root@nfs-01 ~]# vim /etc/exports
/kvm/disk 10.0.0.0/24(rw,sync,no_root_squash,no_all_squash)

[root@nfs-01 ~]# mkdir -p /kvm/disk

[root@nfs-01 ~]# showmount -e
Export list for nfs-01:
/kvm/disk 10.0.0.0/24

kvm-01 、 kvm-02

[root@kvm-01 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.31 kvm-01
10.0.0.32 kvm-02

[root@kvm-01 ~]# scp -rp /etc/hosts [email protected]:/etc/hosts
[email protected]'s password: 
hosts                                                                             100%  192     0.2KB/s   00:00 

[root@kvm-01 ~]# yum -y install nfs-utils
[root@kvm-01 ~]# showmount -e 10.0.0.41
Export list for 10.0.0.41:
/kvm/disk 10.0.0.0/24

# 将磁盘文件放置到nfs服务共享目录中
[root@kvm-01 ~]# scp -rp /opt/* [email protected]:/kvm/disk

# mount 挂载共享目录
[root@kvm-01 ~]# mount -t nfs 10.0.0.41:/kvm/disk /opt/
[root@kvm-02 ~]# mount -t nfs 10.0.0.41:/kvm/disk /opt/
[root@kvm-02 ~]# ls /opt/
add_centos7-test-7-14.qcow2  centos7-bridge-qcow2  centos7-test-7-14.raw-qcow2  CentOS-7-x86_64-Minimal-1810.iso

 磁盘文件共享后,就可以热迁移

[root@kvm-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     centos7-test-7-14              running
 -     centos7-bridge-qcow2           shut off

# 远程连接查看其他宿主机有多少虚拟机
[root@kvm-01 ~]# virsh -c qemu+ssh://[email protected]/system list
[email protected]'s password: 
 Id    Name                           State
----------------------------------------------------

# 迁移正在运行的虚拟机
[root@kvm-01 ~]# virsh migrate centos7-test-7-14 qemu+ssh://[email protected]/system
[email protected]'s password: 
error: Unsafe migration: Migration may lead to data corruption if disks use cache != none or cache != directsync

[root@kvm-01 ~]# virsh migrate centos7-test-7-14 qemu+ssh://[email protected]/system --unsafe 
[email protected]'s password: 

[root@kvm-01 ~]# virsh -c qemu+ssh://[email protected]/system list
[email protected]'s password: 
 Id    Name                           State
----------------------------------------------------
 2     centos7-test-7-14              running

# 注意,这样只是将进程迁移过去了,并没有迁移配置文件,虚拟机关闭后会消失,不是永久迁移,使用以下
[root@kvm-01 ~]# virsh migrate centos7-test-7-14 qemu+ssh://[email protected]/system --unsafe --verbose --persistent --undefinesource 
[email protected]'s password: 
Migration: [100 %]

[root@kvm-01 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-bridge-qcow2           shut off

# 我们发现本地就没有centos7-test-7-14了,被迁移到了10.0.0.32中,永久
[root@kvm-01 ~]# virsh -c qemu+ssh://[email protected]/system list
[email protected]'s password: 
 Id    Name                           State
----------------------------------------------------
 3     centos7-test-7-14              running

[root@kvm-02 ~]# virsh destroy centos7-test-7-14 
Domain centos7-test-7-14 destroyed

[root@kvm-02 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-test-7-14              shut off

你可能感兴趣的:(KVM-虚拟机,linux,服务器,运维)