Skip to content

Commit 41a1c45

Browse files
committed
Addressed comment
1 parent 71e33e3 commit 41a1c45

File tree

2 files changed

+38
-71
lines changed

2 files changed

+38
-71
lines changed

toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func customizeOSContents(ic *ImageCustomizerParameters) error {
379379
return err
380380
}
381381
if verityEnabled && !ic.customizeOSPartitions {
382-
return fmt.Errorf("dm-verity is enabled on the base image. To customize a verity-enabled base image, the verity section must be reconfigured.")
382+
return fmt.Errorf("dm-verity is enabled on the base image so partitions must be specified.")
383383
}
384384

385385
// Customize the partitions.

toolkit/tools/pkg/imagecustomizerlib/partitionutils.go

+37-70
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,16 @@ func findPartition(mountIdType imagecustomizerapi.MountIdentifierType, mountId s
347347
func findExtendedPartition(mountIdType ExtendedMountIdentifierType, mountId string,
348348
partitions []diskutils.PartitionInfo, buildDir string,
349349
) (diskutils.PartitionInfo, int, error) {
350-
var devPartUuid string
351350
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)
354357
if err != nil {
355358
return diskutils.PartitionInfo{}, 0, err
356359
}
357-
mountId = devPartUuid
358-
mountIdType = ExtendedMountIdentifierTypePartUuid
359360
}
360361

361362
matchedPartitionIndexes := []int(nil)
@@ -389,44 +390,6 @@ func findExtendedPartition(mountIdType ExtendedMountIdentifierType, mountId stri
389390
return partition, partitionIndex, nil
390391
}
391392

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-
430393
func extractKernelCmdline(partitions []diskutils.PartitionInfo, buildDir string) ([]grubConfigLinuxArg, error) {
431394
espPartition, err := findSystemBootPartition(partitions)
432395
if err != nil {
@@ -438,46 +401,31 @@ func extractKernelCmdline(partitions []diskutils.PartitionInfo, buildDir string)
438401
return nil, fmt.Errorf("failed to find boot partition: %w", err)
439402
}
440403

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)
449405
if err == nil {
450406
return cmdline, nil
451407
} else if !errors.Is(err, os.ErrNotExist) {
452408
return nil, err
453409
}
454410

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)
463412
if err != nil {
464413
return nil, fmt.Errorf("failed to extract kernel arguments from grub.cfg:\n%w", err)
465414
}
466415

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

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)
473424
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)
475426
}
427+
defer espPartitionMount.Close()
476428

477-
return cmdline, nil
478-
}
479-
480-
func extractKernelCmdlineFromUki(tmpDirEsp, buildDir string) ([]grubConfigLinuxArg, error) {
481429
espLinuxPath := filepath.Join(tmpDirEsp, UkiOutputDir)
482430
ukiFiles, err := filepath.Glob(filepath.Join(espLinuxPath, "vmlinuz-*.efi"))
483431
if err != nil {
@@ -509,10 +457,24 @@ func extractKernelCmdlineFromUki(tmpDirEsp, buildDir string) ([]grubConfigLinuxA
509457
return nil, fmt.Errorf("failed to parse kernel command-line from UKI: %w", err)
510458
}
511459

460+
err = espPartitionMount.CleanClose()
461+
if err != nil {
462+
return nil, fmt.Errorf("failed to close espPartitionMount:\n%w", err)
463+
}
464+
512465
return args, nil
513466
}
514467

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+
516478
grubCfgPath := filepath.Join(tmpDirBoot, DefaultGrubCfgPath)
517479
grubCfgContent, err := os.ReadFile(grubCfgPath)
518480
if err != nil {
@@ -524,6 +486,11 @@ func extractKernelCmdlineFromGrub(tmpDirBoot string) ([]grubConfigLinuxArg, erro
524486
return nil, fmt.Errorf("failed to extract kernel command-line arguments: %w", err)
525487
}
526488

489+
err = bootPartitionMount.CleanClose()
490+
if err != nil {
491+
return nil, fmt.Errorf("failed to close bootPartitionMount:\n%w", err)
492+
}
493+
527494
return args, nil
528495
}
529496

0 commit comments

Comments
 (0)