Skip to content

Commit

Permalink
make systemd and its dependencies optional via 'no_systemd' build tag
Browse files Browse the repository at this point in the history
Running under systemd requires lots of special code that contributes
to ca. 10 percent (ca. 1 MB) to the binary size. This is only needed on
targets that might run systemd - there're dozens of distros, let alone
embedded/edge devices or special images (eg. cluster worker nodes) that
do not and never will run systemd, thus do not need that code at all.

It's not just about reducing memory consumption, but also having over
10.000 lines of code less to audit.

In order not to change default behaviour, introducing an inverse build tag,
'no_systemd', for explicitly opting out from systemd special handlings.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
  • Loading branch information
metux committed Aug 2, 2023
1 parent fe6f2e0 commit 45d2baa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ e.g. to disable seccomp:
make BUILDTAGS=""
```

| Build Tag | Feature | Enabled by default | Dependency |
|-----------|------------------------------------|--------------------|------------|
| seccomp | Syscall filtering | yes | libseccomp |
| Build Tag | Feature | Enabled by default | Dependency |
|------------|------------------------------------|--------------------|------------|
| seccomp | Syscall filtering | yes | libseccomp |
| no_systemd | disable systemd dependencies | no | systemd |

The following build tags were used earlier, but are now obsoleted:
- **nokmem** (since runc v1.0.0-rc94 kernel memory settings are ignored)
Expand Down
3 changes: 1 addition & 2 deletions rootless_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"os"

"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/opencontainers/runc/libcontainer/userns"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
Expand Down Expand Up @@ -37,7 +36,7 @@ func shouldUseRootlessCgroupManager(context *cli.Context) (bool, error) {
// On error, we assume we are root. An error may happen during shelling out to `busctl` CLI,
// mostly when $DBUS_SESSION_BUS_ADDRESS is unset.
if context.GlobalBool("systemd-cgroup") {
ownerUID, err := systemd.DetectUID()
ownerUID, err := sdDetectUID()
if err != nil {
logrus.WithError(err).Debug("failed to get the OwnerUID value, assuming the value to be 0")
ownerUID = 0
Expand Down
9 changes: 1 addition & 8 deletions utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"strconv"

"github.com/coreos/go-systemd/v22/activation"
"github.com/opencontainers/runtime-spec/specs-go"
selinux "github.com/opencontainers/selinux/go-selinux"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -372,17 +371,11 @@ func startContainer(context *cli.Context, action CtAct, criuOpts *libcontainer.C
}
}

// Support on-demand socket activation by passing file descriptors into the container init process.
listenFDs := []*os.File{}
if os.Getenv("LISTEN_FDS") != "" {
listenFDs = activation.Files(false)
}

r := &runner{
enableSubreaper: !context.Bool("no-subreaper"),
shouldDestroy: !context.Bool("keep"),
container: container,
listenFDs: listenFDs,
listenFDs: sdGetListenFDs(),
notifySocket: notifySocket,
consoleSocket: context.String("console-socket"),
detach: context.Bool("detach"),
Expand Down
16 changes: 16 additions & 0 deletions utils_linux_nosystemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build linux,no_systemd

package main

import (
"errors"
"os"
)

func sdGetListenFDs() ([]*os.File) {
return nil
}

func sdDetectUID() (int, error) {
return -1, errors.New("no lennartix")
}
22 changes: 22 additions & 0 deletions utils_linux_systemd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build linux,!no_systemd

package main

import (
"os"
"github.com/coreos/go-systemd/v22/activation"
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
)

func sdGetListenFDs() []*os.File {
// Support on-demand socket activation by passing file descriptors into the container init process.
listenFDs := []*os.File{}
if os.Getenv("LISTEN_FDS") != "" {
listenFDs = activation.Files(false)
}
return listenFDs
}

func sdDetectUID() (int, error) {
return systemd.DetectUID()
}

0 comments on commit 45d2baa

Please sign in to comment.