KVM 虚拟化学习之虚拟磁盘管理 qemu-img

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/AMimiDou_212/article/details/102495606

一、虚拟磁盘简述

1.1 虚拟存储性能的解决方案

  • 问题 :磁盘的I/O 性能一直是虚拟化存储性能的瓶颈。
  • 通过分布式的磁盘I/O 进一步提升存储性能,即多磁盘驱动
  • 通过集中式存储解决方案实现高可用和实时迁移,即SAN/NFS。

1.2 虚拟磁盘存储方案

  • 固定配额存储,即分配多少,占用实际物理磁盘多少。就是在配置时,指定的容量,也会占用配置时指定的大小的主机磁盘容量。
  • 动态扩容方案,即预先指定磁盘大小,随之应用程序、文件等逐渐增大,但增大到最大容量。
  • 差异性存储方案,即只存储变更的数据。

二、qemu-img 命令 – 虚拟磁盘管理

2.1 qemu-img 概述

qemu-img - QEMU disk image utility
qemu-img 是QEMU 磁盘镜像工具。

qemu-img allows you to create, convert and modify images offline. It can handle all image formats supported by QEMU.

qemu-img 允许你在离线(即虚拟机关机状态)下创建、转换和修改镜像文件。它可以处理所有QEMU支持的镜像格式的磁盘镜像。

Warning: Never use qemu-img to modify images in use by a running virtual machine or any other process; this may destroy the image. Also, be aware that querying an image that is being modified by another process may encounter inconsistent state.

注意:切勿使用 qemu-img 修改正在运行的虚拟机或被其他进程使用的镜像,这会摧毁磁盘镜像。另外,请注意,查询一个被其他进程修改的镜像状态可能会不一致。

2.2 qemu-img 功能

操作 功能 使用
creat 创建一个镜像 create [-f fmt] filename [size] 例:create -f qcow2 centos-7.qcow2 10G
check 检查完整性(仅支持"qcow2", "qed" ,"vdi"格式一致性检查) check [-f fmt] filename 例: qemu-img check -f qcow2 /kvm/centos-7.qcow2
convert 镜像格式转换
info 查看镜像信息 info [-f fmt] [–output=ofmt] [–backing-chain] filename
resize 调整镜像大小 resize filename [+/ -]size
rebase 在现有镜像基础上创建新镜像 rebase [-f fmt] [-t cache] [-T src_cache] [-p] [-u] -b backing_file [-F backing_fmt] filename
snapshot 磁盘镜像快照 snapshot [-l , -a snapshot , -c snapshot , -d snapshot ] filename
commit 提交更改 commit [-f fmt] [-t cache] filename

示例:

2.2.1 创建一个镜像 create

[root@localhost kvm]# qemu-img create -f qcow2 -o ? # qcow2 选项
Supported options:    
size             Virtual disk size  # 虚拟磁盘大小
compat           Compatibility level (0.10 or 1.1)
backing_file     File name of a base image #指定基础镜像文件
backing_fmt      Image format of the base image  #设置后端镜像基础镜像格式
encryption       Encrypt the image  # 设置镜像加密
cluster_size     qcow2 cluster size # 设置镜像簇大小,512~2M,默认64KB
preallocation    Preallocation mode (allowed values: off, metadata, falloc, full) #设置镜像文件空间预分配模式
lazy_refcounts   Postpone refcount updates
#创建一个格式为 qcow2 大小10G的虚拟磁盘
[root@localhost ~]# qemu-img create -f qcow2 img2.qcow2 10G
Formatting 'img2.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off

#查看刚创建的虚拟磁盘详细信息
[root@localhost ~]# qemu-img info --output=human img2.qcow2
image: img2.qcow2   
file format: qcow2  #文件格式
virtual size: 10G (10737418240 bytes) # 虚拟磁盘大小
disk size: 196K   #实际占用大小
cluster_size: 65536  #簇默认64KB
Format specific information:
    compat: 1.1
    lazy refcounts: false

在这里插入图片描述

2.2.2 查看虚拟磁盘信息 - info

在这里插入图片描述

2.2.3 检查磁盘镜像完整性(一致性) – check

[root@localhost ~]# qemu-img check img2.qcow2 #img2.qcow2 的完整性
No errors were found on the image.  #检查结果没有错误
Image end offset: 262144

2.2.4 调整磁盘镜像容量 – resize

在这里插入图片描述

注意:

  • 调整虚拟磁盘容量之前,请做好重要数据备份。---- 数据无价
  • 扩容后,需要在客户端使用磁盘管理工具fdisk 、partprobe 工具初始化该分区(建立新分区、格式化并挂载使用)。
  • 缩容,注意在保证虚拟机有足够的空间下进行,否则会发生数据丢失。
  • qcow2 格式的磁盘镜像不支持缩容操作

2.3 磁盘镜像快照管理 – snapshot

2.3.1 快照/检查点简述

  • 磁盘快照:
    • 对数据进行快照,用于虚拟机备份场景
  • 内存快照 – 只能对运行的虚拟机进行拍照
    • 对虚拟机的内存/设备信息进行拍照
    • 适用于快速的恢复、迁移场景
    • 通过 virsh save (qemu migrate to file)实现
    # 内存拍照
    [root@localhost ~]# virsh save CentOS-7  --file save.file
    Domain CentOS-7 saved to save.file
    
    # 恢复内存快照
    [root@localhost ~]# virsh restore save.file
    Domain restored from save.file
    
    
  • 检查点快照
    • 同时保存磁盘快照和内存快照
    • 保存虚拟机的某个时间点,因此,可以将虚拟机恢复到该时间点状态
    • 能够保持数据的一致性
snapshot subcommand:
'snapshot' is the name of the snapshot to create, apply or delete
  '-a' applies a snapshot (revert disk to saved state) # 回滚到指定快照
  '-c' creates a snapshot   # 创建一个快照
  '-d' deletes a snapshot   #删除一个快照
  '-l' lists all snapshots in the given image  # 列出给出镜像的所有快照

2.3.2 示例 快照回滚操作:

  1. 创建磁盘快照
    在这里插入图片描述
  2. 删除数据
  3. 快照回滚,检查数据
[root@localhost ~]# virsh shutdown CentOS-7  # 关闭虚拟机
Domain CentOS-7 is being shutdown
[root@localhost ~]# virsh list --all  #查看虚拟机状态
 Id    Name                           State
----------------------------------------------------
 -     CentOS-7                       shut off
[root@localhost ~]# qemu-img snapshot -a s1  /kvm/centos-7.qcow2 #磁盘快照回滚

注意坑: 进行磁盘快照回滚时需要在虚拟机关闭的状态下进行回滚,否则会破坏磁盘快照。qemu-img 的磁盘快照只支持通过qemu-img 创建的原生qcow2 格式磁盘快照,不支持转换后的qcow2。

猜你喜欢

转载自blog.csdn.net/AMimiDou_212/article/details/102495606
今日推荐