|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package mount |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/Microsoft/hcsshim/internal/protocol/guestresource" |
| 10 | +) |
| 11 | + |
| 12 | +// Controller exposes unified guest mount operations for SCSI and Plan9 devices. |
| 13 | +// |
| 14 | +// Each device type has its own dedicated Mount/Unmount API. Callers use the |
| 15 | +// output of the corresponding device controller's Add call to drive the mount: |
| 16 | +// - SCSI: [scsi.Controller.AttachDiskToVM] returns a VMSlot → [Controller.MountSCSI] |
| 17 | +// - Plan9: [plan9.Controller.AddToVM] returns a shareName → [Controller.MountPlan9] |
| 18 | +type Controller interface { |
| 19 | + // MountSCSI mounts a SCSI disk (identified by controller + LUN) inside the |
| 20 | + // guest at the path described by cfg. Returns the resolved guest path. |
| 21 | + MountSCSI(ctx context.Context, controller, lun uint, cfg SCSIMountConfig) (string, error) |
| 22 | + |
| 23 | + // UnmountSCSI releases a previously mounted SCSI guest path. |
| 24 | + UnmountSCSI(ctx context.Context, guestPath string) error |
| 25 | + |
| 26 | + // MountPlan9 mounts a Plan9 share (identified by shareName returned from the |
| 27 | + // Plan9 device controller's AddToVM) inside the guest. Returns the resolved |
| 28 | + // guest path. |
| 29 | + MountPlan9(ctx context.Context, shareName string, cfg Plan9MountConfig) (string, error) |
| 30 | + |
| 31 | + // UnmountPlan9 releases a previously mounted Plan9 guest path. |
| 32 | + UnmountPlan9(ctx context.Context, guestPath string) error |
| 33 | +} |
| 34 | + |
| 35 | +// todo: maybe have a Linux controller vs windows controller |
| 36 | + |
| 37 | +// SCSIMountConfig describes how a SCSI disk should be mounted inside the guest. |
| 38 | +type SCSIMountConfig struct { |
| 39 | + // GuestPath is the path inside the guest where the disk will be mounted. |
| 40 | + // If empty, a unique path is generated automatically. |
| 41 | + GuestPath string |
| 42 | + // Partition is the target partition index (1-based) on a partitioned device. |
| 43 | + // Only supported for LCOW. |
| 44 | + Partition uint64 |
| 45 | + // ReadOnly mounts the disk read-only. |
| 46 | + ReadOnly bool |
| 47 | + // Encrypted encrypts the device with dm-crypt. Only supported for LCOW. |
| 48 | + Encrypted bool |
| 49 | + // Options are mount flags or data passed to the guest mount call. Only supported for LCOW. |
| 50 | + Options []string |
| 51 | + // EnsureFilesystem formats the mount as Filesystem if not already formatted. |
| 52 | + // Only supported for LCOW. |
| 53 | + EnsureFilesystem bool |
| 54 | + // Filesystem is the target filesystem type. Only supported for LCOW. |
| 55 | + Filesystem string |
| 56 | + // BlockDev mounts the device as a block device. Only supported for LCOW. |
| 57 | + BlockDev bool |
| 58 | + // FormatWithRefs formats the disk using refs. Only supported for WCOW scratch disks. |
| 59 | + FormatWithRefs bool |
| 60 | +} |
| 61 | + |
| 62 | +// Plan9MountConfig describes how a Plan9 share should be mounted inside the guest. |
| 63 | +type Plan9MountConfig struct { |
| 64 | + // GuestPath is the path inside the guest where the share will be mounted. |
| 65 | + // If empty, a unique path is generated automatically. |
| 66 | + GuestPath string |
| 67 | + // ReadOnly mounts the share read-only. |
| 68 | + ReadOnly bool |
| 69 | +} |
| 70 | + |
| 71 | +// linuxGuestSCSI performs LCOW SCSI guest mount/unmount operations. |
| 72 | +type linuxGuestSCSI interface { |
| 73 | + AddLCOWMappedVirtualDisk(ctx context.Context, settings guestresource.LCOWMappedVirtualDisk) error |
| 74 | + RemoveLCOWMappedVirtualDisk(ctx context.Context, settings guestresource.LCOWMappedVirtualDisk) error |
| 75 | +} |
| 76 | + |
| 77 | +// windowsGuestSCSI performs WCOW SCSI guest mount/unmount operations. |
| 78 | +type windowsGuestSCSI interface { |
| 79 | + AddWCOWMappedVirtualDisk(ctx context.Context, settings guestresource.WCOWMappedVirtualDisk) error |
| 80 | + AddWCOWMappedVirtualDiskForContainerScratch(ctx context.Context, settings guestresource.WCOWMappedVirtualDisk) error |
| 81 | + RemoveWCOWMappedVirtualDisk(ctx context.Context, settings guestresource.WCOWMappedVirtualDisk) error |
| 82 | +} |
| 83 | + |
| 84 | +// linuxGuestPlan9 performs Plan9 guest mount/unmount operations in an LCOW guest. |
| 85 | +type linuxGuestPlan9 interface { |
| 86 | + AddLCOWMappedDirectory(ctx context.Context, settings guestresource.LCOWMappedDirectory) error |
| 87 | + RemoveLCOWMappedDirectory(ctx context.Context, settings guestresource.LCOWMappedDirectory) error |
| 88 | +} |
| 89 | + |
| 90 | +// ============================================================================== |
| 91 | +// Internal data structures |
| 92 | +// ============================================================================== |
| 93 | + |
| 94 | +// refTracker holds the per-mount concurrency and lifecycle fields shared by |
| 95 | +// both scsiMount (SCSI) and plan9Mount (Plan9). |
| 96 | +type refTracker struct { |
| 97 | + // mu serializes state transitions and broadcasts completion to concurrent |
| 98 | + // waiters: a goroutine that finds a Pending entry simply locks mu and waits |
| 99 | + // for the owner to move the state to Mounted or Invalid. |
| 100 | + mu sync.Mutex |
| 101 | + |
| 102 | + // state is the forward-only lifecycle position of this mount. |
| 103 | + // Access must be guarded by mu. |
| 104 | + state mountState |
| 105 | + |
| 106 | + // stateErr records the error that caused a transition to mountInvalid. |
| 107 | + // Waiters that find the mount in the invalid state return this error so |
| 108 | + // every caller sees the original failure reason. |
| 109 | + stateErr error |
| 110 | + |
| 111 | + // refCount is the number of active callers sharing this mount. |
| 112 | + // Access must be guarded by mu. |
| 113 | + refCount uint |
| 114 | +} |
| 115 | + |
| 116 | +// scsiMount tracks one SCSI disk mounted in the guest. |
| 117 | +type scsiMount struct { |
| 118 | + refTracker |
| 119 | + |
| 120 | + guestPath string |
| 121 | + controller uint |
| 122 | + lun uint |
| 123 | + config *SCSIMountConfig |
| 124 | +} |
| 125 | + |
| 126 | +// plan9Mount tracks one Plan9 share mounted in the guest. |
| 127 | +type plan9Mount struct { |
| 128 | + refTracker |
| 129 | + |
| 130 | + // guestPath is the path inside the guest where the share is mounted. |
| 131 | + guestPath string |
| 132 | + // shareName is the Plan9 share name returned by [plan9.Controller.AddToVM]. |
| 133 | + shareName string |
| 134 | + config *Plan9MountConfig |
| 135 | +} |
0 commit comments