Method and system for detecting integrity of qcow2 virtual disk mirror image file
By directly parsing the file header of the qcow2 virtual disk image file and automatically calculating the physical offset, the integrity of the MBR or GPT region is detected, solving the problem of detection failure when the virtual machine cannot start in the existing technology, and realizing efficient and accurate virtual disk integrity detection.
Patent Information
- Application Number
- CN202511862286.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-11
- Publication Date
- 2026-01-09
- Estimated Expiration
- 2045-12-11
AI Technical Summary
Existing technologies cannot effectively detect the integrity of the MBR and GPT partition tables in qcow2 format virtual machine disk image files, especially when the virtual machine fails to start, conventional methods fail and the results are not accurate enough.
By directly reading the file header of the qcow2 virtual disk image file, parsing the basic metadata, automatically calculating the physical offset, locating and parsing the MBR or GPT region, performing CRC check and integrity judgment, and outputting the detection results.
It enables detection even when a virtual machine fails to start, providing direct and accurate results, improving operational efficiency, simplifying the operation process, and filling a functional gap in image management tools.
Smart Images

Figure CN121301074A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer virtualization storage technology, specifically relating to a method and system for detecting the integrity of qcow2 format virtual machine disk image files. Background Technology
[0002] With the popularization of cloud computing and virtualization technologies, the qcow2 format, as the mainstream disk image format for the QEMU / KVM virtualization platform, is widely used. The qcow2 format maps virtual disk data through a multi-level L1 and L2 table structure, realizing advanced functions such as copy-on-write, dynamic expansion, and snapshots. However, this also means that the raw virtual disk data stored internally is no longer stored linearly and continuously like in raw format images.
[0003] In qcow2 formatted disk management and system boot process, the Master Boot Record (MBR) and the disk partition table (MBR partition table or Globally Unique Identifier Partition Table, GPT) are crucial data structures that record disk partition information and boot code. If the MBR partition table or GPT partition table is corrupted, it will directly cause the virtual machine to fail to boot or data partitions to become inaccessible.
[0004] Currently, methods for integrity checks and viewing of the MBR and hard disk partition table in qcow2 format virtual machine disk image files all have certain shortcomings, as explained below: 1. Using commands within the virtual machine: Commands such as fdisk -l or parted require starting a virtual machine first, and then the operating system inside the virtual machine reads the block device information.
[0005] The drawback of this method is that it will completely fail when the MBR / GPT is severely damaged, preventing the virtual machine from booting. Furthermore, it displays the results after the operating system kernel has parsed the data, rather than the original disk sector records, and may not expose subtle errors in the underlying data structures.
[0006] 2. Use an image management tool: For example, the image management tool qemu-img check is mainly used to check the consistency and correctness of the metadata (such as L1 / L2 tables and reference counts) of the qcow2 image file itself.
[0007] The drawback of this method is that image management tools generally only detect at the format level of the image file, and do not delve into the MBR or GPT data structure of the virtual disk carried inside the image file. Therefore, they cannot detect logical errors or damage to the virtual disk partition table.
[0008] 3. Manual analysis: Technicians can manually parse the qcow2 format specification, write scripts or use a binary editor to first calculate the physical offset of the data cluster where the virtual disk MBR / GPT is located in the qcow2 file based on the L1 / L2 table, and then extract the data for verification.
[0009] However, this method is extremely cumbersome, error-prone, and inefficient, and requires a high level of professional technical knowledge from the operator, making it unsuitable as a routine maintenance method. Summary of the Invention
[0010] The purpose of this invention is to provide a method and system for detecting the integrity of qcow2 virtual disk image files. This method can penetrate the mapping layer of the qcow2 format, directly locate and read the MBR or GPT area of the virtual disk, automatically complete structure parsing, CRC check and integrity judgment, and output the detection results in a clear form.
[0011] To achieve the above objectives, the technical solution of the present invention is as follows: A method for detecting the integrity of a qcow2 virtual disk image file includes: S1. Read the file header of the qcow2 virtual disk image file and parse out the basic metadata including the cluster size and L1 table offset. S2. Based on the basic metadata, automatically calculate the physical offset of the data cluster corresponding to the starting position of the virtual disk in the qcow2 virtual disk image file; S3. Read the data of a sector from the physical offset and use it as the Master Boot Record (MBR) for parsing and verification to obtain MBR parsing information; S4. Determine whether the virtual disk uses the GPT partitioning scheme based on the Master Boot Record (MBR). If yes, proceed to step S5; otherwise, proceed to step S6. S5. Automatically locate the GPT region, parse and verify it to obtain GPT parsing information; S6. Summarize and output parsing information, verification information, and integrity detection information.
[0012] Furthermore, step S2 includes: S201, Read table L1; S202. Calculate the corresponding L2 table index based on the sector number of the virtual disk, and find the corresponding L2 table entry from the L1 table. S203. Read the L2 table and find the corresponding data cluster table entry based on the offset within the cluster; S204. Parse the data cluster table entry to obtain the actual physical offset of the data cluster corresponding to the starting position in the qcow2 virtual disk image file.
[0013] Furthermore, the parsing and verification methods in step S3 include: S301. Parse the Master Boot Record (MBR) structure, including the boot code, disk signature, and partition table entries; S302. Verify the structural integrity of the MBR; S303. The output includes disk signature, starting sector, size, and type of MBR parsing information for each partition table.
[0014] Furthermore, step S302, which verifies the integrity of the MBR structure, includes verifying whether the MBR end flag is 0x55AA.
[0015] Furthermore, the method for determining whether the virtual disk uses the GPT partitioning scheme in step S4 includes: Check the type code of the partition table entry in the MBR. If the type code is 0xEE, it indicates that the virtual disk uses the GPT partitioning scheme.
[0016] Furthermore, step S5 includes: S501. Based on the basic metadata, automatically calculate the physical offset of the data cluster corresponding to the GPT header position in the qcow2 virtual disk image file, and read the GPT header data; S502. Parse the GPT header structure, including GPT signature, version, header size, header CRC32 checksum, primary GPT location, backup GPT location, partition table starting LBA, number and size of partition table entries, and partition table CRC32 checksum. S503. Recalculate the CRC32 check value of the GPT header data and compare it with the parsed header CRC32 check value to verify the integrity of the GPT header data. S504. Based on the information in the GPT header, locate the partition table region, calculate the physical offset of the partition table region in the qcow2 virtual disk image file, and read the partition table data. S505. Recalculate the CRC32 checksum of the read partition table data and compare it with the CRC32 checksum of the partition table recorded in the GPT header to verify the integrity of the entire partition table. S506. The output includes GPT parsing information such as disk GUID, partition table GUID, unique GUID of each partition, type GUID, start and end LBA, attributes and names.
[0017] Furthermore, in step S6, the final detection report is summarized and output. The report displays GPT parsing information and / or MBR parsing information, CRC check results, and integrity status; for cases of check failure or structural abnormality, clear error identification and prompts are provided.
[0018] In another aspect, this invention also proposes a system for detecting the integrity of qcow2 virtual disk image files, comprising: The qcow2 parsing engine reads the header of the qcow2 virtual disk image file and parses out basic metadata including cluster size and L1 table offset; based on the basic metadata, it automatically calculates the physical offset of the data cluster corresponding to the starting position of the virtual disk in the qcow2 virtual disk image file. MBR processing module: Reads data from a sector at the physical offset, parses and verifies it as the Master Boot Record (MBR), and obtains MBR parsing information; GPT processing module: Determines whether the virtual disk uses the GPT partitioning scheme based on the Master Boot Record (MBR). If so, it automatically locates the GPT region and performs parsing and verification to obtain GPT parsing information; otherwise, it jumps to the output module. Output module: Summarizes and outputs parsed information, verification information, and integrity detection information.
[0019] The present invention also provides a computer-readable storage medium storing a computer program for executing the above-described method for detecting the integrity of a qcow2 virtual disk image file.
[0020] The present invention also provides a computer product, including a computer program, which, when executed by a processor, implements the above-described method for detecting the integrity of a qcow2 virtual disk image file.
[0021] Compared with the prior art, the present invention has the following beneficial effects: 1. No need to start the virtual machine for detection: This invention operates directly on the qcow2 image file on the host machine. Even if the virtual disk is damaged and the virtual machine cannot start, detection can still be performed, which greatly improves the efficiency of operation and maintenance and troubleshooting.
[0022] 2. Achieving directness and accuracy: This invention directly reads and analyzes the MBR / GPT sector data at the lowest level of the virtual disk, avoiding the deviation that may be caused by indirectly obtaining information through the operating system kernel, resulting in more original and accurate results.
[0023] 3. Achieving automation and high efficiency: This invention automatically completes the entire process from qcow2 format parsing to partition table verification, eliminating the need for manual offset calculation and manual verification. It simplifies complex technical operations into a single command, resulting in high speed and efficiency.
[0024] 4. Highly targeted functionality: This invention fills the functional gap in conventional image management tools, specifically designed to detect the integrity of the data structure inside a virtual disk. It complements existing tools and together they form a more comprehensive virtual machine disk health detection system. Attached Figure Description
[0025] Figure 1 This is a flowchart of Embodiment 1 of the present invention; Figure 2 This is a schematic diagram of the L1 / L2 table lookup process in Embodiment 2 of the present invention; Figure 3 This is a flowchart of the automatic location, analysis, and verification of the GPT region in Embodiment 4 of the present invention. Detailed Implementation
[0026] It should be noted that, unless otherwise specified, the embodiments and features described in the present invention can be combined with each other.
[0027] The present invention will now be described in detail with reference to specific embodiments and accompanying drawings.
[0028] Example 1: like Figure 1 As shown, the method for detecting the integrity of the qcow2 virtual disk image file proposed in Embodiment 1 specifically includes the following: S1. Read the header of the qcow2 virtual disk image file and parse out the basic metadata, including the cluster size and L1 table offset.
[0029] The method described in this embodiment can be implemented by developing a command-line tool, which can be written in C language and depends on the qcow2 format parsing library in the QEMU project or implement the relevant parsing logic yourself.
[0030] The first step in parsing requires inputting the path to the qcow2 virtual disk image file. Based on the path, the file header is read, and basic metadata such as cluster size and L1 table offset are parsed out.
[0031] S2. Based on the basic metadata, automatically calculate the physical offset of the data cluster corresponding to the starting position of the virtual disk in the qcow2 virtual disk image file.
[0032] According to the qcow2 format specification, the starting position of the virtual disk is usually sector 0. Therefore, this step automatically calculates the physical offset of the data cluster corresponding to the starting position of the virtual disk, i.e., sector 0, in the qcow2 file.
[0033] S3. Read the data of a sector from the physical offset, and use it as the Master Boot Record (MBR) for parsing and verification to obtain MBR parsing information.
[0034] This step is based on the physical offset obtained in step S2. A sector of data is read from the physical offset. Typically, a sector is 512 bytes. Therefore, after reading this sector of 512 bytes of data, it is processed as the master boot record.
[0035] S4. Determine whether the virtual disk uses the GPT partitioning scheme based on the Master Boot Record (MBR). If yes, proceed to step S5; otherwise, proceed to step S6.
[0036] The partition table type can be determined by checking the type code of the partition table entries in the Master Boot Record (MBR). If the GPT partitioning scheme is used, the virtual disk contains a protective MBR, and the type code of the MBR partition table entries is 0xEE. Therefore, this step determines whether the virtual disk uses the MBR or GPT partitioning scheme by checking the type code of the partition table entries in the MBR. If the type code is found to be 0xEE, it indicates that the virtual disk uses the GPT partitioning scheme.
[0037] S5. Automatically locate the GPT region, perform parsing and verification, and obtain GPT parsing information.
[0038] This step calculates the physical offset, parses the GPT header structure, performs integrity verification, and obtains the GPT parsing information according to the GPT specification.
[0039] S6. Summarize and output parsing information, verification information, and integrity detection information.
[0040] This step involves summarizing and outputting the final inspection report. The report displays the MBR and GPT parsing information, CRC check results (pass / fail), and the overall integrity status in a readable format (such as plain text, JSON, or XML). Clear error indicators and prompts are provided for cases of check failures or structural anomalies.
[0041] The method described in this embodiment detects the integrity of qcow2 virtual disk image files without starting a virtual machine, directly reading and analyzing them to achieve directness and accuracy of the detection results. It improves detection efficiency through automation and has strong functional targeting, specifically designed for detecting the integrity of the data structure inside the virtual disk.
[0042] Example 2: This embodiment refines step S2 based on embodiment 1, and explains in detail the process of calculating the physical offset.
[0043] The process is specifically manifested as a lookup of L1 / L2 tables in qcow2 format, such as... Figure 2 As shown, it includes: S201: Read L1 table.
[0044] exist Figure 2 As can be seen, the data in the L1 table is read based on the L1 table offset in the basic metadata obtained from the qcow2 file header.
[0045] S202: Calculate the corresponding L2 table index based on the sector number of the virtual disk, and find the corresponding L2 table entry from the L1 table.
[0046] like Figure 2 As shown, the L1 table records the offset of each L2 table, and the data of each L2 table can be read according to the offset of each L2 table.
[0047] S203: Read the L2 table and find the corresponding data cluster table entry based on the offset within the cluster.
[0048] like Figure 2 As shown, the L2 table stores the offset of each data cluster, which can be obtained by reading the L2 table.
[0049] S204: Parse the data cluster table entry to obtain the actual physical offset of the data in sector 0 of the virtual disk in the qcow2 file.
[0050] This embodiment provides a method for querying the actual physical offset of the qcow2 image file directly on the host machine, even if the virtual machine is not running, which greatly improves the efficiency of operation and maintenance and troubleshooting.
[0051] Example 3: This embodiment refines step S3 based on embodiment 1, and details the process of parsing and verifying the Master Boot Record (MBR).
[0052] The process specifically includes: S301: Parse the MBR structure, including the boot code, disk signature, and four partition table entries.
[0053] The boot code is located first and is responsible for loading the operating system boot program; the disk signature is located 4 bytes after the boot code and serves as a unique identifier for the disk being written; the partition table entries (4 entries) are 16 bytes each and record partition information: the end marker is in the last 2 bytes and has a fixed value of 0x55AA, which is used to verify the integrity of the MBR.
[0054] S302: Verify the structural integrity of the MBR.
[0055] Given that the last 2 bytes of the end flag have a fixed value of 0x55AA, the operation to verify the integrity of the MBR structure must at least include verifying whether the MBR end flag is 0x55AA.
[0056] S303: Outputs the MBR parsing results, including disk signature, starting sector, size, type, and other information for each partition table.
[0057] This embodiment directly reads and analyzes the MBR data at the lowest level of the virtual disk, which can avoid the deviation that may be caused by indirectly obtaining information through the operating system kernel, and the results are more original and accurate.
[0058] Example 4: This embodiment refines step S5 based on embodiment 1, detailing the process of automatically locating the GPT region and performing analysis and verification.
[0059] The process is as follows Figure 3 As shown, it specifically includes: S501. Based on the basic metadata, automatically calculate the physical offset of the data cluster corresponding to the GPT header position in the qcow2 virtual disk image file, and read the GPT header data.
[0060] According to the GPT specification, the master GPT header is located in the first sector (LBA1) of the virtual disk. The physical offset of LBA1 in the qcow2 file is calculated using the method described in step S2, and the data of that sector is read.
[0061] S502. Parse the GPT header structure, including GPT signature, version, header size, header CRC32 checksum, primary GPT location, backup GPT location, partition table starting LBA, number and size of partition table entries, and partition table CRC32 checksum.
[0062] S503. Recalculate the CRC32 checksum of the GPT header data and compare it with the parsed header CRC32 checksum to verify the integrity of the GPT header data.
[0063] S504. Based on the information in the GPT header, locate the partition table region, calculate the physical offset of the partition table region in the qcow2 virtual disk image file, and read the partition table data.
[0064] S505. Recalculate the CRC32 checksum of the read partition table data and compare it with the CRC32 checksum of the partition table recorded in the GPT header to verify the integrity of the entire partition table. S506. The output includes GPT parsing information such as disk GUID, partition table GUID, unique GUID of each partition, type GUID, start and end LBA, attributes and names.
[0065] This embodiment directly reads and analyzes the GPT sector data at the lowest level of the virtual disk, which can avoid the deviation that may be caused by indirectly obtaining information through the operating system kernel, and the results are more original and accurate.
[0066] Example 5: This embodiment proposes a system for detecting the integrity of qcow2 virtual disk image files, including: The qcow2 parsing engine reads the header of the qcow2 virtual disk image file and parses out basic metadata including cluster size and L1 table offset; based on the basic metadata, it automatically calculates the physical offset of the data cluster corresponding to the starting position of the virtual disk in the qcow2 virtual disk image file. MBR processing module: Reads data from a sector at the physical offset, parses and verifies it as the Master Boot Record (MBR), and obtains MBR parsing information; GPT processing module: Determines whether the virtual disk uses the GPT partitioning scheme based on the Master Boot Record (MBR). If so, it automatically locates the GPT region and performs parsing and verification to obtain GPT parsing information; otherwise, it jumps to the output module. Output module: Summarizes and outputs parsed information, verification information, and integrity detection information.
[0067] The qcow2 parsing engine includes: Read the L1 table; calculate the corresponding L2 table index based on the sector number of the virtual disk, and find the corresponding L2 table entry from the L1 table; read the L2 table, and find the corresponding data cluster table entry based on the offset within the cluster; parse the data cluster table entry to obtain the actual physical offset of the data cluster corresponding to the starting position in the qcow2 virtual disk image file.
[0068] The MBR processing module includes: The process involves parsing the Master Boot Record (MBR) structure, including the boot code, disk signature, and partition table entries; verifying the integrity of the MBR structure; and outputting MBR parsing information including the disk signature, the starting sector, size, and type of each partition table. Verifying the integrity of the MBR structure includes verifying whether the MBR end marker is 0x55AA.
[0069] The GPT processing module includes: Check the type code of the partition table entry in the MBR. If the type code is 0xEE, it indicates that the virtual disk uses the GPT partitioning scheme. Based on the basic metadata, automatically calculate the physical offset of the data cluster corresponding to the GPT header position in the qcow2 virtual disk image file, and read the GPT header data. Parse the GPT header structure, including GPT signature, version, header size, header CRC32 checksum, primary GPT position, backup GPT position, partition table starting LBA, number and size of partition table entries, and partition table CRC32 checksum. Recalculate the CRC32 checksum of the GPT header data and compare it with... The parsed header CRC32 checksum is compared to verify the integrity of the GPT header data. Based on the information in the GPT header, the partition table region is located, the physical offset of the partition table region in the qcow2 virtual disk image file is calculated, and the partition table data is read. The CRC32 checksum of the read partition table data is recalculated and compared with the partition table CRC32 checksum recorded in the GPT header to verify the integrity of the entire partition table. The output includes GPT parsing information including disk GUID, partition table GUID, unique GUID of each partition, type GUID, start and end LBA, attributes, and name.
[0070] The output module summarizes and outputs the final test report, which displays GPT parsing information and / or MBR parsing information, CRC check results, and integrity status; for cases of check failure or structural abnormality, clear error markings and prompts are provided.
[0071] The system for detecting the integrity of qcow2 virtual disk image files proposed in this embodiment can achieve the methods for detecting the integrity of qcow2 virtual disk image files described in Embodiments 1, 2, and 3, and has the same technical effects as Embodiments 1, 2, and 3.
[0072] The above-described embodiments are merely preferred embodiments of the present invention and are only used to help understand the method and core ideas of this application. The scope of protection of the present invention is not limited to the above embodiments, and all technical solutions falling within the scope of the present invention's concept are within the scope of protection of the present invention. It should be noted that for those skilled in the art, any improvements and modifications made without departing from the principles of the present invention should also be considered within the scope of protection of the present invention.
Claims
1. A method for detecting the integrity of a qcow2 virtual disk image file, characterized in that, include: S1. Read the file header of the qcow2 virtual disk image file and parse out the basic metadata including the cluster size and L1 table offset. S2. Based on the basic metadata, automatically calculate the physical offset of the data cluster corresponding to the starting position of the virtual disk in the qcow2 virtual disk image file; S3. Read the data of a sector from the physical offset and use it as the Master Boot Record (MBR) for parsing and verification to obtain MBR parsing information; S4. Determine whether the virtual disk uses the GPT partitioning scheme based on the Master Boot Record (MBR). If yes, proceed to step S5; otherwise, proceed to step S6. S5. Automatically locate the GPT region, parse and verify it to obtain GPT parsing information; S6. Summarize and output parsing information, verification information, and integrity detection information.
2. The method for detecting the integrity of a qcow2 virtual disk image file according to claim 1, characterized in that, Step S2 includes: S201, Read table L1; S202. Calculate the corresponding L2 table index based on the sector number of the virtual disk, and find the corresponding L2 table entry from the L1 table. S203. Read the L2 table and find the corresponding data cluster table entry based on the offset within the cluster; S204. Parse the data cluster table entry to obtain the actual physical offset of the data cluster corresponding to the starting position in the qcow2 virtual disk image file.
3. The method for detecting the integrity of a qcow2 virtual disk image file according to claim 1, characterized in that, The parsing and verification methods in step S3 include: S301. Parse the Master Boot Record (MBR) structure, including the boot code, disk signature, and partition table entries; S302. Verify the structural integrity of the MBR; S303. The output includes disk signature, starting sector, size, and type of MBR parsing information for each partition table.
4. The method for detecting the integrity of a qcow2 virtual disk image file according to claim 3, characterized in that, The verification of MBR structural integrity in step S302 includes verifying whether the MBR end flag is 0x55AA.
5. The method for detecting the integrity of a qcow2 virtual disk image file according to claim 1, characterized in that, The method for determining whether the virtual disk uses the GPT partitioning scheme in step S4 includes: Check the type code of the partition table entry in the MBR. If the type code is 0xEE, it indicates that the virtual disk uses the GPT partitioning scheme.
6. The method for detecting the integrity of a qcow2 virtual disk image file according to claim 1, characterized in that, Step S5 includes: S501. Based on the basic metadata, automatically calculate the physical offset of the data cluster corresponding to the GPT header position in the qcow2 virtual disk image file, and read the GPT header data; S502. Parse the GPT header structure, including GPT signature, version, header size, header CRC32 checksum, primary GPT location, backup GPT location, partition table starting LBA, number and size of partition table entries, and partition table CRC32 checksum. S503. Recalculate the CRC32 check value of the GPT header data and compare it with the parsed header CRC32 check value to verify the integrity of the GPT header data. S504. Based on the information in the GPT header, locate the partition table region, calculate the physical offset of the partition table region in the qcow2 virtual disk image file, and read the partition table data. S505. Recalculate the CRC32 checksum of the read partition table data and compare it with the CRC32 checksum of the partition table recorded in the GPT header to verify the integrity of the entire partition table. S506. The output includes GPT parsing information such as disk GUID, partition table GUID, unique GUID of each partition, type GUID, start and end LBA, attributes and names.
7. The method for detecting the integrity of a qcow2 virtual disk image file according to claim 1, characterized in that, In step S6, the final test report is compiled and output. The report shows GPT parsing information and / or MBR parsing information, CRC check results, and integrity status. For cases of check failure or structural abnormality, clear error markings and prompts are provided.
8. A system for detecting the integrity of qcow2 virtual disk image files, characterized in that, include: The qcow2 parsing engine reads the header of the qcow2 virtual disk image file and parses out basic metadata including cluster size and L1 table offset. Based on the basic metadata, the physical offset of the data cluster corresponding to the starting position of the virtual disk in the qcow2 virtual disk image file is automatically calculated. MBR processing module: Reads data from a sector at the physical offset, parses and verifies it as the Master Boot Record (MBR), and obtains MBR parsing information; GPT processing module: Determines whether the virtual disk uses the GPT partitioning scheme based on the Master Boot Record (MBR). If so, it automatically locates the GPT region and performs parsing and verification to obtain GPT parsing information; otherwise, it jumps to the output module. Output module: Summarizes and outputs parsed information, verification information, and integrity detection information.
9. A computer-readable storage medium storing a computer program, characterized in that, The computer program is used to perform the method for detecting the integrity of the qcow2 virtual disk image file as described in any one of claims 1-7.
10. A computer product comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the method for detecting the integrity of the qcow2 virtual disk image file as described in any one of claims 1-7.
Citation Information
Patent Citations
Copy-on-write-based virtual disk writing method and apparatus, and storage medium
CN107861685A
Virtual disk reduction method and apparatus
CN107918521A
Simulation method and device for partition mirror image file of Linux system
CN114443210A
System starting method and device
CN114816542A
Extracting drive letters for partitions in an offline virtual disk
US20210271427A1