blob: e8594887d0a0d5f8e4acaf978152923864aaa515 [file] [log] [blame]
Lorenz Brun62948542023-01-10 13:28:44 +00001package main
2
3import (
Lorenz Brunaadeb792023-03-27 15:53:56 +02004 "context"
Lorenz Brun62948542023-01-10 13:28:44 +00005 "fmt"
Lorenz Brunaadeb792023-03-27 15:53:56 +02006 "io"
7 "os"
Lorenz Brun62948542023-01-10 13:28:44 +00008
Lorenz Brunaadeb792023-03-27 15:53:56 +02009 "golang.org/x/sys/unix"
10
11 "source.monogon.dev/metropolis/pkg/logtree"
12 "source.monogon.dev/metropolis/pkg/supervisor"
Lorenz Brun62948542023-01-10 13:28:44 +000013)
14
15func main() {
Lorenz Brunaadeb792023-03-27 15:53:56 +020016 setupMounts()
17
18 // Set up logger for the Agent. Currently logs everything to /dev/tty0 and
19 // /dev/ttyS0.
20 consoles := []string{"/dev/tty0", "/dev/ttyS0"}
21 lt := logtree.New()
22 for _, p := range consoles {
23 f, err := os.OpenFile(p, os.O_WRONLY, 0)
24 if err != nil {
25 continue
26 }
27 reader, err := lt.Read("", logtree.WithChildren(), logtree.WithStream())
28 if err != nil {
29 panic(fmt.Errorf("could not set up root log reader: %v", err))
30 }
31 go func(path string, f io.Writer) {
32 for {
33 p := <-reader.Stream
34 fmt.Fprintf(f, "%s\n", p.String())
35 }
36 }(p, f)
37 }
38
39 sCtx := context.Background()
40 supervisor.New(sCtx, agentRunnable, supervisor.WithExistingLogtree(lt))
41 select {}
42}
43
44func mkdirAndMount(dir, fs string, flags uintptr) error {
45 if err := os.MkdirAll(dir, 0o755); err != nil {
46 return fmt.Errorf("could not make %s: %w", dir, err)
47 }
48 if err := unix.Mount(fs, dir, fs, flags, ""); err != nil {
49 return fmt.Errorf("could not mount %s on %s: %w", fs, dir, err)
50 }
51 return nil
52}
53
54// setupMounts sets up basic mounts like sysfs, procfs, devtmpfs and cgroups.
55// This should be called early during init as a lot of processes depend on this
56// being available.
57func setupMounts() error {
58 // Set up target filesystems.
59 for _, el := range []struct {
60 dir string
61 fs string
62 flags uintptr
63 }{
64 {"/sys", "sysfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
65 {"/sys/kernel/tracing", "tracefs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
66 {"/sys/fs/pstore", "pstore", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
67 {"/proc", "proc", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
68 {"/dev", "devtmpfs", unix.MS_NOEXEC | unix.MS_NOSUID},
69 {"/dev/pts", "devpts", unix.MS_NOEXEC | unix.MS_NOSUID},
70 } {
71 if err := mkdirAndMount(el.dir, el.fs, el.flags); err != nil {
72 return err
73 }
74 }
75 return nil
Lorenz Brun62948542023-01-10 13:28:44 +000076}