← Back to Recovery methods

VHDX and QCOW2 Internal Metadata Recovery

VHDX and QCOW2 files carry their own maps from guest addresses to host offsets, plus logs, reference counts and parent or backing references. Recovering the outer file does not prove that this map is valid, and opening, replaying, checking or converting the file can change it or carry an already-wrong view forward.

In short
  • VHDX keeps two headers to choose a current one; they are not copies of the BAT, the log or the payload.
  • An absent block is not necessarily zero or lost: it may be inherited from a parent, defined as zero, or undefined.
  • VHDX log replay writes to the file; it is required before normal I/O, which is why the original is not the place to run it.
  • QCOW2 L1/L2 tables map guest data and refcounts track host clusters; fixing refcounts does not prove guest content is right.
  • Check, repair, convert, commit and rebase have different effects, and none of them replaces validating the guest data.

Two formats, the same question

Both formats answer one question for every guest address: is the data in this file, in a parent or backing file, defined as zero, or not defined at all? VHDX answers it through a Block Allocation Table (BAT) and, in differencing disks, sector bitmaps. QCOW2 answers it through L1 and L2 tables, with a separate refcount structure tracking which host clusters are in use. How several files depend on one another is covered in Virtual Disk Snapshot and Differencing-Chain Recovery; this page is about what happens inside one file.

VHDX headers choose a current state

The first megabyte of a VHDX file holds the file type identifier, two headers and two region tables. Microsoft’s specification treats a header as valid when its signature and checksum validate, and as current when it is the only valid header or the valid one with the greater sequence number; with no current header, the file is corrupt. The rule selects between two valid candidates. An invalid header with a larger counter is not current.

These copies protect header updates. They are not copies of the BAT, the metadata region, the log or the payload, and a current header does not prove that what it points to is complete. The headers also record identity: the specification requires the file-write identifier to change before the first modification of the file on each open, log playback included, and the data-write identifier before the first change to user-visible data. An open that modified anything therefore leaves a trace in the header.

VHDX block states and sector bitmaps

Each BAT entry gives a payload block a state, and the states do not all mean the same thing when read:

Scroll horizontally to see the full table →

VHDX payload block states and what a read returns, simplified from MS-VHDX §2.5.1.1.
StateFixed or dynamic VHDXDifferencing VHDX
Not presentUndefined: a read may return zeros, arbitrary data or earlier contentsNot in this file; the parent should be inspected
UndefinedContents not defined; a read may return zeros, arbitrary data or earlier contents
ZeroDefined as zero; reads must return zeros
UnmappedReceived UNMAP; reads return zeros or the earlier contents
Fully presentData is in this file at the recorded offset
Partially presentNot allowedData is in this file for sectors whose bitmap bit is set; other sectors come from the parent

So not every absent state means “read the parent”, and not every one means zero. A partially present block can only be read with its sector bitmap and the right parent; concatenating bytes from the two files is not a substitute. A sparse range that the format defines as zero or undefined is not lost data either; it is a range for which the file never promised content.

VHDX parent locator and parent identity

A differencing VHDX records its parent in two ways. The linkage entry holds the parent’s data-write identifier, and an implementation opening the parent must check that the parent’s header matches it. Separate path entries — relative, volume-GUID and absolute — only help find the file. A path does not establish identity; the linkage does. The specification also has a successful chain open update stale path entries to point at the current parent, so even a successful open can write.

Preserve the exact relationships and identifiers, the virtual size and the logical sector size. Do not generate a new identifier to make a mismatch go away: a changed parent identity hides whether the parent was modified after the child was created.

VHDX log replay is a write

When the log is not empty, the specification requires an implementation to replay it before any I/O other than reading the header and the log, and an implementation that cannot replay must not open the file. Replay writes data or zeros as the log descriptors direct and can extend the file. The active sequence is found by validating entries and their sequence continuity, not by taking the largest counter; if no valid sequence exists, or the file is shorter than the log says it was flushed to, the file is corrupt or truncated and must not be opened.

The replay requirement is a rule for implementations, not permission to write into evidence. Work on a copy, with a tool whose behaviour on a non-empty log is documented; refusing to open is a legitimate outcome for a tool that cannot replay. An attach or open described as read-only in a user interface does not by itself prove that the log and metadata stayed untouched. And a consistent log says nothing about the guest filesystem or the applications on it: replay cannot recreate payload missing from the file or guest writes that were never logged. See Filesystem Journals and Crash Consistency for the same distinction one layer up.

