@@ -308,18 +308,6 @@ func findSourcePartition(source string, partitions []diskutils.PartitionInfo,
308
308
309
309
func findSourcePartitionHelper (source string , partitions []diskutils.PartitionInfo ,
310
310
buildDir string ,
311
- ) (ExtendedMountIdentifierType , diskutils.PartitionInfo , int , error ) {
312
- mountIdType , partition , partitionIndex , err := findExtendedSourcePartitionHelper (source , partitions , buildDir )
313
- if err != nil {
314
- return ExtendedMountIdentifierTypeDefault , diskutils.PartitionInfo {}, 0 , err
315
- }
316
-
317
- return mountIdType , partition , partitionIndex , nil
318
- }
319
-
320
- // findExtendedSourcePartitionHelper extends the public func findSourcePartitionHelper to handle additional identifier types.
321
- func findExtendedSourcePartitionHelper (source string , partitions []diskutils.PartitionInfo ,
322
- buildDir string ,
323
311
) (ExtendedMountIdentifierType , diskutils.PartitionInfo , int , error ) {
324
312
mountIdType , mountId , err := parseExtendedSourcePartition (source )
325
313
if err != nil {
@@ -369,9 +357,8 @@ func findExtendedPartition(mountIdType ExtendedMountIdentifierType, mountId stri
369
357
if err != nil {
370
358
return diskutils.PartitionInfo {}, 0 , err
371
359
}
372
- // Replace the original mountId with the UUID type.
373
360
mountId = devUuid
374
- mountIdType = ExtendedMountIdentifierTypeUuid
361
+ mountIdType = ExtendedMountIdentifierTypePartUuid
375
362
}
376
363
377
364
matchedPartitionIndexes := []int (nil )
@@ -412,103 +399,111 @@ func convertDevToUuid(partitions []diskutils.PartitionInfo, buildDir string) (st
412
399
return "" , err
413
400
}
414
401
if matches {
415
- return partition .Uuid , nil
402
+ return partition .PartUuid , nil
416
403
}
417
404
}
418
- return "" , fmt .Errorf ("unable to find UUID for /dev path" )
405
+ return "" , fmt .Errorf ("unable to find PARTUUID for /dev path" )
419
406
}
420
407
421
- // checkExtendedDevPartition checks whether a given partition is associated with a verity-enabled root partition.
422
- // This function is specific to the case where the partition type is "dev" (device path, e.g., /dev/mapper/root).
423
- // It is an extension of the findExtendedPartition logic and specializes in handling verity partitions,
424
- // supporting both UKI and non-UKI configurations by validating against the kernel cmdline arguments or grub configuration.
425
- // Returns true if the partition matches the expected verity configuration, otherwise false.
426
408
func checkExtendedDevPartition (partition diskutils.PartitionInfo , partitions []diskutils.PartitionInfo ,
427
409
buildDir string ,
428
410
) (bool , error ) {
429
- espPartition , err := findSystemBootPartition (partitions )
411
+ cmdline , err := extractKernelCmdline (partitions , buildDir )
430
412
if err != nil {
431
413
return false , err
432
414
}
433
415
434
- bootPartition , err := findBootPartitionFromEsp ( espPartition , partitions , buildDir )
416
+ verityPartUUID , err := extractVerityRootPartUUID ( cmdline )
435
417
if err != nil {
436
418
return false , err
437
419
}
438
420
439
- // Temporarily mount ESP partition to check for UKIs.
421
+ return partition .PartUuid == verityPartUUID , nil
422
+ }
423
+
424
+ func extractKernelCmdline (partitions []diskutils.PartitionInfo , buildDir string ) (string , error ) {
425
+ espPartition , err := findSystemBootPartition (partitions )
426
+ if err != nil {
427
+ return "" , fmt .Errorf ("failed to find ESP partition: %w" , err )
428
+ }
429
+
430
+ bootPartition , err := findBootPartitionFromEsp (espPartition , partitions , buildDir )
431
+ if err != nil {
432
+ return "" , fmt .Errorf ("failed to find boot partition: %w" , err )
433
+ }
434
+
440
435
tmpDirEsp := filepath .Join (buildDir , tmpEspPartitionDirName )
441
436
espPartitionMount , err := safemount .NewMount (espPartition .Path , tmpDirEsp , espPartition .FileSystemType , unix .MS_RDONLY , "" , true )
442
437
if err != nil {
443
- return false , fmt .Errorf ("failed to mount ESP partition (%s):\n %w" , espPartition .Path , err )
438
+ return "" , fmt .Errorf ("failed to mount ESP partition (%s):\n %w" , espPartition .Path , err )
444
439
}
445
440
defer espPartitionMount .Close ()
446
441
447
- // Check if there is any UKI images.
448
442
espLinuxPath := filepath .Join (tmpDirEsp , UkiOutputDir )
449
443
ukiFiles , err := filepath .Glob (filepath .Join (espLinuxPath , "vmlinuz-*.efi" ))
450
444
if err != nil {
451
- return false , fmt .Errorf ("failed to search for UKI images in ESP partition:\n %w" , err )
445
+ return "" , fmt .Errorf ("failed to search for UKI images in ESP partition:\n %w" , err )
452
446
}
453
447
454
448
if len (ukiFiles ) > 0 {
455
- // Dump kernel cmdline args from an UKI.
456
449
cmdlinePath := filepath .Join (buildDir , "cmdline.txt" )
457
450
_ , _ , err := shell .Execute ("objcopy" , "--dump-section" , ".cmdline=" + cmdlinePath , ukiFiles [0 ])
458
451
if err != nil {
459
- return false , fmt .Errorf ("failed to dump kernel cmdline args from UKI:\n %w" , err )
452
+ return "" , fmt .Errorf ("failed to dump kernel cmdline args from UKI:\n %w" , err )
460
453
}
461
454
462
455
cmdlineContent , err := os .ReadFile (cmdlinePath )
463
456
if err != nil {
464
- return false , fmt .Errorf ("failed to read kernel cmdline args from dumped file: %w" , err )
457
+ return "" , fmt .Errorf ("failed to read kernel cmdline args from dumped file:\n %w" , err )
465
458
}
466
459
467
- return checkVerityRootPartUUID (partition , string (cmdlineContent ))
468
- } else {
469
- // Temporarily mount the boot partition so that the grub config file can be read.
470
- tmpDirBoot := filepath .Join (buildDir , tmpBootPartitionDirName )
471
- bootPartitionMount , err := safemount .NewMount (bootPartition .Path , tmpDirBoot , bootPartition .FileSystemType , unix .MS_RDONLY , "" , true )
460
+ err = espPartitionMount .CleanClose ()
472
461
if err != nil {
473
- return false , fmt .Errorf ("failed to mount boot partition (%s) :\n %w" , bootPartition . Path , err )
462
+ return "" , fmt .Errorf ("failed to close bootPartitionMount :\n %w" , err )
474
463
}
475
- defer bootPartitionMount .Close ()
476
464
477
- grubCfgPath := filepath .Join (tmpDirBoot , "/grub2/grub.cfg" )
478
- kernelToArgs , err := extractKernelToArgsFromGrub (grubCfgPath )
479
- if err != nil {
480
- return false , fmt .Errorf ("failed to extract kernel arguments from grub.cfg: %w" , err )
481
- }
465
+ return string (cmdlineContent ), nil
466
+ }
482
467
483
- for _ , args := range kernelToArgs {
484
- if matches , err := checkVerityRootPartUUID (partition , args ); matches || err != nil {
485
- return matches , err
486
- }
487
- }
468
+ tmpDirBoot := filepath .Join (buildDir , tmpBootPartitionDirName )
469
+ bootPartitionMount , err := safemount .NewMount (bootPartition .Path , tmpDirBoot , bootPartition .FileSystemType , unix .MS_RDONLY , "" , true )
470
+ if err != nil {
471
+ return "" , fmt .Errorf ("failed to mount boot partition (%s):\n %w" , bootPartition .Path , err )
472
+ }
473
+ defer bootPartitionMount .Close ()
488
474
489
- err = bootPartitionMount .CleanClose ()
490
- if err != nil {
491
- return false , fmt .Errorf ("failed to close bootPartitionMount: %w" , err )
492
- }
475
+ grubCfgPath := filepath .Join (tmpDirBoot , "/grub2/grub.cfg" )
476
+ kernelToArgs , err := extractKernelToArgsFromGrub (grubCfgPath )
477
+ if err != nil {
478
+ return "" , fmt .Errorf ("failed to extract kernel arguments from grub.cfg:\n %w" , err )
479
+ }
480
+
481
+ var combinedArgs []string
482
+ for _ , args := range kernelToArgs {
483
+ combinedArgs = append (combinedArgs , args )
484
+ }
485
+
486
+ err = bootPartitionMount .CleanClose ()
487
+ if err != nil {
488
+ return "" , fmt .Errorf ("failed to close bootPartitionMount:\n %w" , err )
493
489
}
494
490
495
491
err = espPartitionMount .CleanClose ()
496
492
if err != nil {
497
- return false , fmt .Errorf ("failed to close bootPartitionMount: %w" , err )
493
+ return "" , fmt .Errorf ("failed to close bootPartitionMount:\n %w" , err )
498
494
}
499
495
500
- return false , nil
496
+ return strings . Join ( combinedArgs , " " ) , nil
501
497
}
502
498
503
- func checkVerityRootPartUUID ( partition diskutils. PartitionInfo , args string ) (bool , error ) {
504
- argsParts := strings .Split (args , " " )
499
+ func extractVerityRootPartUUID ( cmdline string ) (string , error ) {
500
+ argsParts := strings .Split (cmdline , " " )
505
501
for _ , part := range argsParts {
506
502
if strings .HasPrefix (part , "systemd.verity_root_data=PARTUUID=" ) {
507
- extractedMountId := strings .TrimPrefix (part , "systemd.verity_root_data=PARTUUID=" )
508
- return partition .PartUuid == extractedMountId , nil
503
+ return strings .TrimPrefix (part , "systemd.verity_root_data=PARTUUID=" ), nil
509
504
}
510
505
}
511
- return false , nil
506
+ return "" , fmt . Errorf ( "no verity root PARTUUID found in kernel command-line" )
512
507
}
513
508
514
509
func parseSourcePartition (source string ) (imagecustomizerapi.MountIdentifierType , string , error ) {
0 commit comments