The QCOW2 Image Format

别再纠结了!KVM虚拟化实战:RAW和QCOW2磁盘格式到底怎么选?附qemu-img保姆级操作指南 本文深入探讨KVM虚拟化中RAW和QCOW2磁盘格式的选择策略,通过性能对比、适用场景分析和qemu-img操作指南,帮助用户根据实际需求做出最优决策。QCOW2格式支持快照和压缩等高级功能,而RAW格式则提供更接近物理磁盘的性能表现。 阅读详情


What's QCOW?

QCOW is stands for "Qemu Copy On Write", it's an image format used by the QEMU,

Two versions exist, qcow and qcow2, which use the qcow and qcow2 as the file extensions respectively.


Features:

      compared with the RAW format images , the QCOW2 format has these benefits as below:

1, smaller file size, even though the OS is not support the Hole file(sparse file),

2, support Copy-On-Write,

3, support snap-shot,

4, optional zlib based compression,

5, optional AES encryption .typedef struct


File Header:


/*copy form the Qemu source code
 *directory: qemu/block/qcow2.h
*/

typedef struct QCowHeader {
    uint32_t magic;
    uint32_t version;
    uint64_t backing_file_offset;
    uint32_t backing_file_size;
    uint32_t cluster_bits;
    uint64_t size; /* in bytes */
    uint32_t crypt_method;
    uint32_t l1_size; /* XXX: save number of clusters instead ? */
    uint64_t l1_table_offset;
    uint64_t refcount_table_offset;
    uint32_t refcount_table_clusters;
    uint32_t nb_snapshots;
    uint64_t snapshots_offset;

    /* The following fields are only valid for version >= 3 */
    uint64_t incompatible_features;
    uint64_t compatible_features;
    uint64_t autoclear_features;

    uint32_t refcount_order;
    uint32_t header_length;
} QEMU_PACKED QCowHeader;




The first cluster of a qcow2 image contains the file header:

Byte  0 -  3:   magic,QCOW magic string ("QFI\xfb")

          4 -  7:   version, Version number (valid values are 2 and 3)

          8 - 15:   backing_file_offset

          16 - 19:   backing_file_size

          20 - 23:   cluster_bits

          24 - 31:   size, Virtual disk size in bytes

          32 - 35:   crypt_method
                                0 for no encryption
                                1 for AES encryption
          36 - 39:   L1_size, Number of entries in the active L1 table
          40 - 47:   L1_table_offset,

                              Offset into the image file at which the active L1 table starts. Must be aligned to a cluster boundary.

          48 - 55:   refcount_table_offset

                                      Offset into the image file at which the refcount table
                                      starts. Must be aligned to a cluster boundary.

          56 - 59:   refcount_table_clusters, Number of clusters that the refcount table occupies

          60 - 63:   nb_snapshots, Number of snapshots contained in the image

          64 - 71:   snapshots_offset, Offset into the image file at which the snapshot table starts. Must be aligned to a cluster boundary.


          If the version is 3 or higher, the header has the following additional fields.


          72 - 79:   incompatible_features
                     Bitmask of incompatible features. An implementation must
                     fail to open an image if an unknown bit is set.

                     Bit 0:      Dirty bit.  If this bit is set then refcounts
                                 may be inconsistent, make sure to scan L1/L2
                                 tables to repair refcounts before accessing the
                                 image.

                     Bit 1:      Corrupt bit.  If this bit is set then any data
                                 structure may be corrupt and the image must not
                                 be written to (unless for regaining
                               consistency).

                     Bits 2-63:  Reserved (set to 0)

         80 -  87:  compatible_features
                    Bitmask of compatible features. An implementation can
                    safely ignore any unknown bits that are set.

                    Bit 0:      Lazy refcounts bit.  If this bit is set then
                                lazy refcount updates can be used.  This means
                                marking the image file dirty and postponing
                                refcount metadata updates.

                    Bits 1-63:  Reserved (set to 0)

         88 -  95:  autoclear_features
                    Bitmask of auto-clear features. An implementation may only
                    write to an image with unknown auto-clear features if it
                    clears the respective bits from this field first.

                    Bit 0:      Bitmaps extension bit
                                This bit indicates consistency for the bitmaps
                                extension data.

                                It is an error if this bit is set without the
                                bitmaps extension present.

                                If the bitmaps extension is present but this
                                bit is unset, the bitmaps extension data must be
                                considered inconsistent.

                    Bits 1-63:  Reserved (set to 0)

         96 -  99:  refcount_order
                    Describes the width of a reference count block entry (width
                        in bits: refcount_bits = 1 << refcount_order). For version 2
                    images, the order is always assumed to be 4
                    (i.e. refcount_bits = 16).
                    This value may not exceed 6 (i.e. refcount_bits = 64).

        100 - 103:  header_length
                    Length of the header structure in bytes. For version 2
                    images, the length is always assumed to be 72 bytes.


