Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions toolkit/tools/pkg/imagecustomizerlib/customizeos.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func doOsCustomizations(ctx context.Context, rc *ResolvedConfig, imageConnection
return err
}

verityUpdated, err := enableVerityPartition(ctx, rc.Config.Storage.Verity, imageChroot)
verityUpdated, err := enableVerityPartition(ctx, rc.Config.Storage.Verity, imageChroot, distroHandler)
if err != nil {
return err
}
Expand All @@ -116,7 +116,7 @@ func doOsCustomizations(ctx context.Context, rc *ResolvedConfig, imageConnection
return err
}

err = prepareUki(ctx, rc.BuildDirAbs, rc.Config.OS.Uki, imageChroot)
err = prepareUki(ctx, rc.BuildDirAbs, rc.Config.OS.Uki, imageChroot, distroHandler)
if err != nil {
return err
}
Expand Down
14 changes: 9 additions & 5 deletions toolkit/tools/pkg/imagecustomizerlib/customizeuki.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ type UkiKernelInfo struct {
Initramfs string `json:"initramfs"`
}

func prepareUki(ctx context.Context, buildDir string, uki *imagecustomizerapi.Uki, imageChroot *safechroot.Chroot) error {
func prepareUki(ctx context.Context, buildDir string, uki *imagecustomizerapi.Uki, imageChroot *safechroot.Chroot,
distroHandler distroHandler,
) error {
var err error

if uki == nil {
Expand All @@ -71,7 +73,7 @@ func prepareUki(ctx context.Context, buildDir string, uki *imagecustomizerapi.Uk
defer span.End()

// Check UKI dependency packages.
err = validateUkiDependencies(imageChroot)
err = validateUkiDependencies(imageChroot, distroHandler)
if err != nil {
return fmt.Errorf("%w:\n%w", ErrUKIPackageDependencyValidation, err)
}
Expand Down Expand Up @@ -183,7 +185,7 @@ func prepareUki(ctx context.Context, buildDir string, uki *imagecustomizerapi.Uk
return nil
}

func validateUkiDependencies(imageChroot *safechroot.Chroot) error {
func validateUkiDependencies(imageChroot *safechroot.Chroot, distroHandler distroHandler) error {
// The following packages are required for the UKI feature:
// - "systemd-boot": Checked as a package dependency here to ensure installation,
// but additional configuration is handled elsewhere in the UKI workflow.
Expand All @@ -192,8 +194,10 @@ func validateUkiDependencies(imageChroot *safechroot.Chroot) error {
// Iterate over each required package and check if it's installed.
for _, pkg := range requiredRpms {
logger.Log.Debugf("Checking if package (%s) is installed", pkg)
if !isPackageInstalled(imageChroot, pkg) {
return fmt.Errorf("package (%s) is not installed:\nthe following packages must be installed to use Uki: (%v)", pkg, requiredRpms)
installed := distroHandler.isPackageInstalled(imageChroot, pkg)
if !installed {
return fmt.Errorf("package (%s) is not installed:\n"+
"the following packages must be installed to use Uki: (%v)", pkg, requiredRpms)
}
}

Expand Down
10 changes: 6 additions & 4 deletions toolkit/tools/pkg/imagecustomizerlib/customizeverity.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const (
DracutModuleScriptFileMode = 0o755
)

func enableVerityPartition(ctx context.Context, verity []imagecustomizerapi.Verity, imageChroot *safechroot.Chroot,
func enableVerityPartition(ctx context.Context, verity []imagecustomizerapi.Verity,
imageChroot *safechroot.Chroot, distroHandler distroHandler,
) (bool, error) {
var err error

Expand All @@ -78,7 +79,7 @@ func enableVerityPartition(ctx context.Context, verity []imagecustomizerapi.Veri
_, span := otel.GetTracerProvider().Tracer(OtelTracerName).Start(ctx, "enable_verity_partition")
defer span.End()

err = validateVerityDependencies(imageChroot)
err = validateVerityDependencies(imageChroot, distroHandler)
if err != nil {
return false, fmt.Errorf("%w:\n%w", ErrVerityPackageDependencyValidation, err)
}
Expand Down Expand Up @@ -435,15 +436,16 @@ func parseSystemdVerityOptions(options string) (imagecustomizerapi.CorruptionOpt
return corruptionOption, hashSigPath, nil
}

func validateVerityDependencies(imageChroot *safechroot.Chroot) error {
func validateVerityDependencies(imageChroot *safechroot.Chroot, distroHandler distroHandler) error {
// "device-mapper" is required for dm-verity support because it provides "dmsetup",
// which Dracut needs to install the "dm" module (a dependency of "systemd-veritysetup").
requiredRpms := []string{"device-mapper"}

// Iterate over each required package and check if it's installed.
for _, pkg := range requiredRpms {
logger.Log.Debugf("Checking if package (%s) is installed", pkg)
if !isPackageInstalled(imageChroot, pkg) {
installed := distroHandler.isPackageInstalled(imageChroot, pkg)
if !installed {
return fmt.Errorf("package (%s) is not installed:\nthe following packages must be installed to use Verity: %v", pkg, requiredRpms)
}
}
Expand Down
2 changes: 2 additions & 0 deletions toolkit/tools/pkg/imagecustomizerlib/distrohandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type distroHandler interface {
managePackages(ctx context.Context, buildDir string, baseConfigPath string, config *imagecustomizerapi.OS,
imageChroot *safechroot.Chroot, toolsChroot *safechroot.Chroot, rpmsSources []string, useBaseImageRpmRepos bool,
snapshotTime imagecustomizerapi.PackageSnapshotTime) error

isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool
}

// NewDistroHandlerFromTargetOs creates a distro handler directly from TargetOs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ func (d *azureLinuxDistroHandler) managePackages(ctx context.Context, buildDir s
ctx, buildDir, baseConfigPath, config, imageChroot, toolsChroot, rpmsSources, useBaseImageRpmRepos,
snapshotTime, d.packageManager)
}

// isPackageInstalled implements distroHandler.
func (d *azureLinuxDistroHandler) isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool {
return d.packageManager.isPackageInstalled(imageChroot, packageName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ func (d *fedoraDistroHandler) managePackages(ctx context.Context, buildDir strin
snapshotTime, d.packageManager,
)
}

func (d *fedoraDistroHandler) isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool {
return d.packageManager.isPackageInstalled(imageChroot, packageName)
}
13 changes: 13 additions & 0 deletions toolkit/tools/pkg/imagecustomizerlib/packagemanager_dnf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/logger"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/safechroot"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/shell"
)

// DNF Package Manager Implementation
Expand Down Expand Up @@ -176,3 +178,14 @@ func (pm *dnfPackageManager) createOutputCallback() func(string) {
}
}
}

func (pm *dnfPackageManager) isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool {
err := imageChroot.UnsafeRun(func() error {
_, _, err := shell.Execute("dnf", "info", "--installed", packageName)
return err
})
if err != nil {
return false
}
return true
}
4 changes: 4 additions & 0 deletions toolkit/tools/pkg/imagecustomizerlib/packagemanager_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package imagecustomizerlib

import "github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/safechroot"

// rpmPackageManagerHandler represents the interface for RPM-based package managers (TDNF, DNF)
type rpmPackageManagerHandler interface {
// Package manager configuration
Expand All @@ -19,4 +21,6 @@ type rpmPackageManagerHandler interface {

// Package manager specific snapshot time support
supportsSnapshotTime() bool

isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool
}
13 changes: 13 additions & 0 deletions toolkit/tools/pkg/imagecustomizerlib/packagemanager_tdnf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"

"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/logger"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/safechroot"
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/shell"
)

// TDNF Package Manager Implementation
Expand Down Expand Up @@ -101,3 +103,14 @@ func (pm *tdnfPackageManager) createOutputCallback() func(string) {
}
}
}

func (pm *tdnfPackageManager) isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool {
err := imageChroot.UnsafeRun(func() error {
_, _, err := shell.Execute("tdnf", "info", packageName, "--repo", "@system")
return err
})
if err != nil {
return false
}
return true
}
Loading