Skip to content

Commit

Permalink
Adding better handling for kernel cmdline arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rdoxenham committed Nov 6, 2024
1 parent 6a5d7a4 commit 63a2e56
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

---

Expand Down
6 changes: 4 additions & 2 deletions pkg/build/grub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/build/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pkg/build/templates/grub/guestfish-snippet.tpl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
31 changes: 29 additions & 2 deletions pkg/build/templates/rebuild-iso.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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

0 comments on commit 63a2e56

Please sign in to comment.