@@ -27,16 +27,16 @@ import (
2727 "github.com/rancher/elemental-toolkit/pkg/utils"
2828
2929 efilib "github.com/canonical/go-efilib"
30+
3031 eleefi "github.com/rancher/elemental-toolkit/pkg/efi"
3132)
3233
3334const (
34- grubPrefix = "/grub2"
3535 grubCfgFile = "grub.cfg"
36+ )
3637
37- grubEFICfgTmpl = "search --no-floppy --label --set=root %s" +
38- "\n set prefix=($root)%s" +
39- "\n configfile $prefix/" + grubCfgFile
38+ var (
39+ defaultGrubPrefixes = []string {"/EFI/ELEMENTAL" , "/EFI/BOOT" }
4040)
4141
4242func getGModulePatterns (module string ) []string {
@@ -57,7 +57,7 @@ type Grub struct {
5757 grubEfiImg string
5858 mokMngr string
5959
60- grubPrefix string
60+ grubPrefixes [] string
6161 configFile string
6262 elementalCfg string
6363 disableBootEntry bool
@@ -80,8 +80,8 @@ func NewGrub(cfg *v1.Config, opts ...GrubOptions) *Grub {
8080 logger : cfg .Logger ,
8181 runner : cfg .Runner ,
8282 platform : cfg .Platform ,
83- grubPrefix : grubPrefix ,
8483 configFile : grubCfgFile ,
84+ grubPrefixes : defaultGrubPrefixes ,
8585 elementalCfg : filepath .Join (constants .GrubCfgPath , constants .GrubCfg ),
8686 clearBootEntry : true ,
8787 secureBoot : secureBoot ,
@@ -107,7 +107,7 @@ func WithSecureBoot(secureboot bool) func(g *Grub) error {
107107
108108func WithGrubPrefix (prefix string ) func (g * Grub ) error {
109109 return func (g * Grub ) error {
110- g .grubPrefix = prefix
110+ g .grubPrefixes = append ( g . grubPrefixes , prefix )
111111 return nil
112112 }
113113}
@@ -174,16 +174,18 @@ func (g *Grub) installModules(rootDir, bootDir string, modules ...string) error
174174 if err != nil {
175175 return err
176176 }
177- for _ , module := range modules {
178- fileWriteName := filepath .Join (bootDir , g .grubPrefix , fmt .Sprintf ("%s-efi" , g .platform .Arch ), filepath .Base (module ))
179- g .logger .Debugf ("Copying %s to %s" , module , fileWriteName )
180- err = utils .MkdirAll (g .fs , filepath .Dir (fileWriteName ), constants .DirPerm )
181- if err != nil {
182- return fmt .Errorf ("error creating destination folder: %v" , err )
183- }
184- err = utils .CopyFile (g .fs , module , fileWriteName )
185- if err != nil {
186- return fmt .Errorf ("error copying %s to %s: %s" , module , fileWriteName , err .Error ())
177+ for _ , grubPrefix := range g .grubPrefixes {
178+ for _ , module := range modules {
179+ fileWriteName := filepath .Join (bootDir , grubPrefix , fmt .Sprintf ("%s-efi" , g .platform .Arch ), filepath .Base (module ))
180+ g .logger .Debugf ("Copying %s to %s" , module , fileWriteName )
181+ err = utils .MkdirAll (g .fs , filepath .Dir (fileWriteName ), constants .DirPerm )
182+ if err != nil {
183+ return fmt .Errorf ("error creating destination folder: %v" , err )
184+ }
185+ err = utils .CopyFile (g .fs , module , fileWriteName )
186+ if err != nil {
187+ return fmt .Errorf ("error copying %s to %s: %s" , module , fileWriteName , err .Error ())
188+ }
187189 }
188190 }
189191 return nil
@@ -208,15 +210,15 @@ func (g *Grub) InstallEFI(rootDir, bootDir, efiDir, deviceLabel string) error {
208210 return nil
209211}
210212
211- func (g * Grub ) InstallEFIFallbackBinaries (rootDir , efiDir , deviceLabel string ) error {
212- return g .installEFIPartitionBinaries (rootDir , efiDir , constants .FallbackEFIPath , deviceLabel )
213+ func (g * Grub ) InstallEFIFallbackBinaries (rootDir , efiDir , _ string ) error {
214+ return g .installEFIPartitionBinaries (rootDir , efiDir , constants .FallbackEFIPath )
213215}
214216
215- func (g * Grub ) InstallEFIElementalBinaries (rootDir , efiDir , deviceLabel string ) error {
216- return g .installEFIPartitionBinaries (rootDir , efiDir , constants .EntryEFIPath , deviceLabel )
217+ func (g * Grub ) InstallEFIElementalBinaries (rootDir , efiDir , _ string ) error {
218+ return g .installEFIPartitionBinaries (rootDir , efiDir , constants .EntryEFIPath )
217219}
218220
219- func (g * Grub ) installEFIPartitionBinaries (rootDir , efiDir , efiPath , deviceLabel string ) error {
221+ func (g * Grub ) installEFIPartitionBinaries (rootDir , efiDir , efiPath string ) error {
220222 err := g .findEFIImages (rootDir )
221223 if err != nil {
222224 return err
@@ -274,12 +276,6 @@ func (g *Grub) installEFIPartitionBinaries(rootDir, efiDir, efiPath, deviceLabel
274276 return fmt .Errorf ("failed copying %s to %s: %s" , g .grubEfiImg , installPath , err .Error ())
275277 }
276278
277- grubCfgContent := []byte (fmt .Sprintf (grubEFICfgTmpl , deviceLabel , g .grubPrefix ))
278- err = g .fs .WriteFile (filepath .Join (efiDir , efiPath , grubCfgFile ), grubCfgContent , constants .FilePerm )
279- if err != nil {
280- return fmt .Errorf ("error writing %s: %s" , filepath .Join (efiDir , efiPath , grubCfgFile ), err )
281- }
282-
283279 return nil
284280}
285281
@@ -402,8 +398,8 @@ func (g *Grub) SetDefaultEntry(partMountPoint, imgMountPoint, defaultEntry strin
402398}
403399
404400// Install installs grub into the device, copy the config file and add any extra TTY to grub
405- func (g * Grub ) Install (rootDir , bootDir , stateLabel string ) (err error ) {
406- err = g .InstallEFI (rootDir , bootDir , constants .EfiDir , stateLabel )
401+ func (g * Grub ) Install (rootDir , bootDir , deviceLabel string ) (err error ) {
402+ err = g .InstallEFI (rootDir , bootDir , constants .EfiDir , deviceLabel )
407403 if err != nil {
408404 return err
409405 }
@@ -422,24 +418,29 @@ func (g *Grub) Install(rootDir, bootDir, stateLabel string) (err error) {
422418 return g .InstallConfig (rootDir , bootDir )
423419}
424420
425- // InstallConfig installs grub configuraton files to the expected location. rootDir is the root
426- // of the OS image, bootDir is the folder grub read the configuration from, usually state partition mountpoint
421+ // InstallConfig installs grub configuraton files to the expected location.
422+ // rootDir is the root of the OS image, bootDir is the folder grub read the
423+ // configuration from, usually EFI partition mountpoint
427424func (g Grub ) InstallConfig (rootDir , bootDir string ) error {
428- grubFile := filepath .Join (rootDir , g .elementalCfg )
429- dstGrubFile := filepath .Join (bootDir , g .grubPrefix , g .configFile )
425+ for _ , path := range []string {constants .FallbackEFIPath , constants .EntryEFIPath } {
426+ grubFile := filepath .Join (rootDir , g .elementalCfg )
427+ dstGrubFile := filepath .Join (bootDir , path , g .configFile )
430428
431- g .logger .Infof ("Using grub config file %s" , grubFile )
429+ g .logger .Infof ("Using grub config file %s" , grubFile )
432430
433- // Create Needed dir under state partition to store the grub.cfg and any needed modules
434- err := utils .MkdirAll (g .fs , filepath .Join (bootDir , g . grubPrefix ), constants .DirPerm )
435- if err != nil {
436- return fmt .Errorf ("error creating grub dir: %s" , err )
437- }
431+ // Create Needed dir under state partition to store the grub.cfg and any needed modules
432+ err := utils .MkdirAll (g .fs , filepath .Join (bootDir , path ), constants .DirPerm )
433+ if err != nil {
434+ return fmt .Errorf ("error creating grub dir: %s" , err )
435+ }
438436
439- g .logger .Infof ("Copying grub config file from %s to %s" , grubFile , dstGrubFile )
440- err = utils .CopyFile (g .fs , grubFile , dstGrubFile )
441- if err != nil {
442- g .logger .Errorf ("Failed copying grub config file: %s" , err )
437+ g .logger .Infof ("Copying grub config file from %s to %s" , grubFile , dstGrubFile )
438+ err = utils .CopyFile (g .fs , grubFile , dstGrubFile )
439+ if err != nil {
440+ g .logger .Errorf ("Failed copying grub config file: %s" , err )
441+ return err
442+ }
443443 }
444- return err
444+
445+ return nil
445446}
0 commit comments