diff --git a/cmd/containerd-shim-runhcs-v1/task_wcow_podsandbox.go b/cmd/containerd-shim-runhcs-v1/task_wcow_podsandbox.go index f00934eda7..9f60d6398c 100644 --- a/cmd/containerd-shim-runhcs-v1/task_wcow_podsandbox.go +++ b/cmd/containerd-shim-runhcs-v1/task_wcow_podsandbox.go @@ -8,10 +8,7 @@ import ( "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats" "github.com/Microsoft/hcsshim/internal/cmd" - "github.com/Microsoft/hcsshim/internal/guestrequest" "github.com/Microsoft/hcsshim/internal/log" - "github.com/Microsoft/hcsshim/internal/requesttype" - hcsschema "github.com/Microsoft/hcsshim/internal/schema2" "github.com/Microsoft/hcsshim/internal/shimdiag" "github.com/Microsoft/hcsshim/internal/uvm" eventstypes "github.com/containerd/containerd/api/events" @@ -243,25 +240,7 @@ func (wpst *wcowPodSandboxTask) Share(ctx context.Context, req *shimdiag.ShareRe if wpst.host == nil { return errTaskNotIsolated } - options := wpst.host.DefaultVSMBOptions(req.ReadOnly) - _, err := wpst.host.AddVSMB(ctx, req.HostPath, options) - if err != nil { - return err - } - sharePath, err := wpst.host.GetVSMBUvmPath(ctx, req.HostPath, req.ReadOnly) - if err != nil { - return err - } - guestReq := guestrequest.GuestRequest{ - ResourceType: guestrequest.ResourceTypeMappedDirectory, - RequestType: requesttype.Add, - Settings: &hcsschema.MappedDirectory{ - HostPath: sharePath, - ContainerPath: req.UvmPath, - ReadOnly: req.ReadOnly, - }, - } - return wpst.host.GuestRequest(ctx, guestReq) + return wpst.host.Share(ctx, req.HostPath, req.UvmPath, req.ReadOnly) } func (wpst *wcowPodSandboxTask) Stats(ctx context.Context) (*stats.Statistics, error) { diff --git a/internal/uvm/share.go b/internal/uvm/share.go new file mode 100644 index 0000000000..55a4e4c292 --- /dev/null +++ b/internal/uvm/share.go @@ -0,0 +1,72 @@ +package uvm + +import ( + "context" + "fmt" + "os" + "path/filepath" + + "github.com/Microsoft/hcsshim/internal/guestrequest" + "github.com/Microsoft/hcsshim/internal/requesttype" + hcsschema "github.com/Microsoft/hcsshim/internal/schema2" +) + +// Share shares in file(s) from `reqHostPath` on the host machine to `reqUVMPath` inside the UVM. +// This function handles both LCOW and WCOW scenarios. +func (uvm *UtilityVM) Share(ctx context.Context, reqHostPath, reqUVMPath string, readOnly bool) (err error) { + if uvm.OS() == "windows" { + options := uvm.DefaultVSMBOptions(readOnly) + vsmbShare, err := uvm.AddVSMB(ctx, reqHostPath, options) + if err != nil { + return err + } + defer func() { + if err != nil { + vsmbShare.Release(ctx) + } + }() + + sharePath, err := uvm.GetVSMBUvmPath(ctx, reqHostPath, readOnly) + if err != nil { + return err + } + guestReq := guestrequest.GuestRequest{ + ResourceType: guestrequest.ResourceTypeMappedDirectory, + RequestType: requesttype.Add, + Settings: &hcsschema.MappedDirectory{ + HostPath: sharePath, + ContainerPath: reqUVMPath, + ReadOnly: readOnly, + }, + } + if err := uvm.GuestRequest(ctx, guestReq); err != nil { + return err + } + } else { + st, err := os.Stat(reqHostPath) + if err != nil { + return fmt.Errorf("could not open '%s' path on host: %s", reqHostPath, err) + } + var ( + hostPath string = reqHostPath + restrictAccess bool + fileName string + allowedNames []string + ) + if !st.IsDir() { + hostPath, fileName = filepath.Split(hostPath) + allowedNames = append(allowedNames, fileName) + restrictAccess = true + } + plan9Share, err := uvm.AddPlan9(ctx, hostPath, reqUVMPath, readOnly, restrictAccess, allowedNames) + if err != nil { + return err + } + defer func() { + if err != nil { + plan9Share.Release(ctx) + } + }() + } + return nil +}