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

news2024/12/28 6:05:50

目录

【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虚拟化

【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

 

 

【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

# 关机虚拟机
[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
      <source file='/opt/centos7-test-7-14.raw'/>
      
[root@kvm-01 opt]# virsh start centos7-test 
Domain centos7-test started

# 修改cpu的核数,很多时候修改了配置需要重启生效,reboot不管用
[root@kvm-01 opt]# virsh edit centos7-test
  <vcpu placement='static'>2</vcpu>
  
[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

# 重命名,不能处于运行期间修改
[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 root@192.168.122.75
root@192.168.122.75'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 root@192.168.122.75
root@192.168.122.75'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 root@192.168.122.75
root@192.168.122.75'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 root@192.168.122.75
root@192.168.122.75'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 root@192.168.122.75
root@192.168.122.75'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
      <driver name='qemu' type='qcow2'/>
      <source file='/opt/centos7-test-7-14.raw-qcow2'/>

[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
      <driver name='qemu' type='qcow2'/>
      <source file='/opt/centos7-test-7-14.raw-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
<domainsnapshot>
  <name>1689322674</name>
  <state>running</state>
  <creationTime>1689322674</creationTime>
  <memory snapshot='internal'/>
  <disks>
    <disk name='vda' snapshot='internal'/>
    <disk name='hda' snapshot='no'/>
  </disks>
  ..........

【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
<domain type='kvm'>
  <name>centos7-test-7-14-clone</name>
  ## 删除UUID,会自动生成
  <memory unit='KiB'>1048576</memory>
  ## 删除mac地址
  <source file='/opt/centos7-test-7-14.raw-qcow2-clone'/>  
  
# 导入
[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
<domain type='kvm'>
  <name>centos7-test-7-14-clone</name>
  ## 删除UUID,会自动生成
  <memory unit='KiB'>1048576</memory>
  ## 删除mac地址
  <source file='/opt/centos7-test-7-14.raw-qcow2-clone'/>  
  
# 导入
[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
    <interface type='bridge'>
      <mac address='52:54:00:a5:7f:b9'/>
      <source bridge='br0'/>

 

# 启动测试、验证
[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

【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
      <target dev='vnet0'/>

【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

【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 
....添加
    <interface type='network'>
      <source network='default'/>
      <model type='virtio'/>
    </interface>
    
[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

【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
...添加
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/opt/test.qcow2'/>
      <target dev='vdb' bus='virtio'/>
    </disk>

# 运行中的虚拟机不会直接生效,重启
[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

# 刷新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

【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

# 指定添加的网卡类型
[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

# 卸载网卡
[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】热调整内存大小

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

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

## 调整最大内存值,先关机
[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

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

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

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

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

【31】热添加CPU

# 调整虚拟机的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 

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

【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 root@10.0.0.32:/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 root@10.0.0.32:/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 root@10.0.0.32:/etc/hosts
root@10.0.0.32'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/* root@10.0.0.41:/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://root@10.0.0.32/system list
root@10.0.0.32's password: 
 Id    Name                           State
----------------------------------------------------

# 迁移正在运行的虚拟机
[root@kvm-01 ~]# virsh migrate centos7-test-7-14 qemu+ssh://root@10.0.0.32/system
root@10.0.0.32'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://root@10.0.0.32/system --unsafe 
root@10.0.0.32's password: 

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/758227.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

java学习路程之篇七、知识点、赋值运算符、关系运算符、逻辑运算符、三元运算符、运算符优先级

文章目录 1、赋值运算符2、关系运算符3、逻辑运算符4、三元运算符5、运算符优先级 1、赋值运算符 2、关系运算符 3、逻辑运算符 4、三元运算符 5、运算符优先级

Elasticsearch和MySQL之间的数据同步问题

前言&#xff1a;大家好&#xff0c;我是小威&#xff0c;24届毕业生&#xff0c;在一家满意的公司实习。本篇文章参考网上的课程&#xff0c;介绍Elasticsearch和MySQL之间的数据同步问题。 如果文章有什么需要改进的地方还请大佬不吝赐教&#x1f44f;&#x1f44f;。 小威在…

vue路由接口转发

目录 一、nginx的路由转发 二、vue路由转发 三、两者路由转发的区别 vue的路由和nginx的路由有许多相似之处&#xff0c;也有一些不同的地方。 两者都有接口转发的功能&#xff0c;但是具体转发的方式会有一些区别。 一、nginx的路由转发 nginx的路由是这样的&#xff1a;…

C# Modbus通信从入门到精通(3)——Modbus RTU(0x01功能码)

1、01(0x01)读线圈 使用该功能码能从远程地址中读取1到2000个线圈的状态,每个线圈的状态只能是0或者1,读取的线圈数据由主站读取时指定。 2、发送报文格式 更详细的格式如下: 从站地址+功能码+起始地址高位+起始地址低位+线圈数量高位+线圈数量低位+CRC,一共8个字节,其…

JS-变量

文章目录 1.变量概述1.1 什么是变量1.2 变量在内存中的存储 2.变量的使用2.1 声明变量2.2 赋值2.3 变量的初始化 3.变量语法扩展3.1 更新变量3.2 同时声明多个变量3.3 声明变量特殊情况 4.变量命名规范 1.变量概述 1.1 什么是变量 变量是用于存放数据的容器。 我们通过 变量名…

gulimall-性能监控-压力测试

性能监控与压力测试 前言一、性能监控1.1 jvm 内存模型1.2 jvisualvm 作用1.3 监控指标 二、压力测试2.1 概念2.2 性能指标2.3 JMeter 压测工具 前言 本文继续记录B站谷粒商城项目视频 P141-150 的内容&#xff0c;做到知识点的梳理和总结的作用。 一、性能监控 1.1 jvm 内存…

AcWing 106. 动态中位数—对顶堆

问题链接 AcWing 106. 动态中位数 问题描述 分析 推荐b站董晓算法视频讲解对顶堆 这道题应该用树状数组、平衡树也能解决&#xff0c;这里用对顶堆来做&#xff0c;对顶堆能够用维护第K位置的数&#xff0c;K是固定的&#xff0c;在这道题中&#xff0c;维护两个堆&#xff…

posix ipc之匿名信号量

note 使用信号量进行进程间通信时&#xff0c;信号量要放置在共享内存上&#xff1b;若是线程间通信&#xff0c;放堆上或全局变量区就行 code // 使用信号量进行进程间通信时&#xff0c;信号量要放置在共享内存上&#xff1b;若是线程间通信&#xff0c;放堆上或全局变量区…

Python批量实现Word、EXCLE、PPT转PDF文件

一、绪论背景 在日常办公和文档处理中&#xff0c;有时我们需要将多个Word文档、Excel表格或PPT演示文稿转换为PDF文件。将文档转换为PDF格式的好处是它可以保留文档的布局和格式&#xff0c;并且可以在不同平台上进行方便的查看和共享。 本篇博文将介绍如何使用Python编程语言…

0714两小时配置完服务器环境

之前那个222的服务器&#xff0c;不知道下载的什么东西&#xff0c;包含了一个挖矿程序&#xff0c;然后就停掉了它。 重新配置一个环境在220服务器上。 1、配置anaconda :(直接下载的这个博客的那个版本&#xff09; https://blog.csdn.net/m0_46926492/article/details/1293…

【分布式】ceph存储

目录 一、存储基础单机存储设备单机存储的问题商业存储解决方案 二、 分布式存储 &#xff08;软件定义的存储 SDS&#xff09;分布式存储的类型Ceph 优势Ceph 架构Ceph 核心组件Pool中数据保存方式支持两种类型&#xff1a;Pool、PG 和 OSD 的关系OSD 存储后端BlueStore 的主要…

本地生活直播,和电商直播有什么不一样?

直播正在成为零售业的标配&#xff0c;当下最新的一条赛道是“本地生活直播”。 &#xff08;商家开始在美团等平台进行本地生活直播。摄影&#xff1a;李崧稷&#xff09; 今年618&#xff0c;在老牌电商平台拉着无数网店&#xff0c;拼尽全力想要堆高销量的时候&#xff0c;一…

k8s之Pod详解

目录 一、Pod基础概念1.1 在Kubrenetes集群中Pod有如下两种使用方式1.2 kubernetes中的pause容器主要为每个容器提供以下功能1.3 Kubernetes设计这样的Pod概念和特殊组成结构有什么用意&#xff1f; 二、Pod分类三、容器的分类3.1 pause基础容器&#xff08;infrastructure con…

[SCTF2019]creakme

前言 主要是SEH解密然后AES加密 分析 SEH main函数中获取到程序句柄也就是首地址&#xff0c;然后进入图中函数找到.SCTF段地址后产生异常&#xff0c;在异常中进行代码自解密 装载的SEH异常处理函数 因为在异常中有调试判断&#xff0c;所以处于调试状态中将异常抛给调试者…

玩转内存修改函数——【C语言】

在上篇博客中&#xff0c;我们学习了字符串函数&#xff0c;针对一些字符串我们可以做出一系列操作。接下来我们将学习一些内存修改函数&#xff08;#inlcude<string.h>&#xff09;&#xff0c;让我们一起走进mempy、memmove、memcmp函数中。 目录 mempcy函数​编辑 m…

leetcode刷题(轮转数组、买股票的最佳时机、买卖股票的最佳时机2、跳跃游戏、跳跃游戏2、最大子序列交替和、交替数字和、下降路径最小和)

目录 1、轮转数组 2、买卖股票的最佳时机 3、买卖股票的最佳时机② 4、跳跃游戏 5、跳跃游戏2 6、最大子序列交替和 7、交替数字和 8、下降路径最小和 1、轮转数组 class Solution():def rotate(self, nums, k):def reverse(i, j): #逆置数组while i < j:nums[i]…

Oracle Apex制作一个简单的交互式报表页面&页设计

目录 创建好一个菜单表 简单查询一下&#xff1a; 创建交互式报表 工作报表展示​编辑 菜单报表展示​编辑 页设计器 改变布局样式 修改标签内容 添加验证&#xff06;更改必填项 注意&#xff01; 在主页进行跳转 页跳转按钮 主页可以跳转到指定报表 面包屑 报表也可…

Clickhouse数据一致性和物化视图

Clickhouse 一、数据一致性的保证1.通过Group by对数据去重2.通过 FINAL 查询 二、物化视图1.物化视图与普通视图的区别2.优缺点3.基本语法 三、MaterializeMySQL引擎1.特点2.使用细则 四、常见问题排除分布式 DDL某数据节点的副本不执行数据副本表和数据不一致副本节点全量恢复…

【uniapp开发小程序】设置全屏的开屏广告、长按识别图片、点击跳转通话 拨打电话

设置全屏的开屏广告需求实现 效果图&#xff1a; 点击跳转其他小程序&#xff1a; uni.navigateToMiniProgram() 官方文档&#xff1a;uni.navigateToMiniProgram(OBJECT) | uni-app官网 // 示例代码 uni.navigateToMiniProgram({appId: ,path: pages/index/index?id123,ext…

Cilium核心技术-eBPF XDPTC介绍

eBPF 是一项革命性技术&#xff0c;它能在内核中运行沙箱程序&#xff08;sandbox programs&#xff09;&#xff0c; 而无需修改内核源码或者加载内核模块。 eBPF的一个重要特性是能够使用高级语言(如C)来实现程序。LLVM有一个eBPF后端&#xff0c;用于编辑包含eBPF指令的ELF文…