Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cgroupv2] Add workspace cgroup evacuation #8578

Merged
merged 3 commits into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions components/workspacekit/cmd/rings.go
Expand Up @@ -104,7 +104,7 @@ var ring0Cmd = &cobra.Command{
cmd := exec.Command("/proc/self/exe", "ring1")
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
Cloneflags: syscall.CLONE_NEWUSER | syscall.CLONE_NEWNS,
Cloneflags: syscall.CLONE_NEWUSER | syscall.CLONE_NEWNS | unix.CLONE_NEWCGROUP,
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand Down Expand Up @@ -288,6 +288,12 @@ var ring1Cmd = &cobra.Command{
}
mnts = append(mnts, mnte{Target: "/tmp", Source: "tmpfs", FSType: "tmpfs"})

// If this is a cgroupv2 machine, we'll want to mount the cgroup2 FS ourselves
if _, err := os.Stat("/sys/fs/cgroup/cgroup.controllers"); err == nil {
mnts = append(mnts, mnte{Target: "/sys/fs/cgroup", Source: "tmpfs", FSType: "tmpfs"})
mnts = append(mnts, mnte{Target: "/sys/fs/cgroup", Source: "cgroup", FSType: "cgroup2"})
}

if adds := os.Getenv("GITPOD_WORKSPACEKIT_BIND_MOUNTS"); adds != "" {
var additionalMounts []string
err = json.Unmarshal([]byte(adds), &additionalMounts)
Expand Down Expand Up @@ -335,14 +341,13 @@ var ring1Cmd = &cobra.Command{
}).Debug("mounting new rootfs")
err = unix.Mount(m.Source, dst, m.FSType, m.Flags, "")
if err != nil {
log.WithError(err).WithField("dest", dst).Error("cannot establish mount")
log.WithError(err).WithField("dest", dst).WithField("fsType", m.FSType).Error("cannot establish mount")
return
}
}

// We deliberately do not bind mount `/etc/resolv.conf` and `/etc/hosts`, but instead place a copy
// so that users in the workspace can modify the file.

copyPaths := []string{"/etc/resolv.conf", "/etc/hosts"}
for _, fn := range copyPaths {
err = copyRing2Root(ring2Root, fn)
Expand Down Expand Up @@ -411,12 +416,18 @@ var ring1Cmd = &cobra.Command{
Target: procLoc,
Pid: int64(cmd.Process.Pid),
})
client.Close()

if err != nil {
client.Close()
log.WithError(err).Error("cannot mount proc")
return
}
_, err = client.EvacuateCGroup(ctx, &daemonapi.EvacuateCGroupRequest{})
if err != nil {
client.Close()
log.WithError(err).Error("cannot evacuate cgroup")
return
}
client.Close()

// We have to wait for ring2 to come back to us and connect to the socket we've passed along.
// There's a chance that ring2 crashes or misbehaves, so we don't want to wait forever, hence
Expand Down Expand Up @@ -618,7 +629,7 @@ func findBindMountCandidates(procMounts io.Reader, readlink func(path string) (d
reject bool
)
switch fs {
case "cgroup", "devpts", "mqueue", "shm", "proc", "sysfs":
case "cgroup", "devpts", "mqueue", "shm", "proc", "sysfs", "cgroup2":
reject = true
}
if reject {
Expand Down
2 changes: 1 addition & 1 deletion components/ws-daemon-api/go/daemon.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/ws-daemon-api/go/daemon_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion components/ws-daemon-api/go/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.