Skip to content

Commit

Permalink
gcs: Add SCSIDevice type with remove operation
Browse files Browse the repository at this point in the history
SCSI devices must be unplugged by the guest before removal on the host
side, to ensure smooth operation. Previously a SCSI device was unplugged
when a LCOWMappedVirtualDisk entry was removed. However, we want to
support multiple mounts per disk, which means we need to decouple unplug
from unmount.

This change introduces a new SCSIDevice resource type that has a remove
operation that can be used by the host to trigger an explicit unplug via
SCSI.

This is a breaking change to the bridge protocol:
- With new host/old guest, the host will attempt a SCSIDevice remove
  which will fail due to being unsupported by the guest.
- With old host/new guest, the host will expect the device to be
  unplugged when the disk is removed, which will no longer occur.

Signed-off-by: Kevin Parsons <kevpar@microsoft.com>
  • Loading branch information
kevpar committed Apr 23, 2023
1 parent 064ecea commit f2eb567
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions internal/guest/prot/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ func UnmarshalContainerModifySettings(b []byte) (*ContainerModifySettings, error

// Fill in the ResourceType-specific fields.
switch msr.ResourceType {
case guestresource.ResourceTypeSCSIDevice:
msd := &guestresource.SCSIDevice{}
if err := commonutils.UnmarshalJSONWithHresult(msrRawSettings, msd); err != nil {
return &request, errors.Wrap(err, "failed to unmarshal settings as SCSIDevice")
}
msr.Settings = msd
case guestresource.ResourceTypeMappedVirtualDisk:
mvd := &guestresource.LCOWMappedVirtualDisk{}
if err := commonutils.UnmarshalJSONWithHresult(msrRawSettings, mvd); err != nil {
Expand Down
21 changes: 20 additions & 1 deletion internal/guest/runtime/hcsv2/uvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,8 @@ func (h *Host) CreateContainer(ctx context.Context, id string, settings *prot.VM

func (h *Host) modifyHostSettings(ctx context.Context, containerID string, req *guestrequest.ModificationRequest) (err error) {
switch req.ResourceType {
case guestresource.ResourceTypeSCSIDevice:
return modifySCSIDevice(ctx, req.RequestType, req.Settings.(*guestresource.SCSIDevice))
case guestresource.ResourceTypeMappedVirtualDisk:
mvd := req.Settings.(*guestresource.LCOWMappedVirtualDisk)
// find the actual controller number on the bus and update the incoming request.
Expand Down Expand Up @@ -937,6 +939,23 @@ func newInvalidRequestTypeError(rt guestrequest.RequestType) error {
return errors.Errorf("the RequestType %q is not supported", rt)
}

func modifySCSIDevice(
ctx context.Context,
rt guestrequest.RequestType,
msd *guestresource.SCSIDevice,
) error {
switch rt {
case guestrequest.RequestTypeRemove:
cNum, err := scsi.ActualControllerNumber(ctx, msd.Controller)
if err != nil {
return err
}
return scsi.UnplugDevice(ctx, cNum, msd.Lun)
default:
return newInvalidRequestTypeError(rt)
}
}

func modifyMappedVirtualDisk(
ctx context.Context,
rt guestrequest.RequestType,
Expand Down Expand Up @@ -977,7 +996,7 @@ func modifyMappedVirtualDisk(
return err
}
}
return scsi.UnplugDevice(ctx, mvd.Controller, mvd.Lun)
return nil
default:
return newInvalidRequestTypeError(rt)
}
Expand Down
9 changes: 9 additions & 0 deletions internal/protocol/guestresource/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (
// ResourceTypeMappedDirectory is the modify resource type for mapped
// directories
ResourceTypeMappedDirectory guestrequest.ResourceType = "MappedDirectory"
// ResourceTypeSCSIDevice is the modify resources type for SCSI devices.
// Currently it only supports Remove, to cleanly remove a SCSI device.
ResourceTypeSCSIDevice guestrequest.ResourceType = "SCSIDevice"
// ResourceTypeMappedVirtualDisk is the modify resource type for mapped
// virtual disks
ResourceTypeMappedVirtualDisk guestrequest.ResourceType = "MappedVirtualDisk"
Expand Down Expand Up @@ -64,6 +67,12 @@ type WCOWCombinedLayers struct {

// Defines the schema for hosted settings passed to GCS and/or OpenGCS

// ResourceTypeSCSIDevice represents a SCSI disk that is attached to the system.
type SCSIDevice struct {
Controller uint8 `json:"Controller,omitempty"`
Lun uint8 `json:"Lun,omitempty"`
}

// LCOWMappedVirtualDisk represents a disk on the host which is mapped into a
// directory in the guest in the V2 schema.
type LCOWMappedVirtualDisk struct {
Expand Down

0 comments on commit f2eb567

Please sign in to comment.