Skip to content

Commit

Permalink
Also install fonts when installing grub (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itxaka authored Sep 13, 2023
1 parent 0afdf76 commit 274701a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
30 changes: 10 additions & 20 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const (
PersistentPartName = "persistent"
OEMLabel = "COS_OEM"
OEMPartName = "oem"
ISOLabel = "COS_LIVE"
MountBinary = "/usr/bin/mount"
EfiDevice = "/sys/firmware/efi"
LinuxFs = "ext4"
Expand All @@ -50,8 +49,6 @@ const (
EfiFs = "vfat"
EfiSize = uint(64)
OEMSize = uint(64)
StateSize = uint(15360)
RecoverySize = uint(8192)
PersistentSize = uint(0)
BiosSize = uint(1)
ImgSize = uint(3072)
Expand All @@ -66,7 +63,6 @@ const (
EfiDir = "/run/cos/efi"
RecoverySquashFile = "recovery.squashfs"
IsoRootFile = "rootfs.squashfs"
IsoEFIPath = "/boot/uefi.img"
ActiveImgFile = "active.img"
PassiveImgFile = "passive.img"
RecoveryImgFile = "recovery.img"
Expand All @@ -80,16 +76,13 @@ const (
AfterUpgradeChrootHook = "after-upgrade-chroot"
AfterUpgradeHook = "after-upgrade"
BeforeUpgradeHook = "before-upgrade"
LuetDefaultRepoURI = "quay.io/costoolkit/releases-green"
LuetDefaultRepoPrio = 90
TransitionImgFile = "transition.img"
RunningStateDir = "/run/initramfs/cos-state" // TODO: converge this constant with StateDir/RecoveryDir in dracut module from cos-toolkit
RunningRecoveryStateDir = "/run/initramfs/isoscan" // TODO: converge this constant with StateDir/RecoveryDir in dracut module from cos-toolkit
ActiveImgName = "active"
PassiveImgName = "passive"
RecoveryImgName = "recovery"
GPT = "gpt"
BuildImgName = "elemental"
UsrLocalPath = "/usr/local"
OEMPath = "/oem"

Expand All @@ -98,10 +91,6 @@ const (
SELinuxTargetedContextFile = SELinuxTargetedPath + "/contexts/files/file_contexts"
SELinuxTargetedPolicyPath = SELinuxTargetedPath + "/policy"

// These paths are arbitrary but coupled to grub.cfg
IsoKernelPath = "/boot/kernel"
IsoInitrdPath = "/boot/initrd"

// Default directory and file fileModes
DirPerm = os.ModeDir | os.ModePerm
FilePerm = 0666
Expand Down Expand Up @@ -132,15 +121,6 @@ func GetDefaultSquashfsCompressionOptions() []string {
return []string{"-comp", "gzip"}
}

func GetBuildDiskDefaultPackages() map[string]string {
return map[string]string{
"channel:system/grub2-efi-image": "efi",
"channel:system/grub2-config": "root",
"channel:system/grub2-artifacts": "root/grub2",
"channel:recovery/cos-img": "root/cOS",
}
}

func GetGrubFilePaths(arch string) []string {
var archPath string
switch arch {
Expand All @@ -163,3 +143,13 @@ func GetFallBackEfi(arch string) string {
return "bootx64.efi"
}
}

// GetGrubFonts returns the default font files for grub
func GetGrubFonts() []string {
return []string{"ascii.pf2", "euro.pf2", "unicode.pf2"}
}

// GetGrubModules returns the default module files for grub
func GetGrubModules() []string {
return []string{"loopback.mod", "squash4.mod", "xzio.mod", "gzio.mod"}
}
1 change: 1 addition & 0 deletions pkg/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Client struct {

func NewClient() *Client {
client := grab.NewClient()
client.UserAgent = "Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/117.0"
client.HTTPClient = &http.Client{Timeout: time.Second * constants.HTTPTimeout}
return &Client{client: client}
}
Expand Down
37 changes: 36 additions & 1 deletion pkg/utils/grub.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (g Grub) Install(target, rootDir, bootDir, grubConf, tty string, efi bool,
// as they were before. We now use the bundled grub.efi provided by the shim package
g.config.Logger.Infof("Generating grub files for efi on %s", target)
var foundModules bool
for _, m := range []string{"loopback.mod", "squash4.mod", "xzio.mod", "gzio.mod"} {
for _, m := range constants.GetGrubModules() {
err = fsutils.WalkDirFs(g.config.Fs, rootDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand All @@ -160,6 +160,8 @@ func (g Grub) Install(target, rootDir, bootDir, grubConf, tty string, efi bool,
}
}

copyGrubFonts(g.config, rootDir, grubdir, systemgrub)

err = fsutils.MkdirAll(g.config.Fs, filepath.Join(cnst.EfiDir, "EFI/boot/"), cnst.DirPerm)
if err != nil {
g.config.Logger.Errorf("Error creating dirs: %s", err)
Expand Down Expand Up @@ -225,3 +227,36 @@ func (g Grub) SetPersistentVariables(grubEnvFile string, vars map[string]string)
}
return nil
}

// copyGrubFonts will try to finds and copy the needed grub fonts into the system
// rootdir is the dir where to search for the fonts
// bootdir is the base dir where they will be copied
func copyGrubFonts(cfg *agentConfig.Config, rootDir, bootDir, systemgrub string) {
for _, m := range constants.GetGrubFonts() {
var foundFont bool
_ = fsutils.WalkDirFs(cfg.Fs, rootDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.Name() == m && strings.Contains(path, cfg.Arch) {
fileWriteName := filepath.Join(bootDir, fmt.Sprintf("%s/%s-efi/fonts/%s", systemgrub, cfg.Arch, m))
cfg.Logger.Debugf("Copying %s to %s", path, fileWriteName)
fileContent, err := cfg.Fs.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading %s: %s", path, err)
}
err = cfg.Fs.WriteFile(fileWriteName, fileContent, cnst.FilePerm)
if err != nil {
return fmt.Errorf("error writing %s: %s", fileWriteName, err)
}
foundFont = true
return nil
}
return err
})
if !foundFont {
// Not a real error as to fail install but a big warning
cfg.Logger.Warnf("did not find grub font %s under %s", m, rootDir)
}
}
}

0 comments on commit 274701a

Please sign in to comment.