Documentation
¶
Overview ¶
Package libvhdi provides a pure-Go implementation of the VHD and VHDX virtual disk format support.
This package implements read support for the VHD (Virtual Hard Disk) and VHDX (Virtual Hard Disk v2) formats, written against the Microsoft VHD Image Format Specification and the VHDX Format Specification. It is an independent implementation and shares no code with any C library.
Supported features:
- VHD fixed, dynamic and differencing disks
- VHDX fixed, dynamic and differencing disks
- Sector-granular differencing resolution across a parent chain
- Automatic parent chain resolution, verified against each child's metadata
- VHDX log replay into a read-only in-memory overlay
- Sparse-aware extent mapping with per-link provenance
- Per-format checksum verification
Reads are read-only throughout: no code path writes to an image, and log replay is applied to an overlay so a forensic copy stays byte-identical.
Example usage:
disk, err := libvhdi.OpenFile("disk.vhd")
if err != nil {
log.Fatal(err)
}
defer disk.Close()
buf := make([]byte, 512)
n, err := disk.ReadAt(buf, 0)
if err != nil {
log.Fatal(err)
}
Index ¶
- Constants
- Variables
- func GUIDString(guid [16]byte) string
- func ParseGUID(s string) ([16]byte, error)
- func Version() string
- type ChainEntry
- type DiscoverOptions
- type Disk
- type DiskInfo
- type Extent
- type ExtentKind
- type FooterSource
- type Geometry
- type Lineage
- type LogReplayStats
- type Node
- type Options
- type ParentRequest
- type ParentResolver
- type ParentResolverFunc
- type ParentSource
- type Provenance
- type Role
- type Run
- type StreamOptions
- type StructuralError
- type Tree
- type Warning
- type WarningKind
Constants ¶
const ( FormatUnknown = types.FileFormatUnknown FormatVHD = types.FileFormatVHD FormatVHDX = types.FileFormatVHDX )
FileFormat constants (re-exported for convenience).
const ( DiskTypeFixed = types.DiskTypeFixed DiskTypeDynamic = types.DiskTypeDynamic DiskTypeDifferential = types.DiskTypeDifferential )
DiskType constants (re-exported for convenience).
const ( // RoleUnknown means the role could not be determined, which is the case for // an image that could not be read. RoleUnknown = reader.RoleUnknown // RoleBase is the disk a chain starts from. RoleBase = reader.RoleBase // RoleCheckpoint is a differencing image holding the writes made since the // checkpoint below it was taken. RoleCheckpoint = reader.RoleCheckpoint )
Image roles within a chain.
const ( // ExtentZero has no backing bytes anywhere in the chain and reads as zeroes. ExtentZero = reader.ExtentZero // ExtentMapped is backed by a contiguous run of bytes in one of the files. ExtentMapped = reader.ExtentMapped // ExtentUnresolved resolves to a parent that is not attached. ExtentUnresolved = reader.ExtentUnresolved // ExtentZeroedByChild was explicitly cleared by a differencing disk, // overriding whatever its parent held there. It reads as zeroes like // ExtentZero, but it is a write rather than an absence -- which is how a // deletion shows up at block level. VHDX only; VHD cannot express it. ExtentZeroedByChild = reader.ExtentZeroedByChild )
Extent kinds.
const ( // VHDX image. FooterSourceUnknown = reader.FooterSourceUnknown FooterSourceTrailing = reader.FooterSourceTrailing // Virtual PC before the format was documented. FooterSourceTrailingLegacy = reader.FooterSourceTrailingLegacy // disks carry so a damaged tail stays recoverable. FooterSourceMirror = reader.FooterSourceMirror )
VHD footer copies, in the order the open path tries them.
const ( // so the image is damaged even though it opened. WarningFooterRecovered = reader.WarningFooterRecovered // WarningParentTimestampMismatch means a differencing child records a // different modification time for its parent than the file found carries, // which can mean the parent has been written to since the child was made. WarningParentTimestampMismatch = reader.WarningParentTimestampMismatch // WarningParentIdentityUnverifiable means the child records no parent // identifier, so its parent was matched on virtual size alone. WarningParentIdentityUnverifiable = reader.WarningParentIdentityUnverifiable // WarningParentIdentityUnchecked means identity verification was disabled by // Options.AllowParentGUIDMismatch. WarningParentIdentityUnchecked = reader.WarningParentIdentityUnchecked )
Warning kinds.
const Author = "aoiflux"
Author identifies the project the library belongs to.
const DefaultMaxChainDepth = reader.DefaultMaxChainDepth
DefaultMaxChainDepth bounds how many disks a differencing chain may contain.
const ModulePath = buildinfo.ModulePath
ModulePath is this library's Go module path. It is the key under which the module appears in a consuming binary's build information.
Variables ¶
var ( // ErrParentRequired is returned when a read on a differencing disk resolves // to a parent that has not been attached. Differencing disks fail closed: // silently returning zeroes would be indistinguishable from genuine disk // contents. ErrParentRequired = reader.ErrParentRequired // ErrParentNotFound is returned when a resolver exhausted its search // without locating the parent image. ErrParentNotFound = reader.ErrParentNotFound // ErrParentMismatch is returned when a located image does not match the // parent identity recorded in the child. ErrParentMismatch = reader.ErrParentMismatch // ErrChainTooDeep is returned when a parent chain exceeds the configured // maximum depth. ErrChainTooDeep = reader.ErrChainTooDeep // ErrChainCycle is returned when a parent chain refers back to a disk // already in the chain. ErrChainCycle = reader.ErrChainCycle // ErrSizeUnknown is returned when an image's size can neither be derived // from the reader nor was supplied via Options.Size. ErrSizeUnknown = reader.ErrSizeUnknown // ErrCorruptImage is returned when an image's headers are inconsistent, // fail a checksum, or describe structures that do not fit the file. ErrCorruptImage = reader.ErrCorruptImage // ErrUnsupportedFeature is returned when an image is well-formed but uses // something this library does not implement. // // It is deliberately distinct from ErrCorruptImage. One says the file is // broken; the other says to try a different tool. Conflating them sends an // examiner looking for damage that is not there. ErrUnsupportedFeature = reader.ErrUnsupportedFeature // ErrDirtyImage is returned when a VHDX log cannot be replayed, so the // image may not reflect its last committed state. Set // Options.AllowDirtyImage to read it anyway. ErrDirtyImage = reader.ErrDirtyImage // ErrSplitImage is returned for a file that looks like one piece of a split // or segmented disk. // // Neither specification defines such a layout: a multi-file disk in VHD or // VHDX is always a differencing chain, and split images are a VMDK and VDI // concept. Such a file is a different format, not a VHD this library is // failing to read. ErrSplitImage = reader.ErrSplitImage // ErrNotFound is returned when a disk named by identity or path is not in a // chain tree. ErrNotFound = reader.ErrNotFound )
Errors reported when opening or reading a disk.
var NoParentResolution = reader.NoParentResolution
NoParentResolution suppresses automatic parent chain resolution, so a caller can attach parents by hand with SetParent.
It exists so that a nil Options.ParentResolver can mean "the default" without leaving a caller who genuinely wants no resolution unable to say so.
Functions ¶
func GUIDString ¶ added in v0.3.0
GUIDString renders a 16-byte GUID in the canonical RFC 4122 form, "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".
This is not a plain hex dump. Both formats store the first three fields little-endian and the rest big-endian, so printing the bytes in order produces a string that looks right and names a different GUID -- which is why this is exported rather than left for each caller to re-derive.
func ParseGUID ¶ added in v0.3.0
ParseGUID parses a GUID in the canonical form GUIDString produces. It is the inverse of GUIDString, so a value can survive a round trip through a report.
func Version ¶
func Version() string
Version reports the libvhdi module version recorded in the calling binary's build information, for example "v0.3.0".
The value is read from the metadata the Go toolchain embeds at link time rather than from a constant maintained by hand, so it cannot drift away from the tag consumers actually resolve. A hand-maintained constant did drift exactly that way before v0.3.0, which is why this is a function.
Three results are possible:
- The module version, when libvhdi is a dependency of the running binary.
- "(devel)", when libvhdi is the main module, which is the case while running its own tests and examples.
- "unknown", when build information is unavailable at all. This happens in binaries built without module information, and it is the reason the result is a string rather than a parsed version: a forensic report must be able to record "the producing tool could not name its own version" without the report generator failing.
The returned value is suitable for recording in a report's provenance. It is not suitable for a version comparison without parsing, and callers should not attempt one against "(devel)" or "unknown".
Types ¶
type ChainEntry ¶
type ChainEntry = reader.ChainEntry
ChainEntry describes one disk in a differencing chain: the set of files that together constitute the device, which is what an evidence record must name.
type DiscoverOptions ¶ added in v0.3.0
type DiscoverOptions = reader.DiscoverOptions
DiscoverOptions controls a directory scan. The zero value is usable.
type Disk ¶
type Disk = reader.VirtualDisk
Disk represents an open VHD or VHDX virtual disk. Implements io.ReaderAt.
func Open ¶
Open opens a VHD or VHDX image from r, detecting the format automatically and deriving the image size from the reader when possible.
Readers exposing Size, Stat or Seek are all handled, covering *os.File, *bytes.Reader, *strings.Reader, io.SectionReader and fs.File. A bare io.ReaderAt with none of those must set Options.Size for VHD images; VHDX locates every structure from fixed offsets and needs no size.
When opts.ParentResolver is set, differencing parent chains are resolved automatically, so the caller gets a correct contiguous device with no manual SetParent wiring. opts may be nil.
func OpenFile ¶
OpenFile opens a VHD or VHDX virtual disk file by path. Format detection is automatic. The caller must call disk.Close() when done.
func OpenFileWith ¶
OpenFileWith opens a VHD or VHDX file by path with explicit options.
When opts.ParentResolver is nil it defaults to searching the image's own directory, which is where a differencing chain's parents conventionally live.
type DiskInfo ¶ added in v0.3.0
DiskInfo is what a header-only probe can learn about an image.
type Extent ¶
Extent describes a contiguous run of the virtual disk and what backs it, so a caller can read only what is mapped and skip sparse regions. For a differencing chain each mapped extent also identifies which disk supplies it.
type FooterSource ¶ added in v0.3.0
type FooterSource = reader.FooterSource
FooterSource identifies which copy of the VHD footer an image was opened from, so a report can distinguish an intact read from a recovered one.
type Geometry ¶ added in v0.3.0
Geometry describes the physical shape an image reports for itself: its virtual size, block and sector sizes, and, for VHD, its CHS geometry.
type Lineage ¶ added in v0.3.0
Lineage is one path through a tree, root first: the set of files that together constitute one device.
type LogReplayStats ¶
type LogReplayStats = reader.LogReplayStats
LogReplayStats describes what a VHDX log replay applied.
type Options ¶
Options controls how a disk is opened. The zero value is safe: parent GUIDs are verified, and differencing disks with no parent fail closed on read.
type ParentRequest ¶
type ParentRequest = reader.ParentRequest
Parent resolution types. A ParentResolver locates the parent image of a differencing disk; DirParentResolver and FSParentResolver cover the common cases.
type ParentResolver ¶
type ParentResolver = reader.ParentResolver
Parent resolution types. A ParentResolver locates the parent image of a differencing disk; DirParentResolver and FSParentResolver cover the common cases.
func DirParentResolver ¶
func DirParentResolver(dirs ...string) ParentResolver
DirParentResolver returns a ParentResolver that searches the child's own directory first, then each supplied directory.
func FSParentResolver ¶
func FSParentResolver(fsys fs.FS) ParentResolver
FSParentResolver returns a ParentResolver that searches an fs.FS.
type ParentResolverFunc ¶
type ParentResolverFunc = reader.ParentResolverFunc
Parent resolution types. A ParentResolver locates the parent image of a differencing disk; DirParentResolver and FSParentResolver cover the common cases.
type ParentSource ¶
type ParentSource = reader.ParentSource
Parent resolution types. A ParentResolver locates the parent image of a differencing disk; DirParentResolver and FSParentResolver cover the common cases.
type Provenance ¶ added in v0.3.0
type Provenance = reader.Provenance
Provenance describes what produced an image, when, and how much of the read can be trusted. It is what makes a forensic report self-describing.
type Run ¶ added in v0.3.0
Run is one contiguous piece of a device as Disk.Stream emits it: an extent and, for a mapped extent, its bytes.
type StreamOptions ¶ added in v0.3.0
type StreamOptions = reader.StreamOptions
StreamOptions controls a Disk.Stream walk. The zero value is usable.
type StructuralError ¶ added in v0.3.0
type StructuralError = reader.StructuralError
StructuralError says what was being parsed, where in the image, and what was wrong with it, so an error becomes a starting point for examining the file by hand rather than only a statement that something failed.
It wraps ErrCorruptImage or ErrUnsupportedFeature, so errors.Is still answers the coarse question.
type Tree ¶ added in v0.3.0
Tree is the parent-to-children graph of a set of images.
func DiscoverChain ¶ added in v0.3.0
DiscoverChain scans a directory of images and builds the parent-to-children tree they form, reading only headers.
Chain walks upward from one image to its ancestors, which is the wrong direction for Hyper-V checkpoints: checkpoints branch, and from a leaf a sibling branch is invisible. This sees the whole shape.
type Warning ¶ added in v0.3.0
Warning records something suspicious about an image or its parent chain that does not prevent it from being read, such as having been recovered from a fallback footer or having accepted a parent whose identity could not be verified.
type WarningKind ¶ added in v0.3.0
type WarningKind = reader.WarningKind
WarningKind names a class of warning.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bat provides Block Allocation Table (BAT) parsing and lookup logic.
|
Package bat provides Block Allocation Table (BAT) parsing and lookup logic. |
|
Package block provides block-level reading and sector I/O for virtual disks.
|
Package block provides block-level reading and sector I/O for virtual disks. |
|
Package diff implements differencing (child) disk resolution over a parent chain.
|
Package diff implements differencing (child) disk resolution over a parent chain. |
|
examples
|
|
|
chain
command
chain describes the checkpoint tree formed by a directory of VHD or VHDX images, or the differencing chain behind a single image.
|
chain describes the checkpoint tree formed by a directory of VHD or VHDX images, or the differencing chain behind a single image. |
|
open
command
open prints metadata and geometry information for a VHD or VHDX virtual disk image.
|
open prints metadata and geometry information for a VHD or VHDX virtual disk image. |
|
readat
command
readat reads bytes from a VHD or VHDX virtual disk at a given offset and hex-dumps them.
|
readat reads bytes from a VHD or VHDX virtual disk at a given offset and hex-dumps them. |
|
report
command
report emits a versioned JSON document describing a VHD or VHDX image.
|
report emits a versioned JSON document describing a VHD or VHDX image. |
|
internal
|
|
|
binaryutil
Package binaryutil provides endian-aware binary parsing utilities for the vhdi library.
|
Package binaryutil provides endian-aware binary parsing utilities for the vhdi library. |
|
buildinfo
Package buildinfo reports the libvhdi module's own version.
|
Package buildinfo reports the libvhdi module's own version. |
|
vhdxlog
Package vhdxlog parses and replays the VHDX log.
|
Package vhdxlog parses and replays the VHDX log. |
|
Package metadata provides VHDX metadata table parsing and value extraction.
|
Package metadata provides VHDX metadata table parsing and value extraction. |
|
VHD Dynamic Disk Header Parser
|
VHD Dynamic Disk Header Parser |
|
Package report defines versioned JSON documents describing VHD and VHDX images.
|
Package report defines versioned JSON documents describing VHD and VHDX images. |
|
Package types defines all data structures, enums, and constants for the libvhdi library.
|
Package types defines all data structures, enums, and constants for the libvhdi library. |
|
Package vhdimap declares the bridge between libvhdi's block-level view of a virtual disk and a filesystem's file-level view of it.
|
Package vhdimap declares the bridge between libvhdi's block-level view of a virtual disk and a filesystem's file-level view of it. |