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 2d39a5d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
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
2 changes: 2 additions & 0 deletions pkg/build/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (b *Builder) writeIsoScript(templateContents, outputFilename string) error
ArtefactsDir string
InstallDevice string
KernelArgs string
KernelArgsList []string
}{
IsoExtractDir: isoExtractPath,
RawExtractDir: rawExtractPath,
Expand All @@ -119,6 +120,7 @@ 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,
}

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/x86_64/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/x86_64/loader/initrd \
{{- end }}
-boot_image any replay -changes_pending yes

0 comments on commit 2d39a5d

Please # to comment.