2022-10-23 18:22:32 +00:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-15 09:56:11 +00:00
|
|
|
"github.com/kairos-io/kairos-sdk/mounts"
|
|
|
|
"github.com/kairos-io/kairos-sdk/state"
|
|
|
|
"github.com/kairos-io/kairos-sdk/utils"
|
2022-10-23 18:22:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetGRUBOptions(opts map[string]string) Option {
|
|
|
|
return func(c *Changeset) error {
|
|
|
|
if len(opts) > 0 {
|
|
|
|
c.Add(func() error { return setGRUBOptions(opts) })
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setGRUBOptions(opts map[string]string) error {
|
2023-03-15 09:56:11 +00:00
|
|
|
mountPath := "/tmp/oem"
|
2023-08-07 14:35:15 +00:00
|
|
|
defer mounts.Umount(state.PartitionState{Mounted: true, MountPoint: mountPath}) //nolint:errcheck
|
2022-10-23 18:22:32 +00:00
|
|
|
runtime, err := state.NewRuntime()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
oem := runtime.OEM
|
|
|
|
if runtime.OEM.Name == "" {
|
|
|
|
oem = runtime.Persistent
|
|
|
|
}
|
|
|
|
|
2023-03-15 09:56:11 +00:00
|
|
|
if err := mounts.PrepareWrite(oem, mountPath); err != nil {
|
2022-10-23 18:22:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range opts {
|
2023-03-15 09:56:11 +00:00
|
|
|
out, err := utils.SH(fmt.Sprintf(`%s /tmp/oem/grubenv set "%s=%s"`, utils.FindCommand("grub2-editenv", []string{"grub2-editenv", "grub-editenv"}), k, v))
|
2022-10-23 18:22:32 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not set boot option: %s\n", out+err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|