@@ -347,15 +347,16 @@ func findPartition(mountIdType imagecustomizerapi.MountIdentifierType, mountId s
347
347
func findExtendedPartition (mountIdType ExtendedMountIdentifierType , mountId string ,
348
348
partitions []diskutils.PartitionInfo , buildDir string ,
349
349
) (diskutils.PartitionInfo , int , error ) {
350
- var devPartUuid string
351
350
if mountIdType == ExtendedMountIdentifierTypeDev {
352
- var err error
353
- devPartUuid , err = convertDevToPartUuid (partitions , buildDir )
351
+ cmdline , err := extractKernelCmdline (partitions , buildDir )
352
+ if err != nil {
353
+ return diskutils.PartitionInfo {}, 0 , err
354
+ }
355
+
356
+ mountIdType , mountId , err = extractVerityRootPartitionId (cmdline )
354
357
if err != nil {
355
358
return diskutils.PartitionInfo {}, 0 , err
356
359
}
357
- mountId = devPartUuid
358
- mountIdType = ExtendedMountIdentifierTypePartUuid
359
360
}
360
361
361
362
matchedPartitionIndexes := []int (nil )
@@ -389,44 +390,6 @@ func findExtendedPartition(mountIdType ExtendedMountIdentifierType, mountId stri
389
390
return partition , partitionIndex , nil
390
391
}
391
392
392
- func convertDevToPartUuid (partitions []diskutils.PartitionInfo , buildDir string ) (string , error ) {
393
- for _ , partition := range partitions {
394
- matches , err := checkExtendedDevPartition (partition , partitions , buildDir )
395
- if err != nil {
396
- return "" , err
397
- }
398
- if matches {
399
- return partition .PartUuid , nil
400
- }
401
- }
402
- return "" , fmt .Errorf ("unable to find PARTUUID for /dev path" )
403
- }
404
-
405
- func checkExtendedDevPartition (partition diskutils.PartitionInfo , partitions []diskutils.PartitionInfo ,
406
- buildDir string ,
407
- ) (bool , error ) {
408
- cmdline , err := extractKernelCmdline (partitions , buildDir )
409
- if err != nil {
410
- return false , err
411
- }
412
-
413
- mountIdType , verityPartitionId , err := extractVerityRootPartitionId (cmdline )
414
- if err != nil {
415
- return false , err
416
- }
417
-
418
- switch mountIdType {
419
- case ExtendedMountIdentifierTypePartUuid :
420
- return partition .PartUuid == verityPartitionId , nil
421
- case ExtendedMountIdentifierTypePartLabel :
422
- return partition .PartLabel == verityPartitionId , nil
423
- case ExtendedMountIdentifierTypeUuid :
424
- return partition .Uuid == verityPartitionId , nil
425
- default :
426
- return false , fmt .Errorf ("unsupported mount identifier type: (%s)" , mountIdType )
427
- }
428
- }
429
-
430
393
func extractKernelCmdline (partitions []diskutils.PartitionInfo , buildDir string ) ([]grubConfigLinuxArg , error ) {
431
394
espPartition , err := findSystemBootPartition (partitions )
432
395
if err != nil {
@@ -438,46 +401,31 @@ func extractKernelCmdline(partitions []diskutils.PartitionInfo, buildDir string)
438
401
return nil , fmt .Errorf ("failed to find boot partition: %w" , err )
439
402
}
440
403
441
- tmpDirEsp := filepath .Join (buildDir , tmpEspPartitionDirName )
442
- espPartitionMount , err := safemount .NewMount (espPartition .Path , tmpDirEsp , espPartition .FileSystemType , 0 , "" , true )
443
- if err != nil {
444
- return nil , fmt .Errorf ("failed to mount ESP partition (%s):\n %w" , espPartition .Path , err )
445
- }
446
- defer espPartitionMount .Close ()
447
-
448
- cmdline , err := extractKernelCmdlineFromUki (tmpDirEsp , buildDir )
404
+ cmdline , err := extractKernelCmdlineFromUki (espPartition , buildDir )
449
405
if err == nil {
450
406
return cmdline , nil
451
407
} else if ! errors .Is (err , os .ErrNotExist ) {
452
408
return nil , err
453
409
}
454
410
455
- tmpDirBoot := filepath .Join (buildDir , tmpBootPartitionDirName )
456
- bootPartitionMount , err := safemount .NewMount (bootPartition .Path , tmpDirBoot , bootPartition .FileSystemType , unix .MS_RDONLY , "" , true )
457
- if err != nil {
458
- return nil , fmt .Errorf ("failed to mount boot partition (%s):\n %w" , bootPartition .Path , err )
459
- }
460
- defer bootPartitionMount .Close ()
461
-
462
- cmdline , err = extractKernelCmdlineFromGrub (tmpDirBoot )
411
+ cmdline , err = extractKernelCmdlineFromGrub (bootPartition , buildDir )
463
412
if err != nil {
464
413
return nil , fmt .Errorf ("failed to extract kernel arguments from grub.cfg:\n %w" , err )
465
414
}
466
415
467
- err = bootPartitionMount .CleanClose ()
468
- if err != nil {
469
- return nil , fmt .Errorf ("failed to close bootPartitionMount:\n %w" , err )
470
- }
416
+ return cmdline , nil
417
+ }
471
418
472
- err = espPartitionMount .CleanClose ()
419
+ func extractKernelCmdlineFromUki (espPartition * diskutils.PartitionInfo ,
420
+ buildDir string ,
421
+ ) ([]grubConfigLinuxArg , error ) {
422
+ tmpDirEsp := filepath .Join (buildDir , tmpEspPartitionDirName )
423
+ espPartitionMount , err := safemount .NewMount (espPartition .Path , tmpDirEsp , espPartition .FileSystemType , 0 , "" , true )
473
424
if err != nil {
474
- return nil , fmt .Errorf ("failed to close espPartitionMount :\n %w" , err )
425
+ return nil , fmt .Errorf ("failed to mount ESP partition (%s) :\n %w" , espPartition . Path , err )
475
426
}
427
+ defer espPartitionMount .Close ()
476
428
477
- return cmdline , nil
478
- }
479
-
480
- func extractKernelCmdlineFromUki (tmpDirEsp , buildDir string ) ([]grubConfigLinuxArg , error ) {
481
429
espLinuxPath := filepath .Join (tmpDirEsp , UkiOutputDir )
482
430
ukiFiles , err := filepath .Glob (filepath .Join (espLinuxPath , "vmlinuz-*.efi" ))
483
431
if err != nil {
@@ -509,10 +457,24 @@ func extractKernelCmdlineFromUki(tmpDirEsp, buildDir string) ([]grubConfigLinuxA
509
457
return nil , fmt .Errorf ("failed to parse kernel command-line from UKI: %w" , err )
510
458
}
511
459
460
+ err = espPartitionMount .CleanClose ()
461
+ if err != nil {
462
+ return nil , fmt .Errorf ("failed to close espPartitionMount:\n %w" , err )
463
+ }
464
+
512
465
return args , nil
513
466
}
514
467
515
- func extractKernelCmdlineFromGrub (tmpDirBoot string ) ([]grubConfigLinuxArg , error ) {
468
+ func extractKernelCmdlineFromGrub (bootPartition * diskutils.PartitionInfo ,
469
+ buildDir string ,
470
+ ) ([]grubConfigLinuxArg , error ) {
471
+ tmpDirBoot := filepath .Join (buildDir , tmpBootPartitionDirName )
472
+ bootPartitionMount , err := safemount .NewMount (bootPartition .Path , tmpDirBoot , bootPartition .FileSystemType , unix .MS_RDONLY , "" , true )
473
+ if err != nil {
474
+ return nil , fmt .Errorf ("failed to mount boot partition (%s):\n %w" , bootPartition .Path , err )
475
+ }
476
+ defer bootPartitionMount .Close ()
477
+
516
478
grubCfgPath := filepath .Join (tmpDirBoot , DefaultGrubCfgPath )
517
479
grubCfgContent , err := os .ReadFile (grubCfgPath )
518
480
if err != nil {
@@ -524,6 +486,11 @@ func extractKernelCmdlineFromGrub(tmpDirBoot string) ([]grubConfigLinuxArg, erro
524
486
return nil , fmt .Errorf ("failed to extract kernel command-line arguments: %w" , err )
525
487
}
526
488
489
+ err = bootPartitionMount .CleanClose ()
490
+ if err != nil {
491
+ return nil , fmt .Errorf ("failed to close bootPartitionMount:\n %w" , err )
492
+ }
493
+
527
494
return args , nil
528
495
}
529
496
0 commit comments