A Sample of Typical Qcow2 File's Header:


0000000: 5146 49fb 0000 0003 0000 0000 0000 0108  QFI.............
                    [ magic]     [version]       [backing file offset]
                    Q F I 0Xfb             003                              0X108

0000010: 0000 002a 0000 0010 0000 0002 4000 0000  ...*....N....@...
                   [backSize] [clusterBits]    [size/*in bytes*/]
                            0X2A   0X10=16 bits         0X240000000 bytes

0000020: 0000 0000 0000 0012 0000 0000 0003 0000  ................
                  [ctyptMethod]    [LlSize]  [  LlTableOffset  ]
0000030: 0000 0000 0001 0000 0000 0001 0000 0000  ................
                [ refcountTbOffset ] [refTbClst]     [nbSnapshot]
0000040: 0000 0000 0000 0000 0000 0000 0000 0000  ................
                 [  snapshotOffset  ]
0000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000060: 0000 0004 0000 0068 6803 f857 0000 0090  .......hh..W....
0000070: 0000 6469 7274 7920 6269 7400 0000 0000  ..dirty bit.....
0000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000a0: 0001 636f 7272 7570 7420 6269 7400 0000  ..corrupt bit...
00000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000d0: 0100 6c61 7a79 2072 6566 636f 756e 7473  ..lazy refcounts
00000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000100: 0000 0000 0000 0000 2f72 6f6f 742f 7465  ......../root/te
     0X100     0 1    2  3   4  5    6  7  8
     0X107    = 0X108 - 1

0000110: 7374 2f69 6d61 6765 2f69 6133 3265 5f72  st/image/ia32e_r
0000120: 6865 6c37 7532 5f63 7075 3230 3036 2e69  hel7u2_cpu2006.i
0000130: 6d67 0000 0000 0000 0000 0000 0000 0000  mg..............
     0X130       0 1
     0X131 = 0X107 + 0X2A

0000140: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000150: 0000 0000 0000 0000 0000 0000 0000 0000  ................
0000160: 0000 0000 0000 0000 0000 0000 0000 0000  ................                                                                                                   


now, when we use qemu-img -info to display this image's info, the result as below:

[root@gr-mrb-64 ~]# qemu-img info rhel7_cpu2006.qcow
image: rhel7_cpu2006.qcow
file format: qcow2
virtual size: 9.0G (9663676416 bytes)
disk size: 196K
cluster_size: 65536
backing file: /root/test/image/ia32e_rhel7u2_cpu2006.img
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
[root@gr-mrb-64 ~]#






Reference:

qcow-wikipedia

http://git.qemu-project.org/?p=qemu.git;a=blob;f=docs/specs/qcow2.txt

https://github.com/qemu/qemu

https://people.gnome.org/~markmc/qcow-image-format.html





不止于创建:如何用virt-manager生成的qcow2镜像进行快速克隆与多环境部署 本文详细介绍了如何使用virt-manager生成的qcow2镜像进行快速克隆与多环境部署,涵盖黄金镜像的标准化制备、三种克隆策略选择、批量部署的工业化实践以及跨平台迁移技巧。通过virt-clone和qemu-img工具,实现从单机复制到大规模部署的高效运维,特别适合Linux虚拟机的快速扩展与云环境适配。 阅读详情

相关推荐

Linux系统镜像定制:基于cloud-init与qemu-img构建DigitalOcean Custom Image

Linux系统镜像定制是云原生环境下实现环境可复制性、自动化交付与安全合规部署的核心能力。其本质是将操作系统状态抽象为标准化、无状态、可版本化管理的虚拟磁盘镜像,依托cloud-init完成首次启动时的动态初始化,并通过qemu-img等工具实现格式转换、压缩优化与硬件解耦。该技术显著提升DevOps流水线效率,支撑多区域批量部署、CI/CD集成及边缘计算场景下的秒级交付。本文聚焦DigitalOcean平台,详解从源系统清理、cloud-init深度配置到QCOW2镜像生成与上传的完整工程实践,覆盖dd、

weixin_34354173的博客 326

告别臃肿镜像:用virt-manager+qcow2打造精简可移植的Linux系统模板

本文详细介绍了如何使用virt-manager和qcow2格式创建精简、可移植的Linux系统模板,适用于DevOps和持续集成环境。通过优化系统配置、清理无用文件和使用virt-sparsify压缩镜像,显著减少镜像体积并提升部署效率。文章还涵盖了模板管理、快速克隆和高级维护技巧,帮助用户打造高效的虚拟机工作流程。

