From 41658da9cf729150d22c9a287657c794b738891e Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 4 Dec 2024 11:10:42 -0800 Subject: [PATCH] org.osbuild.grub2.iso.legacy: Add grub2 setup for booting BIOS ISO This is the BIOS version of the grub2 iso stage. It installs the config file and copies over the grub2 modules to /boot/grub2/ --- stages/org.osbuild.grub2.iso | 1 + stages/org.osbuild.grub2.iso.legacy | 90 +++++++++++++++++++ stages/org.osbuild.grub2.iso.legacy.meta.json | 66 ++++++++++++++ 3 files changed, 157 insertions(+) create mode 100755 stages/org.osbuild.grub2.iso.legacy create mode 100644 stages/org.osbuild.grub2.iso.legacy.meta.json diff --git a/stages/org.osbuild.grub2.iso b/stages/org.osbuild.grub2.iso index c81f31bb..56930aee 100755 --- a/stages/org.osbuild.grub2.iso +++ b/stages/org.osbuild.grub2.iso @@ -7,6 +7,7 @@ import sys import osbuild.api # The main grub2 configuration file template. Used for UEFI. +# NOTE: Changes to this should also be applied to the org.osbuild.grub2.iso.legacy stage GRUB2_EFI_CFG_TEMPLATE = """ function load_video { insmod efi_gop diff --git a/stages/org.osbuild.grub2.iso.legacy b/stages/org.osbuild.grub2.iso.legacy new file mode 100755 index 00000000..8e9e464d --- /dev/null +++ b/stages/org.osbuild.grub2.iso.legacy @@ -0,0 +1,90 @@ +#!/usr/bin/python3 +import os +import shutil +import string +import sys + +import osbuild.api + +# The main grub2 configuration file template. Used for BIOS. +# NOTE: Changes to this should also be applied to the org.osbuild.grub2.iso stage +GRUB2_CFG_TEMPLATE = """ +function load_video { + insmod all_video +} + +load_video +set gfxpayload=keep +insmod gzio +insmod part_gpt +insmod ext2 +insmod chain + +set timeout=${timeout} +### END /etc/grub.d/00_header ### + +search --no-floppy --set=root -l '${isolabel}' + +### BEGIN /etc/grub.d/10_linux ### +menuentry 'Install ${product} ${version}' --class fedora --class gnu-linux --class gnu --class os { + linux ${kernelpath} ${root} quiet + initrd ${initrdpath} +} +menuentry 'Test this media & install ${product} ${version}' --class fedora --class gnu-linux --class gnu --class os { + linux ${kernelpath} ${root} rd.live.check quiet + initrd ${initrdpath} +} +submenu 'Troubleshooting -->' { + menuentry 'Install ${product} ${version} in basic graphics mode' --class fedora --class gnu-linux --class gnu --class os { + linux ${kernelpath} ${root} nomodeset quiet + initrd ${initrdpath} + } + menuentry 'Rescue a ${product} system' --class fedora --class gnu-linux --class gnu --class os { + linux ${kernelpath} ${root} inst.rescue quiet + initrd ${initrdpath} + } + menuentry 'Boot first drive' --class fedora --class gnu-linux --class gnu --class os { + chainloader (hd0)+1 + } +} +""" + + +def main(root, options): + name = options["product"]["name"] + version = options["product"]["version"] + isolabel = options["isolabel"] + kdir = options["kernel"].get("dir", "/images/pxeboot") + kopts = options["kernel"].get("opts") + cfg = options.get("config", {}) + timeout = cfg.get("timeout", 60) + + grub2dir = os.path.join(root, "boot", "grub2") + os.makedirs(grub2dir, exist_ok=True) + + # grub2 modules + moduledir = os.path.join(grub2dir, "i386-pc") + shutil.copytree("/usr/lib/grub/i386-pc", moduledir, dirs_exist_ok=True) + + print(f"kernel dir at {kdir}") + + tplt = string.Template(GRUB2_CFG_TEMPLATE) + data = tplt.safe_substitute({ + "version": version, + "product": name, + "kernelpath": os.path.join(kdir, "vmlinuz"), + "initrdpath": os.path.join(kdir, "initrd.img"), + "isolabel": isolabel, + "root": " ".join(kopts), + "timeout": timeout + }) + + config = os.path.join(grub2dir, "grub.cfg") + with open(config, "w", encoding="utf8") as cfg: + cfg.write(data) + + +if __name__ == '__main__': + args = osbuild.api.arguments() + ret = main(args["tree"], args["options"]) + sys.exit(ret) diff --git a/stages/org.osbuild.grub2.iso.legacy.meta.json b/stages/org.osbuild.grub2.iso.legacy.meta.json new file mode 100644 index 00000000..1fba1b55 --- /dev/null +++ b/stages/org.osbuild.grub2.iso.legacy.meta.json @@ -0,0 +1,66 @@ +{ + "summary": "Install a grub config and supporting files, suitable for a BIOS booting ISO", + "description": [], + "schema_2": { + "options": { + "additionalProperties": false, + "required": [ + "product", + "kernel", + "isolabel" + ], + "properties": { + "product": { + "type": "object", + "additionalProperties": false, + "required": [ + "name", + "version" + ], + "properties": { + "name": { + "type": "string" + }, + "version": { + "type": "string" + } + } + }, + "kernel": { + "type": "object", + "required": [ + "dir" + ], + "properties": { + "dir": { + "type": "string" + }, + "opts": { + "description": "Array options to append to the kernel command", + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "isolabel": { + "type": "string" + }, + "config": { + "description": "Configuration options for grub itself", + "type": "object", + "additionalProperties": false, + "properties": { + "timeout": { + "description": "Timeout in seconds", + "type": "integer", + "minimum": 0, + "default": 60 + } + } + } + } + } + } +}