From d4bea73868df8ddc09cd1b0d356339218f45190f Mon Sep 17 00:00:00 2001 From: Markus Hilger Date: Tue, 17 Nov 2026 16:14:49 +0100 Subject: [PATCH] Apply profile installedargs when installing a diskless image to disk The installedargs of profile.yaml only reached systems installed by an OS installer. A diskless image written to disk with installtodisk got none of them, and keeps from the network boot only console=, net.ifnames=, biosdevname= and quiet, so arguments meant for the installed system had to be baked into the image. image2disk.py now fetches the profile.yaml of the deployment and adds its installedargs before deployment.installedargs, in the same places. An argument the image already has, as when it was captured from a node installed this way, is kept once, in its last position, so the node arguments still come last, and a profile.yaml that cannot be read only skips the profile arguments instead of failing the install. --- .../profiles/default/scripts/image2disk.py | 40 ++++++++++++----- .../profiles/default/scripts/image2disk.py | 44 ++++++++++++++----- .../profiles/default/scripts/image2disk.py | 38 ++++++++++++---- 3 files changed, 91 insertions(+), 31 deletions(-) diff --git a/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py index bc8aa53b..7aafa584 100644 --- a/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el8-diskless/profiles/default/scripts/image2disk.py @@ -107,6 +107,11 @@ def set_machine_id(rootdir): swapdev = '/dev/mapper/localstorage-swap' +def append_args(args, newargs): + # newargs go last, and an argument given twice keeps only its last place + merged = args + newargs + return [arg for idx, arg in enumerate(merged) if arg not in merged[idx + 1:]] + def fixup(rootdir, vols): devbymount = {} for vol in vols: @@ -196,16 +201,31 @@ def fixup(rootdir, vols): if arg == 'quiet' or arg.startswith(('console=', 'net.ifnames=', 'biosdevname=')): currcmdline.append(arg) currcmdlinestr = ' '.join(currcmdline) - # deployment.installedargs, which the OS installers append to the - # kernel arguments of the installed system - nodeargs = [] + # installedargs of the profile followed by deployment.installedargs, + # which the OS installers add to the kernel arguments of the installed + # system + profile = None + installedargs = [] with open('/etc/confluent/confluent.deploycfg') as dcfgin: for line in dcfgin: - if line.startswith('installedargs: '): + if line.startswith('profile: '): + profile = line[len('profile: '):].strip() + elif line.startswith('installedargs: '): argstr = line[len('installedargs: '):].strip() if argstr[:1] == "'" and argstr[-1:] == "'": argstr = argstr[1:-1].replace("''", "'") - nodeargs = argstr.split() + installedargs = argstr.split() + if profile: + try: + profileyaml = subprocess.check_output([ + sys.executable, '/opt/confluent/bin/apiclient', + '/confluent-public/os/{}/profile.yaml'.format(profile)]).decode('utf8') + except (subprocess.CalledProcessError, OSError) as e: + sys.stderr.write('Unable to read the installedargs of profile {}: {}\n'.format(profile, e)) + profileyaml = '' + for line in profileyaml.split('\n'): + if line.startswith('installedargs:'): + installedargs = line.split('#', 1)[0][len('installedargs:'):].split() + installedargs if os.path.exists(grubsyscfg): with open(grubsyscfg) as defgrubin: defgrub = defgrubin.read().split('\n') @@ -223,7 +243,7 @@ def fixup(rootdir, vols): ] if not os.path.exists(os.path.join(rootdir, "etc/kernel/cmdline")): with open(os.path.join(rootdir, "etc/kernel/cmdline"), "w") as cmdlineout: - cmdlineout.write("{} root=/dev/mapper/localstorage-root rd.lvm.lv=localstorage/root".format(' '.join(currcmdline + nodeargs))) + cmdlineout.write("{} root=/dev/mapper/localstorage-root rd.lvm.lv=localstorage/root".format(' '.join(append_args(currcmdline, installedargs)))) with open(grubsyscfg, 'w') as defgrubout: for gline in defgrub: gline = gline.split() @@ -234,10 +254,10 @@ def fixup(rootdir, vols): newline.append('"') continue newline.append(ent) - if nodeargs and newline and newline[0].startswith('GRUB_CMDLINE_LINUX=') and newline[-1].endswith('"'): - newline[-1] = newline[-1][:-1] - newline.extend(nodeargs) - newline[-1] += '"' + if installedargs and newline and newline[0].startswith('GRUB_CMDLINE_LINUX=') and newline[-1].endswith('"'): + # an image captured from an installed node may carry them already + present = ' '.join(newline)[len('GRUB_CMDLINE_LINUX="'):-1].split() + newline = ['GRUB_CMDLINE_LINUX="{}"'.format(' '.join(append_args(present, installedargs)))] defgrubout.write(' '.join(newline) + '\n') grubcfg = subprocess.check_output(['find', os.path.join(rootdir, 'boot'), '-name', 'grub.cfg']).decode('utf8').strip().replace(rootdir, '/').replace('//', '/') grubcfg = grubcfg.split('\n') diff --git a/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py index 2c57375d..fb13bd30 100644 --- a/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/el9-diskless/profiles/default/scripts/image2disk.py @@ -124,6 +124,11 @@ def set_machine_id(rootdir): else: os.rename(loadent, newent) +def append_args(args, newargs): + # newargs go last, and an argument given twice keeps only its last place + merged = args + newargs + return [arg for idx, arg in enumerate(merged) if arg not in merged[idx + 1:]] + def fixup(rootdir, vols): devbymount = {} for vol in vols: @@ -216,16 +221,31 @@ def fixup(rootdir, vols): if arg == 'quiet' or arg.startswith(('console=', 'net.ifnames=', 'biosdevname=')): currcmdline.append(arg) currcmdlinestr = ' '.join(currcmdline) - # deployment.installedargs, which the OS installers append to the - # kernel arguments of the installed system - nodeargs = [] + # installedargs of the profile followed by deployment.installedargs, + # which the OS installers add to the kernel arguments of the installed + # system + profile = None + installedargs = [] with open('/etc/confluent/confluent.deploycfg') as dcfgin: for line in dcfgin: - if line.startswith('installedargs: '): + if line.startswith('profile: '): + profile = line[len('profile: '):].strip() + elif line.startswith('installedargs: '): argstr = line[len('installedargs: '):].strip() if argstr[:1] == "'" and argstr[-1:] == "'": argstr = argstr[1:-1].replace("''", "'") - nodeargs = argstr.split() + installedargs = argstr.split() + if profile: + try: + profileyaml = subprocess.check_output([ + sys.executable, '/opt/confluent/bin/apiclient', + '/confluent-public/os/{}/profile.yaml'.format(profile)]).decode('utf8') + except (subprocess.CalledProcessError, OSError) as e: + sys.stderr.write('Unable to read the installedargs of profile {}: {}\n'.format(profile, e)) + profileyaml = '' + for line in profileyaml.split('\n'): + if line.startswith('installedargs:'): + installedargs = line.split('#', 1)[0][len('installedargs:'):].split() + installedargs kcmdline = os.path.join(rootdir, 'etc/kernel/cmdline') if os.path.exists(kcmdline): with open(kcmdline) as kcmdlinein: @@ -250,7 +270,7 @@ def fixup(rootdir, vols): if arg.startswith(('net.ifnames=', 'biosdevname=')) and arg not in newkcmdlineent: newkcmdlineent.append(arg) with open(kcmdline, 'w') as kcmdlineout: - kcmdlineout.write(' '.join(newkcmdlineent + nodeargs) + '\n') + kcmdlineout.write(' '.join(append_args(newkcmdlineent, installedargs)) + '\n') for loadent in glob.glob(os.path.join(rootdir, 'boot/loader/entries/*.conf')): with open(loadent) as loadentin: currentry = loadentin.read().split('\n') @@ -287,7 +307,7 @@ def fixup(rootdir, vols): newcfgparts.append(cfgpart) else: newcfgparts.append(cfgpart) - loadentout.write(' '.join(newcfgparts + nodeargs) + '\n') + loadentout.write(' '.join(append_args(newcfgparts, installedargs)) + '\n') # the logical volumes this install created, for the defaults below lvmargs = '' for lvdev in (newrootdev, newswapdev): @@ -310,7 +330,7 @@ def fixup(rootdir, vols): ] if not os.path.exists(os.path.join(rootdir, "etc/kernel/cmdline")): with open(os.path.join(rootdir, "etc/kernel/cmdline"), "w") as cmdlineout: - cmdlineout.write("{} root={}{}".format(' '.join(currcmdline + nodeargs), newrootdev, lvmargs)) + cmdlineout.write("{} root={}{}".format(' '.join(append_args(currcmdline, installedargs)), newrootdev, lvmargs)) with open(grubsyscfg, 'w') as defgrubout: for gline in defgrub: gline = gline.split() @@ -328,10 +348,10 @@ def fixup(rootdir, vols): newline.append(ent) else: newline.append(ent) - if nodeargs and newline and newline[0].startswith('GRUB_CMDLINE_LINUX=') and newline[-1].endswith('"'): - newline[-1] = newline[-1][:-1] - newline.extend(nodeargs) - newline[-1] += '"' + if installedargs and newline and newline[0].startswith('GRUB_CMDLINE_LINUX=') and newline[-1].endswith('"'): + # an image captured from an installed node may carry them already + present = ' '.join(newline)[len('GRUB_CMDLINE_LINUX="'):-1].split() + newline = ['GRUB_CMDLINE_LINUX="{}"'.format(' '.join(append_args(present, installedargs)))] defgrubout.write(' '.join(newline) + '\n') grubcfg = subprocess.check_output(['find', os.path.join(rootdir, 'boot'), '-name', 'grub.cfg']).decode('utf8').strip().replace(rootdir, '/').replace('//', '/') grubcfg = grubcfg.split('\n') diff --git a/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py b/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py index 7fd9c11e..765d72c1 100644 --- a/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py +++ b/confluent_osdeploy/ubuntu20.04-diskless/profiles/default/scripts/image2disk.py @@ -104,6 +104,11 @@ def set_machine_id(rootdir): swapdev = '/dev/mapper/localstorage-swap' +def append_args(args, newargs): + # newargs go last, and an argument given twice keeps only its last place + merged = args + newargs + return [arg for idx, arg in enumerate(merged) if arg not in merged[idx + 1:]] + def fixup(rootdir, vols): devbymount = {} for vol in vols: @@ -169,16 +174,31 @@ def fixup(rootdir, vols): grubsyscfg = os.path.join(rootdir, 'etc/sysconfig/grub') if not os.path.exists(grubsyscfg): grubsyscfg = os.path.join(rootdir, 'etc/default/grub') - # deployment.installedargs, which the OS installers append to the - # kernel arguments of the installed system - nodeargs = [] + # installedargs of the profile followed by deployment.installedargs, + # which the OS installers add to the kernel arguments of the installed + # system + profile = None + installedargs = [] with open('/etc/confluent/confluent.deploycfg') as dcfgin: for line in dcfgin: - if line.startswith('installedargs: '): + if line.startswith('profile: '): + profile = line[len('profile: '):].strip() + elif line.startswith('installedargs: '): argstr = line[len('installedargs: '):].strip() if argstr[:1] == "'" and argstr[-1:] == "'": argstr = argstr[1:-1].replace("''", "'") - nodeargs = argstr.split() + installedargs = argstr.split() + if profile: + try: + profileyaml = subprocess.check_output([ + sys.executable, '/opt/confluent/bin/apiclient', + '/confluent-public/os/{}/profile.yaml'.format(profile)]).decode('utf8') + except (subprocess.CalledProcessError, OSError) as e: + sys.stderr.write('Unable to read the installedargs of profile {}: {}\n'.format(profile, e)) + profileyaml = '' + for line in profileyaml.split('\n'): + if line.startswith('installedargs:'): + installedargs = line.split('#', 1)[0][len('installedargs:'):].split() + installedargs with open(grubsyscfg) as defgrubin: defgrub = defgrubin.read().split('\n') with open(grubsyscfg, 'w') as defgrubout: @@ -189,10 +209,10 @@ def fixup(rootdir, vols): if ent.startswith('resume=') or ent.startswith('rd.lvm.lv'): continue newline.append(ent) - if nodeargs and newline and newline[0].startswith('GRUB_CMDLINE_LINUX=') and newline[-1].endswith('"'): - newline[-1] = newline[-1][:-1] - newline.extend(nodeargs) - newline[-1] += '"' + if installedargs and newline and newline[0].startswith('GRUB_CMDLINE_LINUX=') and newline[-1].endswith('"'): + # an image captured from an installed node may carry them already + present = ' '.join(newline)[len('GRUB_CMDLINE_LINUX="'):-1].split() + newline = ['GRUB_CMDLINE_LINUX="{}"'.format(' '.join(append_args(present, installedargs)))] defgrubout.write(' '.join(newline) + '\n') grubcfg = subprocess.check_output(['find', os.path.join(rootdir, 'boot'), '-name', 'grub.cfg']).decode('utf8').strip().replace(rootdir, '/').replace('//', '/') grubcfg = grubcfg.split('\n')