Skip to content

Commit 2625d6a

Browse files
committed
implement guest manager interfaces for VM operations
Signed-off-by: Harsh Rawat <harshrawat@microsoft.com>
1 parent 7519c12 commit 2625d6a

18 files changed

Lines changed: 1121 additions & 24 deletions

internal/vm/README.md

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,83 @@ configuration, host-side management, and guest-side actions distinct so each lay
55

66
1. **Builder**: constructs an HCS compute system configuration used to create a VM.
77
2. **VM Manager**: manages host-side VM configuration and lifecycle (NICs, SCSI, VPMem, etc.).
8-
3. **Guest Manager**: intended for guest-side actions (for example, mounting a disk).
8+
3. **Guest Manager**: guest-side actions executed via the GCS connection (for example, mounting a mapped disk).
99

1010
**Note that** this layer does not store UVM host or guest side state. That will be part of the orchestration layer above it.
1111

1212
## Packages and Responsibilities
1313

1414
- `internal/vm`
15-
- Shared types used across layers (for example, `GuestOS`, `SCSIDisk`).
15+
- Shared types used across layers (currently `GuestOS`).
1616
- `internal/vm/builder`
17-
- Interface definitions for shaping the VM configuration (`Builder` interface).
18-
- Concrete implementation of `Builder` for building `hcsschema.ComputeSystem` documents.
19-
- Provides a fluent API for configuring all aspects of the VM document.
20-
- Presently, this package is tightly coupled with HCS backend.
17+
- Interface definitions for shaping the VM configuration (`BootOptions`, `MemoryOptions`, `ProcessorOptions`, `DeviceOptions`,
18+
`NumaOptions`, `StorageQoSOptions`).
19+
- Concrete implementation of `UtilityVMBuilder` for building `hcsschema.ComputeSystem` documents (`UtilityVM`).
20+
- Provies a fluent API for constructing the `hcsschema.ComputeSystem` document used to create a UVM.
21+
- Presently, this package is tightly coupled with the HCS backend.
2122
- `internal/vm/vmmanager`
2223
- Interface definitions for UVM lifecycle and host-side management.
23-
- Concrete implementation of `UVM` for running and managing a UVM instance.
24-
- Presently, this package is tightly coupled with HCS backend and only runs HCS backed UVMs.
25-
- Owns lifecycle calls (start/terminate/close) and host-side modifications (NICs, SCSI, VPMem, pipes, VSMB, Plan9).
26-
- Allows creation of the UVM using `vmmanager.Create` which takes a `Builder` and produces a running UVM.
24+
- Concrete implementation of `UtilityVM` for running and managing a UVM instance created from a builder document.
25+
- Owns lifecycle calls (start/terminate/close/pause/resume/save/wait) and host-side modifications:
26+
- Network adapters (`NetworkManager`), SCSI disks (`SCSIManager`), VPMem (`VPMemManager`), VSMB (`VSMBManager`), Plan9 (`Plan9Manager`).
27+
- Named pipes (`PipeManager`), virtual PCI devices (`PCIManager`), HvSocket services (`VMSocketManager`).
28+
- Resource updates such as CPU group/limits and memory (`ResourceManager`).
29+
- Presently, this package is tightly coupled with the HCS backend and only runs HCS-backed UVMs.
2730
- `internal/vm/guestmanager`
28-
- Reserved for guest-level actions such as mounting disks or performing in-guest configuration.
29-
- Currently empty and intended to grow as guest actions are formalized.
31+
- Interface definitions for guest-side operations executed via the GCS connection.
32+
- Manages GCS connection lifecycle, including HvSocket setup and initial guest state.
33+
- Implements operations for guest resources, split by LCOW/WCOW where needed:
34+
- Network interfaces/namespaces, mapped directories, mapped virtual disks, combined layers, block CIMs (WCOW),
35+
VPCI/VPMem devices (LCOW), and security policy operations.
36+
- Translates guest operations into GCS modify requests.
3037

3138
## Typical Flow
3239

33-
1. Build the config using the builder interfaces.
40+
1. Build the config using the builder options.
3441
2. Create the VM using the VM manager.
35-
3. Use manager interfaces for lifecycle and host-side changes.
36-
4. Use guest manager interfaces for in-guest actions (when available).
42+
3. Use manager methods for lifecycle and host-side changes.
43+
4. Establish a GCS connection and use guest manager interfaces for in-guest actions.
3744

3845
## Example (High Level)
3946

