Introduction
VHD is a file that acts as the hard disk of a virtual machine. It was created by Connectix for Virtual PC, which Microsoft bought in 2003, and Microsoft later published the specification. VHD files are used by Hyper-V and Azure, and Windows can attach one as an ordinary drive.
A VHD can be at most about 2 TB. Its successor, VHDX, lifts that limit and is more resilient to power failures, but VHD remains widely supported.
Structure
Every VHD ends with a 512-byte footer
(511 bytes in images from before Virtual PC 2004)
that starts with the cookie conectix. Its fields are big-endian and include the creator application,
the size of the disk, its geometry, a checksum, a unique ID and the disk type: 2 for a
fixed disk, 3 for a dynamic disk and 4 for a differencing disk.
A fixed disk is simple: the raw contents of the disk, sector by sector, followed by the
footer. A dynamic disk only stores
the parts that have been written. It starts with a copy of the footer, then a 1024-byte
dynamic disk header beginning cxsparse, then
a block allocation table that maps each block of the disk, 2 MB by default, to its place
in the file. Each stored block begins with a bitmap of which sectors in it hold data. A
differencing disk has the same layout and records only the changes from a parent disk.
0Footer copy cookieconectix, sizes, disk type512 bytes512Dynamic disk header cookiecxsparse, table offset, block size1,024 bytes1536Block allocation table one entry per block of the diskvaries…Data blocks each a sector bitmap then the block’s sectorsrepeatedendFooter the last 512 bytes of the file512 bytes
Not described
The reference follows Microsoft’s published specification in full. The viewer reads fixed, dynamic and differencing disks:
the footer (and its copy at the start, and the 511-byte footer of Virtual PC before 2004), its checksum, the dynamic disk
header, the block allocation table, the block bitmaps and the parent locators. It opens the disk’s contents as a file, so the
partition table and file systems inside can be read; a disk of terabytes opens without reading its size, because only the
table is read. It was built and tested with images made from Microsoft’s specification (version 1.0, 11 October 2006) and
with one real Virtual PC image; qemu-img was not available, so the contents were compared with the raw disk each
test image was made from.
Known, not yet covered
- Viewer: a differencing disk’s parent is not opened (it is another file): the sectors that belong to the parent read as zeros and the Block allocation table says which blocks they are. The parent locators and the parent’s name, unique ID and time stamp are shown, so the parent can be opened by hand; this limit comes from a viewer that is given one file at a time.
- Viewer: split images (the
.v01,.v02… files of Virtual PC 2004 and earlier, described under dynamic disks) are not joined: only the file given is read. - Viewer: the specification does not say which bit of a block bitmap byte is the first sector, nor the byte order of the UTF-16 text of a parent locator; the viewer reads the first sector as the most significant bit and a locator as little-endian (big-endian when its bytes say so), as other implementations do, and no file written by Hyper-V was available to check either.
Unknown
- Reference: the creator codes other than
vpcandvs(listed here) come from libvhdi and QEMU; Microsoft’s specification only says other programs use their own identifiers, and no public source lists them all.
Links
Software
- Hyper-V – Microsoft’s virtualisation, built into Windows and Windows Server (Windows)
- Windows Disk Management – attaches VHD files as drives (Windows)
- VirtualBox – free virtual machine software that uses VHD disks (Windows, macOS, Linux)
- QEMU and qemu-img – run and convert VHD files (Windows, macOS, Linux)
- 7-Zip – free archiver that opens VHD files like an archive (Windows)
Developers
- libguestfs – library and tools for reading and changing the files inside disk images, with bindings for Python, Perl and other languages (C)