Skip to content

ttrpc: attach Windows physical and virtual disks via registered handles - #5158

Closed
Daman Mulye (damanm24) wants to merge 16 commits into
microsoft:mainfrom
damanm24:wsl-upstream-physical-disks
Closed

Daman Mulye (damanm24) wants to merge 16 commits into
microsoft:mainfrom
damanm24:wsl-upstream-physical-disks

Conversation

@damanm24

@damanm24 Daman Mulye (damanm24) commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds support for attaching a host disk opened by the client, either a physical disk such as \\.\PhysicalDriveN or a VHD/VHDX, to a VM through the ttrpc/gRPC interface on Windows. Because OpenVMM uses the client's handle instead of opening a path itself, the disk is accessed with the client's rights rather than OpenVMM's own.

Callers open the disk, register the handle through the resource-passing protocol, and reference it by name with a DiskHandle in DiskBackend.physical_disk or DiskBackend.virtual_disk. Physical disks must be opened for synchronous I/O. Virtual disks must be opened with OpenVirtualDisk and attached for raw access with ATTACH_VIRTUAL_DISK_FLAG_NO_LOCAL_HOST, matching how OpenVMM opens VHDs by path. OpenVMM duplicates the handle while processing the request, so the name can be deregistered once the RPC succeeds. Either kind can also be used as the read-only lower disk of a memory_diff overlay.

The new disk_physical crate queries the disk's size and logical and physical sector sizes from the device and fails if the reported geometry is invalid. Overlapped handles are rejected, since I/O is issued through FileDisk using blocking reads and writes. To support handles opened with FILE_FLAG_NO_BUFFERING, FileDisk can now opt into 4 KiB-aligned I/O buffers. Disks whose logical sector size, or whose buffer alignment requirement as reported by FileAlignmentInfo, exceeds 4 KiB are rejected. Ordinary file disks keep their existing allocation behavior.

Virtual disk handles are wrapped as VHDMP disks, so I/O, discard, and read-only handling match VHDs that OpenVMM opens by path. VhdmpDisk now rejects handles that are not attached for raw access with a specific error rather than a failed metadata query.

Add an RPC disk backend for handles duplicated into the VMM process. Duplicate the supplied handle for owned lifetime management and obtain block-device length and sector geometry when regular file metadata is unavailable. Existing file-backed disks retain their behavior.

Source-commit: 6f71c4b

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 72311522-f27a-4daa-988b-80d159a42ac7
Preserve main's disk backend field numbers and move HandleDisk to field 4. Adapt handle dispatch to the refactored disk builder.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c16a3c47-a7cc-4ff1-b115-8e4fc71c0627
Copilot AI balanced review requested due to automatic review settings November 19, 2026 17:12
@github-actions github-actions Bot added the unsafe Related to unsafe code label Nov 19, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The physical-disk test lacks an elevation requirement or default opt-out, and accepted unbuffered disk handles can cause guest I/O failures because read/write buffers are not guaranteed to be sector-aligned.

2 open issues
What changed in this PR

Adds Windows physical-disk passthrough through the existing FileDisk backend. Privileged clients can register an opened disk handle for NVMe or virtio-blk devices without requiring OpenVMM itself to run elevated.

Changes:

  • Queries device capacity and sector geometry when file metadata is unavailable.
  • Adds HandleDisk configuration, registered-handle resolution, and API documentation.
  • Adds handle-resolution tests and a physical-disk integration test.
File Description
vmm_tests/​vmm_tests/​tests/​tests/​ttrpc/​handle_passing.rs Adds physical-disk registration and guest/host I/O testing.
vmm_tests/​vmm_tests/​tests/​tests/​ttrpc.rs Updates the comment describing handle-passing tests.
vm/​devices/​storage/​disk_file/​src/​windows.rs Implements Windows device capacity and sector-geometry queries.
vm/​devices/​storage/​disk_file/​src/​lib.rs Adds device-metadata fallback while preserving regular-file behavior.
vm/​devices/​storage/​disk_file/​Cargo.toml Adds Windows API dependencies for device queries.
openvmm/​openvmm_ttrpc_vmservice/​src/​vmservice.proto Defines the registered-handle disk backend.
openvmm/​openvmm_ttrpc_vmservice/​src/​resource_passing.md Documents disk handles as registered resources.
openvmm/​openvmm_entry/​src/​ttrpc/​mod.rs Resolves disk handles for NVMe and virtio-blk and adds tests.
Guide/​src/​reference/​openvmm/​management/​grpc.md Documents named physical-disk handles in the management API.
Cargo.lock Records the Windows dependency for disk_file.

Review effort: Balanced

Comment thread vm/devices/storage/disk_file/src/lib.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 19, 2026 17:38
@github-actions github-actions Bot added the unsafe Related to unsafe code label Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new writable NVMe path can acknowledge FUA writes without ensuring durability, and image-initialization failures can leave VHDs attached to the host.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Dismount VHD when disk initialization fails

vmm_tests/​vmm_tests/​tests/​tests/​ttrpc/​handle_passing.rs:411

If Mount-DiskImage succeeds but Get-Disk, either Set-Disk, or parsing the returned disk number fails, this function returns before constructing MountedImage. Its Drop cleanup therefore never runs, leaving the VHD attached to the host and preventing removal of its backing temporary file. Please establish a cleanup guard immediately after mounting (or explicitly dismount on every subsequent error), and retain it until initialization succeeds.

