Add KairosFS type and detectBoot with FS (#46)

This commit is contained in:
Itxaka
2023-09-01 14:31:04 +02:00
committed by GitHub
parent fd9cffde95
commit 9e420d9015
2 changed files with 29 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/itchyny/gojq" "github.com/itchyny/gojq"
"github.com/jaypipes/ghw" "github.com/jaypipes/ghw"
"github.com/jaypipes/ghw/pkg/block" "github.com/jaypipes/ghw/pkg/block"
"github.com/kairos-io/kairos-sdk/types"
"github.com/kairos-io/kairos-sdk/utils" "github.com/kairos-io/kairos-sdk/utils"
"github.com/zcalusic/sysinfo" "github.com/zcalusic/sysinfo"
"gopkg.in/yaml.v3" "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 { func detectRuntimeState(r *Runtime) error {
blockDevices, err := block.New(ghw.WithDisableTools(), ghw.WithDisableWarnings()) blockDevices, err := block.New(ghw.WithDisableTools(), ghw.WithDisableWarnings())
// ghw currently only detects if partitions are mounted via the device // ghw currently only detects if partitions are mounted via the device

7
types/fs.go Normal file
View File

@@ -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)
}