QCOW2: guest mapping and host refcounts

QEMU’s format specification resolves a guest cluster through an L1 entry into an L2 table, which gives the host cluster. In a simplified view: an unallocated cluster is read from the backing file if there is one, and as zeros where no backing file covers it. A separate refcount table records how many owners each host cluster has: 0 is free, 1 is used, 2 or more means shared, and a write must copy first.

Those are two different questions. L1/L2 answer which guest data lives where; refcounts answer which host clusters are allocated. A refcount of 0 does not mean the bytes were erased, and a refcount repair does not show that the guest content is correct. Internal snapshots and external backing chains are different mechanisms: an internal snapshot keeps another L1 table in the same file and raises the refcounts of the clusters it reaches, so it shares clusters with the active image through copy-on-write; an external chain spans separate files. They are not unrelated, though — an image in a backing chain can carry internal snapshots of its own, and a refcount error can affect clusters a snapshot still shares.

QCOW2 zero clusters, subclusters and external data

  • Zero clusters. A cluster flagged as zero reads as zeros; the backing file is not read for it. Old backing content does not show through.
  • Extended L2 entries. Clusters are split into 32 subclusters, each with its own allocation status, so “allocated” is decided at a finer grain than the cluster.
  • External data file. Guest data is stored in a separate file whose clusters are not refcounted; the image is incomplete without it.
  • Feature bits, compression and encryption. An implementation must refuse an image with an unknown incompatible feature bit. Compressed clusters and encrypted images further limit which parsers can read the data.

The full field layout is in the specification; recovery mostly needs to know which of these features an image uses before trusting any parser’s view of it.

Check, repair, convert, commit and rebase

QEMU’s qemu-img documentation separates operations that are easily confused:

  • Check is supported only for some formats (including qcow2, vhdx and vmdk). It has separate repair modes: one fixes only leaked clusters; the broader one fixes all error types, with what the documentation calls a higher risk of choosing the wrong fix or hiding corruption.
  • Convert creates a new output. It can carry an already-wrong logical view forward, and with its salvage option it ignores read errors and treats unreadable areas as zeros, producing a partial output.
  • Commit writes the top image’s changes into its backing file, can empty the top, and leaves images between them invalid.
  • Rebase in safe mode writes clusters into the image being rebased; in unsafe mode it changes only the backing reference.

The documentation also warns never to modify an image in use by a running VM or another process. None of these operations replaces validation, and this page deliberately gives no command lines or flag combinations. See VM, Database and Application-Container Recovery Tools for tool classes.

Preservation and validation

Preserve the outer file with known coverage: a complete copy, or one whose unread ranges are recorded. Record its logical and allocated sizes, its parent, backing and external-data dependencies, its format version and feature bits, and the tool and version used to read it. A hash fixes a particular artifact; it does not prove that the artifact is complete. A copy taken from a running VM may not represent a coherent point. See Sparse, Thin-Provisioned and Virtual Storage Recovery for size and sparseness.

Validation runs on two levels. First the container: mappings resolve, coverage is known and every dependency is present and matches its recorded identity. Then the guest: filesystems, files and databases checked on the correct view, as in How to Validate Recovered Data. Ranges that are missing, ranges defined as zero and ranges that are validly sparse are reported separately. A header that opens, or a completed refcount correction, is an intermediate result.

Practical rule

Work on a copy, record the format’s features and dependencies, and validate both the container mappings and the guest data. An image that opens, replays or converts has passed only the first of those checks.

Technical references: MS-VHDX §2.5.1.1 Payload BAT entry states — read behaviour per state, sector bitmaps · MS-VHDX §2.3.3 Log replay — replay before I/O, active sequence · QEMU: qcow2 image format — L1/L2, refcounts, features, snapshots · QEMU: qemu-img — check, convert, commit, rebase. Header selection, sector bitmaps and the parent locator are in MS-VHDX §2.2.2, §2.4 and §2.6.2.6.3. MS-VHDX was checked in its current online version (8.0, April 2024); the QEMU pages are development documentation for version 11.1.50, and installed releases may differ.

Related: Virtual Disk Snapshot and Differencing-Chain Recovery · Virtual Machine Disk Corruption · Filesystem Journals and Crash Consistency · Sparse, Thin-Provisioned and Virtual Storage Recovery · VM, Database and Application-Container Recovery Tools