2 resolved since last review

Review effort: Balanced

Comment thread openvmm/openvmm_entry/src/ttrpc/mod.rs Resolved
Copilot AI balanced review requested due to automatic review settings November 19, 2026 18:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Physical-disk passthrough changes Windows I/O and alignment guarantees, but elevated runtime validation for native 4Kn and unbuffered handles was unavailable, requiring human review of platform-specific evidence.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 20, 2026 16:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A failure after mounting but before constructing the cleanup guard can leave the VHD attached and its temporary backing file locked.

1 open issue

Review effort: Balanced

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 20, 2026 18:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the Windows physical-disk passthrough change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human review is needed to confirm elevated integration testing and representative 4Kn/unbuffered disk behavior because Windows physical-device I/O and geometry queries cannot be runtime-validated here.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 20, 2026 19:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The shared byte view can coexist with atomic access to the same storage, allowing safe callers to violate immutable-reference guarantees and cause cross-thread data races.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 20, 2026 20:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The review records no active issues with the Windows physical disk passthrough changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 20, 2026 20:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Windows runtime validation is unavailable, so writable raw-disk passthrough requires human verification of geometry fallback and sector-aligned I/O on representative 512e/4Kn disks, including unbuffered handles, to address data-integrity risks.

No open issues 🎉

Review effort: Balanced

Fail on invalid disk geometry, move the aligned-buffer sector size check into FileDisk, rename the proto HandleDisk to PhysicalDisk, allow a physical disk under memory_diff, attach the test VHD with virtdisk instead of PowerShell, and share pipette setup helpers across the ttrpc VMM tests.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 23:20
@damanm24 Daman Mulye (damanm24) changed the title disk_file: support Windows physical disk passthrough via registered handles disk_physical: support Windows physical disk passthrough via registered handles Nov 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The geometry check allows physical sector sizes above BounceBuffer’s 4096-byte alignment, which can cause Windows unbuffered I/O to fail on devices enforcing physical-sector alignment.

1 open issue

Review effort: Balanced

Comment thread vm/devices/storage/disk_file/src/lib.rs Resolved
Daman Mulye added 2 commits November 20, 2026 16:05
Add DiskBackend.virtual_disk, which references a VHD or VHDX handle that
the client opened with OpenVirtualDisk and attached for raw access, and
registered through the resource-passing protocol. OpenVMM wraps the
handle as a VHDMP disk, so the image is accessed with the client's
rights rather than OpenVMM's own, and I/O, discard and read-only
handling match disks that OpenVMM opens by path.

Physical and virtual disks now share a DiskHandle message, with the
DiskBackend field determining the kind of handle, and are resolved,
layered under memory diff overlays and rejected outside storage devices
through the same code. VhdmpDisk rejects handles that are not attached
for raw access with a specific error instead of a failed metadata
query. The handle-passing VMM test covers both kinds; the virtual disk
case does not require elevation.
Windows recommends aligning unbuffered I/O buffers to the physical sector
size, but only fails I/O whose buffers violate the device's alignment
requirement, which is often smaller. Query that requirement with
FileAlignmentInfo and reject disks that need more alignment than the
I/O buffers provide, instead of rejecting disks by physical sector size,
which would refuse disks that report large physical sectors but accept
4 KiB-aligned buffers.
Copilot AI balanced review requested due to automatic review settings November 21, 2026 00:10
@damanm24 Daman Mulye (damanm24) changed the title disk_physical: support Windows physical disk passthrough via registered handles ttrpc: attach Windows physical and virtual disks via registered handles Nov 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The current ID assertion cannot detect reversed layers or write-through, so behavioral tests for both disk kinds and nested overlays are needed to verify reads and backing-disk isolation.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread openvmm/openvmm_entry/src/ttrpc/mod.rs Resolved
Move physical disk passthrough (disk_physical, FileDisk aligned buffers, and its VMM test) to a separate follow-up change, since physical and virtual disks have different requirements. Registered disk handles can no longer be the lower disk of a memory diff overlay. VirtualDisk is now its own proto message, and VhdmpDisk rejects a writable disk whose handle was attached read-only.
Copilot AI balanced review requested due to automatic review settings November 21, 2026 01:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Bindings to the same registered disk lack shared I/O serialization, allowing unsafe concurrent operations on one Windows file object, while advertised physical-disk and memory-diff support is also missing.

3 open issues
1 resolved since last review

Review effort: Balanced

.resolve(handle_name)
.with_context(|| format!("failed to resolve virtual disk '{handle_name}'"))?
.into();
Ok(disk_vhdmp::OpenVhdmpDiskConfig(disk_vhdmp::Vhd::from_file(file)).into_resource())
DiskCliKind::Memory(size_bytes)
}
vmservice::disk_backend::Kind::VirtualDisk(_) => {
anyhow::bail!("virtual disk backends must be a storage device's own backend")
// A memory-backed writable overlay over a read-only lower disk.
MemoryDiffDisk memory_diff = 3;
// A VHD or VHDX opened by the client (Windows only).
VirtualDisk virtual_disk = 4;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Guide unsafe Related to unsafe code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants