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" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "strings" |
| 24 | |
| 25 | "golang.org/x/sys/unix" |
| 26 | |
| 27 | "source.monogon.dev/metropolis/pkg/logtree" |
| 28 | ) |
| 29 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 30 | // setupMounts sets up basic mounts like sysfs, procfs, devtmpfs and cgroups. |
| 31 | // This should be called early during init as a lot of processes depend on this |
| 32 | // being available. |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 33 | func setupMounts(log logtree.LeveledLogger) error { |
| 34 | // Set up target filesystems. |
| 35 | for _, el := range []struct { |
| 36 | dir string |
| 37 | fs string |
| 38 | flags uintptr |
| 39 | }{ |
| 40 | {"/sys", "sysfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV}, |
Lorenz Brun | 09c275b | 2021-03-30 12:47:09 +0200 | [diff] [blame] | 41 | {"/sys/kernel/tracing", "tracefs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV}, |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 42 | {"/proc", "proc", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV}, |
| 43 | {"/dev", "devtmpfs", unix.MS_NOEXEC | unix.MS_NOSUID}, |
| 44 | {"/dev/pts", "devpts", unix.MS_NOEXEC | unix.MS_NOSUID}, |
| 45 | } { |
| 46 | if err := os.MkdirAll(el.dir, 0755); err != nil { |
| 47 | return fmt.Errorf("could not make %s: %w", el.dir, err) |
| 48 | } |
| 49 | if err := unix.Mount(el.fs, el.dir, el.fs, el.flags, ""); err != nil { |
| 50 | return fmt.Errorf("could not mount %s on %s: %w", el.fs, el.dir, err) |
| 51 | } |
| 52 | } |
| 53 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 54 | // Mount all available CGroups for v1 (v2 uses a single unified hierarchy |
| 55 | // and is not supported by our runtimes yet) |
Lorenz Brun | 3a99c59 | 2021-01-26 19:57:21 +0100 | [diff] [blame] | 56 | if err := unix.Mount("tmpfs", "/sys/fs/cgroup", "tmpfs", unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV, ""); err != nil { |
| 57 | panic(err) |
| 58 | } |
| 59 | cgroupsRaw, err := ioutil.ReadFile("/proc/cgroups") |
| 60 | if err != nil { |
| 61 | panic(err) |
| 62 | } |
| 63 | |
| 64 | cgroupLines := strings.Split(string(cgroupsRaw), "\n") |
| 65 | for _, cgroupLine := range cgroupLines { |
| 66 | if cgroupLine == "" || strings.HasPrefix(cgroupLine, "#") { |
| 67 | continue |
| 68 | } |
| 69 | cgroupParts := strings.Split(cgroupLine, "\t") |
| 70 | cgroupName := cgroupParts[0] |
| 71 | if err := os.Mkdir("/sys/fs/cgroup/"+cgroupName, 0755); err != nil { |
| 72 | panic(err) |
| 73 | } |
| 74 | if err := unix.Mount("cgroup", "/sys/fs/cgroup/"+cgroupName, "cgroup", unix.MS_NOEXEC|unix.MS_NOSUID|unix.MS_NODEV, cgroupName); err != nil { |
| 75 | panic(err) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Enable hierarchical memory accounting |
| 80 | useMemoryHierarchy, err := os.OpenFile("/sys/fs/cgroup/memory/memory.use_hierarchy", os.O_RDWR, 0) |
| 81 | if err != nil { |
| 82 | panic(err) |
| 83 | } |
| 84 | if _, err := useMemoryHierarchy.WriteString("1"); err != nil { |
| 85 | panic(err) |
| 86 | } |
| 87 | useMemoryHierarchy.Close() |
| 88 | return nil |
| 89 | } |