Skip to content

Commit 2605a68

Browse files
authored
Merge pull request microsoft#2612 from rawahars/refactor-into-vmutils
Refactor GCS logging and guest management for reusability
2 parents d286c1d + 3b03356 commit 2605a68

33 files changed

Lines changed: 964 additions & 360 deletions

cmd/containerd-shim-runhcs-v1/task_hcs.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/Microsoft/hcsshim/internal/jobcontainers"
3838
"github.com/Microsoft/hcsshim/internal/layers"
3939
"github.com/Microsoft/hcsshim/internal/log"
40+
"github.com/Microsoft/hcsshim/internal/logfields"
4041
"github.com/Microsoft/hcsshim/internal/memory"
4142
"github.com/Microsoft/hcsshim/internal/oc"
4243
"github.com/Microsoft/hcsshim/internal/oci"
@@ -768,7 +769,9 @@ func (ht *hcsTask) ExecInHost(ctx context.Context, req *shimdiag.ExecProcessRequ
768769
if ht.host == nil {
769770
return cmd.ExecInShimHost(ctx, cmdReq)
770771
}
771-
return cmd.ExecInUvm(ctx, ht.host, cmdReq)
772+
773+
ctx, _ = log.SetEntry(ctx, logrus.Fields{logfields.UVMID: ht.host.ID()})
774+
return ht.host.ExecInUVM(ctx, cmdReq)
772775
}
773776

