Skip to content

Commit 2d39a5d

Browse files
committed
Adding better handling for kernel cmdline arguments
1 parent 6a5d7a4 commit 2d39a5d

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

pkg/build/grub.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ func (b *Builder) generateGRUBGuestfishCommands() (string, error) {
2727

2828
argLine := strings.Join(b.context.ImageDefinition.OperatingSystem.KernelArgs, " ")
2929
values := struct {
30-
KernelArgs string
30+
KernelArgs string
31+
KernelArgsList []string
3132
}{
32-
KernelArgs: argLine,
33+
KernelArgs: argLine,
34+
KernelArgsList: b.context.ImageDefinition.OperatingSystem.KernelArgs,
3335
}
3436

3537
snippet, err := template.Parse("guestfish-snippet", guestfishSnippet, values)

pkg/build/iso.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func (b *Builder) writeIsoScript(templateContents, outputFilename string) error
110110
ArtefactsDir string
111111
InstallDevice string
112112
KernelArgs string
113+
KernelArgsList []string
113114
}{
114115
IsoExtractDir: isoExtractPath,
115116
RawExtractDir: rawExtractPath,
@@ -119,6 +120,7 @@ func (b *Builder) writeIsoScript(templateContents, outputFilename string) error
119120
ArtefactsDir: b.context.ArtefactsDir,
120121
InstallDevice: b.context.ImageDefinition.OperatingSystem.IsoConfiguration.InstallDevice,
121122
KernelArgs: argLine,
123+
KernelArgsList: b.context.ImageDefinition.OperatingSystem.KernelArgs,
122124
}
123125

124126
contents, err := template.Parse("iso-script", templateContents, arguments)

pkg/build/templates/grub/guestfish-snippet.tpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Configure GRUB defaults
22
# - So that the update below, and later`transactional-update grub.cfg` will persist the changes
33
download /etc/default/grub /tmp/grub
4+
# Remove original kernel arguments that match desired kernelArgs that have values
5+
{{ range .KernelArgsList -}}
6+
! sed -i "/^GRUB_CMDLINE_LINUX_DEFAULT/ s/$(echo {{ . }} | cut -f1 -d"=")=[^=]//" /tmp/grub
7+
{{ end -}}
8+
# Add in the new ones
49
! sed -i '/^GRUB_CMDLINE_LINUX_DEFAULT="/ s/"$/ {{.KernelArgs}} "/' /tmp/grub
510
upload /tmp/grub /etc/default/grub
611

pkg/build/templates/rebuild-iso.sh.tpl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,33 @@ echo -e "set timeout=3\nset timeout_style=menu\n$(cat ${ISO_EXTRACT_DIR}/boot/gr
3535
sed -i '/root=install:CDLABEL=INSTALL/ s|$| rd.kiwi.oem.installdevice={{.InstallDevice}} |' ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg
3636
{{ end -}}
3737

38-
# Ensure that kernel arguments are appended to ISO grub.cfg so they are applied to firstboot via kexec
3938
{{ if (gt (len .KernelArgs) 0) -}}
40-
sed -i '/root=install:CDLABEL=INSTALL/ s|$| rd.kiwi.install.pass.bootparam {{.KernelArgs}} |' ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg
39+
# Remove all original kernel arguments from ISO command line that match input kernelArgs *and* have values
40+
{{ range .KernelArgsList -}}
41+
value=$(echo {{ . }} | cut -f1 -d"=")
42+
sed -i "s/$value=[^=]//" ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg
43+
{{ end -}}
44+
45+
# Unpack the initrd from the SelfInstall ISO and copy the early microcode into new initrd
46+
mkdir -p ${ISO_EXTRACT_DIR}/temp-initram/early/ ${ISO_EXTRACT_DIR}/temp-initram/main/
47+
cp ${ISO_EXTRACT_DIR}/boot/x86_64/loader/initrd ${ISO_EXTRACT_DIR}/temp-initram/
48+
cd ${ISO_EXTRACT_DIR}/temp-initram/early && lsinitrd --unpackearly ${ISO_EXTRACT_DIR}/temp-initram/initrd
49+
find . -print0 | cpio --null --create --format=newc > ${ISO_EXTRACT_DIR}/temp-initram/new-initrd
50+
# NOTE: We pipe the following command to true to avoid issues with mknod failing when unprivileged
51+
cd ${ISO_EXTRACT_DIR}/temp-initram/main && lsinitrd --unpack ${ISO_EXTRACT_DIR}/temp-initram/initrd || true
52+
53+
# Remove the original kernel arguments from initrd config that match input kernelArgs and add desired ones
54+
{{ range .KernelArgsList -}}
55+
value=$(echo {{ . }} | cut -f1 -d"=")
56+
sed -i "s/$value=[^=]//" config.bootoptions
57+
{{ end -}}
58+
sed -i '1s|$| {{ .KernelArgs }}|' config.bootoptions
59+
60+
# Repack the contents of the initrd into the new file, including the new kernel cmdline arguments
61+
find . | cpio --create --format=newc >> ${ISO_EXTRACT_DIR}/temp-initram/new-initrd
62+
63+
# Add the desired kernel cmdline arguments to the ISO kernel cmdline so they're available during deployment
64+
sed -i '/root=install:CDLABEL=INSTALL/ s|$| {{.KernelArgs}} |' ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg
4165
{{ end -}}
4266

4367
cd ${RAW_EXTRACT_DIR}
@@ -51,5 +75,8 @@ xorriso -indev ${ISO_SOURCE} \
5175
-map ${ARTEFACTS_DIR} /artefacts \
5276
{{- if .InstallDevice }}
5377
-map ${ISO_EXTRACT_DIR}/boot/grub2/grub.cfg /boot/grub2/grub.cfg \
78+
{{- end }}
79+
{{- if (gt (len .KernelArgs) 0) }}
80+
-map ${ISO_EXTRACT_DIR}/temp-initram/new-initrd /boot/x86_64/loader/initrd \
5481
{{- end }}
5582
-boot_image any replay -changes_pending yes

0 commit comments

Comments
 (0)