Skip to content

Commit 698a967

Browse files
authored
OSModifier: allow two linux cmdline in grub.cfg (#10472)
1 parent 580e2c5 commit 698a967

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

toolkit/tools/pkg/imagecustomizerlib/grubcfgutils.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,50 @@ func FindLinuxLine(inputGrubCfgContent string) (grub.Line, error) {
128128
return lines[0], nil
129129
}
130130

131+
// Find the linux command within non-recovery mode menuentry block in the grub config file.
132+
func FindNonRecoveryLinuxLine(inputGrubCfgContent string) ([]grub.Line, error) {
133+
grubTokens, err := grub.TokenizeConfig(inputGrubCfgContent)
134+
if err != nil {
135+
return nil, err
136+
}
137+
138+
grubLines := grub.SplitTokensIntoLines(grubTokens)
139+
var linuxLines []grub.Line
140+
inMenuEntry := false
141+
isRecoveryMenu := false
142+
143+
// Iterate over all lines to find non-recovery mode menuentry and its linux line
144+
for _, line := range grubLines {
145+
if len(line.Tokens) > 1 && grub.IsTokenKeyword(line.Tokens[0], "menuentry") {
146+
// Found a new 'menuentry', reset flags
147+
inMenuEntry = true
148+
isRecoveryMenu = false
149+
150+
// Check if the title (second token) contains the word 'recovery'
151+
if strings.Contains(line.Tokens[1].RawContent, "recovery") {
152+
isRecoveryMenu = true
153+
}
154+
155+
// If it's a recovery menuentry, ignore this block
156+
if isRecoveryMenu {
157+
inMenuEntry = false
158+
}
159+
} else if inMenuEntry {
160+
// We are inside a non-recovery menuentry block
161+
if len(line.Tokens) > 0 && grub.IsTokenKeyword(line.Tokens[0], "linux") {
162+
// Append only lines that contain the 'linux' command
163+
linuxLines = append(linuxLines, line)
164+
}
165+
}
166+
}
167+
168+
if len(linuxLines) == 0 {
169+
return nil, fmt.Errorf("no linux line found in non-recovery menuentry")
170+
}
171+
172+
return linuxLines, nil
173+
}
174+
131175
// Overrides the path of the kernel binary of all the linux commands within a grub config file.
132176
func setLinuxOrInitrdPathAll(inputGrubCfgContent string, commandName string, filePath string, allowMultiple bool) (outputGrubCfgContent string, oldFilePaths []string, err error) {
133177
quotedFilePath := grub.QuoteString(filePath)

toolkit/tools/pkg/osmodifierlib/modifydefaultgrub.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ func extractValuesFromGrubConfig(imageChroot safechroot.ChrootInterface) ([]stri
6464
return nil, err
6565
}
6666

67-
line, err := imagecustomizerlib.FindLinuxLine(grubCfgContent)
67+
lines, err := imagecustomizerlib.FindNonRecoveryLinuxLine(grubCfgContent)
6868
if err != nil {
6969
return nil, err
7070
}
7171

72-
argTokens, err := imagecustomizerlib.ParseCommandLineArgs(line.Tokens)
72+
if len(lines) != 1 {
73+
return nil, fmt.Errorf("expected 1 non-recovery linux line, found %d", len(lines))
74+
}
75+
76+
argTokens, err := imagecustomizerlib.ParseCommandLineArgs(lines[0].Tokens)
7377
if err != nil {
7478
return nil, err
7579
}

0 commit comments

Comments
 (0)