Skip to content

Commit

Permalink
boot: introduce parameter for optional kernel command line
Browse files Browse the repository at this point in the history
in functions used to update it.
  • Loading branch information
alfonsosanchezbeato authored and mvo5 committed Feb 27, 2023
1 parent 4d8f4df commit cd49a62
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 72 deletions.
35 changes: 21 additions & 14 deletions boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"

"github.com/snapcore/snapd/bootloader"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/snap"
)

Expand Down Expand Up @@ -429,21 +430,23 @@ func SetRecoveryBootSystemAndMode(dev snap.Device, systemLabel, mode string) err
return bl.SetBootVars(m)
}

// UpdateManagedBootConfigs updates managed boot config assets if those are
// present for the ubuntu-boot bootloader. Returns true when an update was
// carried out.
func UpdateManagedBootConfigs(dev snap.Device, gadgetSnapOrDir string) (updated bool, err error) {
// UpdateManagedBootConfigs updates managed boot config assets if
// those are present for the ubuntu-boot bootloader. To do this it
// needs information from the model, the gadget we are updating to,
// and any additional kernel command line arguments coming from system
// options. Returns true when an update was carried out.
func UpdateManagedBootConfigs(dev snap.Device, gadgetSnapOrDir, cmdlineAppend string) (updated bool, err error) {
if !dev.HasModeenv() {
// only UC20 devices use managed boot config
return false, nil
}
if !dev.RunMode() {
return false, fmt.Errorf("internal error: boot config can only be updated in run mode")
}
return updateManagedBootConfigForBootloader(dev, ModeRun, gadgetSnapOrDir)
return updateManagedBootConfigForBootloader(dev, ModeRun, gadgetSnapOrDir, cmdlineAppend)
}

func updateManagedBootConfigForBootloader(dev snap.Device, mode, gadgetSnapOrDir string) (updated bool, err error) {
func updateManagedBootConfigForBootloader(dev snap.Device, mode, gadgetSnapOrDir, cmdlineAppend string) (updated bool, err error) {
if mode != ModeRun {
return false, fmt.Errorf("internal error: updating boot config of recovery bootloader is not supported yet")
}
Expand All @@ -461,18 +464,20 @@ func updateManagedBootConfigForBootloader(dev snap.Device, mode, gadgetSnapOrDir
return false, err
}
// boot config update can lead to a change of kernel command line
_, err = observeCommandLineUpdate(dev.Model(), commandLineUpdateReasonSnapd, gadgetSnapOrDir)
_, err = observeCommandLineUpdate(dev.Model(), commandLineUpdateReasonSnapd, gadgetSnapOrDir, cmdlineAppend)
if err != nil {
return false, err
}
return tbl.UpdateBootConfig()
}

// UpdateCommandLineForGadgetComponent handles the update of a gadget that
// contributes to the kernel command line of the run system. Returns true when a
// change in command line has been observed and a reboot is needed. The reboot,
// if needed, should be requested at the the earliest possible occasion.
func UpdateCommandLineForGadgetComponent(dev snap.Device, gadgetSnapOrDir string) (needsReboot bool, err error) {
// UpdateCommandLineForGadgetComponent handles the update of a gadget
// that contributes to the kernel command line of the run system
// (appending any additional kernel command line arguments coming from
// system options). Returns true when a change in command line has
// been observed and a reboot is needed. The reboot, if needed, should
// be requested at the the earliest possible occasion.
func UpdateCommandLineForGadgetComponent(dev snap.Device, gadgetSnapOrDir, cmdlineAppend string) (needsReboot bool, err error) {
if !dev.HasModeenv() {
// only UC20 devices are supported
return false, fmt.Errorf("internal error: command line component cannot be updated on pre-UC20 devices")
Expand All @@ -490,8 +495,9 @@ func UpdateCommandLineForGadgetComponent(dev snap.Device, gadgetSnapOrDir string
}
return false, err
}

// gadget update can lead to a change of kernel command line
cmdlineChange, err := observeCommandLineUpdate(dev.Model(), commandLineUpdateReasonGadget, gadgetSnapOrDir)
cmdlineChange, err := observeCommandLineUpdate(dev.Model(), commandLineUpdateReasonGadget, gadgetSnapOrDir, cmdlineAppend)
if err != nil {
return false, err
}
Expand All @@ -500,10 +506,11 @@ func UpdateCommandLineForGadgetComponent(dev snap.Device, gadgetSnapOrDir string
}
// update the bootloader environment, maybe clearing the relevant
// variables
cmdlineVars, err := bootVarsForTrustedCommandLineFromGadget(gadgetSnapOrDir)
cmdlineVars, err := bootVarsForTrustedCommandLineFromGadget(gadgetSnapOrDir, cmdlineAppend)
if err != nil {
return false, fmt.Errorf("cannot prepare bootloader variables for kernel command line: %v", err)
}
logger.Debugf("updating boot vars: %v", cmdlineVars)
if err := tbl.SetBootVars(cmdlineVars); err != nil {
return false, fmt.Errorf("cannot set run system kernel command line arguments: %v", err)
}
Expand Down
Loading

0 comments on commit cd49a62

Please sign in to comment.