Skip to content

Commit db2b848

Browse files
committed
Address comments.
1 parent 223ea98 commit db2b848

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -898,14 +898,10 @@ func isDmVerityEnabled(rawImageFile string) (bool, error) {
898898
}
899899
defer loopback.Close()
900900

901-
var verityEnabled bool
901+
verityEnabled := False
902902

903903
diskPartitions, err := diskutils.GetDiskPartitions(loopback.DevicePath())
904904
if err != nil {
905-
err = loopback.CleanClose()
906-
if err != nil {
907-
return false, fmt.Errorf("failed to cleanly close loopback device:\n%w", err)
908-
}
909905
return false, err
910906
}
911907

toolkit/tools/pkg/imagecustomizerlib/partitionutils.go

+37-12
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ func findPartition(mountIdType imagecustomizerapi.MountIdentifierType, mountId s
350350
func findExtendedPartition(mountIdType ExtendedMountIdentifierType, mountId string,
351351
partitions []diskutils.PartitionInfo, buildDir string,
352352
) (diskutils.PartitionInfo, int, error) {
353-
var devUuid string
353+
var devPartUuid string
354354
if mountIdType == ExtendedMountIdentifierTypeDev {
355355
var err error
356-
devUuid, err = convertDevToUuid(partitions, buildDir)
356+
devPartUuid, err = convertDevToPartUuid(partitions, buildDir)
357357
if err != nil {
358358
return diskutils.PartitionInfo{}, 0, err
359359
}
360-
mountId = devUuid
360+
mountId = devPartUuid
361361
mountIdType = ExtendedMountIdentifierTypePartUuid
362362
}
363363

@@ -392,7 +392,7 @@ func findExtendedPartition(mountIdType ExtendedMountIdentifierType, mountId stri
392392
return partition, partitionIndex, nil
393393
}
394394

395-
func convertDevToUuid(partitions []diskutils.PartitionInfo, buildDir string) (string, error) {
395+
func convertDevToPartUuid(partitions []diskutils.PartitionInfo, buildDir string) (string, error) {
396396
for _, partition := range partitions {
397397
matches, err := checkExtendedDevPartition(partition, partitions, buildDir)
398398
if err != nil {
@@ -413,12 +413,21 @@ func checkExtendedDevPartition(partition diskutils.PartitionInfo, partitions []d
413413
return false, err
414414
}
415415

416-
verityPartUUID, err := extractVerityRootPartUUID(cmdline)
416+
mountIdType, verityPartitionId, err := extractVerityRootPartitionId(cmdline)
417417
if err != nil {
418418
return false, err
419419
}
420420

421-
return partition.PartUuid == verityPartUUID, nil
421+
switch mountIdType {
422+
case ExtendedMountIdentifierTypePartUuid:
423+
return partition.PartUuid == verityPartitionId, nil
424+
case ExtendedMountIdentifierTypePartLabel:
425+
return partition.PartLabel == verityPartitionId, nil
426+
case ExtendedMountIdentifierTypeUuid:
427+
return partition.Uuid == verityPartitionId, nil
428+
default:
429+
return false, fmt.Errorf("unsupported mount identifier type: (%s)", mountIdType)
430+
}
422431
}
423432

424433
func extractKernelCmdline(partitions []diskutils.PartitionInfo, buildDir string) (string, error) {
@@ -459,7 +468,7 @@ func extractKernelCmdline(partitions []diskutils.PartitionInfo, buildDir string)
459468

460469
err = espPartitionMount.CleanClose()
461470
if err != nil {
462-
return "", fmt.Errorf("failed to close bootPartitionMount:\n%w", err)
471+
return "", fmt.Errorf("failed to close espPartitionMount:\n%w", err)
463472
}
464473

465474
return string(cmdlineContent), nil
@@ -490,20 +499,36 @@ func extractKernelCmdline(partitions []diskutils.PartitionInfo, buildDir string)
490499

491500
err = espPartitionMount.CleanClose()
492501
if err != nil {
493-
return "", fmt.Errorf("failed to close bootPartitionMount:\n%w", err)
502+
return "", fmt.Errorf("failed to close espPartitionMount:\n%w", err)
494503
}
495504

496505
return strings.Join(combinedArgs, " "), nil
497506
}
498507

499-
func extractVerityRootPartUUID(cmdline string) (string, error) {
508+
func extractVerityRootPartitionId(cmdline string) (ExtendedMountIdentifierType, string, error) {
500509
argsParts := strings.Split(cmdline, " ")
501510
for _, part := range argsParts {
502-
if strings.HasPrefix(part, "systemd.verity_root_data=PARTUUID=") {
503-
return strings.TrimPrefix(part, "systemd.verity_root_data=PARTUUID="), nil
511+
if strings.HasPrefix(part, "systemd.verity_root_data=") {
512+
identifier := strings.TrimPrefix(part, "systemd.verity_root_data=")
513+
514+
key, value, found := strings.Cut(identifier, "=")
515+
if !found {
516+
return ExtendedMountIdentifierTypeDefault, "", fmt.Errorf("invalid identifier format in kernel cmdline: %s", identifier)
517+
}
518+
519+
switch key {
520+
case "PARTUUID":
521+
return ExtendedMountIdentifierTypePartUuid, value, nil
522+
case "PARTLABEL":
523+
return ExtendedMountIdentifierTypePartLabel, value, nil
524+
case "UUID":
525+
return ExtendedMountIdentifierTypeUuid, value, nil
526+
default:
527+
return ExtendedMountIdentifierTypeDefault, "", fmt.Errorf("unsupported identifier type in kernel cmdline: %s", identifier)
528+
}
504529
}
505530
}
506-
return "", fmt.Errorf("no verity root PARTUUID found in kernel command-line")
531+
return ExtendedMountIdentifierTypeDefault, "", fmt.Errorf("no verity root identifier found in kernel command-line")
507532
}
508533

509534
func parseSourcePartition(source string) (imagecustomizerapi.MountIdentifierType, string, error) {

0 commit comments

Comments
 (0)