Skip to content

Commit c70fd9f

Browse files
authored
adds containerd-shim-lcow-v2 shim (#2627)
In this commit, we are adding the initial draft of `containerd-shim-lcow-v2`. This shim is used for running Linux Containers on Windows. As part of this commit, we are adding the following- - The main entrypoint and supporting logic to start + serve the shim server. - The stubs for Task Service and Shimdiag service. - The implementation for `Sandbox` service. - The implementation for `VM Controller` which manages the VM lifecycle and operations. Signed-off-by: Harsh Rawat <harshrawat@microsoft.com>
1 parent 50f4409 commit c70fd9f

26 files changed

Lines changed: 2736 additions & 2 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
3+
<description>containerd-shim-lcow-v2</description>
4+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
5+
<application>
6+
<!-- Windows 10+, Windows 11, Server 2016/2019/2022/2025 and future -->
7+
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
8+
</application>
9+
</compatibility>
10+
<application xmlns="urn:schemas-microsoft-com:asm.v3">
11+
<!-- Long Path Enablement -->
12+
<windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
13+
<ws2:longPathAware>true</ws2:longPathAware>
14+
</windowsSettings>
15+
</application>
16+
</assembly>
17+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//go:build windows
2+
3+
// containerd-shim-lcow-v2 is a containerd shim implementation for Linux Containers on Windows (LCOW).
4+
package main
5+
6+
import (
7+
"context"
8+
"errors"
9+
"fmt"
10+
"io"
11+
"os"
12+
13+
"github.com/Microsoft/hcsshim/cmd/containerd-shim-lcow-v2/service"
14+
_ "github.com/Microsoft/hcsshim/cmd/containerd-shim-lcow-v2/service/plugin"
15+
runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
16+
"github.com/Microsoft/hcsshim/internal/log"
17+
"github.com/Microsoft/hcsshim/internal/oc"
18+
"github.com/Microsoft/hcsshim/internal/shim"
19+
20+
"github.com/containerd/errdefs"
21+
"github.com/sirupsen/logrus"
22+
"go.opencensus.io/trace"
23+
)
24+
25+
// Add a manifest to get proper Windows version detection.
26+
//go:generate go tool github.com/josephspurrier/goversioninfo/cmd/goversioninfo -platform-specific
27+
28+
func main() {
29+
logrus.AddHook(log.NewHook())
30+
31+
// Register our OpenCensus logrus exporter so that trace spans are emitted via logrus.
32+
trace.ApplyConfig(trace.Config{DefaultSampler: oc.DefaultSampler})
33+
trace.RegisterExporter(&oc.LogrusExporter{})
34+
35+
logrus.SetFormatter(log.NopFormatter{})
36+
logrus.SetOutput(io.Discard)
37+
38+
// Set the log configuration.
39+
// If we encounter an error, we exit with non-zero code.
40+
if err := setLogConfiguration(); err != nil {
41+
_, _ = fmt.Fprintf(os.Stderr, "%s: %s", service.ShimName, err)
42+
os.Exit(1)
43+
}
44+
45+
// Start the shim manager event loop. The manager is responsible for
46+
// handling containerd start/stop lifecycle calls for the shim process.
47+
shim.Run(context.Background(), newShimManager(service.ShimName), func(c *shim.Config) {
48+
// We don't want the shim package to set up logging options.
49+
c.NoSetupLogger = true
50+
})
51+
}
52+
53+
// setLogConfiguration reads the runtime options from stdin and sets the log configuration.
54+
// We only set up the log configuration for serve action.
55+
func setLogConfiguration() error {
56+
// We set up the log configuration in the serve action only.
57+
// This is because we want to avoid reading the stdin in start action,
58+
// so that we can pass it along to the invocation for serve action.
59+
if len(os.Args) > 1 && os.Args[len(os.Args)-1] == "serve" {
60+
// The serve process is started with stderr pointing to panic.log file.
61+
// We want to keep that file only for pure Go panics. Any explicit writes
62+
// to os.Stderr should go to stdout instead, which is connected to the parent's
63+
// stderr for regular logging.
64+
// We can safely redirect os.Stderr to os.Stdout because in case of panics,
65+
// the Go runtime will write the panic stack trace directly to the file descriptor,
66+
// bypassing os.Stderr, so it will still go to panic.log.
67+
os.Stderr = os.Stdout
68+
69+
opts, err := shim.ReadRuntimeOptions[*runhcsopts.Options](os.Stdin)
70+
if err != nil {
71+
if !errors.Is(err, errdefs.ErrNotFound) {
72+
return fmt.Errorf("failed to read runtime options from stdin: %w", err)
73+
}
74+
}
75+
76+
if opts != nil {
77+
if opts.LogLevel != "" {
78+
// If log level is specified, set the corresponding logrus logging level.
79+
lvl, err := logrus.ParseLevel(opts.LogLevel)
80+
if err != nil {
81+
return fmt.Errorf("failed to parse shim log level %q: %w", opts.LogLevel, err)
82+
}
83+
logrus.SetLevel(lvl)
84+
}
85+
86+
if opts.ScrubLogs {
87+
log.SetScrubbing(true)
88+
}
89+
}
90+
_ = os.Stdin.Close()
91+
}
92+
return nil
93+
}

0 commit comments

Comments
 (0)