@@ -17,7 +17,6 @@ import (
1717
1818 "github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagecustomizerapi"
1919 "github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagegen/diskutils"
20- "github.com/microsoft/azure-linux-image-tools/toolkit/tools/imagegen/installutils"
2120 "github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/file"
2221 "github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/grub"
2322 "github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/imageconnection"
@@ -32,12 +31,18 @@ import (
3231)
3332
3433var (
35- bootPartitionRegex = regexp .MustCompile (`(?m)^search -n -u ([a-zA-Z0-9\-]+) -s$` )
34+ bootPartitionRegexAzl3 = regexp .MustCompile (`(?m)^[\t ]*search\s+-n\s+-u\s+([a-zA-Z0-9\-]+)\s+-s\s*$` )
35+ bootPartitionRegexAzl4 = regexp .MustCompile (`(?m)^[\t ]*search\s+--fs-uuid\s+--set=root\s+([a-zA-Z0-9\-]+)\s*$` )
3636
3737 // Extract the partition number from the loopback partition path.
3838 partitionNumberRegex = regexp .MustCompile (`^/dev/loop\d+p(\d+)$` )
3939)
4040
41+ const (
42+ espGrubCfgPathAzl3 = "boot/grub2/grub.cfg"
43+ espGrubCfgPathAzl4 = "EFI/fedora/grub.cfg"
44+ )
45+
4146const (
4247 // BtrfsTopLevelSubvolumeId is the ID of the top-level subvolume in a BTRFS filesystem.
4348 // This is BTRFS_FS_TREE_OBJECTID, the objectid that refers to the global FS_TREE root.
@@ -76,7 +81,9 @@ func findSystemBootPartition(diskPartitions []diskutils.PartitionInfo) (*diskuti
7681 return bootPartition , nil
7782}
7883
79- func findBootPartitionFromEsp (efiSystemPartition * diskutils.PartitionInfo , diskPartitions []diskutils.PartitionInfo , buildDir string ) (* diskutils.PartitionInfo , error ) {
84+ func findBootPartitionFromEsp (efiSystemPartition * diskutils.PartitionInfo , diskPartitions []diskutils.PartitionInfo ,
85+ buildDir string , distroHandler DistroHandler ,
86+ ) (* diskutils.PartitionInfo , error ) {
8087 tmpDir := filepath .Join (buildDir , tmpEspPartitionDirName )
8188
8289 // Mount the EFI System Partition.
@@ -86,9 +93,7 @@ func findBootPartitionFromEsp(efiSystemPartition *diskutils.PartitionInfo, diskP
8693 }
8794 defer efiSystemPartitionMount .Close ()
8895
89- // Read the grub.cfg file.
90- grubConfigFilePath := filepath .Join (tmpDir , installutils .FedoraGrubCfgFile )
91- grubConfigFile , err := os .ReadFile (grubConfigFilePath )
96+ bootPartitionUuid , err := distroHandler .FindBootPartitionUuidFromEsp (tmpDir )
9297 if err != nil {
9398 return nil , fmt .Errorf ("failed to read EFI system partition's grub.cfg file:\n %w" , err )
9499 }
@@ -99,14 +104,6 @@ func findBootPartitionFromEsp(efiSystemPartition *diskutils.PartitionInfo, diskP
99104 return nil , fmt .Errorf ("failed to close EFI system partition mount:\n %w" , err )
100105 }
101106
102- // Look for the bootloader partition declaration line in the grub.cfg file.
103- match := bootPartitionRegex .FindStringSubmatch (string (grubConfigFile ))
104- if match == nil {
105- return nil , fmt .Errorf ("failed to find boot partition in grub.cfg file" )
106- }
107-
108- bootPartitionUuid := match [1 ]
109-
110107 var bootPartition * diskutils.PartitionInfo
111108 for i := range diskPartitions {
112109 diskPartition := diskPartitions [i ]
@@ -124,6 +121,23 @@ func findBootPartitionFromEsp(efiSystemPartition *diskutils.PartitionInfo, diskP
124121 return bootPartition , nil
125122}
126123
124+ // readBootPartitionUuidFromGrubCfg reads the grub.cfg file at grubConfigFilePath and
125+ // returns the UUID captured by bootPartitionRegex's first capture group.
126+ func readBootPartitionUuidFromGrubCfg (grubConfigFilePath string , bootPartitionRegex * regexp.Regexp ,
127+ ) (string , error ) {
128+ grubConfigFile , err := os .ReadFile (grubConfigFilePath )
129+ if err != nil {
130+ return "" , fmt .Errorf ("failed to read EFI system partition's grub.cfg file (%s):\n %w" , grubConfigFilePath , err )
131+ }
132+
133+ match := bootPartitionRegex .FindStringSubmatch (string (grubConfigFile ))
134+ if match == nil {
135+ return "" , fmt .Errorf ("failed to find boot partition in grub.cfg file (%s)" , grubConfigFilePath )
136+ }
137+
138+ return match [1 ], nil
139+ }
140+
127141// Searches for the partition that contains the /etc/fstab file.
128142// While technically it is possible to place /etc on a different partition, doing so is fairly difficult and requires
129143// a custom initramfs module.
0 commit comments