Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 1 | // 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 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 21 | "os" |
| 22 | "strings" |
| 23 | |
| 24 | "golang.org/x/sys/unix" |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 25 | ) |
| 26 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 27 | // 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 Bazanski | e803fc1 | 2022-01-25 14:58:24 +0100 | [diff] [blame] | 30 | func setupMounts() error { |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 31 | // 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 Brun | 09c275b | 2021-03-30 12:47:09 +0200 | [diff] [blame] | 38 | {"/sys/kernel/tracing", "tracefs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV}, |
Lorenz Brun | 6ef7f9b | 2021-10-21 13:02:40 +0200 | [diff] [blame] | 39 | {"/sys/firmware/efi/efivars", "efivarfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV}, |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 40 | {"/proc", "proc", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV}, |
| 41 | {"/dev", "devtmpfs", unix.MS_NOEXEC | unix.MS_NOSUID}, |
| 42 | {"/dev/pts", "devpts", unix.MS_NOEXEC | unix.MS_NOSUID}, |
| 43 | } { |
| 44 | if err := os.MkdirAll(el.dir, 0755); err != nil { |
| 45 | return fmt.Errorf("could not make %s: %w", el.dir, err) |
| 46 | } |
| 47 | if err := unix.Mount(el.fs, el.dir, el.fs, el.flags, ""); err != nil { |
| 48 | return fmt.Errorf("could not mount %s on %s: %w", el.fs, el.dir, err) |
| 49 | } |
| 50 | } |
| 51 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 52 | // Mount all available CGroups for v1 (v2 uses a single unified hierarchy |
| 53 | // and is not supported by our runtimes yet) |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 54 | if err := unix.Mount("tmpfs", "/sys/fs/cgroup", "tmpfs", unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV, ""); err != nil { |
| 55 | panic(err) |
| 56 | } |
Lorenz Brun | 764a2de | 2021-11-22 16:26:36 +0100 | [diff] [blame] | 57 | cgroupsRaw, err := os.ReadFile("/proc/cgroups") |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 58 | if err != nil { |
| 59 | panic(err) |
| 60 | } |
| 61 | |
| 62 | cgroupLines := strings.Split(string(cgroupsRaw), "\n") |
| 63 | for _, cgroupLine := range cgroupLines { |
| 64 | if cgroupLine == "" || strings.HasPrefix(cgroupLine, "#") { |
| 65 | continue |
| 66 | } |
| 67 | cgroupParts := strings.Split(cgroupLine, "\t") |
| 68 | cgroupName := cgroupParts[0] |
| 69 | if err := os.Mkdir("/sys/fs/cgroup/"+cgroupName, 0755); err != nil { |
| 70 | panic(err) |
| 71 | } |
| 72 | if err := unix.Mount("cgroup", "/sys/fs/cgroup/"+cgroupName, "cgroup", unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV, cgroupName); err != nil { |
| 73 | panic(err) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Enable hierarchical memory accounting |
| 78 | useMemoryHierarchy, err := os.OpenFile("/sys/fs/cgroup/memory/memory.use_hierarchy", os.O_RDWR, 0) |
| 79 | if err != nil { |
| 80 | panic(err) |
| 81 | } |
| 82 | if _, err := useMemoryHierarchy.WriteString("1"); err != nil { |
| 83 | panic(err) |
| 84 | } |
| 85 | useMemoryHierarchy.Close() |
| 86 | return nil |
| 87 | } |