From 836604503651fa38b9efbbb00ca5640745e0c5ef Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 08:45:52 +0100 Subject: [PATCH 01/14] drop defaults Signed-off-by: Itxaka --- pkg/mount/fs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/mount/fs.go b/pkg/mount/fs.go index 1e5325e..d2236be 100644 --- a/pkg/mount/fs.go +++ b/pkg/mount/fs.go @@ -160,7 +160,7 @@ func mountWithBaseOverlay(mountpoint, root, base string) (mountOperation, error) Type: "overlay", Source: "overlay", Options: []string{ - "defaults", + //"defaults", fmt.Sprintf("lowerdir=%s", rootMount), fmt.Sprintf("upperdir=%s", upperdir), fmt.Sprintf("workdir=%s", workdir), From 9aee940e6443170e4048b9c88e6de8c33b0b281f Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 10:09:21 +0100 Subject: [PATCH 02/14] more debug Signed-off-by: Itxaka --- pkg/mount/mount.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index ae7ed4f..6244644 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -115,7 +115,7 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Msg("Mount point does not exist, creating") err = os.MkdirAll(where, os.ModeDir|os.ModePerm) if err != nil { - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(err).Msg("") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(err).Msg("Creating dir") continue } } @@ -135,7 +135,7 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du err := op.run() if err != nil { - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(err).Msg("") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(err).Msg("mounting") continue } @@ -144,11 +144,11 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du return nil case <-c.Done(): e := fmt.Errorf("context canceled") - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(e).Msg("") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(e).Msg("mount canceled") return e case <-cc: e := fmt.Errorf("timeout exhausted") - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(e).Msg("") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(e).Msg("Mount timeout") return e } } @@ -394,6 +394,7 @@ func (s *State) Register(g *herd.Graph) error { var err error for id, mountpoint := range s.CustomMounts { + s.Logger.Debug().Str("what", id).Str("where", s.path(mountpoint)).Msg("mounting custom mounts") err = multierror.Append(err, s.MountOP( id, From c2deb7619263f822dfee76b2bd9ac444ad2d2a90 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 10:32:11 +0100 Subject: [PATCH 03/14] Check symlink before creating it Signed-off-by: Itxaka --- pkg/mount/mount.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index 6244644..ee44c58 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -88,10 +88,13 @@ func (s *State) RunStageOp(stage string) func(context.Context) error { return func(ctx context.Context) error { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() if stage == "rootfs" { - err := os.Symlink("/sysroot/system", "/system") - if err != nil { - s.Logger.Err(err).Msg("creating symlink") - return err + if _, err := os.Stat("/system"); os.IsNotExist(err) { + s.Logger.Debug().Str("from", "/sysroot/system").Str("to", "/system").Msg("Creating symlink") + err = os.Symlink("/sysroot/system", "/system") + if err != nil { + s.Logger.Err(err).Msg("creating symlink") + return err + } } } From b628f959d36b0cf3496b319fbd0708f33d95ecb0 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 11:01:21 +0100 Subject: [PATCH 04/14] Translate persistent to runtime device Signed-off-by: Itxaka --- pkg/mount/mount.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index ee44c58..ba7f861 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -203,9 +203,6 @@ func (s *State) Register(g *herd.Graph) error { // TODO: add hooks, fstab (might have missed some), systemd compat // TODO: We should also set tmpfs here (not -related) - // symlink - // execute the rootfs hook - // All of this below need to run after rootfs stage runs (so the layout file is created) // This is legacy - in UKI we don't need to found the img, this needs to run in a conditional if s.MountRoot { @@ -396,12 +393,16 @@ func (s *State) Register(g *herd.Graph) error { herd.WithCallback(func(ctx context.Context) error { var err error - for id, mountpoint := range s.CustomMounts { - s.Logger.Debug().Str("what", id).Str("where", s.path(mountpoint)).Msg("mounting custom mounts") + for what, where := range s.CustomMounts { + // Translate label to disk for COS_PERSISTENT + if what == "/dev/disk/by-label/COS_PERSISTENT" { + what = runtime.Persistent.Name + } + s.Logger.Debug().Str("what", what).Str("where", s.path(where)).Msg("mounting custom mounts") err = multierror.Append(err, s.MountOP( - id, - s.path(mountpoint), + what, + s.path(where), "auto", []string{ "ro", // or rw From b67c89c5930753d8651368f7652d4eba7d28185a Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 11:33:11 +0100 Subject: [PATCH 05/14] Translate auto fstype to actual type Signed-off-by: Itxaka --- pkg/mount/mount.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index ba7f861..5c0015a 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -394,16 +394,18 @@ func (s *State) Register(g *herd.Graph) error { var err error for what, where := range s.CustomMounts { + fstype := "auto" // Translate label to disk for COS_PERSISTENT if what == "/dev/disk/by-label/COS_PERSISTENT" { what = runtime.Persistent.Name + fstype = runtime.Persistent.Type } - s.Logger.Debug().Str("what", what).Str("where", s.path(where)).Msg("mounting custom mounts") + s.Logger.Debug().Str("what", what).Str("where", s.path(where)).Str("type", fstype).Msg("mounting custom mounts") err = multierror.Append(err, s.MountOP( what, s.path(where), - "auto", + fstype, []string{ "ro", // or rw }, From 6fedeef4eaa66feeca9c3c1f0cf7f8f40477570f Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 11:49:03 +0100 Subject: [PATCH 06/14] Replace /sysroot on fstab Signed-off-by: Itxaka --- pkg/mount/mount.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index 5c0015a..3bd12f4 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -72,12 +72,14 @@ func (s *State) WriteFstab(fstabFile string) func(context.Context) error { return err } defer f.Close() - toWrite := fmt.Sprintf("%s\n", fst.String()) - s.Logger.Debug().Str("fstab", toWrite) + // As we mount on /sysroot during initramfs but the fstab file is for the real init process, we need to remove + // Any mentions to /sysroot from the fstab lines, otherwise they wont work + fstCleaned := strings.ReplaceAll(fst.String(), "/sysroot", "") + toWrite := fmt.Sprintf("%s\n", fstCleaned) if _, err := f.WriteString(toWrite); err != nil { return err } - log.Logger.Debug().Str("fstabline", litter.Sdump(fst)).Str("fstabfile", fstabFile).Msg("Done fstab line") + log.Logger.Debug().Str("fstabline", litter.Sdump(fstCleaned)).Str("fstabfile", fstabFile).Msg("Done fstab line") } } return nil @@ -394,10 +396,10 @@ func (s *State) Register(g *herd.Graph) error { var err error for what, where := range s.CustomMounts { + // TODO: scan for the custom mount disk to know the underlying fs and set it proper fstype := "auto" // Translate label to disk for COS_PERSISTENT - if what == "/dev/disk/by-label/COS_PERSISTENT" { - what = runtime.Persistent.Name + if strings.Contains(what, "COS_PERSISTENT") { fstype = runtime.Persistent.Type } s.Logger.Debug().Str("what", what).Str("where", s.path(where)).Str("type", fstype).Msg("mounting custom mounts") From 45c69d24fa869c0d93e7fdcbc1304b7e2c272e55 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 12:21:05 +0100 Subject: [PATCH 07/14] Remove defaults from bind and extra logging Signed-off-by: Itxaka --- pkg/mount/fs.go | 2 +- pkg/mount/mount.go | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pkg/mount/fs.go b/pkg/mount/fs.go index d2236be..3c81b0e 100644 --- a/pkg/mount/fs.go +++ b/pkg/mount/fs.go @@ -111,7 +111,7 @@ func mountBind(mountpoint, root, stateTarget string) (mountOperation, error) { Type: "overlay", Source: stateDir, Options: []string{ - "defaults", + //"defaults", "bind", }, } diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index 3bd12f4..e4b8e68 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -79,7 +79,7 @@ func (s *State) WriteFstab(fstabFile string) func(context.Context) error { if _, err := f.WriteString(toWrite); err != nil { return err } - log.Logger.Debug().Str("fstabline", litter.Sdump(fstCleaned)).Str("fstabfile", fstabFile).Msg("Done fstab line") + log.Logger.Debug().Str("fstabline", fstCleaned).Str("fstabfile", fstabFile).Msg("Done fstab line") } } return nil @@ -117,10 +117,10 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du select { default: if _, err := os.Stat(where); os.IsNotExist(err) { - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Msg("Mount point does not exist, creating") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Strs("options", options).Msg("Mount point does not exist, creating") err = os.MkdirAll(where, os.ModeDir|os.ModePerm) if err != nil { - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(err).Msg("Creating dir") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Strs("options", options).Err(err).Msg("Creating dir") continue } } @@ -140,20 +140,20 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du err := op.run() if err != nil { - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(err).Msg("mounting") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Strs("options", options).Err(err).Msg("mounting") continue } s.fstabs = append(s.fstabs, tmpFstab) - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Msg("Mounted") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Strs("options", options).Msg("Mounted") return nil case <-c.Done(): e := fmt.Errorf("context canceled") - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(e).Msg("mount canceled") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Strs("options", options).Err(e).Msg("mount canceled") return e case <-cc: e := fmt.Errorf("timeout exhausted") - log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(e).Msg("Mount timeout") + log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Strs("options", options).Err(e).Msg("Mount timeout") return e } } @@ -275,6 +275,7 @@ func (s *State) Register(g *herd.Graph) error { // depending on /run/cos-layout.env // This is building the mountRoot dependendency if it was enabled mountRootCondition := herd.ConditionalOption(func() bool { return s.MountRoot }, herd.WithDeps(opMountRoot)) + s.Logger.Debug().Bool("mountRootCondition", s.MountRoot).Msg("condition") // TODO: this needs to be run after sysroot so we can link to /sysroot/system/oem and after /oem mounted s.Logger.Debug().Str("what", opRootfsHook).Msg("Add operation") @@ -304,6 +305,8 @@ func (s *State) Register(g *herd.Graph) error { // TODO: PERSISTENT_STATE_TARGET /usr/local/.state s.BindMounts = strings.Split(env["PERSISTENT_STATE_PATHS"], " ") + log.Logger.Debug().Strs("paths", s.BindMounts).Msg("persistent paths") + log.Logger.Debug().Str("pathsraw", env["PERSISTENT_STATE_PATHS"]).Msg("persistent paths") s.StateDir = env["PERSISTENT_STATE_TARGET"] if s.StateDir == "" { @@ -359,6 +362,7 @@ func (s *State) Register(g *herd.Graph) error { } overlayCondition := herd.ConditionalOption(func() bool { return rootFSType(s.Rootdir) != "overlay" }, herd.WithDeps(opMountBaseOverlay)) + s.Logger.Debug().Bool("overlaycondition", rootFSType(s.Rootdir) != "overlay").Msg("condition") // TODO: Add fsck // mount overlay s.Logger.Debug().Str("what", opOverlayMount).Msg("Add operation") @@ -432,11 +436,14 @@ func (s *State) Register(g *herd.Graph) error { herd.WithCallback( func(ctx context.Context) error { var err error + s.Logger.Debug().Msg("Mounting bind") + s.Logger.Debug().Strs("binds", s.BindMounts).Msg("Mounting bind") for _, p := range s.BindMounts { - + s.Logger.Debug().Str("what", p).Str("where", s.StateDir).Msg("Mounting bind") op, err := mountBind(p, s.Rootdir, s.StateDir) if err != nil { + s.Logger.Err(err).Msg("Mounting bind") return err } s.fstabs = append(s.fstabs, &op.FstabEntry) From bfc337b499d8aff8f06a8c871391ee723c7393df Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 14:14:37 +0100 Subject: [PATCH 08/14] sdfasd Signed-off-by: Itxaka --- pkg/mount/mount.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index e4b8e68..99e7f91 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -59,7 +59,7 @@ func (s *State) path(p ...string) string { } func (s *State) WriteFstab(fstabFile string) func(context.Context) error { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() return func(ctx context.Context) error { for _, fst := range s.fstabs { select { @@ -88,7 +88,7 @@ func (s *State) WriteFstab(fstabFile string) func(context.Context) error { func (s *State) RunStageOp(stage string) func(context.Context) error { return func(ctx context.Context) error { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() if stage == "rootfs" { if _, err := os.Stat("/system"); os.IsNotExist(err) { s.Logger.Debug().Str("from", "/sysroot/system").Str("to", "/system").Msg("Creating symlink") @@ -109,7 +109,7 @@ func (s *State) RunStageOp(stage string) func(context.Context) error { } func (s *State) MountOP(what, where, t string, options []string, timeout time.Duration) func(context.Context) error { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() return func(c context.Context) error { cc := time.After(timeout) @@ -214,7 +214,7 @@ func (s *State) Register(g *herd.Graph) error { herd.WithDeps(opMountState), herd.WithCallback( func(ctx context.Context) error { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() cmd := fmt.Sprintf("losetup --show -f %s", s.path("/run/initramfs/cos-state", s.TargetImage)) log.Logger.Debug().Str("targetImage", s.TargetImage).Str("path", s.Rootdir).Str("fullcmd", cmd).Msg("Mounting image") _, err := utils.SH(cmd) @@ -290,7 +290,7 @@ func (s *State) Register(g *herd.Graph) error { err = g.Add(opLoadConfig, herd.WithDeps(opRootfsHook), herd.WithCallback(func(ctx context.Context) error { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() if s.CustomMounts == nil { s.CustomMounts = map[string]string{} } @@ -397,6 +397,7 @@ func (s *State) Register(g *herd.Graph) error { overlayCondition, herd.WithDeps(opLoadConfig), herd.WithCallback(func(ctx context.Context) error { + s.Logger.Debug().Msg("Start" + opCustomMounts) var err error for what, where := range s.CustomMounts { @@ -419,6 +420,7 @@ func (s *State) Register(g *herd.Graph) error { )(ctx)) } + s.Logger.Debug().Msg("End" + opCustomMounts) return err }), ) @@ -436,6 +438,7 @@ func (s *State) Register(g *herd.Graph) error { herd.WithCallback( func(ctx context.Context) error { var err error + s.Logger.Debug().Msg("Start" + opMountBind) s.Logger.Debug().Msg("Mounting bind") s.Logger.Debug().Strs("binds", s.BindMounts).Msg("Mounting bind") @@ -449,6 +452,8 @@ func (s *State) Register(g *herd.Graph) error { s.fstabs = append(s.fstabs, &op.FstabEntry) err = multierror.Append(err, op.run()) } + s.Logger.Debug().Msg("End" + opMountBind) + s.Logger.Err(err).Send() return err }, ), From 98a2506859fb7c2d2b66f6036bd6960bc647d6de Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 14:43:58 +0100 Subject: [PATCH 09/14] check mount status in mountoperation.run not on bind mountpoint preparation Signed-off-by: Itxaka --- internal/cmd/commands.go | 2 +- pkg/mount/fs.go | 61 +++++++++++++++++++--------------------- pkg/mount/mount.go | 19 ++++++------- 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/internal/cmd/commands.go b/internal/cmd/commands.go index 16fa0a7..03872cf 100644 --- a/internal/cmd/commands.go +++ b/internal/cmd/commands.go @@ -30,7 +30,7 @@ Sends a generic event payload with the configuration found in the scanned direct }, }, Action: func(c *cli.Context) (err error) { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() g := herd.DAG() s := &mount.State{ Logger: log.Logger, diff --git a/pkg/mount/fs.go b/pkg/mount/fs.go index 3c81b0e..f21c9fa 100644 --- a/pkg/mount/fs.go +++ b/pkg/mount/fs.go @@ -99,44 +99,41 @@ func mountToStab(m mount.Mount) *fstab.Mount { } // https://github.com/kairos-io/packages/blob/94aa3bef3d1330cb6c6905ae164f5004b6a58b8c/packages/system/dracut/immutable-rootfs/30cos-immutable-rootfs/cos-mount-layout.sh#L183 -func mountBind(mountpoint, root, stateTarget string) (mountOperation, error) { +func mountBind(mountpoint, root, stateTarget string) mountOperation { mountpoint = strings.TrimLeft(mountpoint, "/") // normalize, remove / upfront as we are going to re-use it in subdirs rootMount := filepath.Join(root, mountpoint) bindMountPath := strings.ReplaceAll(mountpoint, "/", "-") stateDir := filepath.Join(root, stateTarget, fmt.Sprintf("%s.bind", bindMountPath)) - if mounted, _ := mountinfo.Mounted(rootMount); !mounted { - tmpMount := mount.Mount{ - Type: "overlay", - Source: stateDir, - Options: []string{ - //"defaults", - "bind", - }, - } - - tmpFstab := mountToStab(tmpMount) - tmpFstab.File = fmt.Sprintf("/%s", mountpoint) - tmpFstab.Spec = strings.ReplaceAll(tmpFstab.Spec, root, "") - return mountOperation{ - MountOption: tmpMount, - FstabEntry: *tmpFstab, - Target: rootMount, - PrepareCallback: func() error { - if err := createIfNotExists(rootMount); err != nil { - return err - } - - if err := createIfNotExists(stateDir); err != nil { - return err - } - - return syncState(appendSlash(rootMount), appendSlash(stateDir)) - }, - }, nil + tmpMount := mount.Mount{ + Type: "overlay", + Source: stateDir, + Options: []string{ + //"defaults", + "bind", + }, + } + + tmpFstab := mountToStab(tmpMount) + tmpFstab.File = fmt.Sprintf("/%s", mountpoint) + tmpFstab.Spec = strings.ReplaceAll(tmpFstab.Spec, root, "") + return mountOperation{ + MountOption: tmpMount, + FstabEntry: *tmpFstab, + Target: rootMount, + PrepareCallback: func() error { + if err := createIfNotExists(rootMount); err != nil { + return err + } + + if err := createIfNotExists(stateDir); err != nil { + return err + } + + return syncState(appendSlash(rootMount), appendSlash(stateDir)) + }, } - return mountOperation{}, fmt.Errorf("already mounted") } func syncState(src, dst string) error { @@ -145,7 +142,7 @@ func syncState(src, dst string) error { // https://github.com/kairos-io/packages/blob/94aa3bef3d1330cb6c6905ae164f5004b6a58b8c/packages/system/dracut/immutable-rootfs/30cos-immutable-rootfs/cos-mount-layout.sh#L145 func mountWithBaseOverlay(mountpoint, root, base string) (mountOperation, error) { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() log.Debug().Str("mountpoint", mountpoint).Str("root", root).Str("base", base).Msg("mount with base overlay") mountpoint = strings.TrimLeft(mountpoint, "/") // normalize, remove / upfront as we are going to re-use it in subdirs rootMount := filepath.Join(root, mountpoint) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index 99e7f91..290b747 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -438,22 +438,19 @@ func (s *State) Register(g *herd.Graph) error { herd.WithCallback( func(ctx context.Context) error { var err error - s.Logger.Debug().Msg("Start" + opMountBind) - s.Logger.Debug().Msg("Mounting bind") - s.Logger.Debug().Strs("binds", s.BindMounts).Msg("Mounting bind") + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() + log.Logger.Debug().Msg("Start" + opMountBind) + log.Logger.Debug().Msg("Mounting bind") + log.Logger.Debug().Strs("binds", s.BindMounts).Msg("Mounting bind") for _, p := range s.BindMounts { - s.Logger.Debug().Str("what", p).Str("where", s.StateDir).Msg("Mounting bind") - op, err := mountBind(p, s.Rootdir, s.StateDir) - if err != nil { - s.Logger.Err(err).Msg("Mounting bind") - return err - } + log.Logger.Debug().Str("what", p).Str("where", s.StateDir).Msg("Mounting bind") + op := mountBind(p, s.Rootdir, s.StateDir) s.fstabs = append(s.fstabs, &op.FstabEntry) err = multierror.Append(err, op.run()) } - s.Logger.Debug().Msg("End" + opMountBind) - s.Logger.Err(err).Send() + log.Logger.Debug().Msg("End" + opMountBind) + log.Logger.Err(err).Send() return err }, ), From 4407bef252796b3e05626db8bf85b4cad0160a85 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 15:40:18 +0100 Subject: [PATCH 10/14] return proper multierror or nil, print dag after execution Signed-off-by: Itxaka --- internal/cmd/commands.go | 4 +++- pkg/mount/mount.go | 7 ++++--- pkg/mount/operation.go | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/internal/cmd/commands.go b/internal/cmd/commands.go index 03872cf..d8acec8 100644 --- a/internal/cmd/commands.go +++ b/internal/cmd/commands.go @@ -64,7 +64,9 @@ Sends a generic event payload with the configuration found in the scanned direct } log.Print("Calling dag") - return g.Run(context.Background()) + err = g.Run(context.Background()) + log.Print(s.WriteDAG(g)) + return err }, }, } diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index 290b747..3ea8b44 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -398,7 +398,7 @@ func (s *State) Register(g *herd.Graph) error { herd.WithDeps(opLoadConfig), herd.WithCallback(func(ctx context.Context) error { s.Logger.Debug().Msg("Start" + opCustomMounts) - var err error + var err *multierror.Error for what, where := range s.CustomMounts { // TODO: scan for the custom mount disk to know the underlying fs and set it proper @@ -408,7 +408,6 @@ func (s *State) Register(g *herd.Graph) error { fstype = runtime.Persistent.Type } s.Logger.Debug().Str("what", what).Str("where", s.path(where)).Str("type", fstype).Msg("mounting custom mounts") - err = multierror.Append(err, s.MountOP( what, s.path(where), @@ -421,7 +420,9 @@ func (s *State) Register(g *herd.Graph) error { } s.Logger.Debug().Msg("End" + opCustomMounts) - return err + s.Logger.Err(err).Str("error", err.Error()).Send() + + return err.ErrorOrNil() }), ) if err != nil { diff --git a/pkg/mount/operation.go b/pkg/mount/operation.go index 176b2f8..4e7ee5b 100644 --- a/pkg/mount/operation.go +++ b/pkg/mount/operation.go @@ -17,7 +17,7 @@ type mountOperation struct { } func (m mountOperation) run() error { - log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger() + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() if m.PrepareCallback != nil { if err := m.PrepareCallback(); err != nil { log.Logger.Err(err).Str("what", m.MountOption.Source).Str("where", m.Target).Str("type", m.MountOption.Type).Msg("executing mount callback") From 2a62ff5dac2da60c218ea7a0281f20adab543196 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 15:51:21 +0100 Subject: [PATCH 11/14] Persistent should be RW Signed-off-by: Itxaka --- pkg/mount/mount.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index 3ea8b44..fa507d8 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -403,18 +403,19 @@ func (s *State) Register(g *herd.Graph) error { for what, where := range s.CustomMounts { // TODO: scan for the custom mount disk to know the underlying fs and set it proper fstype := "auto" + mountOptions := []string{"ro"} // Translate label to disk for COS_PERSISTENT + // Persistent needs to be RW if strings.Contains(what, "COS_PERSISTENT") { fstype = runtime.Persistent.Type + mountOptions = []string{"rw"} } s.Logger.Debug().Str("what", what).Str("where", s.path(where)).Str("type", fstype).Msg("mounting custom mounts") err = multierror.Append(err, s.MountOP( what, s.path(where), fstype, - []string{ - "ro", // or rw - }, + mountOptions, 10*time.Second, )(ctx)) From 3f4b782fe933f1a6ec183e1875058f0f7acc61d3 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 15:59:04 +0100 Subject: [PATCH 12/14] Add missing module rsync to initramfs Signed-off-by: Itxaka --- dracut/28immucore/module-setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dracut/28immucore/module-setup.sh b/dracut/28immucore/module-setup.sh index 0fd3d73..6d6fc9d 100755 --- a/dracut/28immucore/module-setup.sh +++ b/dracut/28immucore/module-setup.sh @@ -28,7 +28,7 @@ install() { # Add missing elemental binary, drop once we get yip lib inside immucore as its only needed to run the stages inst_multiple immucore elemental # add utils used by elemental or stages - inst_multiple partprobe sync udevadm parted mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.vfat mkfs.fat blkid e2fsck resize2fs mount umount sgdisk + inst_multiple partprobe sync udevadm parted mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.vfat mkfs.fat blkid e2fsck resize2fs mount umount sgdisk rsync # missing mkfs.xfs xfs_growfs in image? inst_script "${moddir}/generator.sh" "${systemdutildir}/system-generators/immucore-generator" inst_simple "${moddir}/immucore.service" "${systemdsystemunitdir}/immucore.service" From 0939929821c2a714f8b63522f14bdc99cc101920 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 16:23:13 +0100 Subject: [PATCH 13/14] drop empty binds, more logging Signed-off-by: Itxaka --- pkg/mount/fs.go | 5 ++++- pkg/mount/mount.go | 21 +++++++++++++++++++++ pkg/mount/operation.go | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/mount/fs.go b/pkg/mount/fs.go index f21c9fa..e0da7a3 100644 --- a/pkg/mount/fs.go +++ b/pkg/mount/fs.go @@ -123,14 +123,17 @@ func mountBind(mountpoint, root, stateTarget string) mountOperation { FstabEntry: *tmpFstab, Target: rootMount, PrepareCallback: func() error { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() if err := createIfNotExists(rootMount); err != nil { + log.Logger.Err(err).Str("what", rootMount).Msg("create if not exists") return err } if err := createIfNotExists(stateDir); err != nil { + log.Logger.Err(err).Str("what", stateDir).Msg("create if not exists") return err } - + log.Logger.Debug().Str("what", tmpMount.Source).Str("where", rootMount).Str("type", tmpMount.Type).Strs("options", tmpMount.Options).Msg("create if not exists") return syncState(appendSlash(rootMount), appendSlash(stateDir)) }, } diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index fa507d8..481a929 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -446,6 +446,27 @@ func (s *State) Register(g *herd.Graph) error { log.Logger.Debug().Strs("binds", s.BindMounts).Msg("Mounting bind") for _, p := range s.BindMounts { + // TODO: Check why p can be empty, Example: + /* + 3:12PM DBG Mounting bind binds=[ + "/etc/systemd","/etc/modprobe.d", + "/etc/rancher","/etc/sysconfig", + "/etc/runlevels","/etc/ssh", + "/etc/ssl/certs","/etc/iscsi", + "", <----- HERE + "/etc/cni","/etc/kubernetes", + "/home","/opt","/root","/snap", + "/var/snap","/usr/libexec", + "/var/log","/var/lib/rancher", + "/var/lib/kubelet","/var/lib/snapd" + ,"/var/lib/wicked","/var/lib/longhorn" + ,"/var/lib/cni","/usr/share/pki/trust" + ,"/usr/share/pki/trust/anchors", + "/var/lib/ca-certificates"] + */ + if p == "" { + continue + } log.Logger.Debug().Str("what", p).Str("where", s.StateDir).Msg("Mounting bind") op := mountBind(p, s.Rootdir, s.StateDir) s.fstabs = append(s.fstabs, &op.FstabEntry) diff --git a/pkg/mount/operation.go b/pkg/mount/operation.go index 4e7ee5b..e8e3280 100644 --- a/pkg/mount/operation.go +++ b/pkg/mount/operation.go @@ -19,6 +19,7 @@ type mountOperation struct { func (m mountOperation) run() error { log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() if m.PrepareCallback != nil { + log.Logger.Debug().Str("what", m.MountOption.Source).Str("where", m.Target).Str("type", m.MountOption.Type).Msg("executing mount callback") if err := m.PrepareCallback(); err != nil { log.Logger.Err(err).Str("what", m.MountOption.Source).Str("where", m.Target).Str("type", m.MountOption.Type).Msg("executing mount callback") return err From c533ab825ad4633082f6be4bcf197de30975f0a7 Mon Sep 17 00:00:00 2001 From: Itxaka Date: Wed, 8 Feb 2023 16:36:12 +0100 Subject: [PATCH 14/14] bind: only add to fstab if we have no errors Signed-off-by: Itxaka --- pkg/mount/mount.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/mount/mount.go b/pkg/mount/mount.go index 481a929..3002e83 100644 --- a/pkg/mount/mount.go +++ b/pkg/mount/mount.go @@ -441,10 +441,7 @@ func (s *State) Register(g *herd.Graph) error { func(ctx context.Context) error { var err error log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Logger() - log.Logger.Debug().Msg("Start" + opMountBind) - log.Logger.Debug().Msg("Mounting bind") log.Logger.Debug().Strs("binds", s.BindMounts).Msg("Mounting bind") - for _, p := range s.BindMounts { // TODO: Check why p can be empty, Example: /* @@ -469,10 +466,15 @@ func (s *State) Register(g *herd.Graph) error { } log.Logger.Debug().Str("what", p).Str("where", s.StateDir).Msg("Mounting bind") op := mountBind(p, s.Rootdir, s.StateDir) - s.fstabs = append(s.fstabs, &op.FstabEntry) - err = multierror.Append(err, op.run()) + err2 := op.run() + if err2 != nil { + log.Logger.Err(err2).Send() + err = multierror.Append(err, err2) + } else { + // Only append to fstabs if there was no error, otherwise we will try to mount it after switch_root + s.fstabs = append(s.fstabs, &op.FstabEntry) + } } - log.Logger.Debug().Msg("End" + opMountBind) log.Logger.Err(err).Send() return err },