weixin_30243533的博客 457

基于R语言的DICE模型实践技术应用;预测不同减排路径下的经济增长、碳排放量、气候变化程度以及社会福利变化

DICE模型是一个动态综合气候经济模型,由诺贝尔经济学奖得主William Nordhaus开发,用于评估气候变化对经济的影响以及不同减排政策的经济成本和效益。以下是一个关于DICE模型在气候变化影响评估中的实际应用案例,结合R语言代码进行讲解。

KY_chenzhao的博客 867

错误kvm: --drive file=./ubuntu,cache=none,format=qcow2: could not open disk image : Invalid argument

出现这种错误,一般表示镜像文件的相关选项有问题: 例如:镜像文件本身是raw的格式,但kvm命令行中却给出qcow2 文件系统不支持direct io,但cache model却给出none

韦远科的专栏 3940

药物重定位实战:如何用反向找靶技术挖掘老药新用(附Python代码示例)

本文详细介绍了药物重定位中的反向找靶技术,通过分子相似性计算、机器学习模型和结构对接等方法,挖掘已有药物的新治疗潜力。附Python代码示例,帮助读者快速实现从分子结构到潜在靶标的智能预测,显著提升药物研发效率。

weixin_28726671的博客 259

【鸿蒙游戏上架审核规范指南】

华为鸿蒙游戏上架审核规范指南摘要

weixin_44161102的博客 918

虚拟机KVM自动安装CentOS7

虚拟机KVM自动安装CentOS7一、软件环境宿主机版本如下:准备安装文件方法1:http提供安装源方法2:ftp提供安装源方法3:新增一个raw硬盘,放好ks文件。准备自动安装脚本二、kvm图形界面新建虚拟机自动安装三、命令行自动安装问题1:virt-install运行后有virt-viewer报错:问题2:vnc连接被拒绝的问题:问题3:进入安装界面后提示“No disks selected”...

weixin_43869959的博客 2961

Google antigravity 不支持你所在地区?手把手教你把账号改成支持地区

Google antigravity功能火爆,但国内老账号根本用不了🤬本文分享几种有效的账号国家地区切换方法,最快几分钟就能让antigravity直接亮起🔥

XiaoqiangClub的博客 2万+

OpenStack虚拟机创建过程中镜像格式的的变化过程

http://labs.chinamobile.com/mblog/854855_196136 Glance用来作为独立的大规模镜像查找服务,当它与Nova和Swift配合使用时,就为OpenStack提供了虚拟机镜像的查找服务,像所有的OpenStack项目一样,遵循以下设计思想: 基于组件的架构 - 便于快速增加新特性高可用性 - 支持大负荷容错性 - 独立的进程地址空

ztejiagn的专栏 1174

AUTOSAR进阶图解==>AUTOSAR_SWS_ADCDriver

本文深入解析了AUTOSAR ADC Driver的架构设计和技术实现。主要内容包括:1)ADC Driver在AUTOSAR分层架构中的定位及核心功能;2)详细的模块架构设计,涵盖初始化管理、组转换管理、优先级处理等核心子组件;3)全面的配置模型说明,包括主配置类、通用配置、通道配置等关键数据结构;4)状态机机制和操作序列分析。通过分层设计和标准化接口,ADC Driver实现了硬件抽象和软件解耦,提供了良好的可移植性和可维护性。

一名留德华的,康德的信徒的,专注于汽车电子领域的博客 786

YOLO11多个点组合创新:GC10-DET缺陷检测 | DCNv4结合SPPF+11Detect创新性结合

DCNv4结合SPPF+11Detect创新性结合,原始mAP50为0.633 提升至0.651

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 1290

error: internal error: unable to execute QEMU command ‘__com.redhat_drive_add‘

本次实验为了验证下面场景下,磁盘是否可以被在线卸载?如果可以被卸载,那么是否可以再次被在线挂载? 场景:磁盘被挂载到虚机后,通过LVM已经创建PV,追加给LV,并且被格式化,将空间扩展给了filesystem。 执行在线卸载磁盘命令,成功! # virsh detach-disk LDAP05 sdc Disk detached successfully 再次在线挂载磁盘时,报错: # virsh attach-disk LDAP05 /var/lib/libvirt/im...

刘元林的博客 4284

【WorkBuddy专栏53】我的WB为什么变聪明了——SOUL与USER配置管理实战(上)

【WorkBuddy专栏53】我的WB为什么变聪明了——SOUL与USER配置管理实战(上)

AI人人懂 244

qcow2文件压缩

