...

Source file src/github.com/Microsoft/hcsshim/internal/vm/hcs/scsi.go

Documentation: github.com/Microsoft/hcsshim/internal/vm/hcs

     1  //go:build windows
     2  
     3  package hcs
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"strconv"
     9  
    10  	"github.com/pkg/errors"
    11  
    12  	"github.com/Microsoft/hcsshim/internal/hcs/resourcepaths"
    13  	hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
    14  	"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
    15  	"github.com/Microsoft/hcsshim/internal/vm"
    16  )
    17  
    18  func (uvmb *utilityVMBuilder) AddSCSIController(id uint32) error {
    19  	if uvmb.doc.VirtualMachine.Devices.Scsi == nil {
    20  		uvmb.doc.VirtualMachine.Devices.Scsi = make(map[string]hcsschema.Scsi, 1)
    21  	}
    22  	uvmb.doc.VirtualMachine.Devices.Scsi[strconv.Itoa(int(id))] = hcsschema.Scsi{
    23  		Attachments: make(map[string]hcsschema.Attachment),
    24  	}
    25  	return nil
    26  }
    27  
    28  func (uvmb *utilityVMBuilder) AddSCSIDisk(ctx context.Context, controller uint32, lun uint32, path string, typ vm.SCSIDiskType, readOnly bool) error {
    29  	if uvmb.doc.VirtualMachine.Devices.Scsi == nil {
    30  		return errors.New("no SCSI controller found")
    31  	}
    32  
    33  	ctrl, ok := uvmb.doc.VirtualMachine.Devices.Scsi[strconv.Itoa(int(controller))]
    34  	if !ok {
    35  		return fmt.Errorf("no scsi controller with index %d found", controller)
    36  	}
    37  
    38  	ctrl.Attachments[strconv.Itoa(int(lun))] = hcsschema.Attachment{
    39  		Path:     path,
    40  		Type_:    string(typ),
    41  		ReadOnly: readOnly,
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  func (uvmb *utilityVMBuilder) RemoveSCSIDisk(ctx context.Context, controller uint32, lun uint32, path string) error {
    48  	return vm.ErrNotSupported
    49  }
    50  
    51  func (uvm *utilityVM) AddSCSIController(id uint32) error {
    52  	return vm.ErrNotSupported
    53  }
    54  
    55  func (uvm *utilityVM) AddSCSIDisk(ctx context.Context, controller uint32, lun uint32, path string, typ vm.SCSIDiskType, readOnly bool) error {
    56  	diskTypeString, err := getSCSIDiskTypeString(typ)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	request := &hcsschema.ModifySettingRequest{
    61  		RequestType: guestrequest.RequestTypeAdd,
    62  		Settings: hcsschema.Attachment{
    63  			Path:     path,
    64  			Type_:    diskTypeString,
    65  			ReadOnly: readOnly,
    66  		},
    67  		ResourcePath: fmt.Sprintf(resourcepaths.SCSIResourceFormat, strconv.Itoa(int(controller)), lun),
    68  	}
    69  	return uvm.cs.Modify(ctx, request)
    70  }
    71  
    72  func (uvm *utilityVM) RemoveSCSIDisk(ctx context.Context, controller uint32, lun uint32, path string) error {
    73  	request := &hcsschema.ModifySettingRequest{
    74  		RequestType:  guestrequest.RequestTypeRemove,
    75  		ResourcePath: fmt.Sprintf(resourcepaths.SCSIResourceFormat, strconv.Itoa(int(controller)), lun),
    76  	}
    77  
    78  	return uvm.cs.Modify(ctx, request)
    79  }
    80  
    81  func getSCSIDiskTypeString(typ vm.SCSIDiskType) (string, error) {
    82  	switch typ {
    83  	case vm.SCSIDiskTypeVHD1:
    84  		fallthrough
    85  	case vm.SCSIDiskTypeVHDX:
    86  		return "VirtualDisk", nil
    87  	case vm.SCSIDiskTypePassThrough:
    88  		return "PassThru", nil
    89  	default:
    90  		return "", fmt.Errorf("unsupported SCSI disk type: %d", typ)
    91  	}
    92  }
    93  

View as plain text