Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkksiso: option --replace to replace arbitrary text in boot config. #1402

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions src/bin/mkksiso
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def GetCmdline(line, commands):
return "", line


def EditIsolinux(rm_args, add_args, new_volid, old_volid, tmpdir):
def EditIsolinux(rm_args, add_args, replace_list, tmpdir):
"""
Modify the cmdline for an isolinux.cfg
Remove args, add new arguments and change existing volid if requested
Expand All @@ -322,14 +322,13 @@ def EditIsolinux(rm_args, add_args, new_volid, old_volid, tmpdir):
log.warning("No isolinux/isolinux.cfg file found")
return

change_volid = old_volid != new_volid

# Edit the config file, save the new one as .new
with open(orig_cfg, "r") as in_fp:
with open(orig_cfg + ".new", "w") as out_fp:
for line in in_fp:
if change_volid and old_volid in line:
line = line.replace(old_volid, new_volid)
for from_item, to_item in replace_list:
if from_item in line:
line = line.replace(from_item, to_item)
prefix, cmdline = GetCmdline(line, ["append"])
if prefix:
args = SplitCmdline(cmdline)
Expand All @@ -342,7 +341,7 @@ def EditIsolinux(rm_args, add_args, new_volid, old_volid, tmpdir):
os.replace(orig_cfg + ".new", orig_cfg)


def EditGrub2(rm_args, add_args, new_volid, old_volid, tmpdir):
def EditGrub2(rm_args, add_args, replace_list, tmpdir):
"""
Modify the cmdline for GRUB2 UEFI and BIOS config files
Add the new arguments and change existing volid if requested
Expand All @@ -354,8 +353,6 @@ def EditGrub2(rm_args, add_args, new_volid, old_volid, tmpdir):
log.warning("No grub config files found")
return

change_volid = old_volid != new_volid

for cfg in grub_cfgs:
orig_cfg = tmpdir + "/" + cfg
if not os.path.exists(orig_cfg):
Expand All @@ -364,8 +361,9 @@ def EditGrub2(rm_args, add_args, new_volid, old_volid, tmpdir):
with open(orig_cfg, "r") as in_fp:
with open(orig_cfg + ".new", "w") as out_fp:
for line in in_fp:
if change_volid and old_volid in line:
line = line.replace(old_volid, new_volid)
for from_item, to_item in replace_list:
if from_item in line:
line = line.replace(from_item, to_item)
# Some start with linux (BIOS/aarch64), others with linuxefi (x86_64)
prefix, cmdline = GetCmdline(line, ["linuxefi", "linux"])
if prefix:
Expand All @@ -379,7 +377,7 @@ def EditGrub2(rm_args, add_args, new_volid, old_volid, tmpdir):
os.replace(orig_cfg + ".new", orig_cfg)


