...

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

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

     1  //go:build windows
     2  
     3  package remotevm
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  
     9  	"github.com/Microsoft/hcsshim/internal/vm"
    10  	"github.com/Microsoft/hcsshim/internal/vmservice"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  func getSCSIDiskType(typ vm.SCSIDiskType) (vmservice.DiskType, error) {
    15  	var diskType vmservice.DiskType
    16  	switch typ {
    17  	case vm.SCSIDiskTypeVHD1:
    18  		diskType = vmservice.DiskType_SCSI_DISK_TYPE_VHD1
    19  	case vm.SCSIDiskTypeVHDX:
    20  		diskType = vmservice.DiskType_SCSI_DISK_TYPE_VHDX
    21  	case vm.SCSIDiskTypePassThrough:
    22  		diskType = vmservice.DiskType_SCSI_DISK_TYPE_PHYSICAL
    23  	default:
    24  		return -1, fmt.Errorf("unsupported SCSI disk type: %d", typ)
    25  	}
    26  	return diskType, nil
    27  }
    28  
    29  func (uvmb *utilityVMBuilder) AddSCSIController(id uint32) error {
    30  	return nil
    31  }
    32  
    33  func (uvmb *utilityVMBuilder) AddSCSIDisk(ctx context.Context, controller, lun uint32, path string, typ vm.SCSIDiskType, readOnly bool) error {
    34  	diskType, err := getSCSIDiskType(typ)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	disk := &vmservice.SCSIDisk{
    40  		Controller: controller,
    41  		Lun:        lun,
    42  		HostPath:   path,
    43  		Type:       diskType,
    44  		ReadOnly:   readOnly,
    45  	}
    46  
    47  	uvmb.config.DevicesConfig.ScsiDisks = append(uvmb.config.DevicesConfig.ScsiDisks, disk)
    48  	return nil
    49  }
    50  
    51  func (uvmb *utilityVMBuilder) RemoveSCSIDisk(ctx context.Context, controller, lun uint32, path string) error {
    52  	return vm.ErrNotSupported
    53  }
    54  
    55  func (uvm *utilityVM) AddSCSIController(id uint32) error {
    56  	return vm.ErrNotSupported
    57  }
    58  
    59  func (uvm *utilityVM) AddSCSIDisk(ctx context.Context, controller, lun uint32, path string, typ vm.SCSIDiskType, readOnly bool) error {
    60  	diskType, err := getSCSIDiskType(typ)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	disk := &vmservice.SCSIDisk{
    66  		Controller: controller,
    67  		Lun:        lun,
    68  		HostPath:   path,
    69  		Type:       diskType,
    70  		ReadOnly:   readOnly,
    71  	}
    72  
    73  	if _, err := uvm.client.ModifyResource(ctx,
    74  		&vmservice.ModifyResourceRequest{
    75  			Type: vmservice.ModifyType_ADD,
    76  			Resource: &vmservice.ModifyResourceRequest_ScsiDisk{
    77  				ScsiDisk: disk,
    78  			},
    79  		},
    80  	); err != nil {
    81  		return errors.Wrap(err, "failed to add SCSI disk")
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func (uvm *utilityVM) RemoveSCSIDisk(ctx context.Context, controller, lun uint32, path string) error {
    88  	disk := &vmservice.SCSIDisk{
    89  		Controller: controller,
    90  		Lun:        lun,
    91  		HostPath:   path,
    92  	}
    93  
    94  	if _, err := uvm.client.ModifyResource(ctx,
    95  		&vmservice.ModifyResourceRequest{
    96  			Type: vmservice.ModifyType_REMOVE,
    97  			Resource: &vmservice.ModifyResourceRequest_ScsiDisk{
    98  				ScsiDisk: disk,
    99  			},
   100  		},
   101  	); err != nil {
   102  		return errors.Wrapf(err, "failed to remove SCSI disk %q", path)
   103  	}
   104  
   105  	return nil
   106  }
   107  

View as plain text