Skip to content
Open
3 changes: 3 additions & 0 deletions cmd/containerd-shim-runhcs-v1/task_hcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/Microsoft/hcsshim/internal/jobcontainers"
"github.com/Microsoft/hcsshim/internal/layers"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/internal/memory"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/internal/oci"
Expand Down Expand Up @@ -768,6 +769,8 @@ func (ht *hcsTask) ExecInHost(ctx context.Context, req *shimdiag.ExecProcessRequ
if ht.host == nil {
return cmd.ExecInShimHost(ctx, cmdReq)
}

ctx, _ = log.WithContext(ctx, log.G(ctx).WithField(logfields.UVMID, ht.host.ID()))
return cmd.ExecInUvm(ctx, ht.host, cmdReq)
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/containerd-shim-runhcs-v1/task_wcow_podsandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats"
"github.com/Microsoft/hcsshim/internal/cmd"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/internal/shimdiag"
"github.com/Microsoft/hcsshim/internal/uvm"
Expand Down Expand Up @@ -253,6 +254,8 @@ func (wpst *wcowPodSandboxTask) ExecInHost(ctx context.Context, req *shimdiag.Ex
if wpst.host == nil {
return 0, errTaskNotIsolated
}

ctx, _ = log.WithContext(ctx, log.G(ctx).WithField(logfields.UVMID, wpst.host.ID()))
return cmd.ExecInUvm(ctx, wpst.host, cmdReq)
}

Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/diag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"errors"
"os/exec"

"github.com/Microsoft/hcsshim/internal/cow"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/internal/uvm"
)

