From 1a493f9beefd527a988c54627b348e530a8d60c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20L=C3=B6nnegren?= Date: Wed, 21 Feb 2024 15:24:50 +0100 Subject: [PATCH] Use backup legacy grub.cfg location MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the new /etc/elemental/grub.cfg config is not found, revert to using /etc/cos/grub.cfg for backwards compatibility. Signed-off-by: Fredrik Lönnegren --- pkg/bootloader/grub.go | 40 ++++++++++++++++++++++++-------------- pkg/constants/constants.go | 1 + 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/pkg/bootloader/grub.go b/pkg/bootloader/grub.go index d8e2a3c8dce..4b55091354b 100644 --- a/pkg/bootloader/grub.go +++ b/pkg/bootloader/grub.go @@ -19,6 +19,7 @@ package bootloader import ( "bytes" "fmt" + "os" "path/filepath" "regexp" @@ -57,12 +58,13 @@ type Grub struct { grubEfiImg string mokMngr string - grubPrefixes []string - configFile string - elementalCfg string - disableBootEntry bool - clearBootEntry bool - secureBoot bool + grubPrefixes []string + configFile string + elementalCfg string + legacyElementalCfg string + disableBootEntry bool + clearBootEntry bool + secureBoot bool } var _ v1.Bootloader = (*Grub)(nil) @@ -76,15 +78,16 @@ func NewGrub(cfg *v1.Config, opts ...GrubOptions) *Grub { secureBoot = false } g := &Grub{ - fs: cfg.Fs, - logger: cfg.Logger, - runner: cfg.Runner, - platform: cfg.Platform, - configFile: grubCfgFile, - grubPrefixes: defaultGrubPrefixes, - elementalCfg: filepath.Join(constants.GrubCfgPath, constants.GrubCfg), - clearBootEntry: true, - secureBoot: secureBoot, + fs: cfg.Fs, + logger: cfg.Logger, + runner: cfg.Runner, + platform: cfg.Platform, + configFile: grubCfgFile, + grubPrefixes: defaultGrubPrefixes, + elementalCfg: filepath.Join(constants.GrubCfgPath, constants.GrubCfg), + legacyElementalCfg: filepath.Join(constants.LegacyGrubCfgPath, constants.GrubCfg), + clearBootEntry: true, + secureBoot: secureBoot, } for _, o := range opts { @@ -413,6 +416,13 @@ func (g *Grub) Install(rootDir, bootDir string) (err error) { func (g Grub) InstallConfig(rootDir, bootDir string) error { for _, path := range g.grubPrefixes { grubFile := filepath.Join(rootDir, g.elementalCfg) + if _, err := os.Stat(grubFile); err != nil { + if os.IsNotExist(err) { + grubFile = filepath.Join(rootDir, g.legacyElementalCfg) + g.logger.Warnf("Grub config not found, using legacy config: %s", grubFile) + } + } + dstGrubFile := filepath.Join(bootDir, path, g.configFile) g.logger.Infof("Using grub config file %s", grubFile) diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 29cadfac95d..79ddc93edaf 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -182,6 +182,7 @@ const ( LegacyPassivePath = LegacyImagesPath + "/passive.img" LegacyActivePath = LegacyImagesPath + "/active.img" LegacyStateDir = "/run/initramfs/cos-state" + LegacyGrubCfgPath = "/etc/cos" ) // GetDefaultSystemEcludes returns a list of transient paths