Add fsck before mounting (#56)

Respect all options from systemd-fsck as that it what is currently used
on kairos.

Signed-off-by: Itxaka <itxaka.garcia@spectrocloud.com>
This commit is contained in:
Itxaka
2023-02-17 16:15:26 +01:00
committed by GitHub
parent c081f987f8
commit 5dde396331
3 changed files with 55 additions and 1 deletions

View File

@@ -119,3 +119,53 @@ func CleanSysrootForFstab(path string) string {
}
return cleaned
}
// Fsck will run fsck over the device
// options are set on cmdline, but they are for systemd-fsck,
// so we need to interpret ourselves
func Fsck(device string) error {
if device == "tmpfs" {
return nil
}
mode := ReadCMDLineArg("fsck.mode=")
repair := ReadCMDLineArg("fsck.repair=")
// Be safe with defaults
if len(mode) == 0 {
mode = []string{"auto"}
}
if len(repair) == 0 {
repair = []string{"preen"}
}
args := []string{"fsck", device}
// Check the mode
// skip means just skip the fsck
// force means force even if fs is deemed clean
// auto or others means normal fsck call
switch mode[0] {
case "skip":
return nil
case "force":
args = append(args, "-f")
}
// Check repair type
// preen means try to fix automatically
// yes means say yes to everything (potentially destructive)
// no means say no to everything
switch repair[0] {
case "preen":
args = append(args, "-a")
case "yes":
args = append(args, "-y")
case "no":
args = append(args, "-n")
}
cmd := strings.Join(args, " ")
log.Logger.Debug().Str("cmd", cmd).Msg("fsck command")
out, e := utils.SH(cmd)
log.Logger.Debug().Str("output", out).Msg("fsck output")
if e != nil {
log.Logger.Warn().Str("error", e.Error()).Str("what", device).Msg("fsck")
}
return e
}

View File

@@ -56,7 +56,7 @@ func (s *State) MountRootDagStep(g *herd.Graph) error {
log.Logger.Debug().Str("targetImage", s.TargetImage).Str("path", s.Rootdir).Str("TargetDevice", s.TargetDevice).Msg("Not mounting loop, already mounted")
return nil
}
// TODO: squashfs recovery image?
_ = internalUtils.Fsck(s.path("/run/initramfs/cos-state", s.TargetImage))
cmd := fmt.Sprintf("losetup --show -f %s", s.path("/run/initramfs/cos-state", s.TargetImage))
_, err := utils.SH(cmd)
s.LogIfError(err, "losetup")

View File

@@ -116,6 +116,10 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du
MountOption: mountPoint,
FstabEntry: *tmpFstab,
Target: where,
PrepareCallback: func() error {
_ = internalUtils.Fsck(what)
return nil
},
}
err = op.run()