2023-02-06 10:31:46 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2023-02-06 13:49:35 +00:00
|
|
|
"github.com/kairos-io/kairos/sdk/state"
|
2023-02-06 15:20:18 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-02-06 10:31:46 +00:00
|
|
|
)
|
|
|
|
|
2023-02-06 15:20:18 +00:00
|
|
|
func BootedFromCD() (bool, error) {
|
2023-02-06 13:49:35 +00:00
|
|
|
runtime, err := state.NewRuntime()
|
2023-02-06 10:31:46 +00:00
|
|
|
if err != nil {
|
2023-02-06 13:49:35 +00:00
|
|
|
return false, err
|
2023-02-06 10:31:46 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:49:35 +00:00
|
|
|
return runtime.BootState == state.LiveCD, nil
|
2023-02-06 10:31:46 +00:00
|
|
|
}
|
2023-02-06 15:20:18 +00:00
|
|
|
|
|
|
|
func BootStateToLabel() string {
|
|
|
|
runtime, err := state.NewRuntime()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
switch runtime.BootState {
|
|
|
|
case "active_boot":
|
|
|
|
return "COS_ACTIVE"
|
|
|
|
case "passive_boot":
|
|
|
|
return "COS_PASSIVE"
|
|
|
|
case "recovery_boot":
|
2023-02-08 18:11:00 +00:00
|
|
|
return "COS_SYSTEM"
|
2023-02-06 15:20:18 +00:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 17:51:53 +00:00
|
|
|
func IsRecovery() bool {
|
2023-02-06 15:20:18 +00:00
|
|
|
runtime, err := state.NewRuntime()
|
|
|
|
if err != nil {
|
2023-02-08 17:51:53 +00:00
|
|
|
return false
|
2023-02-06 15:20:18 +00:00
|
|
|
}
|
|
|
|
switch runtime.BootState {
|
|
|
|
case "recovery_boot":
|
2023-02-08 17:51:53 +00:00
|
|
|
return true
|
2023-02-06 15:20:18 +00:00
|
|
|
default:
|
2023-02-08 17:51:53 +00:00
|
|
|
return false
|
2023-02-06 15:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRootDir() string {
|
|
|
|
cmdline, _ := os.ReadFile("/proc/cmdline")
|
|
|
|
switch {
|
|
|
|
case strings.Contains(string(cmdline), "IMMUCORE_NOPIVOT"):
|
|
|
|
return "/"
|
|
|
|
default:
|
|
|
|
// Default is sysroot for normal no-pivot boot
|
|
|
|
return "/sysroot"
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 21:54:17 +00:00
|
|
|
|
|
|
|
// UniqueSlice removes duplicated entries from a slice.So dumb. Like really? Why not have a set which enforces uniqueness????
|
|
|
|
func UniqueSlice(slice []string) []string {
|
|
|
|
keys := make(map[string]bool)
|
|
|
|
var list []string
|
|
|
|
for _, entry := range slice {
|
|
|
|
if _, value := keys[entry]; !value {
|
|
|
|
keys[entry] = true
|
|
|
|
list = append(list, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|