|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# This script inspects a given json proving a list of addons, and |
| 4 | +# creates an addon for each key/value pair matching the given uki, distro and |
| 5 | +# arch provided in input. |
| 6 | +# |
| 7 | +# Usage: python uki_create_addons.py input_json out_dir uki distro arch |
| 8 | +# |
| 9 | +# This tool requires the systemd-ukify and systemd-boot packages. |
| 10 | +# |
| 11 | +# Addon file |
| 12 | +#----------- |
| 13 | +# Each addon terminates with .addon |
| 14 | +# Each addon contains only two types of lines: |
| 15 | +# Lines beginning with '#' are description and thus ignored |
| 16 | +# All other lines are command line to be added. |
| 17 | +# The name of the end resulting addon is taken from the json hierarchy. |
| 18 | +# For example, and addon in json['virt']['rhel']['x86_64']['hello.addon'] will |
| 19 | +# result in an UKI addon file generated in out_dir called |
| 20 | +# hello-virt.rhel.x86_64.addon.efi |
| 21 | +# |
| 22 | +# The common key, present in any sub-dict in the provided json (except the leaf dict) |
| 23 | +# is used as place for default addons when the same addon is not defined deep |
| 24 | +# in the hierarchy. For example, if we define test.addon (text: 'test1\n') in |
| 25 | +# json['common']['test.addon'] = ['test1\n'] and another test.addon (text: test2) in |
| 26 | +# json['virt']['common']['test.addon'] = ['test2'], any other uki except virt |
| 27 | +# will have a test.addon.efi with text "test1", and virt will have a |
| 28 | +# test.addon.efi with "test2" |
| 29 | +# |
| 30 | +# sbat.conf |
| 31 | +#---------- |
| 32 | +# This dict is containing the sbat string for *all* addons being created. |
| 33 | +# This dict is optional, but when used has to be put in a sub-dict with |
| 34 | +# { 'sbat' : { 'sbat.conf' : ['your text here'] }} |
| 35 | +# It follows the same syntax as the addon files, meaning '#' is comment and |
| 36 | +# the rest is taken as sbat string and feed to ukify. |
| 37 | + |
| 38 | +import os |
| 39 | +import sys |
| 40 | +import json |
| 41 | +import collections |
| 42 | +import subprocess |
| 43 | + |
| 44 | + |
| 45 | +UKIFY_PATH = '/usr/lib/systemd/ukify' |
| 46 | + |
| 47 | +def usage(err): |
| 48 | + print(f'Usage: {os.path.basename(__file__)} input_json output_dir uki distro arch') |
| 49 | + print(f'Error:{err}') |
| 50 | + sys.exit(1) |
| 51 | + |
| 52 | +def check_clean_arguments(input_json, out_dir): |
| 53 | + # Remove end '/' |
| 54 | + if out_dir[-1:] == '/': |
| 55 | + out_dir = out_dir[:-1] |
| 56 | + if not os.path.isfile(input_json): |
| 57 | + usage(f'input_json {input_json} is not a file, or does not exist!') |
| 58 | + if not os.path.isdir(out_dir): |
| 59 | + usage(f'out_dir_dir {out_dir} is not a dir, or does not exist!') |
| 60 | + return out_dir |
| 61 | + |
| 62 | +UKICmdlineAddon = collections.namedtuple('UKICmdlineAddon', ['name', 'cmdline']) |
| 63 | +uki_addons_list = [] |
| 64 | +uki_addons = {} |
| 65 | +addon_sbat_string = None |
| 66 | + |
| 67 | +def parse_lines(lines, rstrip=True): |
| 68 | + cmdline = '' |
| 69 | + for l in lines: |
| 70 | + l = l.lstrip() |
| 71 | + if not l: |
| 72 | + continue |
| 73 | + if l[0] == '#': |
| 74 | + continue |
| 75 | + # rstrip is used only for addons cmdline, not sbat.conf, as it replaces |
| 76 | + # return lines with spaces. |
| 77 | + if rstrip: |
| 78 | + l = l.rstrip() + ' ' |
| 79 | + cmdline += l |
| 80 | + if cmdline == '': |
| 81 | + return '' |
| 82 | + return cmdline |
| 83 | + |
| 84 | +def parse_all_addons(in_obj): |
| 85 | + global addon_sbat_string |
| 86 | + |
| 87 | + for el in in_obj.keys(): |
| 88 | + # addon found: copy it in our global dict uki_addons |
| 89 | + if el.endswith('.addon'): |
| 90 | + uki_addons[el] = in_obj[el] |
| 91 | + |
| 92 | + if 'sbat' in in_obj and 'sbat.conf' in in_obj['sbat']: |
| 93 | + # sbat.conf found: override sbat with the most specific one found |
| 94 | + addon_sbat_string = parse_lines(in_obj['sbat']['sbat.conf'], rstrip=False) |
| 95 | + |
| 96 | +def recursively_find_addons(in_obj, folder_list): |
| 97 | + # end of recursion, leaf directory. Search all addons here |
| 98 | + if len(folder_list) == 0: |
| 99 | + parse_all_addons(in_obj) |
| 100 | + return |
| 101 | + |
| 102 | + # first, check for common folder |
| 103 | + if 'common' in in_obj: |
| 104 | + parse_all_addons(in_obj['common']) |
| 105 | + |
| 106 | + # second, check if there is a match with the searched folder |
| 107 | + if folder_list[0] in in_obj: |
| 108 | + folder_next = in_obj[folder_list[0]] |
| 109 | + folder_list = folder_list[1:] |
| 110 | + recursively_find_addons(folder_next, folder_list) |
| 111 | + |
| 112 | +def parse_in_json(in_json, uki_name, distro, arch): |
| 113 | + with open(in_json, 'r') as f: |
| 114 | + in_obj = json.load(f) |
| 115 | + recursively_find_addons(in_obj, [uki_name, distro, arch]) |
| 116 | + |
| 117 | + for addon_name, cmdline in uki_addons.items(): |
| 118 | + addon_name = addon_name.replace(".addon","") |
| 119 | + addon_full_name = f'{addon_name}-{uki_name}.{distro}.{arch}.addon.efi' |
| 120 | + cmdline = parse_lines(cmdline).rstrip() |
| 121 | + if cmdline: |
| 122 | + uki_addons_list.append(UKICmdlineAddon(addon_full_name, cmdline)) |
| 123 | + |
| 124 | +def create_addons(out_dir): |
| 125 | + for uki_addon in uki_addons_list: |
| 126 | + out_path = os.path.join(out_dir, uki_addon.name) |
| 127 | + cmd = [ |
| 128 | + f'{UKIFY_PATH}', 'build', |
| 129 | + f'--cmdline="{uki_addon.cmdline}"', |
| 130 | + f'--output={out_path}'] |
| 131 | + if addon_sbat_string: |
| 132 | + cmd.append('--sbat="' + addon_sbat_string.rstrip() +'"') |
| 133 | + |
| 134 | + subprocess.check_call(cmd, text=True) |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + argc = len(sys.argv) - 1 |
| 138 | + if argc != 5: |
| 139 | + usage('too few or too many parameters!') |
| 140 | + |
| 141 | + input_json = sys.argv[1] |
| 142 | + out_dir = sys.argv[2] |
| 143 | + uki_name = sys.argv[3] |
| 144 | + distro = sys.argv[4] |
| 145 | + arch = sys.argv[5] |
| 146 | + |
| 147 | + out_dir = check_clean_arguments(input_json, out_dir) |
| 148 | + parse_in_json(input_json, uki_name, distro, arch) |
| 149 | + create_addons(out_dir) |
| 150 | + |
| 151 | + |
0 commit comments