4047
```
41-
builder, _ := builder.New("owner", vm.Linux)
48+
b, _ := builder.New("owner", vm.Linux)
49+
50+
// Parse into the correct builder option.
51+
memoryOpts := builder.MemoryOptions(b)
52+
processorOpts := builder.ProcessorOptions(b)
4253
4354
// Configure the VM document.
44-
builder.Memory().SetMemoryLimit(1024)
45-
builder.Processor().SetProcessorConfig(&vm.ProcessorConfig{Count: 2})
55+
memoryOpts.SetMemoryLimit(1024)
56+
processorOpts.SetProcessorLimits(&hcsschema.VirtualMachineProcessor{Count: 2})
4657
// ... other builder configuration
4758
48-
// Create and start the VM.
49-
uvm, _ := vmmanager.Create(ctx, "uvm-id", builder)
50-
_ = uvm.LifetimeManager().Start(ctx)
59+
// Create the VM.
60+
uvm, _ := vmmanager.Create(ctx, "uvm-id", b)
61+
_ = uvm.Start(ctx)
62+
63+
// Create the Guest Connection.
64+
g, _ := guestmanager.New(ctx, uvm)
65+
66+
// Start the UVM.
67+
lifetime := vmmanager.LifetimeManager(uvm)
68+
_ = lifetime.Start(ctx)
69+
70+
// Create the connection.
71+
guest := guestmanager.Manager(g)
72+
_ = guest.CreateConnection(ctx)
5173
5274
// Apply host-side updates.
53-
_ = uvm.NetworkManager().AddNIC(ctx, nicID, endpointID, macAddr)
75+
network := vmmanager.NetworkManager(uvm)
76+
_ = network.AddNIC(ctx, nicID, &hcsschema.NetworkAdapter{})
77+
78+
// Apply guest-side updates.
79+
guestNetwork := guestmanager.LCOWNetworkManager(g)
80+
_ = guestNetwork.AddLCOWNetworkInterface(ctx, &guestresource.LCOWNetworkAdapter{})
5481
```
5582

5683
## Layer Boundaries (Quick Reference)
5784

