Skip to content

Commit 4085202

Browse files
csweichelroboquat
authored andcommitted
[workspacekit] Establish ring1 cgroup namespace
1 parent 5702a9c commit 4085202

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

components/workspacekit/cmd/rings.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ var ring0Cmd = &cobra.Command{
104104
cmd := exec.Command("/proc/self/exe", "ring1")
105105
cmd.SysProcAttr = &syscall.SysProcAttr{
106106
Pdeathsig: syscall.SIGKILL,
107-
Cloneflags: syscall.CLONE_NEWUSER | syscall.CLONE_NEWNS,
107+
Cloneflags: syscall.CLONE_NEWUSER | syscall.CLONE_NEWNS | unix.CLONE_NEWCGROUP,
108108
}
109109
cmd.Stdin = os.Stdin
110110
cmd.Stdout = os.Stdout
@@ -288,6 +288,12 @@ var ring1Cmd = &cobra.Command{
288288
}
289289
mnts = append(mnts, mnte{Target: "/tmp", Source: "tmpfs", FSType: "tmpfs"})
290290

291+
// If this is a cgroupv2 machine, we'll want to mount the cgroup2 FS ourselves
292+
if _, err := os.Stat("/sys/fs/cgroup/cgroup.controllers"); err == nil {
293+
mnts = append(mnts, mnte{Target: "/sys/fs/cgroup", Source: "tmpfs", FSType: "tmpfs"})
294+
mnts = append(mnts, mnte{Target: "/sys/fs/cgroup", Source: "cgroup", FSType: "cgroup2"})
295+
}
296+
291297
if adds := os.Getenv("GITPOD_WORKSPACEKIT_BIND_MOUNTS"); adds != "" {
292298
var additionalMounts []string
293299
err = json.Unmarshal([]byte(adds), &additionalMounts)
@@ -335,14 +341,13 @@ var ring1Cmd = &cobra.Command{
335341
}).Debug("mounting new rootfs")
336342
err = unix.Mount(m.Source, dst, m.FSType, m.Flags, "")
337343
if err != nil {
338-
log.WithError(err).WithField("dest", dst).Error("cannot establish mount")
344+
log.WithError(err).WithField("dest", dst).WithField("fsType", m.FSType).Error("cannot establish mount")
339345
return
340346
}
341347
}
342348

343349
// We deliberately do not bind mount `/etc/resolv.conf` and `/etc/hosts`, but instead place a copy
344350
// so that users in the workspace can modify the file.
345-
346351
copyPaths := []string{"/etc/resolv.conf", "/etc/hosts"}
347352
for _, fn := range copyPaths {
348353
err = copyRing2Root(ring2Root, fn)
@@ -411,12 +416,18 @@ var ring1Cmd = &cobra.Command{
411416
Target: procLoc,
412417
Pid: int64(cmd.Process.Pid),
413418
})
414-
client.Close()
415-
416419
if err != nil {
420+
client.Close()
417421
log.WithError(err).Error("cannot mount proc")
418422
return
419423
}
424+
_, err = client.EvacuateCGroup(ctx, &daemonapi.EvacuateCGroupRequest{})
425+
if err != nil {
426+
client.Close()
427+
log.WithError(err).Error("cannot evacuate cgroup")
428+
return
429+
}
430+
client.Close()
420431

421432
// We have to wait for ring2 to come back to us and connect to the socket we've passed along.
422433
// There's a chance that ring2 crashes or misbehaves, so we don't want to wait forever, hence
@@ -618,7 +629,7 @@ func findBindMountCandidates(procMounts io.Reader, readlink func(path string) (d
618629
reject bool
619630
)
620631
switch fs {
621-
case "cgroup", "devpts", "mqueue", "shm", "proc", "sysfs":
632+
case "cgroup", "devpts", "mqueue", "shm", "proc", "sysfs", "cgroup2":
622633
reject = true
623634
}
624635
if reject {

0 commit comments

Comments
 (0)