diff --git a/state/state.go b/state/state.go index ee31b6f..4cf42dc 100644 --- a/state/state.go +++ b/state/state.go @@ -10,6 +10,7 @@ import ( "github.com/itchyny/gojq" "github.com/jaypipes/ghw" "github.com/jaypipes/ghw/pkg/block" + "github.com/kairos-io/kairos-sdk/types" "github.com/kairos-io/kairos-sdk/utils" "github.com/zcalusic/sysinfo" "gopkg.in/yaml.v3" @@ -132,6 +133,27 @@ func detectBoot() Boot { } } +// DetectBootWithVFS will detect the boot state using a vfs so it can be used for tests as well +func DetectBootWithVFS(fs types.KairosFS) (Boot, error) { + cmdline, err := fs.ReadFile("/proc/cmdline") + if err != nil { + return Unknown, err + } + cmdlineS := string(cmdline) + switch { + case strings.Contains(cmdlineS, "COS_ACTIVE"): + return Active, nil + case strings.Contains(cmdlineS, "COS_PASSIVE"): + return Passive, nil + case strings.Contains(cmdlineS, "COS_RECOVERY"), strings.Contains(cmdlineS, "COS_SYSTEM"): + return Recovery, nil + case strings.Contains(cmdlineS, "live:LABEL"), strings.Contains(cmdlineS, "live:CDLABEL"), strings.Contains(cmdlineS, "netboot"): + return LiveCD, nil + default: + return Unknown, nil + } +} + func detectRuntimeState(r *Runtime) error { blockDevices, err := block.New(ghw.WithDisableTools(), ghw.WithDisableWarnings()) // ghw currently only detects if partitions are mounted via the device diff --git a/types/fs.go b/types/fs.go new file mode 100644 index 0000000..f5b12e6 --- /dev/null +++ b/types/fs.go @@ -0,0 +1,7 @@ +package types + +// KairosFS is our interface for methods that need an FS +// We should try to keep it to a minimum so we can change between backends easily if needed +type KairosFS interface { + ReadFile(filename string) ([]byte, error) +}