@@ -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.
132176func setLinuxOrInitrdPathAll (inputGrubCfgContent string , commandName string , filePath string , allowMultiple bool ) (outputGrubCfgContent string , oldFilePaths []string , err error ) {
133177 quotedFilePath := grub .QuoteString (filePath )
0 commit comments