blob: 02173c5c2cfc269bb8a82bb6b2cfd7797ff138d7 [file] [log] [blame]
Lorenz Brun3a99c592021-01-26 19:57:21 +01001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package main
18
19import (
20 "fmt"
Lorenz Brun3a99c592021-01-26 19:57:21 +010021 "os"
Lorenz Brun3a99c592021-01-26 19:57:21 +010022
Lorenz Brunfe6b5062024-07-02 16:32:35 +000023 "github.com/opencontainers/runc/libcontainer/cgroups"
Lorenz Brun3a99c592021-01-26 19:57:21 +010024 "golang.org/x/sys/unix"
Lorenz Brun3a99c592021-01-26 19:57:21 +010025)
26
Serge Bazanski216fe7b2021-05-21 18:36:16 +020027// setupMounts sets up basic mounts like sysfs, procfs, devtmpfs and cgroups.
28// This should be called early during init as a lot of processes depend on this
29// being available.
Serge Bazanskie803fc12022-01-25 14:58:24 +010030func setupMounts() error {
Lorenz Brun3a99c592021-01-26 19:57:21 +010031 // Set up target filesystems.
32 for _, el := range []struct {
33 dir string
34 fs string
35 flags uintptr
36 }{
37 {"/sys", "sysfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
Lorenz Brun09c275b2021-03-30 12:47:09 +020038 {"/sys/kernel/tracing", "tracefs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
Lorenz Brun6ef7f9b2021-10-21 13:02:40 +020039 {"/sys/firmware/efi/efivars", "efivarfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
Lorenz Brun1b2df232022-06-14 12:42:03 +020040 {"/sys/fs/pstore", "pstore", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
Lorenz Brun3a99c592021-01-26 19:57:21 +010041 {"/proc", "proc", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
42 {"/dev", "devtmpfs", unix.MS_NOEXEC | unix.MS_NOSUID},
43 {"/dev/pts", "devpts", unix.MS_NOEXEC | unix.MS_NOSUID},
Leopold Schabelc5e0dbd2024-07-24 13:18:45 +000044 // Nothing in Metropolis currently uses /dev/shm, but it's required
45 // by containerd when the host IPC namespace is shared, which
46 // is required by "kubectl debug node/" and specific customer applications.
47 // https://github.com/monogon-dev/monogon/issues/305.
48 {"/dev/shm", "tmpfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
Lorenz Brun3a99c592021-01-26 19:57:21 +010049 } {
50 if err := os.MkdirAll(el.dir, 0755); err != nil {
51 return fmt.Errorf("could not make %s: %w", el.dir, err)
52 }
53 if err := unix.Mount(el.fs, el.dir, el.fs, el.flags, ""); err != nil {
54 return fmt.Errorf("could not mount %s on %s: %w", el.fs, el.dir, err)
55 }
56 }
57
Lorenz Brunfe6b5062024-07-02 16:32:35 +000058 if err := unix.Mount("cgroup2", "/sys/fs/cgroup", "cgroup2", unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV, "nsdelegate,memory_recursiveprot"); err != nil {
Lorenz Brun3a99c592021-01-26 19:57:21 +010059 panic(err)
60 }
Lorenz Brunfe6b5062024-07-02 16:32:35 +000061 // Create main cgroup "everything" and move ourselves into it.
62 if err := os.Mkdir("/sys/fs/cgroup/everything", 0755); err != nil {
Lorenz Brun3a99c592021-01-26 19:57:21 +010063 panic(err)
64 }
Lorenz Brunfe6b5062024-07-02 16:32:35 +000065 if err := cgroups.WriteCgroupProc("/sys/fs/cgroup/everything", os.Getpid()); err != nil {
Lorenz Brun3a99c592021-01-26 19:57:21 +010066 panic(err)
67 }
Lorenz Brun3a99c592021-01-26 19:57:21 +010068 return nil
69}