c/agent: implement
Implement the currently-required agent functionality, i.e. running with
both autoconfigured as well as static network configuration, interacting
with the BMaaS API and installing Monogon OS.
The early-stage setup is similar to Monogon OS itself, but after setting
up the root supervisor this instead calls into the agent runnable which
then performs the rest of the work.
In the process I made both logtree as well as supervisor public as they
are very generic and I see no reason to keep them scoped so tightly.
Maybe we should move them to go/ at some point.
This currently calls into osimage without the optimization the
regular installer performs, this is intentional as I have code which
will replace osimage with a high-performance version, obviating the
need to manually make this fast here.
This also comes with an end-to-end test
which exercises the whole flow, installing TestOS and checking if it
launches.
Change-Id: Iab3f89598a30072ea565ec2db3b198c8df7999ef
Reviewed-on: https://review.monogon.dev/c/monogon/+/1405
Reviewed-by: Serge Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/cloud/agent/main.go b/cloud/agent/main.go
index 704b252..e859488 100644
--- a/cloud/agent/main.go
+++ b/cloud/agent/main.go
@@ -1,15 +1,76 @@
package main
import (
+ "context"
"fmt"
+ "io"
+ "os"
- "google.golang.org/protobuf/encoding/prototext"
+ "golang.org/x/sys/unix"
+
+ "source.monogon.dev/metropolis/pkg/logtree"
+ "source.monogon.dev/metropolis/pkg/supervisor"
)
func main() {
- fmt.Println("Monogon BMaaS Agent started")
- report, errs := gatherHWReport()
- // Just print the report for now
- fmt.Println(prototext.Format(report))
- fmt.Println("Encountered errors:", errs)
+ setupMounts()
+
+ // Set up logger for the Agent. Currently logs everything to /dev/tty0 and
+ // /dev/ttyS0.
+ consoles := []string{"/dev/tty0", "/dev/ttyS0"}
+ lt := logtree.New()
+ for _, p := range consoles {
+ f, err := os.OpenFile(p, os.O_WRONLY, 0)
+ if err != nil {
+ continue
+ }
+ reader, err := lt.Read("", logtree.WithChildren(), logtree.WithStream())
+ if err != nil {
+ panic(fmt.Errorf("could not set up root log reader: %v", err))
+ }
+ go func(path string, f io.Writer) {
+ for {
+ p := <-reader.Stream
+ fmt.Fprintf(f, "%s\n", p.String())
+ }
+ }(p, f)
+ }
+
+ sCtx := context.Background()
+ supervisor.New(sCtx, agentRunnable, supervisor.WithExistingLogtree(lt))
+ select {}
+}
+
+func mkdirAndMount(dir, fs string, flags uintptr) error {
+ if err := os.MkdirAll(dir, 0o755); err != nil {
+ return fmt.Errorf("could not make %s: %w", dir, err)
+ }
+ if err := unix.Mount(fs, dir, fs, flags, ""); err != nil {
+ return fmt.Errorf("could not mount %s on %s: %w", fs, dir, err)
+ }
+ return nil
+}
+
+// setupMounts sets up basic mounts like sysfs, procfs, devtmpfs and cgroups.
+// This should be called early during init as a lot of processes depend on this
+// being available.
+func setupMounts() error {
+ // Set up target filesystems.
+ for _, el := range []struct {
+ dir string
+ fs string
+ flags uintptr
+ }{
+ {"/sys", "sysfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
+ {"/sys/kernel/tracing", "tracefs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
+ {"/sys/fs/pstore", "pstore", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
+ {"/proc", "proc", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
+ {"/dev", "devtmpfs", unix.MS_NOEXEC | unix.MS_NOSUID},
+ {"/dev/pts", "devpts", unix.MS_NOEXEC | unix.MS_NOSUID},
+ } {
+ if err := mkdirAndMount(el.dir, el.fs, el.flags); err != nil {
+ return err
+ }
+ }
+ return nil
}