Skip to content

Commit 203cbd9

Browse files
committed
multiple containerd options
Signed-off-by: Avi Deitcher <avi@deitcher.net>
1 parent 9f1825f commit 203cbd9

File tree

20 files changed

+4897
-13
lines changed

20 files changed

+4897
-13
lines changed

docs/faq.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Please open an issue if you want to add a question here.
66

77
LinuxKit does not require being installed on a disk, it is often run from an ISO, PXE or other
88
such means, so it does not require an on disk upgrade method such as the ChromeOS code that
9-
is often used. It would definitely be possible to use that type of upgrade method if the
9+
is often used. It would definitely be possible to use that type of upgrade method if the
1010
system is installed, and it would be useful to support this for that use case, and an
1111
updater container to control this for people who want to use this.
1212

@@ -37,18 +37,41 @@ If you're not seeing `containerd` logs in the console during boot, make sure tha
3737

3838
`init` and other processes like `containerd` will use the last defined console in the kernel `cmdline`. When using `qemu`, to see the console you need to list `ttyS0` as the last console to properly see the output.
3939

40-
## Enabling debug or trace log levels on containerd
40+
## Enabling and controlling containerd logs
4141

42-
On startup, linuxkit looks for and parses a file `/etc/containerd/cli-opts`. If it exists, the content is used as arguments to containerd. Thus, to enable
42+
On startup, linuxkit looks for and parses a file `/etc/containerd/runtime-config.toml`. If it exists, the content is used to configure containerd runtime.
43+
44+
Sample config is below:
45+
46+
```toml
47+
cliopts="--log-level debug"
48+
stderr="/var/log/containerd.out.log"
49+
stdout="stdout"
50+
```
51+
52+
The options are as follows:
53+
54+
* `cliopts`: options to pass to the containerd command-line as is.
55+
* `stderr`: where to send stderr from containerd. If blank, it sends it to the default stderr, which is the console.
56+
* `stdout`: where to send stdout from containerd. If blank, it sends it to the default stdout, which is the console. containerd normally does not have any stdout.
57+
58+
The `stderr` and `stdout` options can take exactly one of the following options:
59+
60+
* `stderr` - send to stderr
61+
* `stdout` - send to stdout
62+
* any absolute path (beginning with `/`) - send to that file. If the file exists, append to it; if not, create it and append to it.
63+
64+
Thus, to enable
4365
a higher log level, for example `debug`, create a file whose contents are `--log-level debug` and place it on the image:
4466

4567
```yml
4668
files:
47-
- path: /etc/containerd/cli-opts
48-
contents: "--log-level debug"
69+
- path: /etc/containerd/runtime-config.toml
70+
source: "/path/to/runtime-config.toml"
71+
mode: "0644"
4972
```
5073
51-
Note that the package that parses the contents splits on _all_ whitespace. It does not, as of this writing, support shell-like parsing, so the following will work:
74+
Note that the package that parses the `cliopts` splits on _all_ whitespace. It does not, as of this writing, support shell-like parsing, so the following will work:
5275

5376
```
5477
--log-level debug --arg abcd

pkg/init/cmd/service/system_init.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"flag"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"os"
910
"os/exec"
@@ -14,12 +15,13 @@ import (
1415

1516
"github.com/containerd/containerd"
1617
"github.com/containerd/containerd/errdefs"
18+
"github.com/pelletier/go-toml"
1719
"github.com/pkg/errors"
1820
log "github.com/sirupsen/logrus"
1921
)
2022

2123
const (
22-
containerdOptsFile = "/etc/containerd/cli-opts"
24+
containerdOptsFile = "/etc/containerd/runtime-config.toml"
2325
)
2426

2527
func cleanupTask(ctx context.Context, ctr containerd.Container) error {
@@ -85,14 +87,43 @@ func systemInitCmd(ctx context.Context, args []string) {
8587

8688
// look for containerd options
8789
ctrdArgs := []string{}
90+
var (
91+
stderr io.Writer = os.Stderr
92+
stdout io.Writer = os.Stdout
93+
)
8894
if b, err := ioutil.ReadFile(containerdOptsFile); err == nil {
89-
ctrdArgs = strings.Fields(string(b))
95+
config, err := toml.LoadBytes(b)
96+
if err != nil {
97+
log.Fatalf("error reading toml file %s: %v", containerdOptsFile, err)
98+
}
99+
if config != nil {
100+
// did we have any CLI opts?
101+
cliOptsLine := config.Get("cliopts")
102+
if cliOptsLine != nil {
103+
ctrdArgs = strings.Fields(cliOptsLine.(string))
104+
}
105+
// stderr?
106+
stderrLine := config.Get("stderr")
107+
if stderrLine != nil {
108+
stderr, err = getWriter(stderrLine.(string))
109+
if err != nil {
110+
log.Fatal(err)
111+
}
112+
}
113+
stdoutLine := config.Get("stdout")
114+
if stdoutLine != nil {
115+
stdout, err = getWriter(stdoutLine.(string))
116+
if err != nil {
117+
log.Fatal(err)
118+
}
119+
}
120+
}
90121
}
91122

92123
// start up containerd
93124
cmd := exec.Command(*binary, ctrdArgs...)
94-
cmd.Stdout = os.Stdout
95-
cmd.Stderr = os.Stderr
125+
cmd.Stdout = stdout
126+
cmd.Stderr = stderr
96127
if err := cmd.Start(); err != nil {
97128
log.WithError(err).Fatal("cannot start containerd")
98129
}
@@ -155,3 +186,21 @@ func systemInitCmd(ctx context.Context, args []string) {
155186
}
156187
}
157188
}
189+
190+
func getWriter(line string) (io.Writer, error) {
191+
switch {
192+
case line == "stderr":
193+
return os.Stderr, nil
194+
case line == "stdout":
195+
return os.Stdout, nil
196+
case strings.HasPrefix(line, "/"):
197+
// does the file exist?
198+
f, err := os.OpenFile(line,
199+
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
200+
if err != nil {
201+
return nil, fmt.Errorf("unable to open file %s for creation or appending: %v", line, err)
202+
}
203+
return f, nil
204+
}
205+
return nil, fmt.Errorf("invalid option for writer: %s", line)
206+
}

pkg/init/cmd/service/vendor.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
github.com/vishvananda/netlink f5a6f697a596c788d474984a38a0ac4ba0719e93
22
github.com/vishvananda/netns 86bef332bfc3b59b7624a600bd53009ce91a9829
3+
github.com/pelletier/go-toml 5b4e7e5dcc567bbc53b25ad81e06493ede66d301

pkg/init/cmd/service/vendor/github.com/pelletier/go-toml/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/init/cmd/service/vendor/github.com/pelletier/go-toml/README.md

Lines changed: 151 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/init/cmd/service/vendor/github.com/pelletier/go-toml/doc.go

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/init/cmd/service/vendor/github.com/pelletier/go-toml/fuzz.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/init/cmd/service/vendor/github.com/pelletier/go-toml/go.mod

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)