def EditS390(rm_args, add_args, new_volid, old_volid, tmpdir):
def EditS390(rm_args, add_args, replace_list, tmpdir):
"""
Modify the cmdline for s390 config files
Add the new arguments and change existing volid if requested
Expand All @@ -390,8 +388,6 @@ def EditS390(rm_args, add_args, new_volid, old_volid, tmpdir):
log.warning("No s390 config files found")
return

change_volid = old_volid != new_volid

for cfg in s390_cfgs:
orig_cfg = tmpdir + "/" + cfg
if not os.path.exists(orig_cfg):
Expand All @@ -404,8 +400,9 @@ def EditS390(rm_args, add_args, new_volid, old_volid, tmpdir):

cmdline = " ".join(l.strip() for l in lines)
# Replace the volid
if change_volid and old_volid in cmdline:
cmdline = cmdline.replace(old_volid, new_volid)
for from_item, to_item in replace_list:
if from_item in cmdline:
cmdline = cmdline.replace(from_item, to_item)
args = SplitCmdline(cmdline)
new_args = AlterKernelArgs(args, rm_args, add_args)

Expand Down Expand Up @@ -435,13 +432,15 @@ def CheckDiscinfo(path):


def MakeKickstartISO(input_iso, output_iso, ks="", updates_image="", add_paths=None,
cmdline="", rm_args="", new_volid="", implantmd5=True,
cmdline="", rm_args="", new_volid="", replace_list=None, implantmd5=True,
skip_efi=False):
"""
Make a kickstart ISO from a boot.iso or dvd
"""
if add_paths is None:
add_paths = []
if replace_list is None:
replace_list = []

# Gather information about the input iso
old_volid, files = GetISODetails(input_iso)
Expand Down Expand Up @@ -476,10 +475,12 @@ def MakeKickstartISO(input_iso, output_iso, ks="", updates_image="", add_paths=N
add_args["inst.ks"] = ["hd:LABEL=%s:/%s" % (new_volid or old_volid, os.path.basename(ks))]
add_paths.append(ks)

replace_list.append((old_volid, new_volid))
log.debug(replace_list)
# Add kickstart command and optionally change the volid of the available config files
EditIsolinux(remove_args, add_args, new_volid, old_volid, tmpdir)
EditGrub2(remove_args, add_args, new_volid, old_volid, tmpdir)
EditS390(remove_args, add_args, new_volid, old_volid, tmpdir)
EditIsolinux(remove_args, add_args, replace_list, tmpdir)
EditGrub2(remove_args, add_args, replace_list, tmpdir)
EditS390(remove_args, add_args, replace_list, tmpdir)

if os.uname().machine.startswith("s390"):
RebuildS390CDBoot(tmpdir)
Expand Down Expand Up @@ -563,6 +564,8 @@ def setup_arg_parser():
parser.add_argument("-u", "--updates", type=os.path.abspath, metavar="IMAGE",
help="Optional updates image to add to the ISO")
parser.add_argument("-V", "--volid", dest="volid", help="Set the ISO volume id, defaults to input's", default=None)
parser.add_argument("-R", "--replace", nargs=2, action="append", metavar=("FROM", "TO"),
help="Replace string in grub.cfg. Can be used multiple times")
parser.add_argument("--skip-mkefiboot", action="store_true", dest="skip_efi",
help="Skip running mkefiboot")

Expand Down Expand Up @@ -606,16 +609,16 @@ def main():
log.error("Use either --ks KICKSTART or positional KICKSTART but not both")
errors = True

if not any([args.ks or args.ks_pos, args.updates, args.add_paths, args.cmdline, args.rm_args, args.volid]):
log.error("Nothing to do - pass one or more of --ks, --updates, --add, --cmdline, --rm-args, --volid")
if not any([args.ks or args.ks_pos, args.updates, args.add_paths, args.cmdline, args.rm_args, args.volid, args.replace]):
log.error("Nothing to do - pass one or more of --ks, --updates, --add, --cmdline, --rm-args, --volid, --replace")
errors = True

if errors:
raise RuntimeError("Problems running %s" % sys.argv[0])

MakeKickstartISO(args.input_iso, args.output_iso, args.ks or args.ks_pos, args.updates,
args.add_paths, args.cmdline, args.rm_args,
args.volid, args.no_md5sum, args.skip_efi)
args.volid, args.replace, args.no_md5sum, args.skip_efi)
except RuntimeError as e:
log.error(str(e))
return 1
Expand Down
4 changes: 3 additions & 1 deletion tests/mkksiso/test_mkksiso.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def setUp(self):
})
self.new_volid = "Fedora-mkksiso-rawhide-test"
self.old_volid = "Fedora-rawhide-test"
self.replace_list = [(self.old_volid, self.new_volid)]


def run_test(self, configs, tmpdir, test_fn):
"""
Expand All @@ -142,7 +144,7 @@ def run_test(self, configs, tmpdir, test_fn):
os.makedirs(os.path.dirname(tmpdir + "/" + path), exist_ok=True)
shutil.copy(test_data + "/" + cfg, tmpdir + "/" + path)

test_fn(self.rm_args, self.add_args, self.new_volid, self.old_volid, tmpdir)
test_fn(self.rm_args, self.add_args, self.replace_list, tmpdir)

# Read the modified config file(s) and compare to result file
check_cfg_results(self, tmpdir, configs)
Expand Down