diff --git a/osbuild/util/bls.py b/osbuild/util/bls.py new file mode 100644 index 00000000..c4922bec --- /dev/null +++ b/osbuild/util/bls.py @@ -0,0 +1,34 @@ +""" +Function for appending parameters to +Boot Loader Specification (BLS). +""" +import glob +from typing import List + + +def options_append(root_path: str, kernel_arguments: List[str]) -> None: + """ + Add kernel arguments to the Boot Loader Specification (BLS) configuration files. + There is unlikely to be more than one BLS config, but just in case, we'll iterate over them. + + Parameters + ---------- + + root_path (str): The root path for locating BLS configuration files. + kernel_arguments (list): A list of kernel arguments to be added. + + """ + bls_glob = f"{root_path}/loader/entries/*.conf" + bls_conf_files = glob.glob(bls_glob) + if len(bls_conf_files) == 0: + raise RuntimeError(f"no BLS configuration found in {bls_glob}") + for entry in bls_conf_files: + # Read in the file and then append to the options line. + with open(entry, encoding="utf8") as f: + lines = f.read().splitlines() + with open(entry, "w", encoding="utf8") as f: + for line in lines: + if line.startswith('options '): + f.write(f"{line} {' '.join(kernel_arguments)}\n") + else: + f.write(f"{line}\n") diff --git a/stages/org.osbuild.kernel-cmdline.bls-append b/stages/org.osbuild.kernel-cmdline.bls-append index dd166624..9b9006a0 100755 --- a/stages/org.osbuild.kernel-cmdline.bls-append +++ b/stages/org.osbuild.kernel-cmdline.bls-append @@ -7,11 +7,11 @@ the tree or in a mount. """ -import glob import sys from urllib.parse import urlparse import osbuild.api +from osbuild.util import bls SCHEMA_2 = r""" "options": { @@ -60,24 +60,7 @@ def main(paths, tree, options): assert url.path.startswith("/") bootroot = root + url.path - - # There is unlikely to be more than one bls config, but just - # in case we'll iterate over them. - entries = [] - for entry in glob.glob(f"{bootroot}/loader/entries/*.conf"): - entries.append(entry) - # Read in the file and then append to the options line. - with open(entry, encoding="utf8") as f: - lines = f.read().splitlines() - with open(entry, "w", encoding="utf8") as f: - for line in lines: - if line.startswith('options '): - f.write(f"{line} {' '.join(kopts)}\n") - else: - f.write(f"{line}\n") - assert len(entries) != 0 - print(f"Added {','.join(kopts)} to: {','.join(entries)}") - return 0 + bls.options_append(bootroot, kopts) if __name__ == '__main__':