// ExecInUvm is a helper function used to execute commands specified in `req` inside the given UVM.
func ExecInUvm(ctx context.Context, vm *uvm.UtilityVM, req *CmdProcessRequest) (int, error) {
func ExecInUvm(ctx context.Context, vm cow.ProcessHost, req *CmdProcessRequest) (int, error) {
if len(req.Args) == 0 {
return 0, errors.New("missing command")
}
Expand All @@ -33,7 +32,7 @@ func ExecInUvm(ctx context.Context, vm *uvm.UtilityVM, req *CmdProcessRequest) (
cmd.Stdin = np.Stdin()
cmd.Stdout = np.Stdout()
cmd.Stderr = np.Stderr()
cmd.Log = log.G(ctx).WithField(logfields.UVMID, vm.ID())
cmd.Log = log.G(ctx)
err = cmd.Run()
return cmd.ExitState.ExitCode(), err
}
Expand Down
18 changes: 2 additions & 16 deletions internal/devices/assigned_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ package devices
import (
"context"
"fmt"
"path/filepath"
"strconv"

"github.com/Microsoft/hcsshim/internal/cmd"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/uvm"
"github.com/Microsoft/hcsshim/internal/vm/vmutils"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ func AddDevice(ctx context.Context, vm *uvm.UtilityVM, idType, deviceID string,
}
}()

if uvm.IsValidDeviceType(idType) {
if vmutils.IsValidDeviceType(idType) {
vpci, err = vm.AssignDevice(ctx, deviceID, index, "")
if err != nil {
return vpci, nil, errors.Wrapf(err, "failed to assign device %s of type %s to pod %s", deviceID, idType, vm.ID())
Expand Down Expand Up @@ -107,16 +106,3 @@ func createDeviceUtilChildrenCommand(deviceUtilPath string, vmBusInstanceID stri
args := []string{deviceUtilPath, "children", parentIDsFlag, "--property=location"}
return args
}

// GetDeviceInfoFromPath takes a device path and parses it into the PCI ID and
// virtual function index if one is specified.
func GetDeviceInfoFromPath(rawDevicePath string) (string, uint16) {
indexString := filepath.Base(rawDevicePath)
index, err := strconv.ParseUint(indexString, 10, 16)
if err == nil {
// we have a vf index
return filepath.Dir(rawDevicePath), uint16(index)
}
// otherwise, just use default index and full device ID given
return rawDevicePath, 0
}
2 changes: 1 addition & 1 deletion internal/devices/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/Microsoft/hcsshim/internal/uvm/scsi"
)

// InstallDriver mounts a share from the host into the UVM, installs any kernel drivers in the share,
// InstallDrivers mounts a share from the host into the UVM, installs any kernel drivers in the share,
// and configures the environment for library files and/or binaries in the share.
//
// InstallDriver mounts a specified kernel driver, then installs it in the UVM.
Expand Down
10 changes: 10 additions & 0 deletions internal/hcs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type System struct {
exitError error
os, typ, owner string
startTime time.Time
stopTime time.Time
}

var _ cow.Container = &System{}
Expand Down Expand Up @@ -292,6 +293,7 @@ func (computeSystem *System) waitBackground() {
}
computeSystem.closedWaitOnce.Do(func() {
computeSystem.waitError = err
computeSystem.stopTime = time.Now()
close(computeSystem.waitBlock)
})
oc.SetSpanStatus(span, err)
Expand Down Expand Up @@ -871,3 +873,11 @@ func (computeSystem *System) Modify(ctx context.Context, config interface{}) err

return nil
}

func (computeSystem *System) StoppedTime() time.Time {
return computeSystem.stopTime
}

func (computeSystem *System) StartedTime() time.Time {
return computeSystem.startTime
}
4 changes: 4 additions & 0 deletions internal/hcsoci/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/Microsoft/hcsshim/internal/hvsocket"
"github.com/Microsoft/hcsshim/internal/layers"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/Microsoft/hcsshim/internal/oci"
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
"github.com/Microsoft/hcsshim/internal/resources"
Expand Down Expand Up @@ -204,6 +205,9 @@ func CreateContainer(ctx context.Context, createOptions *CreateOptions) (_ cow.C
}()

if coi.HostingSystem != nil {
// Set the UVM ID in ctx so that it gets logged in all subsequent calls.
ctx, _ = log.WithContext(ctx, log.G(ctx).WithField(logfields.UVMID, coi.HostingSystem.ID()))

if coi.Spec.Linux != nil {
r.SetContainerRootInUVM(fmt.Sprintf(lcowRootInUVM, coi.ID))
} else {
Expand Down
7 changes: 4 additions & 3 deletions internal/hcsoci/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/Microsoft/hcsshim/internal/oci"
"github.com/Microsoft/hcsshim/internal/resources"
"github.com/Microsoft/hcsshim/internal/uvm"
"github.com/Microsoft/hcsshim/internal/vm/vmutils"
"github.com/Microsoft/hcsshim/osversion"
"github.com/Microsoft/hcsshim/pkg/annotations"
)
Expand Down Expand Up @@ -130,7 +131,7 @@ func handleAssignedDevicesWindows(

// assign device into UVM and create corresponding spec windows devices
for _, d := range specDevs {
pciID, index := devices.GetDeviceInfoFromPath(d.ID)
pciID, index := vmutils.GetDeviceInfoFromPath(d.ID)
vpciCloser, locationPaths, err := devices.AddDevice(ctx, vm, d.IDType, pciID, index, deviceUtilPath)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -174,11 +175,11 @@ func handleAssignedDevicesLCOW(

// assign device into UVM and create corresponding spec windows devices
for _, d := range specDevs {
if !uvm.IsValidDeviceType(d.IDType) {
if !vmutils.IsValidDeviceType(d.IDType) {
return resultDevs, closers, errors.Errorf("specified device %s has unsupported type %s", d.ID, d.IDType)
}

pciID, index := devices.GetDeviceInfoFromPath(d.ID)
pciID, index := vmutils.GetDeviceInfoFromPath(d.ID)
vpci, err := vm.AssignDevice(ctx, pciID, index, "")
if err != nil {
return resultDevs, closers, errors.Wrapf(err, "failed to assign device %s, function %d to pod %s", pciID, index, vm.ID())
Expand Down
16 changes: 8 additions & 8 deletions internal/oci/uvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"strconv"

runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
"github.com/Microsoft/hcsshim/internal/vm/vmutils"
"github.com/Microsoft/hcsshim/pkg/annotations"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"

iannotations "github.com/Microsoft/hcsshim/internal/annotations"
"github.com/Microsoft/hcsshim/internal/devices"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/uvm"
)
Expand Down Expand Up @@ -156,7 +156,7 @@ func handleAnnotationBootFilesPath(ctx context.Context, a map[string]string, lop
func handleAnnotationKernelDirectBoot(ctx context.Context, a map[string]string, lopts *uvm.OptionsLCOW) {
lopts.KernelDirect = ParseAnnotationsBool(ctx, a, annotations.KernelDirectBoot, lopts.KernelDirect)
if !lopts.KernelDirect {
lopts.KernelFile = uvm.KernelFile
lopts.KernelFile = vmutils.KernelFile
}
}

Expand All @@ -166,9 +166,9 @@ func handleAnnotationPreferredRootFSType(ctx context.Context, a map[string]strin
lopts.PreferredRootFSType = parseAnnotationsPreferredRootFSType(ctx, a, annotations.PreferredRootFSType, lopts.PreferredRootFSType)
switch lopts.PreferredRootFSType {
case uvm.PreferredRootFSTypeInitRd:
lopts.RootFSFile = uvm.InitrdFile
lopts.RootFSFile = vmutils.InitrdFile
case uvm.PreferredRootFSTypeVHD:
lopts.RootFSFile = uvm.VhdFile
lopts.RootFSFile = vmutils.VhdFile
}
}

Expand Down Expand Up @@ -206,7 +206,7 @@ func handleLCOWSecurityPolicy(ctx context.Context, a map[string]string, lopts *u
// VPMem not supported by the enlightened kernel for SNP so set count to zero.
lopts.VPMemDeviceCount = 0
// set the default GuestState filename.
lopts.GuestStateFilePath = uvm.GuestStateFile
lopts.GuestStateFilePath = vmutils.DefaultGuestStateFile
lopts.KernelBootOptions = ""
lopts.AllowOvercommit = false
lopts.SecurityPolicyEnabled = true
Expand All @@ -219,7 +219,7 @@ func handleLCOWSecurityPolicy(ctx context.Context, a map[string]string, lopts *u
// The default behavior is to use kernel.vmgs and a rootfs-verity.vhd file with Merkle tree appended to ext4 filesystem.
lopts.PreferredRootFSType = uvm.PreferredRootFSTypeNA
lopts.RootFSFile = ""
lopts.DmVerityRootFsVhd = uvm.DefaultDmVerityRootfsVhd
lopts.DmVerityRootFsVhd = vmutils.DefaultDmVerityRootfsVhd
lopts.DmVerityMode = true
}

Expand Down Expand Up @@ -285,8 +285,8 @@ func parseDevices(ctx context.Context, specWindows *specs.Windows) []uvm.VPCIDev
}
extraDevices := []uvm.VPCIDeviceID{}
for _, d := range specWindows.Devices {
pciID, index := devices.GetDeviceInfoFromPath(d.ID)
if uvm.IsValidDeviceType(d.IDType) {
pciID, index := vmutils.GetDeviceInfoFromPath(d.ID)
if vmutils.IsValidDeviceType(d.IDType) {
key := uvm.NewVPCIDeviceID(pciID, index)
extraDevices = append(extraDevices, key)
} else {
Expand Down
17 changes: 9 additions & 8 deletions internal/tools/uvmboot/lcow.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/Microsoft/hcsshim/internal/memory"
"github.com/Microsoft/hcsshim/internal/oci"
"github.com/Microsoft/hcsshim/internal/uvm"
"github.com/Microsoft/hcsshim/internal/vm/vmutils"
)

const (
Expand Down Expand Up @@ -196,10 +197,10 @@ func createLCOWOptions(ctx context.Context, c *cli.Context, id string) (*uvm.Opt
}
if c.IsSet(kernelFileArgName) {
switch strings.ToLower(c.String(kernelFileArgName)) {
case uvm.KernelFile:
options.KernelFile = uvm.KernelFile
case uvm.UncompressedKernelFile:
options.KernelFile = uvm.UncompressedKernelFile
case vmutils.KernelFile:
options.KernelFile = vmutils.KernelFile
case vmutils.UncompressedKernelFile:
options.KernelFile = vmutils.UncompressedKernelFile
default:
return nil, unrecognizedError(c.String(kernelFileArgName), kernelFileArgName)
}
Expand All @@ -212,10 +213,10 @@ func createLCOWOptions(ctx context.Context, c *cli.Context, id string) (*uvm.Opt
if c.IsSet(rootFSTypeArgName) {
switch strings.ToLower(c.String(rootFSTypeArgName)) {
case "initrd":
options.RootFSFile = uvm.InitrdFile
options.RootFSFile = vmutils.InitrdFile
options.PreferredRootFSType = uvm.PreferredRootFSTypeInitRd
case "vhd":
options.RootFSFile = uvm.VhdFile
options.RootFSFile = vmutils.VhdFile
options.PreferredRootFSType = uvm.PreferredRootFSTypeVHD
case "none":
options.RootFSFile = ""
Expand Down Expand Up @@ -247,7 +248,7 @@ func createLCOWOptions(ctx context.Context, c *cli.Context, id string) (*uvm.Opt
if c.IsSet(outputHandlingArgName) {
switch strings.ToLower(c.String(outputHandlingArgName)) {
case "stdout":
options.OutputHandlerCreator = func(*uvm.Options) uvm.OutputHandler {
options.OutputHandlerCreator = func(string) vmutils.OutputHandler {
return func(r io.Reader) {
_, _ = io.Copy(os.Stdout, r)
}
Expand All @@ -274,7 +275,7 @@ func createLCOWOptions(ctx context.Context, c *cli.Context, id string) (*uvm.Opt
options.SecurityPolicyEnforcer = c.String(securityPolicyEnforcerArgName)
}
if c.IsSet(securityHardwareFlag) {
options.GuestStateFilePath = uvm.GuestStateFile
options.GuestStateFilePath = vmutils.DefaultGuestStateFile
options.SecurityPolicyEnabled = true
options.AllowOvercommit = false
}
Expand Down
15 changes: 0 additions & 15 deletions internal/uvm/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,9 @@ package uvm
import (
"errors"

"github.com/Microsoft/hcsshim/internal/memory"
"github.com/Microsoft/hcsshim/internal/protocol/guestrequest"
)

const (
// MaxVPMEMCount is the maximum number of VPMem devices that may be added to an LCOW
// utility VM
MaxVPMEMCount = 128

// DefaultVPMEMCount is the default number of VPMem devices that may be added to an LCOW
// utility VM if the create request doesn't specify how many.
DefaultVPMEMCount = 64

// DefaultVPMemSizeBytes is the default size of a VPMem device if the create request
// doesn't specify.
DefaultVPMemSizeBytes = 4 * memory.GiB // 4GB
)

var (
errNotSupported = errors.New("not supported")
errBadUVMOpts = errors.New("UVM options incorrect")
Expand Down
3 changes: 0 additions & 3 deletions internal/uvm/cpugroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import (
"github.com/Microsoft/hcsshim/internal/cpugroup"
"github.com/Microsoft/hcsshim/internal/hcs/resourcepaths"
hcsschema "github.com/Microsoft/hcsshim/internal/hcs/schema2"
"github.com/Microsoft/hcsshim/osversion"
)

var errCPUGroupCreateNotSupported = fmt.Errorf("cpu group assignment on create requires a build of %d or higher", osversion.V21H1)

// ReleaseCPUGroup unsets the cpugroup from the VM
func (uvm *UtilityVM) ReleaseCPUGroup(ctx context.Context) error {
if err := uvm.unsetCPUGroup(ctx); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/uvm/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func verifyOptions(_ context.Context, options interface{}) error {
if opts.SCSIControllerCount > MaxSCSIControllers {
return fmt.Errorf("SCSI controller count can't be more than %d", MaxSCSIControllers)
}
if opts.VPMemDeviceCount > MaxVPMEMCount {
return fmt.Errorf("VPMem device count cannot be greater than %d", MaxVPMEMCount)
if opts.VPMemDeviceCount > vmutils.MaxVPMEMCount {
return fmt.Errorf("VPMem device count cannot be greater than %d", vmutils.MaxVPMEMCount)
}
if opts.VPMemDeviceCount > 0 {
if opts.VPMemSizeBytes%4096 != 0 {
Expand Down
Loading
Loading