Skip to content

Commit a63ab95

Browse files
himaja-kesariHimaja Kesari
andauthored
Make isPackageInstalled function generic (#487)
<!-- Description: Please provide a summary of the changes and the motivation behind them. --> --- This PR refactors the isPackageInstalled function to be distribution-agnostic Todo - need to delete the old function and update few calls in liveiso.go and cosi common files ### **Checklist** - [ ] Tests added/updated - [ ] Documentation updated (if needed) - [ ] Code conforms to style guidelines Co-authored-by: Himaja Kesari <himajakesari@microsoft.com>
1 parent 6e33eab commit a63ab95

File tree

9 files changed

+58
-11
lines changed

9 files changed

+58
-11
lines changed

toolkit/tools/pkg/imagecustomizerlib/customizeos.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func doOsCustomizations(ctx context.Context, rc *ResolvedConfig, imageConnection
101101
return err
102102
}
103103

104-
verityUpdated, err := enableVerityPartition(ctx, rc.Config.Storage.Verity, imageChroot)
104+
verityUpdated, err := enableVerityPartition(ctx, rc.Config.Storage.Verity, imageChroot, distroHandler)
105105
if err != nil {
106106
return err
107107
}
@@ -118,7 +118,7 @@ func doOsCustomizations(ctx context.Context, rc *ResolvedConfig, imageConnection
118118
return err
119119
}
120120

121-
err = prepareUki(ctx, rc.BuildDirAbs, rc.Config.OS.Uki, imageChroot)
121+
err = prepareUki(ctx, rc.BuildDirAbs, rc.Config.OS.Uki, imageChroot, distroHandler)
122122
if err != nil {
123123
return err
124124
}

toolkit/tools/pkg/imagecustomizerlib/customizeuki.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ type UkiKernelInfo struct {
5858
Initramfs string `json:"initramfs"`
5959
}
6060

61-
func prepareUki(ctx context.Context, buildDir string, uki *imagecustomizerapi.Uki, imageChroot *safechroot.Chroot) error {
61+
func prepareUki(ctx context.Context, buildDir string, uki *imagecustomizerapi.Uki, imageChroot *safechroot.Chroot,
62+
distroHandler distroHandler,
63+
) error {
6264
var err error
6365

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

7375
// Check UKI dependency packages.
74-
err = validateUkiDependencies(imageChroot)
76+
err = validateUkiDependencies(imageChroot, distroHandler)
7577
if err != nil {
7678
return fmt.Errorf("%w:\n%w", ErrUKIPackageDependencyValidation, err)
7779
}
@@ -183,7 +185,7 @@ func prepareUki(ctx context.Context, buildDir string, uki *imagecustomizerapi.Uk
183185
return nil
184186
}
185187

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

toolkit/tools/pkg/imagecustomizerlib/customizeverity.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ const (
6565
DracutModuleScriptFileMode = 0o755
6666
)
6767

68-
func enableVerityPartition(ctx context.Context, verity []imagecustomizerapi.Verity, imageChroot *safechroot.Chroot,
68+
func enableVerityPartition(ctx context.Context, verity []imagecustomizerapi.Verity,
69+
imageChroot *safechroot.Chroot, distroHandler distroHandler,
6970
) (bool, error) {
7071
var err error
7172

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

81-
err = validateVerityDependencies(imageChroot)
82+
err = validateVerityDependencies(imageChroot, distroHandler)
8283
if err != nil {
8384
return false, fmt.Errorf("%w:\n%w", ErrVerityPackageDependencyValidation, err)
8485
}
@@ -435,15 +436,16 @@ func parseSystemdVerityOptions(options string) (imagecustomizerapi.CorruptionOpt
435436
return corruptionOption, hashSigPath, nil
436437
}
437438

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

443444
// Iterate over each required package and check if it's installed.
444445
for _, pkg := range requiredRpms {
445446
logger.Log.Debugf("Checking if package (%s) is installed", pkg)
446-
if !isPackageInstalled(imageChroot, pkg) {
447+
installed := distroHandler.isPackageInstalled(imageChroot, pkg)
448+
if !installed {
447449
return fmt.Errorf("package (%s) is not installed:\nthe following packages must be installed to use Verity: %v", pkg, requiredRpms)
448450
}
449451
}

toolkit/tools/pkg/imagecustomizerlib/distrohandler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type distroHandler interface {
3838
managePackages(ctx context.Context, buildDir string, baseConfigPath string, config *imagecustomizerapi.OS,
3939
imageChroot *safechroot.Chroot, toolsChroot *safechroot.Chroot, rpmsSources []string, useBaseImageRpmRepos bool,
4040
snapshotTime imagecustomizerapi.PackageSnapshotTime) error
41+
42+
isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool
4143
}
4244

4345
// NewDistroHandlerFromTargetOs creates a distro handler directly from TargetOs

toolkit/tools/pkg/imagecustomizerlib/distrohandler_azurelinux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ func (d *azureLinuxDistroHandler) managePackages(ctx context.Context, buildDir s
4444
ctx, buildDir, baseConfigPath, config, imageChroot, toolsChroot, rpmsSources, useBaseImageRpmRepos,
4545
snapshotTime, d.packageManager)
4646
}
47+
48+
// isPackageInstalled implements distroHandler.
49+
func (d *azureLinuxDistroHandler) isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool {
50+
return d.packageManager.isPackageInstalled(imageChroot, packageName)
51+
}

toolkit/tools/pkg/imagecustomizerlib/distrohandler_fedora.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ func (d *fedoraDistroHandler) managePackages(ctx context.Context, buildDir strin
4343
snapshotTime, d.packageManager,
4444
)
4545
}
46+
47+
func (d *fedoraDistroHandler) isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool {
48+
return d.packageManager.isPackageInstalled(imageChroot, packageName)
49+
}

toolkit/tools/pkg/imagecustomizerlib/packagemanager_dnf.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strings"
99

1010
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/logger"
11+
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/safechroot"
12+
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/shell"
1113
)
1214

1315
// DNF Package Manager Implementation
@@ -176,3 +178,14 @@ func (pm *dnfPackageManager) createOutputCallback() func(string) {
176178
}
177179
}
178180
}
181+
182+
func (pm *dnfPackageManager) isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool {
183+
err := imageChroot.UnsafeRun(func() error {
184+
_, _, err := shell.Execute("dnf", "info", "--installed", packageName)
185+
return err
186+
})
187+
if err != nil {
188+
return false
189+
}
190+
return true
191+
}

toolkit/tools/pkg/imagecustomizerlib/packagemanager_rpm.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package imagecustomizerlib
55

6+
import "github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/safechroot"
7+
68
// rpmPackageManagerHandler represents the interface for RPM-based package managers (TDNF, DNF)
79
type rpmPackageManagerHandler interface {
810
// Package manager configuration
@@ -19,4 +21,6 @@ type rpmPackageManagerHandler interface {
1921

2022
// Package manager specific snapshot time support
2123
supportsSnapshotTime() bool
24+
25+
isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool
2226
}

toolkit/tools/pkg/imagecustomizerlib/packagemanager_tdnf.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010

1111
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/logger"
12+
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/safechroot"
13+
"github.com/microsoft/azure-linux-image-tools/toolkit/tools/internal/shell"
1214
)
1315

1416
// TDNF Package Manager Implementation
@@ -101,3 +103,14 @@ func (pm *tdnfPackageManager) createOutputCallback() func(string) {
101103
}
102104
}
103105
}
106+
107+
func (pm *tdnfPackageManager) isPackageInstalled(imageChroot safechroot.ChrootInterface, packageName string) bool {
108+
err := imageChroot.UnsafeRun(func() error {
109+
_, _, err := shell.Execute("tdnf", "info", packageName, "--repo", "@system")
110+
return err
111+
})
112+
if err != nil {
113+
return false
114+
}
115+
return true
116+
}

0 commit comments

Comments
 (0)