774777
func (ht *hcsTask) DumpGuestStacks(ctx context.Context) string {

cmd/containerd-shim-runhcs-v1/task_wcow_podsandbox.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ import (
1111
"github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats"
1212
"github.com/Microsoft/hcsshim/internal/cmd"
1313
"github.com/Microsoft/hcsshim/internal/log"
14+
"github.com/Microsoft/hcsshim/internal/logfields"
1415
"github.com/Microsoft/hcsshim/internal/oc"
1516
"github.com/Microsoft/hcsshim/internal/shimdiag"
1617
"github.com/Microsoft/hcsshim/internal/uvm"
18+
1719
eventstypes "github.com/containerd/containerd/api/events"
1820
task "github.com/containerd/containerd/api/runtime/task/v2"
1921
"github.com/containerd/containerd/v2/core/runtime"
2022
"github.com/containerd/errdefs"
2123
typeurl "github.com/containerd/typeurl/v2"
2224
"github.com/opencontainers/runtime-spec/specs-go"
2325
"github.com/pkg/errors"
26+
"github.com/sirupsen/logrus"
2427
"go.opencensus.io/trace"
2528
)
2629

@@ -253,7 +256,9 @@ func (wpst *wcowPodSandboxTask) ExecInHost(ctx context.Context, req *shimdiag.Ex
253256
if wpst.host == nil {
254257
return 0, errTaskNotIsolated
255258
}
256-
return cmd.ExecInUvm(ctx, wpst.host, cmdReq)
259+
260+
ctx, _ = log.SetEntry(ctx, logrus.Fields{logfields.UVMID: wpst.host.ID()})
261+
return wpst.host.ExecInUVM(ctx, cmdReq)
257262
}
258263

259264
func (wpst *wcowPodSandboxTask) DumpGuestStacks(ctx context.Context) string {

internal/cmd/diag.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import (
77
"errors"
88
"os/exec"
99

10+
"github.com/Microsoft/hcsshim/internal/gcs"
1011
"github.com/Microsoft/hcsshim/internal/log"
11-
"github.com/Microsoft/hcsshim/internal/logfields"
12-
"github.com/Microsoft/hcsshim/internal/uvm"
1312
)
1413

1514
// ExecInUvm is a helper function used to execute commands specified in `req` inside the given UVM.
16-
func ExecInUvm(ctx context.Context, vm *uvm.UtilityVM, req *CmdProcessRequest) (int, error) {
15+
func ExecInUvm(ctx context.Context, guestVM *gcs.GuestConnection, req *CmdProcessRequest) (int, error) {
1716
if len(req.Args) == 0 {
1817
return 0, errors.New("missing command")
1918
}
@@ -22,18 +21,18 @@ func ExecInUvm(ctx context.Context, vm *uvm.UtilityVM, req *CmdProcessRequest) (
2221
return 0, err
2322
}
2423
defer np.Close(ctx)
25-
cmd := CommandContext(ctx, vm, req.Args[0], req.Args[1:]...)
24+
cmd := CommandContext(ctx, guestVM, req.Args[0], req.Args[1:]...)
2625
if req.Workdir != "" {
2726
cmd.Spec.Cwd = req.Workdir
2827
}
29-
if vm.OS() == "windows" {
28+
if guestVM.OS() == "windows" {
3029
cmd.Spec.User.Username = `NT AUTHORITY\SYSTEM`
3130
}
3231
cmd.Spec.Terminal = req.Terminal
3332
cmd.Stdin = np.Stdin()
3433
cmd.Stdout = np.Stdout()
3534
cmd.Stderr = np.Stderr()
36-
cmd.Log = log.G(ctx).WithField(logfields.UVMID, vm.ID())
35+
cmd.Log = log.G(ctx)
3736
err = cmd.Run()
3837
return cmd.ExitState.ExitCode(), err
3938
}

internal/devices/assigned_devices.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func getChildrenDeviceLocationPaths(ctx context.Context, vm *uvm.UtilityVM, vmBu
7575
Args: args,
7676
Stdout: p,
7777
}
78-
exitCode, err := cmd.ExecInUvm(ctx, vm, cmdReq)
78+
exitCode, err := vm.ExecInUVM(ctx, cmdReq)
7979
if err != nil {
8080
return nil, errors.Wrapf(err, "failed to find devices with exit code %d", exitCode)
8181
}

internal/devices/drivers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import (
1717
"github.com/Microsoft/hcsshim/internal/uvm/scsi"
1818
)
1919

20-
// InstallDriver mounts a share from the host into the UVM, installs any kernel drivers in the share,
20+
// InstallDrivers mounts a share from the host into the UVM, installs any kernel drivers in the share,
2121
// and configures the environment for library files and/or binaries in the share.
2222
//
23-
// InstallDriver mounts a specified kernel driver, then installs it in the UVM.
23+
// InstallDrivers mounts a specified kernel driver, then installs it in the UVM.
2424
//
2525
// `share` is a directory path on the host that contains files for standard driver installation.
2626
// For windows this means files for pnp installation (.inf, .cat, .sys, .cert files).
@@ -112,7 +112,7 @@ func execGCSInstallDriver(ctx context.Context, vm *uvm.UtilityVM, driverDir stri
112112
// - There's an error copying IO. No need to wait for stderr logs.
113113
//
114114
// Since we cannot distinguish between the cases above, we should always wait to read the stderr output.
115-
exitCode, execErr := cmd.ExecInUvm(ctx, vm, req)
115+
exitCode, execErr := vm.ExecInUVM(ctx, req)
116116

117117
// wait to finish parsing stdout results
118118
select {

internal/devices/pnp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func execPnPInstallDriver(ctx context.Context, vm *uvm.UtilityVM, driverDir stri
5151
cmdReq := &cmd.CmdProcessRequest{
5252
Args: args,
5353
}
54-
exitCode, err := cmd.ExecInUvm(ctx, vm, cmdReq)
54+
exitCode, err := vm.ExecInUVM(ctx, cmdReq)
5555
if err != nil && exitCode != winapi.ERROR_NO_MORE_ITEMS {
5656
return errors.Wrapf(err, "failed to install driver %s in uvm with exit code %d", driverDir, exitCode)
5757
} else if exitCode == winapi.ERROR_NO_MORE_ITEMS {

internal/hcs/system.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type System struct {
3737
exitError error
3838
os, typ, owner string
3939
startTime time.Time
40+
stopTime time.Time
4041
}
4142

4243
var _ cow.Container = &System{}
@@ -292,6 +293,7 @@ func (computeSystem *System) waitBackground() {
292293
}
293294
computeSystem.closedWaitOnce.Do(func() {
294295
computeSystem.waitError = err
296+
computeSystem.stopTime = time.Now()
295297
close(computeSystem.waitBlock)
296298
})
297299
oc.SetSpanStatus(span, err)
@@ -871,3 +873,11 @@ func (computeSystem *System) Modify(ctx context.Context, config interface{}) err
871873

872874
return nil
873875
}
876+
877+
func (computeSystem *System) StoppedTime() time.Time {
878+
return computeSystem.stopTime
879+
}
880+
881+
func (computeSystem *System) StartedTime() time.Time {
882+
return computeSystem.startTime
883+
}

internal/hcsoci/create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/Microsoft/hcsshim/internal/hvsocket"
2323
"github.com/Microsoft/hcsshim/internal/layers"
2424
"github.com/Microsoft/hcsshim/internal/log"
25+
"github.com/Microsoft/hcsshim/internal/logfields"
2526
"github.com/Microsoft/hcsshim/internal/oci"
2627
"github.com/Microsoft/hcsshim/internal/protocol/guestresource"
2728
"github.com/Microsoft/hcsshim/internal/resources"
@@ -204,6 +205,9 @@ func CreateContainer(ctx context.Context, createOptions *CreateOptions) (_ cow.C
204205
}()
205206

206207
if coi.HostingSystem != nil {
208+
// Set the UVM ID in ctx so that it gets logged in all subsequent calls.
209+
ctx, _ = log.SetEntry(ctx, logrus.Fields{logfields.UVMID: coi.HostingSystem.ID()})
210+
207211
if coi.Spec.Linux != nil {
208212
r.SetContainerRootInUVM(fmt.Sprintf(lcowRootInUVM, coi.ID))
209213
} else {

internal/hcsoci/resources_wcow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func setupMounts(ctx context.Context, coi *createOptionsInternal, r *resources.R
217217
Args: []string{"cmd", "/c", "mkdir", sandboxPath, "&", "dir", sandboxPath},
218218
Stderr: stderr,
219219
}
220-
exitCode, err := cmd.ExecInUvm(ctx, coi.HostingSystem, req)
220+
exitCode, err := coi.HostingSystem.ExecInUVM(ctx, req)
221221
if err != nil {
222222
return errors.Wrapf(err, "failed to create sandbox mount directory in utility VM with exit code %d %q", exitCode, b.String())
223223
}

internal/oci/uvm.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strconv"
1111

1212
runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
13+
"github.com/Microsoft/hcsshim/internal/vm/vmutils"
1314
"github.com/Microsoft/hcsshim/pkg/annotations"
1415
"github.com/opencontainers/runtime-spec/specs-go"
1516
"github.com/sirupsen/logrus"
@@ -156,7 +157,7 @@ func handleAnnotationBootFilesPath(ctx context.Context, a map[string]string, lop
156157
func handleAnnotationKernelDirectBoot(ctx context.Context, a map[string]string, lopts *uvm.OptionsLCOW) {
157158
lopts.KernelDirect = ParseAnnotationsBool(ctx, a, annotations.KernelDirectBoot, lopts.KernelDirect)
158159
if !lopts.KernelDirect {
159-
lopts.KernelFile = uvm.KernelFile
160+
lopts.KernelFile = vmutils.KernelFile
160161
}
161162
}
162163

@@ -166,9 +167,9 @@ func handleAnnotationPreferredRootFSType(ctx context.Context, a map[string]strin
166167
lopts.PreferredRootFSType = parseAnnotationsPreferredRootFSType(ctx, a, annotations.PreferredRootFSType, lopts.PreferredRootFSType)
167168
switch lopts.PreferredRootFSType {
168169
case uvm.PreferredRootFSTypeInitRd:
169-
lopts.RootFSFile = uvm.InitrdFile
170+
lopts.RootFSFile = vmutils.InitrdFile
170171
case uvm.PreferredRootFSTypeVHD:
171-
lopts.RootFSFile = uvm.VhdFile
172+
lopts.RootFSFile = vmutils.VhdFile
172173
}
173174
}
174175

@@ -206,7 +207,7 @@ func handleLCOWSecurityPolicy(ctx context.Context, a map[string]string, lopts *u
206207
// VPMem not supported by the enlightened kernel for SNP so set count to zero.
207208
lopts.VPMemDeviceCount = 0
208209
// set the default GuestState filename.
209-
lopts.GuestStateFilePath = uvm.GuestStateFile
210+
lopts.GuestStateFilePath = vmutils.DefaultGuestStateFile
210211
lopts.KernelBootOptions = ""
211212
lopts.AllowOvercommit = false
212213
lopts.SecurityPolicyEnabled = true
@@ -219,7 +220,7 @@ func handleLCOWSecurityPolicy(ctx context.Context, a map[string]string, lopts *u
219220
// The default behavior is to use kernel.vmgs and a rootfs-verity.vhd file with Merkle tree appended to ext4 filesystem.
220221
lopts.PreferredRootFSType = uvm.PreferredRootFSTypeNA
221222
lopts.RootFSFile = ""
222-
lopts.DmVerityRootFsVhd = uvm.DefaultDmVerityRootfsVhd
223+
lopts.DmVerityRootFsVhd = vmutils.DefaultDmVerityRootfsVhd
223224
lopts.DmVerityMode = true
224225
}
225226

0 commit comments

Comments
 (0)