Skip to content

Commit

Permalink
This commit does removes the assumption of a /boot folder
Browse files Browse the repository at this point in the history
With this commit all grub binaries (EFI images and modules)
and configurations are installed always in defined prefixes.

Defined prefixes are always appended to the EFI path, which
is usually the EFI partition mountpoint at run time.

This causes a duplication of data if multiple prefixes are
defined.

Signed-off-by: David Cassany <dcassany@suse.com>
  • Loading branch information
davidcassany committed Jan 30, 2024
1 parent c2d9965 commit 91ba5f9
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 171 deletions.
1 change: 0 additions & 1 deletion pkg/action/build-disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ func (b *BuildDiskAction) BuildDiskRun() (err error) { //nolint:gocyclo

err = b.bootloader.InstallEFI(
activeRoot, b.roots[constants.EfiPartName],
b.roots[constants.EfiPartName], b.spec.Partitions.State.FilesystemLabel,
)
if err != nil {
b.cfg.Logger.Errorf("failed installing grub efi binaries: %s", err.Error())
Expand Down
26 changes: 14 additions & 12 deletions pkg/action/build-iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
)

const (
grubPrefixDir = "/EFI/BOOT"
isoBootCatalog = "/boot/boot.catalog"
)

Expand Down Expand Up @@ -73,7 +72,7 @@ func NewBuildISOAction(cfg *v1.BuildConfig, spec *v1.LiveISO, opts ...BuildISOAc
}

if b.bootloader == nil {
b.bootloader = bootloader.NewGrub(&cfg.Config)
b.bootloader = bootloader.NewGrub(&cfg.Config, bootloader.WithGrubPrefixes(constants.FallbackEFIPath))
}

return b
Expand Down Expand Up @@ -187,27 +186,30 @@ func (b *BuildISOAction) ISORun() error {
}

func (b *BuildISOAction) PrepareEFI(rootDir, uefiDir string) error {
return b.bootloader.InstallEFIFallbackBinaries(rootDir, uefiDir, b.spec.Label)
err := b.renderGrubTemplate(uefiDir)
if err != nil {
return err
}
return b.bootloader.InstallEFI(rootDir, uefiDir)
}

func (b *BuildISOAction) PrepareISO(rootDir, imageDir string) error {
err := utils.MkdirAll(b.cfg.Fs, filepath.Join(imageDir, grubPrefixDir), constants.DirPerm)
// Include EFI contents in iso root too
return b.PrepareEFI(rootDir, imageDir)
}

func (b *BuildISOAction) renderGrubTemplate(rootDir string) error {
err := utils.MkdirAll(b.cfg.Fs, filepath.Join(rootDir, constants.FallbackEFIPath), constants.DirPerm)
if err != nil {
return err
}

// Write grub.cfg file
err = b.cfg.Fs.WriteFile(
filepath.Join(imageDir, grubPrefixDir, constants.GrubCfg),
return b.cfg.Fs.WriteFile(
filepath.Join(rootDir, constants.FallbackEFIPath, constants.GrubCfg),
[]byte(fmt.Sprintf(grubCfgTemplate(b.cfg.Platform.Arch), b.spec.GrubEntry, b.spec.Label)),
constants.FilePerm,
)
if err != nil {
return err
}

// Include EFI contents in iso root too
return b.PrepareEFI(rootDir, imageDir)
}

func (b BuildISOAction) prepareISORoot(isoDir string, rootDir string) error {
Expand Down
1 change: 0 additions & 1 deletion pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ func (i InstallAction) Run() (err error) {
err = i.bootloader.Install(
cnst.WorkingImgDir,
i.spec.Partitions.EFI.MountPoint,
i.spec.Partitions.EFI.FilesystemLabel,
)
if err != nil {
return elementalError.NewFromError(err, elementalError.InstallGrub)
Expand Down
1 change: 0 additions & 1 deletion pkg/action/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ func (r ResetAction) Run() (err error) {
err = r.bootloader.Install(
cnst.WorkingImgDir,
r.spec.Partitions.EFI.MountPoint,
r.spec.Partitions.EFI.FilesystemLabel,
)

if err != nil {
Expand Down
43 changes: 16 additions & 27 deletions pkg/bootloader/grub.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
)

var (
defaultGrubPrefixes = []string{"/EFI/ELEMENTAL", "/EFI/BOOT"}
defaultGrubPrefixes = []string{constants.FallbackEFIPath, constants.EntryEFIPath}
)

func getGModulePatterns(module string) []string {
Expand Down Expand Up @@ -105,9 +105,9 @@ func WithSecureBoot(secureboot bool) func(g *Grub) error {
}
}

func WithGrubPrefix(prefix string) func(g *Grub) error {
func WithGrubPrefixes(prefixes ...string) func(g *Grub) error {
return func(g *Grub) error {
g.grubPrefixes = append(g.grubPrefixes, prefix)
g.grubPrefixes = prefixes
return nil
}
}
Expand Down Expand Up @@ -191,40 +191,29 @@ func (g *Grub) installModules(rootDir, bootDir string, modules ...string) error
return nil
}

func (g *Grub) InstallEFI(rootDir, bootDir, efiDir, deviceLabel string) error {
err := g.installModules(rootDir, bootDir, constants.GetDefaultGrubModules()...)
func (g *Grub) InstallEFI(rootDir, efiDir string) error {
err := g.installModules(rootDir, efiDir, constants.GetDefaultGrubModules()...)
if err != nil {
return err
}

err = g.InstallEFIFallbackBinaries(rootDir, efiDir, deviceLabel)
if err != nil {
return err
}

err = g.InstallEFIElementalBinaries(rootDir, efiDir, deviceLabel)
if err != nil {
return err
for _, prefix := range g.grubPrefixes {
err = g.InstallEFIBinaries(rootDir, efiDir, prefix)
if err != nil {
return err
}
}

return nil
}

func (g *Grub) InstallEFIFallbackBinaries(rootDir, efiDir, _ string) error {
return g.installEFIPartitionBinaries(rootDir, efiDir, constants.FallbackEFIPath)
}

func (g *Grub) InstallEFIElementalBinaries(rootDir, efiDir, _ string) error {
return g.installEFIPartitionBinaries(rootDir, efiDir, constants.EntryEFIPath)
}

func (g *Grub) installEFIPartitionBinaries(rootDir, efiDir, efiPath string) error {
func (g *Grub) InstallEFIBinaries(rootDir, efiDir, prefix string) error {
err := g.findEFIImages(rootDir)
if err != nil {
return err
}

installPath := filepath.Join(efiDir, efiPath)
installPath := filepath.Join(efiDir, prefix)
err = utils.MkdirAll(g.fs, installPath, constants.DirPerm)
if err != nil {
g.logger.Errorf("Error creating dirs: %s", err)
Expand All @@ -235,7 +224,7 @@ func (g *Grub) installEFIPartitionBinaries(rootDir, efiDir, efiPath string) erro
grubEfi := filepath.Join(installPath, filepath.Base(g.grubEfiImg))

var bootImg string
if efiPath == constants.FallbackEFIPath {
if prefix == constants.FallbackEFIPath {
switch g.platform.Arch {
case constants.ArchAmd64, constants.Archx86:
bootImg = filepath.Join(installPath, constants.EfiImgX86)
Expand Down Expand Up @@ -398,8 +387,8 @@ func (g *Grub) SetDefaultEntry(partMountPoint, imgMountPoint, defaultEntry strin
}

// Install installs grub into the device, copy the config file and add any extra TTY to grub
func (g *Grub) Install(rootDir, bootDir, deviceLabel string) (err error) {
err = g.InstallEFI(rootDir, bootDir, constants.EfiDir, deviceLabel)
func (g *Grub) Install(rootDir, bootDir string) (err error) {
err = g.InstallEFI(rootDir, bootDir)
if err != nil {
return err
}
Expand All @@ -422,7 +411,7 @@ func (g *Grub) Install(rootDir, bootDir, deviceLabel string) (err error) {
// rootDir is the root of the OS image, bootDir is the folder grub read the
// configuration from, usually EFI partition mountpoint
func (g Grub) InstallConfig(rootDir, bootDir string) error {
for _, path := range []string{constants.FallbackEFIPath, constants.EntryEFIPath} {
for _, path := range g.grubPrefixes {
grubFile := filepath.Join(rootDir, g.elementalCfg)
dstGrubFile := filepath.Join(bootDir, path, g.configFile)

Expand Down
Loading

0 comments on commit 91ba5f9

Please sign in to comment.