-
Notifications
You must be signed in to change notification settings - Fork 192
[WIP] vhdx: add pure-Rust VHDX parser and disk layer #3347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jstarks
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
jstarks:vhdx
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # VHDX parser | ||
|
|
||
| The `vhdx` crate (`vm/devices/storage/vhdx/`) is a pure-Rust | ||
| implementation of the | ||
| [VHDX format specification](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-vhdx/). | ||
| It supports dynamic, fixed, and differencing VHDX virtual hard disk | ||
| files on all platforms — no Windows APIs or kernel drivers required. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Create** and **open** VHDX files (read-only or writable) | ||
| - Dynamic block allocation with four-priority free space management | ||
| - Write-ahead log (WAL) for crash-consistent metadata updates | ||
| - Sector bitmap tracking for partially-present (differencing) blocks | ||
| - Block trim/unmap with multiple modes (file space, free space, zero, | ||
| transparent, soft-anchor removal) | ||
| - Concurrent flush coalescing | ||
| - Parent locator parsing for differencing disk chains | ||
|
|
||
| ## Architecture | ||
|
|
||
| A VHDX file stores a virtual disk as a collection of fixed-size data | ||
| blocks (default 2 MiB) tracked by a Block Allocation Table (BAT). | ||
| The crate's write path uses a three-stage pipeline for crash | ||
| consistency: | ||
|
|
||
| ```text | ||
| ┌───────────┐ commit ┌──────────┐ apply ┌────────────┐ | ||
| │ Cache │ ──────────►│ Log Task │ ─────────►│ Apply Task │ | ||
| │ (dirty │ dirty │ (WAL │ logged │ (final │ | ||
| │ pages) │ pages │ writer) │ pages │ offsets) │ | ||
| └───────────┘ └──────────┘ └────────────┘ | ||
| ``` | ||
|
|
||
| 1. The **cache** accumulates dirty 4 KiB metadata pages (BAT entries, | ||
| sector bitmap bits). When the dirty count reaches a threshold or | ||
| `flush()` is called, pages are committed to the log task. | ||
| 2. The **log task** writes WAL entries to the circular log region in | ||
| the VHDX file. On crash, `replay_log()` restores metadata from | ||
| the WAL. | ||
| 3. The **apply task** writes logged pages to their final file offsets. | ||
|
|
||
| Backpressure is managed by a permit semaphore that limits in-flight | ||
| pages. A flush sequencer coalesces concurrent flush requests so at | ||
| most one file flush is in progress at a time. | ||
|
|
||
| ## Lifecycle | ||
|
|
||
| ```rust,ignore | ||
| // Create a new empty VHDX file. | ||
| create::create(&file, &mut params).await?; | ||
|
|
||
| // Open for writing. | ||
| let vhdx = VhdxFile::open(file) | ||
| .block_alignment(2 * 1024 * 1024) | ||
| .writable(&spawner) | ||
| .await?; | ||
|
|
||
| // Resolve a read — returns file-level ranges. | ||
| let mut ranges = Vec::new(); | ||
| let guard = vhdx.resolve_read(offset, len, &mut ranges).await?; | ||
| // ... perform file I/O at the returned offsets ... | ||
| drop(guard); | ||
|
|
||
| // Resolve a write — returns file-level ranges + I/O guard. | ||
| let mut ranges = Vec::new(); | ||
| let guard = vhdx.resolve_write(offset, len, &mut ranges).await?; | ||
| // ... write data at the returned offsets ... | ||
| guard.complete().await?; | ||
|
|
||
| // Flush to stable storage. | ||
| vhdx.flush().await?; | ||
|
|
||
| // Clean close (clears log GUID). | ||
| vhdx.close().await?; | ||
| ``` | ||
|
|
||
| ## I/O model | ||
|
|
||
| The crate separates **metadata I/O** from **payload I/O**. | ||
|
|
||
| Metadata I/O (headers, BAT pages, sector bitmaps, WAL entries) is | ||
| handled internally through the `AsyncFile` trait — the caller provides | ||
| an `AsyncFile` implementation at open time and never thinks about | ||
| metadata again. | ||
|
|
||
| Payload I/O (guest data reads and writes) is the caller's | ||
| responsibility. `resolve_read()` and `resolve_write()` translate | ||
| virtual disk offsets into file-level byte ranges (`ReadRange` / | ||
| `WriteRange`). The caller performs its own data I/O at those offsets | ||
| using whatever mechanism it prefers (io_uring, standard file I/O, | ||
| etc.), then finalizes metadata via the returned I/O guard. This | ||
| separation lets the caller use a different, potentially more | ||
| performant I/O path for bulk data without the crate imposing any | ||
| particular strategy. | ||
|
|
||
| - The `vhdx` crate provides the low-level VHDX format implementation | ||
| and I/O resolution API. For OpenVMM integration, the `disklayer_vhdx` | ||
| crate supplies a `LayerIo`-compatible backend used in the layered | ||
| disk storage pipeline. | ||
| - For differencing disks, the `vhdx` crate parses parent locator | ||
| metadata, while `disklayer_vhdx::chain::open_vhdx_chain` walks and | ||
| opens parent chains automatically. | ||
|
|
||
| ## Related pages | ||
|
|
||
| - [Storage backends](./storage.md) — catalog of all storage backends | ||
| - [Storage pipeline](../architecture/devices/storage.md) — how | ||
| frontends, backends, and layers connect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.