...

Source file src/github.com/Microsoft/hcsshim/internal/uvm/share.go

Documentation: github.com/Microsoft/hcsshim/internal/uvm

     1  //go:build windows
     2  
     3  package uvm
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
    12  	"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
    13  	"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  func (uvm *UtilityVM) AddVsmbAndGetSharePath(ctx context.Context, reqHostPath, reqUVMPath string, readOnly bool) (*VSMBShare, string, error) {
    18  	options := uvm.DefaultVSMBOptions(readOnly)
    19  	vsmbShare, err := uvm.AddVSMB(ctx, reqHostPath, options)
    20  	if err != nil {
    21  		return nil, "", errors.Wrapf(err, "failed to add mount as vSMB share to UVM")
    22  	}
    23  	defer func() {
    24  		if err != nil {
    25  			_ = vsmbShare.Release(ctx)
    26  		}
    27  	}()
    28  
    29  	sharePath, err := uvm.GetVSMBUvmPath(ctx, reqHostPath, readOnly)
    30  	if err != nil {
    31  		return nil, "", errors.Wrapf(err, "failed to get vsmb path")
    32  	}
    33  
    34  	return vsmbShare, sharePath, nil
    35  }
    36  
    37  // Share shares in file(s) from `reqHostPath` on the host machine to `reqUVMPath` inside the UVM.
    38  // This function handles both LCOW and WCOW scenarios.
    39  func (uvm *UtilityVM) Share(ctx context.Context, reqHostPath, reqUVMPath string, readOnly bool) (err error) {
    40  	if uvm.OS() == "windows" {
    41  		_, sharePath, err := uvm.AddVsmbAndGetSharePath(ctx, reqHostPath, reqUVMPath, readOnly)
    42  		if err != nil {
    43  			return err
    44  		}
    45  		guestReq := guestrequest.ModificationRequest{
    46  			ResourceType: guestresource.ResourceTypeMappedDirectory,
    47  			RequestType:  guestrequest.RequestTypeAdd,
    48  			Settings: &hcsschema.MappedDirectory{
    49  				HostPath:      sharePath,
    50  				ContainerPath: reqUVMPath,
    51  				ReadOnly:      readOnly,
    52  			},
    53  		}
    54  		if err := uvm.GuestRequest(ctx, guestReq); err != nil {
    55  			return err
    56  		}
    57  	} else {
    58  		st, err := os.Stat(reqHostPath)
    59  		if err != nil {
    60  			return fmt.Errorf("could not open '%s' path on host: %s", reqHostPath, err)
    61  		}
    62  		var (
    63  			hostPath       string = reqHostPath
    64  			restrictAccess bool
    65  			fileName       string
    66  			allowedNames   []string
    67  		)
    68  		if !st.IsDir() {
    69  			hostPath, fileName = filepath.Split(hostPath)
    70  			allowedNames = append(allowedNames, fileName)
    71  			restrictAccess = true
    72  		}
    73  		plan9Share, err := uvm.AddPlan9(ctx, hostPath, reqUVMPath, readOnly, restrictAccess, allowedNames)
    74  		if err != nil {
    75  			return err
    76  		}
    77  		defer func() {
    78  			if err != nil {
    79  				_ = plan9Share.Release(ctx)
    80  			}
    81  		}()
    82  	}
    83  	return nil
    84  }
    85  

View as plain text