sparkles: Boot options (#91)

*  Add grub_options to config

Split post-install into hooks

* 🤖 Adapt test to latest releases
This commit is contained in:
Ettore Di Giacinto
2022-09-08 15:39:26 +02:00
committed by Itxaka
parent 01c6a99577
commit b7fec41fe3
5 changed files with 109 additions and 7 deletions

View File

@@ -0,0 +1,38 @@
package hook
import (
"fmt"
"strings"
config "github.com/c3os-io/c3os/pkg/config"
"github.com/c3os-io/c3os/pkg/utils"
)
type GrubOptions struct{}
func (b GrubOptions) Run(c config.Config) error {
oem, _ := utils.SH("blkid -L COS_OEM")
if oem == "" {
fmt.Println("OEM partition not found")
return nil // do not error out
}
oem = strings.TrimSuffix(oem, "\n")
oemMount, err := utils.SH(fmt.Sprintf("mkdir /tmp/oem && mount %s /tmp/oem", oem))
if err != nil {
fmt.Printf("could not mount oem: %s\n", oemMount+err.Error())
return nil // do not error out
}
for k, v := range c.Install.GrubOptions {
out, err := utils.SH(fmt.Sprintf("grub2-editenv /tmp/oem/grubenv set %s=%s", k, v))
if err != nil {
fmt.Printf("could not set boot option: %s\n", out+err.Error())
return nil // do not error out
}
}
utils.SH("umount /tmp/oem") //nolint:errcheck
return nil
}

View File

@@ -0,0 +1,24 @@
package hook
import (
config "github.com/c3os-io/c3os/pkg/config"
)
type Interface interface {
Run(c config.Config) error
}
var All = []Interface{
&RunStage{}, // Shells out to stages defined from the container image
&GrubOptions{}, // Set custom GRUB options
&Lifecycle{}, // Handles poweroff/reboot by config options
}
func Run(c config.Config, hooks ...Interface) error {
for _, h := range hooks {
if err := h.Run(c); err != nil {
return err
}
}
return nil
}

View File

@@ -0,0 +1,19 @@
package hook
import (
config "github.com/c3os-io/c3os/pkg/config"
"github.com/c3os-io/c3os/pkg/utils"
)
type Lifecycle struct{}
func (s Lifecycle) Run(c config.Config) error {
if c.Install.Reboot {
utils.Reboot()
}
if c.Install.Poweroff {
utils.PowerOFF()
}
return nil
}

View File

@@ -0,0 +1,16 @@
package hook
import (
config "github.com/c3os-io/c3os/pkg/config"
"github.com/c3os-io/c3os/pkg/utils"
events "github.com/c3os-io/c3os/sdk/bus"
)
type RunStage struct{}
func (r RunStage) Run(c config.Config) error {
utils.SH("elemental run-stage c3os-install.after") //nolint:errcheck
events.RunHookScript("/usr/bin/c3os-agent.install.after.hook") //nolint:errcheck
return nil
}

View File

@@ -14,6 +14,7 @@ import (
config "github.com/c3os-io/c3os/pkg/config"
hook "github.com/c3os-io/c3os/internal/agent/hooks"
"github.com/c3os-io/c3os/internal/bus"
"github.com/c3os-io/c3os/internal/cmd"
@@ -196,6 +197,15 @@ func RunInstall(options map[string]string) error {
_, reboot := options["reboot"]
_, poweroff := options["poweroff"]
if c.Install == nil {
c.Install = &config.Install{}
}
if poweroff {
c.Install.Poweroff = true
}
if reboot {
c.Install.Reboot = true
}
err := ioutil.WriteFile(f.Name(), []byte(cloudInit), os.ModePerm)
if err != nil {
@@ -215,15 +225,10 @@ func RunInstall(options map[string]string) error {
fmt.Println(err)
os.Exit(1)
}
utils.SH("elemental run-stage c3os-install.after") //nolint:errcheck
events.RunHookScript("/usr/bin/c3os-agent.install.after.hook") //nolint:errcheck
if reboot || c.Install != nil && c.Install.Reboot {
utils.Reboot()
if err := hook.Run(*c, hook.All...); err != nil {
return err
}
if poweroff || c.Install != nil && c.Install.Poweroff {
utils.PowerOFF()
}
return nil
}