Skip to content

Commit 223ea98

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

File tree

2 files changed

+63
-63
lines changed

2 files changed

+63
-63
lines changed

toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -892,24 +892,29 @@ func customizeVerityImageHelper(buildDir string, baseConfigPath string, config *
892892
}
893893

894894
func isDmVerityEnabled(rawImageFile string) (bool, error) {
895-
logger.Log.Debugf("Check if dm-verity is enabled in base image")
896-
897895
loopback, err := safeloopback.NewLoopback(rawImageFile)
898896
if err != nil {
899-
return false, fmt.Errorf("failed to check if dm-verity is enabled in base image:\n%w", err)
897+
return false, fmt.Errorf("failed to create a loopback device for checking if dm-verity is enabled on the base image:\n%w", err)
900898
}
901899
defer loopback.Close()
902900

901+
var verityEnabled bool
902+
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+
}
905909
return false, err
906910
}
907911

908912
for i := range diskPartitions {
909913
diskPartition := diskPartitions[i]
910914

911915
if diskPartition.FileSystemType == "DM_verity_hash" {
912-
return true, nil
916+
verityEnabled = true
917+
break
913918
}
914919
}
915920

@@ -918,7 +923,7 @@ func isDmVerityEnabled(rawImageFile string) (bool, error) {
918923
return false, fmt.Errorf("failed to cleanly close loopback device:\n%w", err)
919924
}
920925

921-
return false, nil
926+
return verityEnabled, nil
922927
}
923928

924929
func warnOnLowFreeSpace(buildDir string, imageConnection *ImageConnection) {

toolkit/tools/pkg/imagecustomizerlib/partitionutils.go

+53-58
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,6 @@ func findSourcePartition(source string, partitions []diskutils.PartitionInfo,
308308

309309
func findSourcePartitionHelper(source string, partitions []diskutils.PartitionInfo,
310310
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,
323311
) (ExtendedMountIdentifierType, diskutils.PartitionInfo, int, error) {
324312
mountIdType, mountId, err := parseExtendedSourcePartition(source)
325313
if err != nil {
@@ -369,9 +357,8 @@ func findExtendedPartition(mountIdType ExtendedMountIdentifierType, mountId stri
369357
if err != nil {
370358
return diskutils.PartitionInfo{}, 0, err
371359
}
372-
// Replace the original mountId with the UUID type.
373360
mountId = devUuid
374-
mountIdType = ExtendedMountIdentifierTypeUuid
361+
mountIdType = ExtendedMountIdentifierTypePartUuid
375362
}
376363

377364
matchedPartitionIndexes := []int(nil)
@@ -412,103 +399,111 @@ func convertDevToUuid(partitions []diskutils.PartitionInfo, buildDir string) (st
412399
return "", err
413400
}
414401
if matches {
415-
return partition.Uuid, nil
402+
return partition.PartUuid, nil
416403
}
417404
}
418-
return "", fmt.Errorf("unable to find UUID for /dev path")
405+
return "", fmt.Errorf("unable to find PARTUUID for /dev path")
419406
}
420407

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.
426408
func checkExtendedDevPartition(partition diskutils.PartitionInfo, partitions []diskutils.PartitionInfo,
427409
buildDir string,
428410
) (bool, error) {
429-
espPartition, err := findSystemBootPartition(partitions)
411+
cmdline, err := extractKernelCmdline(partitions, buildDir)
430412
if err != nil {
431413
return false, err
432414
}
433415

434-
bootPartition, err := findBootPartitionFromEsp(espPartition, partitions, buildDir)
416+
verityPartUUID, err := extractVerityRootPartUUID(cmdline)
435417
if err != nil {
436418
return false, err
437419
}
438420

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+
440435
tmpDirEsp := filepath.Join(buildDir, tmpEspPartitionDirName)
441436
espPartitionMount, err := safemount.NewMount(espPartition.Path, tmpDirEsp, espPartition.FileSystemType, unix.MS_RDONLY, "", true)
442437
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)
444439
}
445440
defer espPartitionMount.Close()
446441

447-
// Check if there is any UKI images.
448442
espLinuxPath := filepath.Join(tmpDirEsp, UkiOutputDir)
449443
ukiFiles, err := filepath.Glob(filepath.Join(espLinuxPath, "vmlinuz-*.efi"))
450444
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)
452446
}
453447

454448
if len(ukiFiles) > 0 {
455-
// Dump kernel cmdline args from an UKI.
456449
cmdlinePath := filepath.Join(buildDir, "cmdline.txt")
457450
_, _, err := shell.Execute("objcopy", "--dump-section", ".cmdline="+cmdlinePath, ukiFiles[0])
458451
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)
460453
}
461454

462455
cmdlineContent, err := os.ReadFile(cmdlinePath)
463456
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)
465458
}
466459

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()
472461
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)
474463
}
475-
defer bootPartitionMount.Close()
476464

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+
}
482467

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()
488474

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)
493489
}
494490

495491
err = espPartitionMount.CleanClose()
496492
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)
498494
}
499495

500-
return false, nil
496+
return strings.Join(combinedArgs, " "), nil
501497
}
502498

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, " ")
505501
for _, part := range argsParts {
506502
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
509504
}
510505
}
511-
return false, nil
506+
return "", fmt.Errorf("no verity root PARTUUID found in kernel command-line")
512507
}
513508

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

0 commit comments

Comments
 (0)