# qemu-img create -f qcow2 centos.qcow2 5G #创建5G的qcow2镜像 # virt-sparsify -x /root/test1.qcow2 --convert qcow2 /root/test2.qcow2 #消除镜像空洞 # virt-sparsify --compress --convert qcow2 centos.img cent...

风云刀的优快云博客 1969

linux内核协议栈 TCP选项之SACK选项概述

目录 1SACK概述 1.1 SACK允许选项 1.2 SACK选项 2SACK允许选项的发送 2.1 SYN段发送 2.2 接收SYN段 2.2.1 struct tcp_options_received 2.2.2 tcp_parse_options() 2.3 发送SYN+ACK段 3D-SACK 1SACK概述 标准的TCP确认机制中,如果发送方发送了0-1000序号之间的数据,接收方收到了0-100、300-1000,那么接收方只能向发送方确认101,这时发送方会重...

2731

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Complete Guide to Converting qcow2 Format to VMDK Format for VMware

【代码】Complete Guide to Converting qcow2 Format to VMDK Format for VMware。

allway2的博客 660

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

cinder报错:Converted to raw, but format is now qcow2

前几天收到用户共享给我的镜像,我上传glance后,发现无法用镜像创建volume,查看cinder日志,发现如下报错: 2019-03-28 10:06:50.446 34 ERROR cinder.volume.flows.manager.create_volume 'file_format': data. 2019-03-28 10:06:50.446 34 ERROR cinder...

yasyal515的博客 544

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

linux查看raw格式磁盘,KVM之raw和qcow2格式虚拟磁盘创建

在创建虚拟机之前,首先添加一个虚拟机磁盘,在KVM中用image来表示虚拟磁盘,在添加虚拟机磁盘之前首先需要知道如何制作一个imageqemu-img是QEMU的磁盘管理工具一、raw格式的image创建1、创建一个raw格式的image,容量为20gqemu-img create -f raw Ubuntu14.04.raw 20G2、查看image信息root@kvmhost1:/datapo...

weixin_30722051的博客 1555

kvm--磁盘格式以及磁盘扩容(raw、qcow2)

磁盘格式 [root@kvm qemu]# qemu-img create -f raw /kvm_data/test_1.img 2G Formatting '/kvm_data/test_1.img', fmt=raw size=2147483648 [root@kvm qemu]# qemu-img info /kvm_data/test_1.img image: /kvm_data/test_1.img file format: raw virtual size: 2.0G (2147483648

qq_51085767的博客 1460

Qemu/qemu-img raw.img to qcow2

### version lake@lake-Latitude-5491:~/qemu$ qemu-system-i386 -version QEMU emulator version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.10) Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project deve...

hushui的专栏 3164

qemu 修改qcow2 image文件

qemu-img check -r all /work/vdisk/ceph_centos65_x86_64_ceph_osd1.img

eyf0917的专栏 5268

vmware虚拟机转换格式qcow2和raw

目前在做TensorFlow分布式,采用ubuntu1804发现云上镜像有问题,把自己安装的虚拟机做一份qcow2镜像 apt-get install dnsmasq libvirt-daemon-system qemu-kvm qemu-utils virt-manager 这个可以安装qemu(kail2) qemu-img convert -f vmdk -O qcow2 source-n...

张金玉的专栏 8062

Arsenal Image Mounter支持的磁盘镜像格式全解析:VHD、EWF等格式使用方法

Arsenal Image Mounter是一款强大的磁盘镜像挂载工具,能在Windows系统中将各类磁盘镜像内容作为完整磁盘挂载。它支持多种主流镜像格式,为用户提供便捷的镜像文件访问方式。本文将详细解析其支持的VHD、EWF等格式及使用方法。 ## 支持的主流磁盘镜像格式 Arsenal Image Mounter支持多种常见的磁盘镜像格式,满足不同场景下的使用需求。 ### VHD(Vi

gitblog_00342的博客 687

openstack image guide总结

引言 What is a virtual machine image? A virtual machine image is a single file which contains a virtual disk that has a bootable operating system installed on it. Virtual machine images come in d

翟海飞 1932

openstack-glance上传镜像

openstack-glance上传镜像 openstack 中怎么上传image ? 1. 需要我们提前准备好镜像,具体的镜像可以自己在官网下载 2. 在命令行通过glance 上传镜像 命令: glance image-create --name "ubuntu14-04" --file ubuntu-14.04.6-server-amd64.qcow2 --disk-format qcow2 --container-format bare --visibility public...

weixin_46767505的博客 2279
上一篇: how to use tmux?
下一篇: 虚拟化相关概念与技术整理(1)——关于VMM类型
Benjamin_Xu
博客等级 码龄14年 46粉丝 54原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值