Skip to content

Commit 62d05cd

Browse files
committed
fix kernel command line parsing for BLS
1 parent 471bd8c commit 62d05cd

2 files changed

Lines changed: 63 additions & 13 deletions

File tree

toolkit/tools/pkg/imagecustomizerlib/customizepartitions_test.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,36 @@ func testCustomizeImageKernelCommandLineHelper(t *testing.T, testName string, ba
323323
}
324324
defer imageConnection.Close()
325325

326-
// Check that the extraCommandLine was added to the grub.cfg file.
327-
grubCfgFilePath := filepath.Join(imageConnection.Chroot().RootDir(), "/boot/grub2/grub.cfg")
328-
grubCfgContents, err := file.Read(grubCfgFilePath)
329-
assert.NoError(t, err, "read grub.cfg file")
330-
assert.Regexp(t, "linux.* console=tty0 console=ttyS0 ", grubCfgContents)
326+
if baseImageInfo.Version == baseImageVersionAzl4 {
327+
// AZL4 uses BLS (Boot Loader Specification) entries instead of inline linux lines in grub.cfg.
328+
blsEntriesDir := filepath.Join(imageConnection.Chroot().RootDir(), "/boot/loader/entries")
329+
blsEntries, err := os.ReadDir(blsEntriesDir)
330+
if !assert.NoError(t, err, "read BLS entries dir") {
331+
return
332+
}
333+
334+
blsOptionsRegex := regexp.MustCompile(`(?m)^options\s+.* console=tty0 console=ttyS0`)
335+
entryCount := 0
336+
for _, entry := range blsEntries {
337+
if !entry.IsDir() && filepath.Ext(entry.Name()) == ".conf" {
338+
entryCount++
339+
blsPath := filepath.Join(blsEntriesDir, entry.Name())
340+
blsContents, readErr := file.Read(blsPath)
341+
if !assert.NoError(t, readErr, "read BLS entry %s", entry.Name()) {
342+
continue
343+
}
344+
assert.Regexp(t, blsOptionsRegex, blsContents,
345+
"BLS entry %s should contain extraCommandLine args", entry.Name())
346+
}
347+
}
348+
assert.GreaterOrEqual(t, entryCount, 1, "BLS entry files in %s", blsEntriesDir)
349+
} else {
350+
// Check that the extraCommandLine was added to the grub.cfg file.
351+
grubCfgFilePath := filepath.Join(imageConnection.Chroot().RootDir(), "/boot/grub2/grub.cfg")
352+
grubCfgContents, err := file.Read(grubCfgFilePath)
353+
assert.NoError(t, err, "read grub.cfg file")
354+
assert.Regexp(t, "linux.* console=tty0 console=ttyS0 ", grubCfgContents)
355+
}
331356
}
332357

333358
func TestCustomizeImageNewUUIDs(t *testing.T) {

toolkit/tools/pkg/imagecustomizerlib/imagecustomizer_test.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path/filepath"
10+
"regexp"
1011
"testing"
1112

1213
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagecustomizerapi"
@@ -652,15 +653,39 @@ func testCustomizeImageKernelCommandLineAddHelper(t *testing.T, testName string,
652653
}
653654
defer imageConnection.Close()
654655

655-
// Read the grub.cfg file.
656-
grub2ConfigFilePath := filepath.Join(imageConnection.Chroot().RootDir(), installutils.FedoraGrubCfgFile)
657-
658-
grub2ConfigFile, err := os.ReadFile(grub2ConfigFilePath)
659-
if !assert.NoError(t, err) {
660-
return
656+
if baseImageInfo.Version == baseImageVersionAzl4 {
657+
// AZL4 uses BLS (Boot Loader Specification) entries instead of inline linux lines in grub.cfg.
658+
blsEntriesDir := filepath.Join(imageConnection.Chroot().RootDir(), "/boot/loader/entries")
659+
blsEntries, err := os.ReadDir(blsEntriesDir)
660+
if !assert.NoError(t, err, "read BLS entries dir") {
661+
return
662+
}
663+
664+
blsOptionsRegex := regexp.MustCompile(`(?m)^options\s+.* console=tty0 console=ttyS0`)
665+
entryCount := 0
666+
for _, entry := range blsEntries {
667+
if !entry.IsDir() && filepath.Ext(entry.Name()) == ".conf" {
668+
entryCount++
669+
blsPath := filepath.Join(blsEntriesDir, entry.Name())
670+
blsContents, readErr := file.Read(blsPath)
671+
if !assert.NoError(t, readErr, "read BLS entry %s", entry.Name()) {
672+
continue
673+
}
674+
assert.Regexp(t, blsOptionsRegex, blsContents,
675+
"BLS entry %s should contain extraCommandLine args", entry.Name())
676+
}
677+
}
678+
assert.GreaterOrEqual(t, entryCount, 1, "BLS entry files in %s", blsEntriesDir)
679+
} else {
680+
// Read the grub.cfg file.
681+
grub2ConfigFilePath := filepath.Join(imageConnection.Chroot().RootDir(), installutils.FedoraGrubCfgFile)
682+
grub2ConfigFile, err := os.ReadFile(grub2ConfigFilePath)
683+
if !assert.NoError(t, err) {
684+
return
685+
}
686+
687+
assert.Regexp(t, `linux\s+.*\s+console=tty0 console=ttyS0\s+`, grub2ConfigFile)
661688
}
662-
663-
assert.Regexp(t, `linux\s+.*\s+console=tty0 console=ttyS0\s+`, grub2ConfigFile)
664689
}
665690

666691
func TestCustomizeImage_OutputImageFileSelection(t *testing.T) {

0 commit comments

Comments
 (0)