Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init system information to state output #56

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type PartitionState struct {
type Kairos struct {
Flavor string `yaml:"flavor" json:"flavor"`
Version string `yaml:"version" json:"version"`
Init string `yaml:"init" json:"init"`
}

type Runtime struct {
Expand Down Expand Up @@ -224,6 +225,7 @@ func detectKairos(r *Runtime) {
if err == nil {
k.Version = v
}
k.Init = utils.GetInit()
r.Kairos = *k
}

Expand Down
30 changes: 28 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import (
"github.com/qeesung/image2ascii/convert"
)

const (
systemd = "systemd"
openrc = "openrc"
unknown = "unknown"
)

func SH(c string) (string, error) {
cmd := exec.Command("/bin/sh", "-c", c)
cmd.Env = os.Environ()
Expand Down Expand Up @@ -110,6 +116,27 @@ func Flavor() string {
return v
}

// GetInit Return the init system used by the OS
func GetInit() string {
for _, file := range []string{"/run/systemd/system", "/sbin/systemctl", "/usr/bin/systemctl", "/usr/sbin/systemctl", "/usr/bin/systemctl"} {
_, err := os.Stat(file)
// Found systemd
if err == nil {
return systemd
}
}

for _, file := range []string{"/sbin/openrc", "/usr/sbin/openrc", "/bin/openrc", "/usr/bin/openrc"} {
_, err := os.Stat(file)
// Found openrc
if err == nil {
return openrc
}
}

return unknown
}

func Name() string {
v, err := OSRelease("NAME")
if err != nil {
Expand All @@ -120,8 +147,7 @@ func Name() string {
}

func IsOpenRCBased() bool {
n := Name()
return strings.Contains(n, "alpine")
return GetInit() == openrc
}

func ShellSTDIN(s, c string) (string, error) {
Expand Down