osbase/bringup: Add panichandler and pstore handling
This expands bringup to use cgroups,
adds our panichandler and
implements a redirect of stdout and stderr.
The last one for intercepting using it with the race detector or
third party code which uses said outputs.
By adding RunWith it is now possible to add metrics collection and
LogTree filtering.
Change-Id: I9795d7b1d7f66cc67003dbaee0aadf134ebdba65
Reviewed-on: https://review.monogon.dev/c/monogon/+/3705
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
Tested-by: Jenkins CI
Reviewed-by: Jan Schär <jan@monogon.tech>
diff --git a/osbase/bringup/pstore.go b/osbase/bringup/pstore.go
new file mode 100644
index 0000000..8aa41d9
--- /dev/null
+++ b/osbase/bringup/pstore.go
@@ -0,0 +1,43 @@
+package bringup
+
+import (
+ "context"
+
+ "source.monogon.dev/osbase/pstore"
+ "source.monogon.dev/osbase/supervisor"
+)
+
+// dumpAndCleanPstore dumps all files accumulated in the pstore into the log
+// and clears them from the pstore. This allows looking at these logs and also
+// keeps the pstore from overflowing the generally limited storage it has.
+func dumpAndCleanPstore(ctx context.Context) error {
+ logger := supervisor.Logger(ctx)
+ dumps, err := pstore.GetKmsgDumps()
+ if err != nil {
+ logger.Errorf("Failed to recover logs from pstore: %v", err)
+ return nil
+ }
+ for _, dump := range dumps {
+ logger.Errorf("Recovered log from %v at %v. Reconstructed log follows.", dump.Reason, dump.OccurredAt)
+ for _, line := range dump.Lines {
+ logger.Warning(line)
+ }
+ }
+ userspaceLines, err := pstore.GetPmsgDump()
+ if err != nil {
+ logger.Errorf("Failed to recover userspace logs from pstore: %v", err)
+ }
+ for _, line := range userspaceLines {
+ logger.Warning(line)
+ }
+ cleanErr := pstore.ClearAll()
+ if cleanErr != nil {
+ logger.Errorf("Failed to clear pstore: %v", err)
+ }
+ // Retrying this is extremely unlikely to result in any change and is most
+ // likely just going to generate large amounts of useless logs obscuring
+ // errors.
+ supervisor.Signal(ctx, supervisor.SignalHealthy)
+ supervisor.Signal(ctx, supervisor.SignalDone)
+ return nil
+}