Give a logger to State and add debug logs

Signed-off-by: Itxaka <itxaka@spectrocloud.com>
This commit is contained in:
Itxaka
2023-02-04 10:33:58 +01:00
parent 8973979ceb
commit 93ceae89a2
2 changed files with 26 additions and 12 deletions

View File

@@ -23,10 +23,10 @@ Sends a generic event payload with the configuration found in the scanned direct
Aliases: []string{}, Aliases: []string{},
Flags: []cli.Flag{}, Flags: []cli.Flag{},
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
g := herd.DAG() g := herd.DAG()
s := &mount.State{Rootdir: "/"} s := &mount.State{Logger: log.Logger, Rootdir: "/"}
s.Register(g) s.Register(g)

View File

@@ -3,6 +3,7 @@ package mount
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/rs/zerolog"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -18,6 +19,7 @@ import (
) )
type State struct { type State struct {
Logger zerolog.Logger
Rootdir string // e.g. /sysroot inside initrd with pivot, / with nopivot Rootdir string // e.g. /sysroot inside initrd with pivot, / with nopivot
TargetImage string // e.g. /cOS/active.img TargetImage string // e.g. /cOS/active.img
TargetLabel string // e.g. COS_ACTIVE TargetLabel string // e.g. COS_ACTIVE
@@ -66,7 +68,9 @@ func (s *State) WriteFstab(fstabFile string) func(context.Context) error {
return err return err
} }
defer f.Close() defer f.Close()
if _, err := f.WriteString(fmt.Sprintf("%s\n", fst.String())); err != nil { toWrite := fmt.Sprintf("%s\n", fst.String())
s.Logger.Debug().Str("fstab", toWrite)
if _, err := f.WriteString(toWrite); err != nil {
return err return err
} }
} }
@@ -78,12 +82,15 @@ func (s *State) WriteFstab(fstabFile string) func(context.Context) error {
// ln -sf -t / /sysroot/system // ln -sf -t / /sysroot/system
func (s *State) RunStageOp(stage string) func(context.Context) error { func (s *State) RunStageOp(stage string) func(context.Context) error {
return func(ctx context.Context) error { return func(ctx context.Context) error {
_, err := utils.SH(fmt.Sprintf("elemental run-stage %s", stage)) cmd := fmt.Sprintf("elemental run-stage %s", stage)
s.Logger.Debug().Str("cmd", cmd)
_, err := utils.SH(cmd)
return err return err
} }
} }
func (s *State) MountOP(what, where, t string, options []string, timeout time.Duration) func(context.Context) error { func (s *State) MountOP(what, where, t string, options []string, timeout time.Duration) func(context.Context) error {
s.Logger.Debug().Str("what", what).Str("where", where).Str("type", t)
return func(c context.Context) error { return func(c context.Context) error {
for { for {
select { select {
@@ -94,11 +101,11 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du
Source: what, Source: what,
Options: options, Options: options,
} }
fstab := mountToStab(mountPoint) tmpFstab := mountToStab(mountPoint)
fstab.File = where tmpFstab.File = where
op := mountOperation{ op := mountOperation{
MountOption: mountPoint, MountOption: mountPoint,
FstabEntry: *fstab, FstabEntry: *tmpFstab,
Target: where, Target: where,
} }
@@ -107,7 +114,7 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du
continue continue
} }
s.fstabs = append(s.fstabs, fstab) s.fstabs = append(s.fstabs, tmpFstab)
return nil return nil
case <-c.Done(): case <-c.Done():
@@ -163,6 +170,7 @@ func (s *State) Register(g *herd.Graph) error {
// This is legacy - in UKI we don't need to found the img, this needs to run in a conditional // This is legacy - in UKI we don't need to found the img, this needs to run in a conditional
if s.MountRoot { if s.MountRoot {
// setup loopback mount for the image target for booting // setup loopback mount for the image target for booting
s.Logger.Debug().Str("what", opDiscoverState).Msg("Add operation")
g.Add(opDiscoverState, g.Add(opDiscoverState,
herd.WithDeps(opMountState), herd.WithDeps(opMountState),
herd.WithCallback( herd.WithCallback(
@@ -173,6 +181,7 @@ func (s *State) Register(g *herd.Graph) error {
)) ))
// mount the state partition so to find the loopback device // mount the state partition so to find the loopback device
s.Logger.Debug().Str("what", opMountState).Msg("Add operation")
g.Add(opMountState, g.Add(opMountState,
herd.WithCallback( herd.WithCallback(
s.MountOP( s.MountOP(
@@ -186,6 +195,7 @@ func (s *State) Register(g *herd.Graph) error {
) )
// mount the loopback device as root of the fs // mount the loopback device as root of the fs
s.Logger.Debug().Str("what", opMountRoot).Msg("Add operation")
g.Add(opMountRoot, g.Add(opMountRoot,
herd.WithDeps(opDiscoverState), herd.WithDeps(opDiscoverState),
herd.WithCallback( herd.WithCallback(
@@ -214,10 +224,12 @@ func (s *State) Register(g *herd.Graph) error {
// TODO: this needs to be run after state is discovered // TODO: this needs to be run after state is discovered
// TODO: add symlink if Rootdir != "" // TODO: add symlink if Rootdir != ""
// TODO: chroot? // TODO: chroot?
s.Logger.Debug().Str("what", opRootfsHook).Msg("Add operation")
g.Add(opRootfsHook, mountRootCondition, herd.WithDeps(opMountOEM), herd.WithCallback(s.RunStageOp("rootfs"))) g.Add(opRootfsHook, mountRootCondition, herd.WithDeps(opMountOEM), herd.WithCallback(s.RunStageOp("rootfs")))
// /run/cos-layout.env // /run/cos-layout.env
// populate state bindmounts, overlaymounts, custommounts // populate state bindmounts, overlaymounts, custommounts
s.Logger.Debug().Str("what", opLoadConfig).Msg("Add operation")
g.Add(opLoadConfig, g.Add(opLoadConfig,
herd.WithDeps(opRootfsHook), herd.WithDeps(opRootfsHook),
herd.WithCallback(func(ctx context.Context) error { herd.WithCallback(func(ctx context.Context) error {
@@ -242,6 +254,7 @@ func (s *State) Register(g *herd.Graph) error {
// overlay mount start // overlay mount start
if rootFSType(s.Rootdir) != "overlay" { if rootFSType(s.Rootdir) != "overlay" {
s.Logger.Debug().Str("what", opMountBaseOverlay).Msg("Add operation")
g.Add(opMountBaseOverlay, g.Add(opMountBaseOverlay,
herd.WithCallback( herd.WithCallback(
func(ctx context.Context) error { func(ctx context.Context) error {
@@ -262,7 +275,7 @@ func (s *State) Register(g *herd.Graph) error {
overlayCondition := herd.ConditionalOption(func() bool { return rootFSType(s.Rootdir) != "overlay" }, herd.WithDeps(opMountBaseOverlay)) overlayCondition := herd.ConditionalOption(func() bool { return rootFSType(s.Rootdir) != "overlay" }, herd.WithDeps(opMountBaseOverlay))
// TODO: Add fsck // TODO: Add fsck
// mount overlay // mount overlay
s.Logger.Debug().Str("what", opOverlayMount).Msg("Add operation")
g.Add( g.Add(
opOverlayMount, opOverlayMount,
overlayCondition, overlayCondition,
@@ -284,7 +297,7 @@ func (s *State) Register(g *herd.Graph) error {
}, },
), ),
) )
s.Logger.Debug().Str("what", opCustomMounts).Msg("Add operation")
g.Add( g.Add(
opCustomMounts, opCustomMounts,
mountRootCondition, mountRootCondition,
@@ -310,8 +323,8 @@ func (s *State) Register(g *herd.Graph) error {
}), }),
) )
// mount state
// mount state is defined over a custom mount (/usr/local/.state for instance, needs to be mounted over a device) // mount state is defined over a custom mount (/usr/local/.state for instance, needs to be mounted over a device)
s.Logger.Debug().Str("what", opMountBind).Msg("Add operation")
g.Add( g.Add(
opMountBind, opMountBind,
overlayCondition, overlayCondition,
@@ -336,6 +349,7 @@ func (s *State) Register(g *herd.Graph) error {
) )
// overlay mount end // overlay mount end
s.Logger.Debug().Str("what", opMountOEM).Msg("Add operation")
g.Add(opMountOEM, g.Add(opMountOEM,
overlayCondition, overlayCondition,
mountRootCondition, mountRootCondition,
@@ -355,7 +369,7 @@ func (s *State) Register(g *herd.Graph) error {
}, 60*time.Second), }, 60*time.Second),
), ),
) )
s.Logger.Debug().Str("what", opWriteFstab).Msg("Add operation")
g.Add(opWriteFstab, g.Add(opWriteFstab,
overlayCondition, overlayCondition,
mountRootCondition, mountRootCondition,