Skip to content

Commit

Permalink
pr feedback: constants and uvmconstants to guestpath package
Browse files Browse the repository at this point in the history
Add new guestpath package that contains all of the guest path
constants used in guest and hcsshim

Signed-off-by: Maksim An <maksiman@microsoft.com>
  • Loading branch information
anmaxvl committed Mar 14, 2022
1 parent 66df084 commit a6c0980
Show file tree
Hide file tree
Showing 27 changed files with 107 additions and 111 deletions.
4 changes: 2 additions & 2 deletions cmd/gcs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"

"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/guest/bridge"
"github.com/Microsoft/hcsshim/internal/guest/kmsg"
"github.com/Microsoft/hcsshim/internal/guest/runtime/hcsv2"
"github.com/Microsoft/hcsshim/internal/guest/runtime/runc"
"github.com/Microsoft/hcsshim/internal/guest/transport"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/cenkalti/backoff/v4"
Expand Down Expand Up @@ -231,7 +231,7 @@ func main() {

log.SetScrubbing(*scrubLogs)

baseLogPath := constants.LCOWRootPrefixInUVM
baseLogPath := guestpath.LCOWRootPrefixInUVM

logrus.Info("GCS started")

Expand Down
3 changes: 2 additions & 1 deletion internal/devices/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/Microsoft/hcsshim/internal/cmd"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/resources"
"github.com/Microsoft/hcsshim/internal/uvm"
Expand Down Expand Up @@ -45,7 +46,7 @@ func InstallKernelDriver(ctx context.Context, vm *uvm.UtilityVM, driver string)
}
return closer, execPnPInstallDriver(ctx, vm, uvmPath)
}
uvmPathForShare := fmt.Sprintf(uvm.LCOWGlobalMountPrefix, vm.UVMMountCounter())
uvmPathForShare := fmt.Sprintf(guestpath.LCOWGlobalMountPrefix, vm.UVMMountCounter())
scsiCloser, err := vm.AddSCSI(ctx, driver, uvmPathForShare, true, false, []string{}, uvm.VMAccessTypeIndividual)
if err != nil {
return closer, fmt.Errorf("failed to add SCSI disk to utility VM for path %+v: %s", driver, err)
Expand Down
4 changes: 2 additions & 2 deletions internal/guest/runtime/hcsv2/nvidia_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/pkg/errors"

"github.com/Microsoft/hcsshim/cmd/gcstools/generichook"
"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/guest/storage/pci"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/hooks"
"github.com/Microsoft/hcsshim/pkg/annotations"
)
Expand Down Expand Up @@ -77,7 +77,7 @@ func addNvidiaDevicePreHook(ctx context.Context, spec *oci.Spec) error {

// updateEnvWithNvidiaVariables creates an env with the nvidia gpu vhd in PATH and insecure mode set
func updateEnvWithNvidiaVariables() []string {
nvidiaBin := fmt.Sprintf("%s/bin", constants.LCOWNvidiaMountPath)
nvidiaBin := fmt.Sprintf("%s/bin", guestpath.LCOWNvidiaMountPath)
env := updatePathEnv(nvidiaBin)
// NVC_INSECURE_MODE allows us to run nvidia-container-cli without seccomp
// we don't currently use seccomp in the uvm, so avoid using it here for now as well
Expand Down
4 changes: 2 additions & 2 deletions internal/guest/runtime/hcsv2/sandbox_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"github.com/pkg/errors"
"go.opencensus.io/trace"

"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/guest/network"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/pkg/annotations"
)

func getSandboxRootDir(id string) string {
return filepath.Join(constants.LCOWRootPrefixInUVM, id)
return filepath.Join(guestpath.LCOWRootPrefixInUVM, id)
}

func getSandboxHugePageMountsDir(id string) string {
Expand Down
4 changes: 2 additions & 2 deletions internal/guest/runtime/hcsv2/standalone_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/pkg/errors"
"go.opencensus.io/trace"

"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/guest/network"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/oc"
)

func getStandaloneRootDir(id string) string {
return filepath.Join(constants.LCOWRootPrefixInUVM, id)
return filepath.Join(guestpath.LCOWRootPrefixInUVM, id)
}

func getStandaloneHostnamePath(id string) string {
Expand Down
12 changes: 6 additions & 6 deletions internal/guest/runtime/hcsv2/workload_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"go.opencensus.io/trace"
"golang.org/x/sys/unix"

"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/pkg/annotations"
)

func getWorkloadRootDir(id string) string {
return filepath.Join(constants.LCOWRootPrefixInUVM, id)
return filepath.Join(guestpath.LCOWRootPrefixInUVM, id)
}

// os.MkdirAll combines the given permissions with the running process's
Expand All @@ -35,9 +35,9 @@ func mkdirAllModePerm(target string) error {

func updateSandboxMounts(sbid string, spec *oci.Spec) error {
for i, m := range spec.Mounts {
if strings.HasPrefix(m.Source, constants.SandboxMountPrefix) {
if strings.HasPrefix(m.Source, guestpath.SandboxMountPrefix) {
mountsDir := getSandboxMountsDir(sbid)
subPath := strings.TrimPrefix(m.Source, constants.SandboxMountPrefix)
subPath := strings.TrimPrefix(m.Source, guestpath.SandboxMountPrefix)
sandboxSource := filepath.Join(mountsDir, subPath)

// filepath.Join cleans the resulting path before returning so it would resolve the relative path if one was given.
Expand All @@ -61,9 +61,9 @@ func updateSandboxMounts(sbid string, spec *oci.Spec) error {

func updateHugePageMounts(sbid string, spec *oci.Spec) error {
for i, m := range spec.Mounts {
if strings.HasPrefix(m.Source, constants.HugePagesMountPrefix) {
if strings.HasPrefix(m.Source, guestpath.HugePagesMountPrefix) {
mountsDir := getSandboxHugePageMountsDir(sbid)
subPath := strings.TrimPrefix(m.Source, constants.HugePagesMountPrefix)
subPath := strings.TrimPrefix(m.Source, guestpath.HugePagesMountPrefix)
pageSize := strings.Split(subPath, string(os.PathSeparator))[0]
hugePageMountSource := filepath.Join(mountsDir, subPath)

Expand Down
15 changes: 10 additions & 5 deletions internal/constants/constants.go → internal/guestpath/paths.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package constants
package guestpath

const (
// LCOWNvidiaMountPath is the path format in LCOW UVM where nvidia tools are mounted
// keep this value in sync with opengcs
LCOWNvidiaMountPath = "/run/nvidia"

// LCOWRootPrefixInUVM is the path inside UVM where LCOW container's root file system will be mounted
LCOWRootPrefixInUVM = "/run/gcs/c"

// WCOWRootPrefixInUVM is the path inside UVM where WCOW container's root file system will be mounted
WCOWRootPrefixInUVM = `C:\c`

// SandboxMountPrefix is mount prefix used in container spec to mark a sandbox-mount
SandboxMountPrefix = "sandbox://"

// HugePagesMountPrefix is mount prefix used in container spec to mark a huge-pages mount
HugePagesMountPrefix = "hugepages://"
// LCOWMountPathPrefix is the path format in the LCOW UVM where non global mounts, such
// as Plan9 mounts are added
LCOWMountPathPrefix = "/mounts/m%d"
// LCOWGlobalMountPrefix is the path format in the LCOW UVM where global mounts are added
LCOWGlobalMountPrefix = "/run/mounts/m%d"
// WCOWGlobalMountPrefix is the path prefix format in the WCOW UVM where mounts are added
WCOWGlobalMountPrefix = "C:\\mounts\\m%d"
// RootfsPath is part of the container's rootfs path
RootfsPath = "rootfs"
)
4 changes: 2 additions & 2 deletions internal/hcsoci/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"context"
"errors"
"fmt"
"github.com/Microsoft/hcsshim/internal/constants"
"os"
"path/filepath"
"strconv"

"github.com/Microsoft/go-winio/pkg/guid"
"github.com/Microsoft/hcsshim/internal/clone"
"github.com/Microsoft/hcsshim/internal/cow"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/hcs"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
"github.com/Microsoft/hcsshim/internal/log"
Expand All @@ -28,7 +28,7 @@ import (
)

var (
lcowRootInUVM = constants.LCOWRootPrefixInUVM + "/%s"
lcowRootInUVM = guestpath.LCOWRootPrefixInUVM + "/%s"
wcowRootInUVM = `C:\c\%s`
)

Expand Down
6 changes: 3 additions & 3 deletions internal/hcsoci/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"

"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/devices"
"github.com/Microsoft/hcsshim/internal/guestpath"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/oci"
Expand Down Expand Up @@ -224,14 +224,14 @@ func handleAssignedDevicesLCOW(
scsiMount, err := vm.AddSCSI(
ctx,
gpuSupportVhdPath,
constants.LCOWNvidiaMountPath,
guestpath.LCOWNvidiaMountPath,
true,
false,
options,
uvm.VMAccessTypeNoop,
)
if err != nil {
return resultDevs, closers, errors.Wrapf(err, "failed to add scsi device %s in the UVM %s at %s", gpuSupportVhdPath, vm.ID(), constants.LCOWNvidiaMountPath)
return resultDevs, closers, errors.Wrapf(err, "failed to add scsi device %s in the UVM %s at %s", gpuSupportVhdPath, vm.ID(), guestpath.LCOWNvidiaMountPath)
}
closers = append(closers, scsiMount)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/hcsoci/hcsdoc_wcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"

"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/hcs/schema1"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
"github.com/Microsoft/hcsshim/internal/layers"
Expand Down Expand Up @@ -80,7 +80,7 @@ func createMountsConfig(ctx context.Context, coi *createOptionsInternal) (*mount
return nil, err
}
mdv2.HostPath = uvmPath
} else if strings.HasPrefix(mount.Source, constants.SandboxMountPrefix) {
} else if strings.HasPrefix(mount.Source, guestpath.SandboxMountPrefix) {
// Convert to the path in the guest that was asked for.
mdv2.HostPath = convertToWCOWSandboxMountPath(mount.Source)
} else {
Expand Down
18 changes: 9 additions & 9 deletions internal/hcsoci/resources_lcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"

"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/layers"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/resources"
Expand All @@ -41,7 +41,7 @@ func allocateLinuxResources(ctx context.Context, coi *createOptionsInternal, r *
// This is the "Plan 9" root filesystem.
// TODO: We need a test for this. Ask @jstarks how you can even lay this out on Windows.
hostPath := coi.Spec.Root.Path
uvmPathForContainersFileSystem := path.Join(r.ContainerRootInUVM(), uvm.RootfsPath)
uvmPathForContainersFileSystem := path.Join(r.ContainerRootInUVM(), guestpath.RootfsPath)
share, err := coi.HostingSystem.AddPlan9(ctx, hostPath, uvmPathForContainersFileSystem, coi.Spec.Root.Readonly, false, nil)
if err != nil {
return errors.Wrap(err, "adding plan9 root")
Expand All @@ -67,7 +67,7 @@ func allocateLinuxResources(ctx context.Context, coi *createOptionsInternal, r *

if coi.HostingSystem != nil {
hostPath := mount.Source
uvmPathForShare := path.Join(containerRootInUVM, fmt.Sprintf(uvm.LCOWMountPathPrefix, i))
uvmPathForShare := path.Join(containerRootInUVM, fmt.Sprintf(guestpath.LCOWMountPathPrefix, i))
uvmPathForFile := uvmPathForShare

readOnly := false
Expand All @@ -81,7 +81,7 @@ func allocateLinuxResources(ctx context.Context, coi *createOptionsInternal, r *
l := log.G(ctx).WithField("mount", fmt.Sprintf("%+v", mount))
if mount.Type == "physical-disk" {
l.Debug("hcsshim::allocateLinuxResources Hot-adding SCSI physical disk for OCI mount")
uvmPathForShare = fmt.Sprintf(uvm.LCOWGlobalMountPrefix, coi.HostingSystem.UVMMountCounter())
uvmPathForShare = fmt.Sprintf(guestpath.LCOWGlobalMountPrefix, coi.HostingSystem.UVMMountCounter())
scsiMount, err := coi.HostingSystem.AddSCSIPhysicalDisk(ctx, hostPath, uvmPathForShare, readOnly, mount.Options)
if err != nil {
return errors.Wrapf(err, "adding SCSI physical disk mount %+v", mount)
Expand All @@ -92,7 +92,7 @@ func allocateLinuxResources(ctx context.Context, coi *createOptionsInternal, r *
coi.Spec.Mounts[i].Type = "none"
} else if mount.Type == "virtual-disk" {
l.Debug("hcsshim::allocateLinuxResources Hot-adding SCSI virtual disk for OCI mount")
uvmPathForShare = fmt.Sprintf(uvm.LCOWGlobalMountPrefix, coi.HostingSystem.UVMMountCounter())
uvmPathForShare = fmt.Sprintf(guestpath.LCOWGlobalMountPrefix, coi.HostingSystem.UVMMountCounter())

// if the scsi device is already attached then we take the uvm path that the function below returns
// that is where it was previously mounted in UVM
Expand All @@ -112,18 +112,18 @@ func allocateLinuxResources(ctx context.Context, coi *createOptionsInternal, r *
uvmPathForFile = scsiMount.UVMPath
r.Add(scsiMount)
coi.Spec.Mounts[i].Type = "none"
} else if strings.HasPrefix(mount.Source, constants.SandboxMountPrefix) {
} else if strings.HasPrefix(mount.Source, guestpath.SandboxMountPrefix) {
// Mounts that map to a path in UVM are specified with 'sandbox://' prefix.
// example: sandbox:///a/dirInUvm destination:/b/dirInContainer
uvmPathForFile = mount.Source
} else if strings.HasPrefix(mount.Source, constants.HugePagesMountPrefix) {
} else if strings.HasPrefix(mount.Source, guestpath.HugePagesMountPrefix) {
// currently we only support 2M hugepage size
hugePageSubDirs := strings.Split(strings.TrimPrefix(mount.Source, constants.HugePagesMountPrefix), "/")
hugePageSubDirs := strings.Split(strings.TrimPrefix(mount.Source, guestpath.HugePagesMountPrefix), "/")
if len(hugePageSubDirs) < 2 {
return errors.Errorf(
`%s mount path is invalid, expected format: %s<hugepage-size>/<hugepage-src-location>`,
mount.Source,
constants.HugePagesMountPrefix,
guestpath.HugePagesMountPrefix,
)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/hcsoci/resources_wcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/pkg/errors"

"github.com/Microsoft/hcsshim/internal/cmd"
"github.com/Microsoft/hcsshim/internal/constants"
"github.com/Microsoft/hcsshim/internal/credentials"
"github.com/Microsoft/hcsshim/internal/guestpath"
"github.com/Microsoft/hcsshim/internal/layers"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/resources"
Expand Down Expand Up @@ -142,7 +142,7 @@ func setupMounts(ctx context.Context, coi *createOptionsInternal, r *resources.R
}

if coi.HostingSystem != nil && schemaversion.IsV21(coi.actualSchemaVersion) {
uvmPath := fmt.Sprintf(uvm.WCOWGlobalMountPrefix, coi.HostingSystem.UVMMountCounter())
uvmPath := fmt.Sprintf(guestpath.WCOWGlobalMountPrefix, coi.HostingSystem.UVMMountCounter())
readOnly := false
for _, o := range mount.Options {
if strings.ToLower(o) == "ro" {
Expand Down Expand Up @@ -180,7 +180,7 @@ func setupMounts(ctx context.Context, coi *createOptionsInternal, r *resources.R
return errors.Wrapf(err, "adding SCSI EVD mount failed %+v", mount)
}
r.Add(scsiMount)
} else if strings.HasPrefix(mount.Source, constants.SandboxMountPrefix) {
} else if strings.HasPrefix(mount.Source, guestpath.SandboxMountPrefix) {
// Mounts that map to a path in the UVM are specified with a 'sandbox://' prefix.
//
// Example: sandbox:///a/dirInUvm destination:C:\\dirInContainer.
Expand Down Expand Up @@ -230,6 +230,6 @@ func setupMounts(ctx context.Context, coi *createOptionsInternal, r *resources.R
}

func convertToWCOWSandboxMountPath(source string) string {
subPath := strings.TrimPrefix(source, constants.SandboxMountPrefix)
subPath := strings.TrimPrefix(source, guestpath.SandboxMountPrefix)
return filepath.Join(wcowSandboxMountPath, subPath)
}
14 changes: 8 additions & 6 deletions internal/layers/layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import (
"path/filepath"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"

"github.com/Microsoft/hcsshim/internal/guestpath"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
"github.com/Microsoft/hcsshim/internal/hcserror"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/ospath"
"github.com/Microsoft/hcsshim/internal/uvm"
"github.com/Microsoft/hcsshim/internal/wclayer"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
)

// ImageLayers contains all the layers for an image.
Expand Down Expand Up @@ -261,7 +263,7 @@ func MountContainerLayers(ctx context.Context, containerID string, layerFolders
err = vm.CombineLayersWCOW(ctx, layers, containerScratchPathInUVM)
rootfs = containerScratchPathInUVM
} else {
rootfs = ospath.Join(vm.OS(), guestRoot, uvm.RootfsPath)
rootfs = ospath.Join(vm.OS(), guestRoot, guestpath.RootfsPath)
err = vm.CombineLayersLCOW(ctx, containerID, lcowUvmLayerPaths, containerScratchPathInUVM, rootfs)
}
if err != nil {
Expand Down Expand Up @@ -289,7 +291,7 @@ func addLCOWLayer(ctx context.Context, vm *uvm.UtilityVM, layerPath string) (uvm
}

options := []string{"ro"}
uvmPath = fmt.Sprintf(uvm.LCOWGlobalMountPrefix, vm.UVMMountCounter())
uvmPath = fmt.Sprintf(guestpath.LCOWGlobalMountPrefix, vm.UVMMountCounter())
sm, err := vm.AddSCSI(ctx, layerPath, uvmPath, true, false, options, uvm.VMAccessTypeNoop)
if err != nil {
return "", fmt.Errorf("failed to add SCSI layer: %s", err)
Expand Down Expand Up @@ -460,7 +462,7 @@ func containerRootfsPath(vm *uvm.UtilityVM, rootPath string) string {
if vm.OS() == "windows" {
return ospath.Join(vm.OS(), rootPath)
}
return ospath.Join(vm.OS(), rootPath, uvm.RootfsPath)
return ospath.Join(vm.OS(), rootPath, guestpath.RootfsPath)
}

func getScratchVHDPath(layerFolders []string) (string, error) {
Expand Down
Loading

0 comments on commit a6c0980

Please sign in to comment.