5885
- **Builder**: static, pre-create configuration only. No host mutations.
5986
- **VM Manager**: host-side changes and lifecycle operations on an existing UVM.
60-
- **Guest Manager**: guest-side actions, scoped to work that requires in-guest context.
61-
87+
- **Guest Manager**: guest-side actions, scoped to work that requires in-guest context (GCS-backed).
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//go:build windows
2+
3+
package guestmanager
4+
5+
import (
6+
"context"
7+
8+
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
9+
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
10+
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
11+
12+
"github.com/pkg/errors"
13+
)
14+
15+
// CIMsManager exposes guest WCOW block CIM operations.
16+
type CIMsManager interface {
17+
// AddWCOWBlockCIMs adds WCOW block CIM mounts in the guest.
18+
AddWCOWBlockCIMs(ctx context.Context, settings *guestresource.CWCOWBlockCIMMounts) error
19+
// RemoveWCOWBlockCIMs removes WCOW block CIM mounts from the guest.
20+
RemoveWCOWBlockCIMs(ctx context.Context, settings *guestresource.CWCOWBlockCIMMounts) error
21+
}
22+
23+
var _ CIMsManager = (*Guest)(nil)
24+
25+
// AddWCOWBlockCIMs adds WCOW block CIM mounts in the guest.
26+
func (gm *Guest) AddWCOWBlockCIMs(ctx context.Context, settings *guestresource.CWCOWBlockCIMMounts) error {
27+
request := &hcsschema.ModifySettingRequest{
28+
GuestRequest: guestrequest.ModificationRequest{
29+
ResourceType: guestresource.ResourceTypeWCOWBlockCims,
30+
RequestType: guestrequest.RequestTypeAdd,
31+
Settings: settings,
32+
},
33+
}
34+
35+
err := gm.modify(ctx, request.GuestRequest)
36+
if err != nil {
37+
return errors.Wrap(err, "failed to add WCOW block CIMs")
38+
}
39+
40+
return nil
41+
}
42+
43+
// RemoveWCOWBlockCIMs removes WCOW block CIM mounts in the guest.
44+
func (gm *Guest) RemoveWCOWBlockCIMs(ctx context.Context, settings *guestresource.CWCOWBlockCIMMounts) error {
45+
request := &hcsschema.ModifySettingRequest{
46+
GuestRequest: guestrequest.ModificationRequest{
47+
ResourceType: guestresource.ResourceTypeWCOWBlockCims,
48+
RequestType: guestrequest.RequestTypeRemove,
49+
Settings: settings,
50+
},
51+
}
52+
53+
err := gm.modify(ctx, request.GuestRequest)
54+
if err != nil {
55+
return errors.Wrap(err, "failed to remove WCOW block CIMs")
56+
}
57+
58+
return nil
59+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//go:build windows
2+
3+
package guestmanager
4+
5+
import (
6+
"context"
7+
8+
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
9+
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
10+
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
11+
"github.com/pkg/errors"
12+
)
13+
14+
// LCOWLayersManager exposes combined layer operations in the LCOW guest.
15+
type LCOWLayersManager interface {
16+
// AddLCOWCombinedLayers adds combined layers to the LCOW guest.
17+
AddLCOWCombinedLayers(ctx context.Context, settings guestresource.LCOWCombinedLayers) error
18+
// RemoveLCOWCombinedLayers removes combined layers from the LCOW guest.
19+
RemoveLCOWCombinedLayers(ctx context.Context, settings guestresource.LCOWCombinedLayers) error
20+
}
21+
22+
var _ LCOWLayersManager = (*Guest)(nil)
23+
24+
// AddLCOWCombinedLayers adds LCOW combined layers in the guest.
25+
func (gm *Guest) AddLCOWCombinedLayers(ctx context.Context, settings guestresource.LCOWCombinedLayers) error {
26+
modifyRequest := &hcsschema.ModifySettingRequest{
27+
GuestRequest: guestrequest.ModificationRequest{
28+
ResourceType: guestresource.ResourceTypeCombinedLayers,
29+
RequestType: guestrequest.RequestTypeAdd,
30+
Settings: settings,
31+
},
32+
}
33+
34+
err := gm.modify(ctx, modifyRequest.GuestRequest)
35+
if err != nil {
36+
return errors.Wrap(err, "failed to add LCOW combined layers")
37+
}
38+
return nil
39+
}
40+
41+
// RemoveLCOWCombinedLayers removes LCOW combined layers in the guest.
42+
func (gm *Guest) RemoveLCOWCombinedLayers(ctx context.Context, settings guestresource.LCOWCombinedLayers) error {
43+
modifyRequest := &hcsschema.ModifySettingRequest{
44+
GuestRequest: guestrequest.ModificationRequest{
45+
ResourceType: guestresource.ResourceTypeCombinedLayers,
46+
RequestType: guestrequest.RequestTypeRemove,
47+
Settings: settings,
48+
},
49+
}
50+
51+
err := gm.modify(ctx, modifyRequest.GuestRequest)
52+
if err != nil {
53+
return errors.Wrap(err, "failed to remove LCOW combined layers")
54+
}
55+
return nil
56+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//go:build windows
2+
3+
package guestmanager
4+
5+
import (
6+
"context"
7+
8+
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
9+
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
10+
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
11+
12+
"github.com/pkg/errors"
13+
)
14+
15+
// WCOWLayersManager exposes combined layer operations in the WCOW guest.
16+
type WCOWLayersManager interface {
17+
// AddWCOWCombinedLayers adds combined layers to the WCOW guest.
18+
AddWCOWCombinedLayers(ctx context.Context, settings guestresource.WCOWCombinedLayers) error
19+
// AddCWCOWCombinedLayers adds combined layers to the CWCOW guest.
20+
AddCWCOWCombinedLayers(ctx context.Context, settings guestresource.CWCOWCombinedLayers) error
21+
// RemoveWCOWCombinedLayers removes combined layers from the WCOW guest.
22+
RemoveWCOWCombinedLayers(ctx context.Context, settings guestresource.WCOWCombinedLayers) error
23+
// RemoveCWCOWCombinedLayers removes combined layers from the CWCOW guest.
24+
RemoveCWCOWCombinedLayers(ctx context.Context, settings guestresource.CWCOWCombinedLayers) error
25+
}
26+
27+
var _ WCOWLayersManager = (*Guest)(nil)
28+
29+
// AddWCOWCombinedLayers adds WCOW combined layers in the guest.
30+
func (gm *Guest) AddWCOWCombinedLayers(ctx context.Context, settings guestresource.WCOWCombinedLayers) error {
31+
modifyRequest := &hcsschema.ModifySettingRequest{
32+
GuestRequest: guestrequest.ModificationRequest{
33+
ResourceType: guestresource.ResourceTypeCombinedLayers,
34+
RequestType: guestrequest.RequestTypeAdd,
35+
Settings: settings,
36+
},
37+
}
38+
39+
err := gm.modify(ctx, modifyRequest.GuestRequest)
40+
if err != nil {
41+
return errors.Wrap(err, "failed to add WCOW combined layers")
42+
}
43+
return nil
44+
}
45+
46+
// AddCWCOWCombinedLayers adds combined layers in CWCOW guest.
47+
func (gm *Guest) AddCWCOWCombinedLayers(ctx context.Context, settings guestresource.CWCOWCombinedLayers) error {
48+
modifyRequest := &hcsschema.ModifySettingRequest{
49+
GuestRequest: guestrequest.ModificationRequest{
50+
ResourceType: guestresource.ResourceTypeCWCOWCombinedLayers,
51+
RequestType: guestrequest.RequestTypeAdd,
52+
Settings: settings,
53+
},
54+
}
55+
56+
err := gm.modify(ctx, modifyRequest.GuestRequest)
57+
if err != nil {
58+
return errors.Wrap(err, "failed to add CWCOW combined layers")
59+
}
60+
return nil
61+
}
62+
63+
// RemoveWCOWCombinedLayers removes WCOW combined layers in the guest.
64+
func (gm *Guest) RemoveWCOWCombinedLayers(ctx context.Context, settings guestresource.WCOWCombinedLayers) error {
65+
modifyRequest := &hcsschema.ModifySettingRequest{
66+
GuestRequest: guestrequest.ModificationRequest{
67+
ResourceType: guestresource.ResourceTypeCombinedLayers,
68+
RequestType: guestrequest.RequestTypeRemove,
69+
Settings: settings,
70+
},
71+
}
72+
73+
err := gm.modify(ctx, modifyRequest.GuestRequest)
74+
if err != nil {
75+
return errors.Wrap(err, "failed to remove WCOW combined layers")
76+
}
77+
return nil
78+
}
79+
80+
// RemoveCWCOWCombinedLayers removes combined layers in CWCOW guest.
81+
func (gm *Guest) RemoveCWCOWCombinedLayers(ctx context.Context, settings guestresource.CWCOWCombinedLayers) error {
82+
modifyRequest := &hcsschema.ModifySettingRequest{
83+
GuestRequest: guestrequest.ModificationRequest{
84+
ResourceType: guestresource.ResourceTypeCWCOWCombinedLayers,
85+
RequestType: guestrequest.RequestTypeRemove,
86+
Settings: settings,
87+
},
88+
}
89+
90+
err := gm.modify(ctx, modifyRequest.GuestRequest)
91+
if err != nil {
92+
return errors.Wrap(err, "failed to remove CWCOW combined layers")
93+
}
94+
return nil
95+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//go:build windows
2+
3+
package guestmanager
4+
5+
import (
6+
"context"
7+
8+
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
9+
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
10+
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
11+
"github.com/pkg/errors"
12+
)
13+
14+
// LCOWDeviceManager exposes guest device operations.
15+
type LCOWDeviceManager interface {
16+
// AddVPCIDevice adds a VPCI device to the guest.
17+
AddVPCIDevice(ctx context.Context, settings guestresource.LCOWMappedVPCIDevice) error
18+
// AddVPMemDevice adds a VPMem device to the guest.
19+
AddVPMemDevice(ctx context.Context, settings guestresource.LCOWMappedVPMemDevice) error
20+
// RemoveVPMemDevice removes a VPMem device from the guest.
21+
RemoveVPMemDevice(ctx context.Context, settings guestresource.LCOWMappedVPMemDevice) error
22+
}
23+
24+
var _ LCOWDeviceManager = (*Guest)(nil)
25+
26+
// AddVPCIDevice adds a VPCI device in the guest.
27+
func (gm *Guest) AddVPCIDevice(ctx context.Context, settings guestresource.LCOWMappedVPCIDevice) error {
28+
request := &hcsschema.ModifySettingRequest{
29+
GuestRequest: guestrequest.ModificationRequest{
30+
ResourceType: guestresource.ResourceTypeVPCIDevice,
31+
RequestType: guestrequest.RequestTypeAdd,
32+
Settings: settings,
33+
},
34+
}
35+
36+
err := gm.modify(ctx, request.GuestRequest)
37+
if err != nil {
38+
return errors.Wrap(err, "failed to add VPCI device")
39+
}
40+
return nil
41+
}
42+
43+
// AddVPMemDevice adds a VPMem device in the guest.
44+
func (gm *Guest) AddVPMemDevice(ctx context.Context, settings guestresource.LCOWMappedVPMemDevice) error {
45+
request := &hcsschema.ModifySettingRequest{
46+
GuestRequest: guestrequest.ModificationRequest{
47+
ResourceType: guestresource.ResourceTypeVPMemDevice,
48+
RequestType: guestrequest.RequestTypeAdd,
49+
Settings: settings,
50+
},
51+
}
52+
53+
err := gm.modify(ctx, request.GuestRequest)
54+
if err != nil {
55+
return errors.Wrap(err, "failed to add VPMem device")
56+
}
57+
return nil
58+
}
59+
60+
// RemoveVPMemDevice removes a VPMem device in the guest.
61+
func (gm *Guest) RemoveVPMemDevice(ctx context.Context, settings guestresource.LCOWMappedVPMemDevice) error {
62+
request := &hcsschema.ModifySettingRequest{
63+
GuestRequest: guestrequest.ModificationRequest{
64+
ResourceType: guestresource.ResourceTypeVPMemDevice,
65+
RequestType: guestrequest.RequestTypeRemove,
66+
Settings: settings,
67+
},
68+
}
69+
70+
err := gm.modify(ctx, request.GuestRequest)
71+
if err != nil {
72+
return errors.Wrap(err, "failed to remove VPMem device")
73+
}
74+
return nil
75+
}

0 commit comments

Comments
 (0)