qcow2文件分析

qcow2文件分析Aqcow2virtua atwolevel nbsp lookuptable areferenceta anddataclust as nbsp showninFigur Theimagehead nbsp ofafilesyste whichco

A qcow2 virtual disk contains an image header, a twolevel lookup table, a reference table, and data clusters, as shown in Figure 1. The image header resembles the superblock of a file system, which contains the basic information of the image file such as the base address of the lookup table and the reference table. The image file is organized at the granularity of cluster, and the size of the cluster is stored in the image header. The lookup table is used for address translation. A virtual block address (VBA) a in the guest VM is split into three parts, i.e., a=(a1, a2, a3): a1 is used as the L1 table’s index to locate the corresponding L2 table; a2 is used as the L2 table’s index to locate the corresponding data cluster; a3 is the offset in the data cluster. The reference table is used to track each cluster used by snapshots. The refcount in reference table is set to 1 for a newly allocated cluster, and its value grows when more snapshots use the cluster.

qcow2文件分析

2 If the L2 table is not allocated, then set the corresponding reference table entry to allocate a cluster for the L2 table, and initialize the new L2 table.

3 Update the L1 table entry to point to the new L2 table if a new L2 table is allocated.

4 Set the reference table to allocate a cluster for data.

1) “overwrite benchmark”, which allocates blocks in advance in the disk image (i.e., the qcow2 image size remains the same before and after the test);

简介

Qcow2镜像格式是qemu支持的磁盘镜像格式之一。qcow2的表现形式为在一个文件中模拟一个固定大小的块设备。对与qcow2格式,相对于raw格式来说,有几个优点:

  1. 1.更小的文件大小,即使是不支持holes的文件系统也可以(这样的话,ls跟du看到的就一样了);
  2. 2.Copy-on-write的支持;
  3. 3.快照的支持,可以维护多个快照;
  4. 4.基于zlib的压缩;
  5. 5.AES加密

 

  • 头4个字节包含了字符’Q’, ‘F’, ‘I’,并以0xfb结尾;
  • 之后的4个字节包含了这个文件所用的格式版本,当前存在两种版本的格式,版本1和版本2。在本文,我们讨论的是版本2,即qcow2。版本1将在本文最后做简要介绍;
  • backing_file_offset字段给出相对于qcow2文件起始位置的偏移,指出一个字符串的位置,该字符串为backing file文件的绝对路径。由于该字符串不是以’\0’结束,所以backing_file_size指出字符串的长度。如果当前镜像是一个copy-on-write镜像,则存在backing file文件,否则没有;
  • cluster_bits字段,决定了怎样映射镜像偏移地址到文件偏移地址,其决定了在一个簇中,将拿偏移地址的多少位(低位)来作为索引。L2表占据一个单独的簇,包含若干8字节的项,cluster_bits最少用3bits作为L2表的索引。L2表的详细介绍,见下一节的2级索引;
  • size字段指示镜像以块设备呈现时的大小,单位字节;
  • crypt_method只有两种值,0表示没有加密,1表示采用了AES加密;
  • l1_size字段指示了在L1表中,可用的8字节项的个数,l1_table_offset字段给出了L1 table的文件偏移;
  • 相似的,refcount_table_offset字段给出了refcount table的文件偏移,refcount_table_clusters字段描述了refcount table大小(单位为clusters);
  • nb_snapshots字段给出了当前镜像中有多少个快照,snapshots_offset字段给出了QCowSnapshotHeader headers的文件偏移,每个快照都会有这样一个header。
  •  

一个典型的镜像文件,其布局如下:

  • 一个header, 如上描述;
  • 在下一个簇开始,存放L1 table;
  • refcount table,仍然是簇对齐的;
  • 一个或者多个的refcount blocks;
  • Snapshot headers,第一个header要求簇对齐,之后的header要求8字节对齐;
  • L2 tables,每一个table占据一个单独的cluster;
  • Data clusters。
  • 低12位用来定位一个4Kb的簇内偏移;
  • 之后的9位为一个512项的数组的偏移,每一项为一个8字节的文件偏移,即L2 table。 这里的9位是这么算出来的, l2_bits = cluster_bits – 3,L2 table是一个单独的包含若干8字节项的cluster;
  • 剩下的43位为另外一个8字节的文件偏移的数组的偏移,即L1 table。
  1. 通过qcow2 header中的l1_table_offset字段获取L1 table的地址;
  2. 使用高(64 – l2_bits – cluser_bits)位的地址来索引L1 table,L1 table是一个数组,数组元素是一个64位的数;
  3. 通过L1 table中的表项来获取L2 table的地址;
  4. 通过L2 table中的表项来获取cluster的地址;
  5. 剩余的cluster_bits位来索引cluster内的位置。
  • 快照有名字和ID,都是字符串,id_str_size,name_size给出字符串长度,字符串紧接在QCowSnapshotHeader后面;
  • 快照至少有原来的L1 table的副本,其通过l1_table_offset和l1_size来定位;
  • 在快照被创建的时候,qemu会调用gettimeofday(),快照时间被保存在date_sec和date_nsec字段中;
  • vm_clock_nsec给出VM clock当前的状态;
  • vm_state_size表示作为快照的一部分被保存的虚拟机状态的大小。这个状态被保存在原来L1 table的位置,直接在镜像header的后面;
  • extra_data_size表示在QCowSnapshotHeader之后的扩展数据的长度,不包括id和name字符串。这一段扩展数据是留给以后用的。
  • 如果cluster offset的第二最高有效位是1,则这是一个被压缩的cluster;
  • cluster offset中之后的cluster_bits – 8 位是这个压缩过的cluster的大小,单位是sectors;
  • cluster offset剩余的位是压缩的cluster在文件中的实际偏移地址。
  1. 1.支持快照的概念,qcow只支持增量镜像;
  2. 2.在qcow2中,引入了cluster的引用计数的概念;引用计数也被用来支持快照;
  3. 3.在qcow2中,L2 table将一直占一个单独的cluster; 之前,是通过QCowHeader中的l2_bits来确定的;
  4. 4.压缩的cluster的大小,现在单位为sector,之前是字节。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230519.html原文链接:https://javaforall.net

(0)
上一篇 2026年2月18日 上午11:01
下一篇 2026年2月18日 上午11:22


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号