Skip to content

Commit

Permalink
try new SCSIManager exports
Browse files Browse the repository at this point in the history
  • Loading branch information
kevpar committed May 3, 2023
1 parent 82e3d2e commit cb906a3
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 197 deletions.
7 changes: 3 additions & 4 deletions internal/devices/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ func InstallDrivers(ctx context.Context, vm *uvm.UtilityVM, share string, gpuDri
if err := wclayer.GrantVmAccess(ctx, vm.ID(), share); err != nil {
return closer, err
}
mount, err := vm.SCSIManager.AddVirtualDisk(
mount, err := vm.SCSIManager.Add(
ctx,
share,
true,
&scsi.MountConfig{},
&scsi.AttachConfig{Path: share, ReadOnly: true, Type: scsi.AttachmentTypeVirtualDisk},
&scsi.MountConfig{ReadOnly: true, Verity: scsi.ReadVerityInfo(ctx, share)},
)
if err != nil {
return closer, fmt.Errorf("failed to add SCSI disk to utility VM for path %+v: %s", share, err)
Expand Down
14 changes: 6 additions & 8 deletions internal/hcsoci/resources_lcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ func allocateLinuxResources(ctx context.Context, coi *createOptionsInternal, r *
if err := wclayer.GrantVmAccess(ctx, coi.HostingSystem.ID(), hostPath); err != nil {
return err
}
scsiMount, err := coi.HostingSystem.SCSIManager.AddPhysicalDisk(
scsiMount, err := coi.HostingSystem.SCSIManager.Add(
ctx,
hostPath,
readOnly,
&scsi.MountConfig{Options: mount.Options},
&scsi.AttachConfig{Path: hostPath, ReadOnly: readOnly, Type: scsi.AttachmentTypePassThru},
&scsi.MountConfig{ReadOnly: readOnly, Verity: scsi.ReadVerityInfo(ctx, hostPath), Options: mount.Options},
)
if err != nil {
return errors.Wrapf(err, "adding SCSI physical disk mount %+v", mount)
Expand All @@ -110,11 +109,10 @@ func allocateLinuxResources(ctx context.Context, coi *createOptionsInternal, r *
if err := wclayer.GrantVmAccess(ctx, coi.HostingSystem.ID(), hostPath); err != nil {
return err
}
scsiMount, err := coi.HostingSystem.SCSIManager.AddVirtualDisk(
scsiMount, err := coi.HostingSystem.SCSIManager.Add(
ctx,
hostPath,
readOnly,
&scsi.MountConfig{Options: mount.Options},
&scsi.AttachConfig{Path: hostPath, ReadOnly: readOnly, Type: scsi.AttachmentTypeVirtualDisk},
&scsi.MountConfig{ReadOnly: readOnly, Verity: scsi.ReadVerityInfo(ctx, hostPath), Options: mount.Options},
)
if err != nil {
return errors.Wrapf(err, "adding SCSI virtual disk mount %+v", mount)
Expand Down
25 changes: 13 additions & 12 deletions internal/hcsoci/resources_wcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,30 +165,31 @@ func setupMounts(ctx context.Context, coi *createOptionsInternal, r *resources.R
if err := wclayer.GrantVmAccess(ctx, coi.HostingSystem.ID(), mount.Source); err != nil {
return err
}
scsiMount, err = coi.HostingSystem.SCSIManager.AddPhysicalDisk(
scsiMount, err = coi.HostingSystem.SCSIManager.Add(
ctx,
mount.Source,
readOnly,
&scsi.MountConfig{Options: mount.Options},
&scsi.AttachConfig{Path: mount.Source, ReadOnly: readOnly, Type: scsi.AttachmentTypePassThru},
&scsi.MountConfig{ReadOnly: readOnly, Verity: scsi.ReadVerityInfo(ctx, mount.Source), Options: mount.Options},
)
case "virtual-disk":
l.Debug("hcsshim::allocateWindowsResources Hot-adding SCSI virtual disk for OCI mount")
if err := wclayer.GrantVmAccess(ctx, coi.HostingSystem.ID(), mount.Source); err != nil {
return err
}
scsiMount, err = coi.HostingSystem.SCSIManager.AddVirtualDisk(
scsiMount, err = coi.HostingSystem.SCSIManager.Add(
ctx,
mount.Source,
readOnly,
&scsi.MountConfig{Options: mount.Options},
&scsi.AttachConfig{Path: mount.Source, ReadOnly: readOnly, Type: scsi.AttachmentTypeVirtualDisk},
&scsi.MountConfig{ReadOnly: readOnly, Verity: scsi.ReadVerityInfo(ctx, mount.Source), Options: mount.Options},
)
case "extensible-virtual-disk":
l.Debug("hcsshim::allocateWindowsResource Hot-adding ExtensibleVirtualDisk")
scsiMount, err = coi.HostingSystem.SCSIManager.AddExtensibleVirtualDisk(
evdType, path, err := scsi.ParseExtensibleVirtualDiskPath(mount.Source)
if err != nil {
return err
}
scsiMount, err = coi.HostingSystem.SCSIManager.Add(
ctx,
mount.Source,
readOnly,
&scsi.MountConfig{},
&scsi.AttachConfig{Path: path, ReadOnly: readOnly, Type: scsi.AttachmentTypeExtensibleVirtualDisk, EVDType: evdType},
&scsi.MountConfig{ReadOnly: readOnly},
)
}
if err != nil {
Expand Down
19 changes: 13 additions & 6 deletions internal/layers/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ func MountLCOWLayers(ctx context.Context, containerID string, layerFolders []str
if err := wclayer.GrantVmAccess(ctx, vm.ID(), hostPath); err != nil {
return "", "", nil, err
}
scsiMount, err := vm.SCSIManager.AddVirtualDisk(
scsiMount, err := vm.SCSIManager.Add(
ctx,
hostPath,
false,
&scsi.MountConfig{Encrypted: vm.ScratchEncryptionEnabled()},
&scsi.AttachConfig{Path: hostPath, Type: scsi.AttachmentTypeVirtualDisk},
&scsi.MountConfig{Encrypted: vm.ScratchEncryptionEnabled(), Verity: scsi.ReadVerityInfo(ctx, hostPath)},
)
if err != nil {
return "", "", nil, fmt.Errorf("failed to add SCSI scratch VHD: %s", err)
Expand Down Expand Up @@ -342,7 +341,11 @@ func mountWCOWIsolatedLayers(ctx context.Context, containerID string, layerFolde
if err := wclayer.GrantVmAccess(ctx, vm.ID(), hostPath); err != nil {
return "", nil, err
}
scsiMount, err := vm.SCSIManager.AddVirtualDisk(ctx, hostPath, false, &scsi.MountConfig{})
scsiMount, err := vm.SCSIManager.Add(
ctx,
&scsi.AttachConfig{Path: hostPath, Type: scsi.AttachmentTypeVirtualDisk},
&scsi.MountConfig{Verity: scsi.ReadVerityInfo(ctx, hostPath)},
)
if err != nil {
return "", nil, fmt.Errorf("failed to add SCSI scratch VHD: %s", err)
}
Expand Down Expand Up @@ -394,7 +397,11 @@ func addLCOWLayer(ctx context.Context, vm *uvm.UtilityVM, layerPath string) (uvm
}
}

sm, err := vm.SCSIManager.AddVirtualDisk(ctx, layerPath, true, &scsi.MountConfig{Options: []string{"ro"}})
sm, err := vm.SCSIManager.Add(
ctx,
&scsi.AttachConfig{Path: layerPath, ReadOnly: true, Type: scsi.AttachmentTypeVirtualDisk},
&scsi.MountConfig{ReadOnly: true, Verity: scsi.ReadVerityInfo(ctx, layerPath), Options: []string{"ro"}},
)
if err != nil {
return "", nil, fmt.Errorf("failed to add SCSI layer: %s", err)
}
Expand Down
26 changes: 17 additions & 9 deletions internal/uvm/scsi/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,28 @@ func newAttachManager(attacher Attacher, unplugger Unplugger, numControllers, nu
type attachment struct {
controller uint
lun uint
config *attachConfig
config *AttachConfig
waitErr error
waitCh chan struct{}
refCount uint
}

type attachConfig struct {
path string
readOnly bool
typ string
evdType string
type AttachConfig struct {
Path string
ReadOnly bool
Type AttachmentType
EVDType string // Only valid if Type is AttachmentTypeExtensibleVirtualDisk.
}

func (am *attachManager) attach(ctx context.Context, c *attachConfig) (controller uint, lun uint, err error) {
type AttachmentType string

const (
AttachmentTypeVirtualDisk AttachmentType = "VirtualDisk"
AttachmentTypePassThru AttachmentType = "PassThru"
AttachmentTypeExtensibleVirtualDisk AttachmentType = "ExtensibleVirtualDisk"
)

func (am *attachManager) attach(ctx context.Context, c *AttachConfig) (controller uint, lun uint, err error) {
att, existed, err := am.trackAttachment(c)
if err != nil {
return 0, 0, err
Expand All @@ -77,7 +85,7 @@ func (am *attachManager) attach(ctx context.Context, c *attachConfig) (controlle
}()

if err := am.attacher.attach(ctx, att.controller, att.lun, att.config); err != nil {
return 0, 0, fmt.Errorf("attach %s/%s at controller %d lun %d: %w", att.config.typ, att.config.path, att.controller, att.lun, err)
return 0, 0, fmt.Errorf("attach %s/%s at controller %d lun %d: %w", att.config.Type, att.config.Path, att.controller, att.lun, err)
}
return att.controller, att.lun, nil
}
Expand Down Expand Up @@ -108,7 +116,7 @@ func (am *attachManager) detach(ctx context.Context, controller, lun uint) (bool
return true, nil
}

func (am *attachManager) trackAttachment(c *attachConfig) (*attachment, bool, error) {
func (am *attachManager) trackAttachment(c *AttachConfig) (*attachment, bool, error) {
am.m.Lock()
defer am.m.Unlock()

Expand Down
40 changes: 20 additions & 20 deletions internal/uvm/scsi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (

// Attacher provides the low-level operations for attaching a SCSI device to a VM.
type Attacher interface {
attach(ctx context.Context, controller, lun uint, config *attachConfig) error
attach(ctx context.Context, controller, lun uint, config *AttachConfig) error
detach(ctx context.Context, controller, lun uint) error
}

// Mounter provides the low-level operations for mounting a SCSI device inside the guest OS.
type Mounter interface {
mount(ctx context.Context, controller, lun uint, path string, config *mountConfig) error
unmount(ctx context.Context, controller, lun uint, path string, config *mountConfig) error
mount(ctx context.Context, controller, lun uint, path string, config *MountConfig) error
unmount(ctx context.Context, controller, lun uint, path string, config *MountConfig) error
}

// Unplugger provides the low-level operations for cleanly removing a SCSI device inside the guest OS.
Expand All @@ -45,14 +45,14 @@ func NewHCSAttacher(system *hcs.System) Attacher {
return &hcsAttacher{system}
}

func (ha *hcsAttacher) attach(ctx context.Context, controller, lun uint, config *attachConfig) error {
func (ha *hcsAttacher) attach(ctx context.Context, controller, lun uint, config *AttachConfig) error {
req := &hcsschema.ModifySettingRequest{
RequestType: guestrequest.RequestTypeAdd,
Settings: hcsschema.Attachment{
Path: config.path,
Type_: config.typ,
ReadOnly: config.readOnly,
ExtensibleVirtualDiskType: config.evdType,
Path: config.Path,
Type_: config.Type,
ReadOnly: config.ReadOnly,
ExtensibleVirtualDiskType: config.EVDType,
},
ResourcePath: fmt.Sprintf(resourcepaths.SCSIResourceFormat, guestrequest.ScsiControllerGuids[controller], lun),
}
Expand Down Expand Up @@ -81,15 +81,15 @@ func NewBridgeMounter(gc *gcs.GuestConnection, osType string) Mounter {
return &bridgeMounter{gc, osType}
}

func (bm *bridgeMounter) mount(ctx context.Context, controller, lun uint, path string, config *mountConfig) error {
func (bm *bridgeMounter) mount(ctx context.Context, controller, lun uint, path string, config *MountConfig) error {
req, err := mountRequest(controller, lun, path, config, bm.osType)
if err != nil {
return err
}
return bm.gc.Modify(ctx, req)
}

func (bm *bridgeMounter) unmount(ctx context.Context, controller, lun uint, path string, config *mountConfig) error {
func (bm *bridgeMounter) unmount(ctx context.Context, controller, lun uint, path string, config *MountConfig) error {
req, err := unmountRequest(controller, lun, path, config, bm.osType)
if err != nil {
return err
Expand All @@ -111,15 +111,15 @@ func NewHCSMounter(system *hcs.System, osType string) Mounter {
return &hcsMounter{system, osType}
}

func (hm *hcsMounter) mount(ctx context.Context, controller, lun uint, path string, config *mountConfig) error {
func (hm *hcsMounter) mount(ctx context.Context, controller, lun uint, path string, config *MountConfig) error {
req, err := mountRequest(controller, lun, path, config, hm.osType)
if err != nil {
return err
}
return hm.system.Modify(ctx, &hcsschema.ModifySettingRequest{GuestRequest: req})
}

func (hm *hcsMounter) unmount(ctx context.Context, controller, lun uint, path string, config *mountConfig) error {
func (hm *hcsMounter) unmount(ctx context.Context, controller, lun uint, path string, config *MountConfig) error {
req, err := unmountRequest(controller, lun, path, config, hm.osType)
if err != nil {
return err
Expand Down Expand Up @@ -177,7 +177,7 @@ func (hu *hcsUnplugger) unplug(ctx context.Context, controller, lun uint) error
return hu.system.Modify(ctx, &hcsschema.ModifySettingRequest{GuestRequest: req})
}

func mountRequest(controller, lun uint, path string, config *mountConfig, osType string) (guestrequest.ModificationRequest, error) {
func mountRequest(controller, lun uint, path string, config *MountConfig, osType string) (guestrequest.ModificationRequest, error) {
req := guestrequest.ModificationRequest{
ResourceType: guestresource.ResourceTypeMappedVirtualDisk,
RequestType: guestrequest.RequestTypeAdd,
Expand All @@ -188,7 +188,7 @@ func mountRequest(controller, lun uint, path string, config *mountConfig, osType
if controller != 0 {
return guestrequest.ModificationRequest{}, errors.New("WCOW only supports SCSI controller 0")
}
if config.encrypted || config.verity != nil || len(config.options) != 0 {
if config.Encrypted || config.Verity != nil || len(config.Options) != 0 {
return guestrequest.ModificationRequest{}, errors.New("WCOW does not support encrypted, verity, or guest options on mounts")
}
req.Settings = guestresource.WCOWMappedVirtualDisk{
Expand All @@ -200,18 +200,18 @@ func mountRequest(controller, lun uint, path string, config *mountConfig, osType
MountPath: path,
Controller: uint8(controller),
Lun: uint8(lun),
ReadOnly: config.readOnly,
Encrypted: config.encrypted,
Options: config.options,
VerityInfo: config.verity,
ReadOnly: config.ReadOnly,
Encrypted: config.Encrypted,
Options: config.Options,
VerityInfo: config.Verity,
}
default:
return guestrequest.ModificationRequest{}, fmt.Errorf("unsupported os type: %s", osType)
}
return req, nil
}

func unmountRequest(controller, lun uint, path string, config *mountConfig, osType string) (guestrequest.ModificationRequest, error) {
func unmountRequest(controller, lun uint, path string, config *MountConfig, osType string) (guestrequest.ModificationRequest, error) {
req := guestrequest.ModificationRequest{
ResourceType: guestresource.ResourceTypeMappedVirtualDisk,
RequestType: guestrequest.RequestTypeRemove,
Expand All @@ -227,7 +227,7 @@ func unmountRequest(controller, lun uint, path string, config *mountConfig, osTy
MountPath: path,
Lun: uint8(lun),
Controller: uint8(controller),
VerityInfo: config.verity,
VerityInfo: config.Verity,
}
default:
return guestrequest.ModificationRequest{}, fmt.Errorf("unsupported os type: %s", osType)
Expand Down
Loading

0 comments on commit cb906a3

Please sign in to comment.