From 63a2e5648b250699f02aa1d57fabc73267739ac1 Mon Sep 17 00:00:00 2001 From: Rhys Oxenham Date: Wed, 6 Nov 2024 14:04:23 +0000 Subject: [PATCH] Adding better handling for kernel cmdline arguments --- RELEASE_NOTES.md | 5 +++ pkg/build/grub.go | 6 ++-- pkg/build/iso.go | 4 +++ .../templates/grub/guestfish-snippet.tpl | 5 +++ pkg/build/templates/rebuild-iso.sh.tpl | 31 +++++++++++++++++-- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 66e7c885..01935fe7 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -4,6 +4,10 @@ ## General +* Adds better handling for user-supplied kernel cmdline arguments: + * Removes duplicates found between default and user-supplied arguments in all scenarios (RAW cmdline, ISO cmdline, and `kexec` config.bootoptions). + * Removes the reliance on `rd.kiwi.install.pass.bootparam` to pull user-supplied arguments post-`kexec` (with SelfInstall ISO). + ## API ### Image Definition Changes @@ -14,6 +18,7 @@ * [#593](https://github.com/suse-edge/edge-image-builder/issues/593) - OS files script should mount /var * [#594](https://github.com/suse-edge/edge-image-builder/issues/594) - Package install breaks package resolution if packages is already installed on root OS +* [#604](https://github.com/suse-edge/edge-image-builder/issues/604) - Handle kernel arguments better, especially when duplicated --- diff --git a/pkg/build/grub.go b/pkg/build/grub.go index a05bcf63..323ca23d 100644 --- a/pkg/build/grub.go +++ b/pkg/build/grub.go @@ -27,9 +27,11 @@ func (b *Builder) generateGRUBGuestfishCommands() (string, error) { argLine := strings.Join(b.context.ImageDefinition.OperatingSystem.KernelArgs, " ") values := struct { - KernelArgs string + KernelArgs string + KernelArgsList []string }{ - KernelArgs: argLine, + KernelArgs: argLine, + KernelArgsList: b.context.ImageDefinition.OperatingSystem.KernelArgs, } snippet, err := template.Parse("guestfish-snippet", guestfishSnippet, values) diff --git a/pkg/build/iso.go b/pkg/build/iso.go index d5d7f903..0e9ebe6b 100644 --- a/pkg/build/iso.go +++ b/pkg/build/iso.go @@ -110,6 +110,8 @@ func (b *Builder) writeIsoScript(templateContents, outputFilename string) error ArtefactsDir string InstallDevice string KernelArgs string + KernelArgsList []string + Arch string }{ IsoExtractDir: isoExtractPath, RawExtractDir: rawExtractPath, @@ -119,6 +121,8 @@ func (b *Builder) writeIsoScript(templateContents, outputFilename string) error ArtefactsDir: b.context.ArtefactsDir, InstallDevice: b.context.ImageDefinition.OperatingSystem.IsoConfiguration.InstallDevice, KernelArgs: argLine, + KernelArgsList: b.context.ImageDefinition.OperatingSystem.KernelArgs, + Arch: string(b.context.ImageDefinition.Image.Arch), } contents, err := template.Parse("iso-script", templateContents, arguments) diff --git a/pkg/build/templates/grub/guestfish-snippet.tpl b/pkg/build/templates/grub/guestfish-snippet.tpl index db53ab9e..f49a08bc 100644 --- a/pkg/build/templates/grub/guestfish-snippet.tpl +++ b/pkg/build/templates/grub/guestfish-snippet.tpl @@ -1,6 +1,11 @@ # Configure GRUB defaults # - So that the update below, and later`transactional-update grub.cfg` will persist the changes download /etc/default/grub /tmp/grub +# Remove original kernel arguments that match desired kernelArgs that have values +{{ range .KernelArgsList -}} +! sed -i "/^GRUB_CMDLINE_LINUX_DEFAULT/ s/$(echo {{ . }} | cut -f1 -d"=")=[^=]//" /tmp/grub +{{ end -}} +# Add in the new ones ! sed -i '/^GRUB_CMDLINE_LINUX_DEFAULT="/ s/"$/ {{.KernelArgs}} "/' /tmp/grub upload /tmp/grub /etc/default/grub diff --git a/pkg/build/templates/rebuild-iso.sh.tpl b/pkg/build/templates/rebuild-iso.sh.tpl index 60babd4c..be1b481e 100644 --- a/pkg/build/templates/rebuild-iso.sh.tpl +++ b/pkg/build/templates/rebuild-iso.sh.tpl @@ -35,9 +35,33 @@ echo -e "set timeout=3\nset timeout_style=menu\n$(cat ${ISO_EXTRACT_DIR}/boot/gr sed -i '/root=install:CDLABEL=INSTALL/ s|$| rd.kiwi.oem.installdevice={{.InstallDevice}} |' ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg {{ end -}} -# Ensure that kernel arguments are appended to ISO grub.cfg so they are applied to firstboot via kexec {{ if (gt (len .KernelArgs) 0) -}} -sed -i '/root=install:CDLABEL=INSTALL/ s|$| rd.kiwi.install.pass.bootparam {{.KernelArgs}} |' ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg +# Remove all original kernel arguments from ISO command line that match input kernelArgs *and* have values +{{ range .KernelArgsList -}} +value=$(echo {{ . }} | cut -f1 -d"=") +sed -i "s/$value=[^=]//" ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg +{{ end -}} + +# Unpack the initrd from the SelfInstall ISO and copy the early microcode into new initrd +mkdir -p ${ISO_EXTRACT_DIR}/temp-initram/early/ ${ISO_EXTRACT_DIR}/temp-initram/main/ +cp ${ISO_EXTRACT_DIR}/boot/{{ .Arch }}/loader/initrd ${ISO_EXTRACT_DIR}/temp-initram/ +cd ${ISO_EXTRACT_DIR}/temp-initram/early && lsinitrd --unpackearly ${ISO_EXTRACT_DIR}/temp-initram/initrd +find . -print0 | cpio --null --create --format=newc > ${ISO_EXTRACT_DIR}/temp-initram/new-initrd +# NOTE: We pipe the following command to true to avoid issues with mknod failing when unprivileged +cd ${ISO_EXTRACT_DIR}/temp-initram/main && lsinitrd --unpack ${ISO_EXTRACT_DIR}/temp-initram/initrd || true + +# Remove the original kernel arguments from initrd config that match input kernelArgs and add desired ones +{{ range .KernelArgsList -}} +value=$(echo {{ . }} | cut -f1 -d"=") +sed -i "s/$value=[^=]//" config.bootoptions +{{ end -}} +sed -i '1s|$| {{ .KernelArgs }}|' config.bootoptions + +# Repack the contents of the initrd into the new file, including the new kernel cmdline arguments +find . | cpio --create --format=newc >> ${ISO_EXTRACT_DIR}/temp-initram/new-initrd + +# Add the desired kernel cmdline arguments to the ISO kernel cmdline so they're available during deployment +sed -i '/root=install:CDLABEL=INSTALL/ s|$| {{.KernelArgs}} |' ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg {{ end -}} cd ${RAW_EXTRACT_DIR} @@ -51,5 +75,8 @@ xorriso -indev ${ISO_SOURCE} \ -map ${ARTEFACTS_DIR} /artefacts \ {{- if .InstallDevice }} -map ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg /boot/grub2/grub.cfg \ +{{- end }} +{{- if (gt (len .KernelArgs) 0) }} + -map ${ISO_EXTRACT_DIR}/temp-initram/new-initrd /boot/{{ .Arch }}/loader/initrd \ {{- end }} -boot_image any replay -changes_pending yes