Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSModifier: Add services and modules support in EMU API #11153

Open
wants to merge 4 commits into
base: 3.0-dev
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# Image Customizer
/toolkit/tools/imagecustomizer/ @microsoft/cbl-mariner-imagecustomizer
/toolkit/tools/imagecustomizerapi/ @microsoft/cbl-mariner-imagecustomizer
/toolkit/tools/osmodifier @microsoft/cbl-mariner-imagecustomizer
/toolkit/tools/osmodifier/ @microsoft/cbl-mariner-imagecustomizer
/toolkit/tools/pkg/imagecustomizerlib/ @microsoft/cbl-mariner-imagecustomizer
/toolkit/tools/pkg/osmodifierlib @microsoft/cbl-mariner-imagecustomizer
/toolkit/tools/pkg/osmodifierlib/ @microsoft/cbl-mariner-imagecustomizer
/toolkit/tools/osmodifierapi/ @microsoft/cbl-mariner-imagecustomizer
/toolkit/scripts/build_tag_imagecustomizer.mk @microsoft/cbl-mariner-imagecustomizer
19 changes: 19 additions & 0 deletions toolkit/tools/imagecustomizerapi/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ type Module struct {
Options map[string]string `yaml:"options"`
}

type ModuleList []Module

func (m *ModuleList) IsValid() error {
moduleMap := make(map[string]int)
for i, module := range *m {
// Check if module is duplicated to avoid conflicts with modules potentially having different LoadMode
if _, exists := moduleMap[module.Name]; exists {
return fmt.Errorf("duplicate module found: %s at index %d", module.Name, i)
}
moduleMap[module.Name] = i
err := module.IsValid()
if err != nil {
return fmt.Errorf("invalid modules item at index %d:\n%w", i, err)
}
}

return nil
}

func (m *Module) IsValid() error {
if err := validateModuleName(m.Name); err != nil {
return err
Expand Down
15 changes: 3 additions & 12 deletions toolkit/tools/imagecustomizerapi/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type OS struct {
AdditionalDirs DirConfigList `yaml:"additionalDirs"`
Users []User `yaml:"users"`
Services Services `yaml:"services"`
Modules []Module `yaml:"modules"`
Modules ModuleList `yaml:"modules"`
Overlays *[]Overlay `yaml:"overlays"`
}

Expand Down Expand Up @@ -69,17 +69,8 @@ func (s *OS) IsValid() error {
return err
}

moduleMap := make(map[string]int)
for i, module := range s.Modules {
// Check if module is duplicated to avoid conflicts with modules potentially having different LoadMode
if _, exists := moduleMap[module.Name]; exists {
return fmt.Errorf("duplicate module found: %s at index %d", module.Name, i)
}
moduleMap[module.Name] = i
err = module.IsValid()
if err != nil {
return fmt.Errorf("invalid modules item at index %d:\n%w", i, err)
}
if err := s.Modules.IsValid(); err != nil {
return err
}

if s.Overlays != nil {
Expand Down
8 changes: 4 additions & 4 deletions toolkit/tools/imagecustomizerapi/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestOSIsValidInvalidServices(t *testing.T) {

func TestOSIsValidInvalidModule(t *testing.T) {
os := OS{
Modules: []Module{
Modules: ModuleList{
{
Name: "",
},
Expand All @@ -147,11 +147,11 @@ func TestOSIsValidInvalidModule(t *testing.T) {

func TestOSIsValidModuleDuplicateName(t *testing.T) {
os := OS{
Modules: []Module{
{
Modules: ModuleList{
Module{
Name: "nbd",
},
{
Module{
Name: "nbd",
},
},
Expand Down
18 changes: 14 additions & 4 deletions toolkit/tools/osmodifierapi/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (

elainezhao96 marked this conversation as resolved.
Show resolved Hide resolved
// OS defines how each system present on the image is supposed to be configured.
type OS struct {
Hostname string `yaml:"hostname"`
SELinux imagecustomizerapi.SELinux `yaml:"selinux"`
Users []imagecustomizerapi.User `yaml:"users"`
Overlays *[]Overlay `yaml:"overlays"`
Hostname string `yaml:"hostname"`
SELinux imagecustomizerapi.SELinux `yaml:"selinux"`
Users []imagecustomizerapi.User `yaml:"users"`
Overlays *[]Overlay `yaml:"overlays"`
Services imagecustomizerapi.Services `yaml:"services"`
Modules imagecustomizerapi.ModuleList `yaml:"modules"`
}

func (s *OS) IsValid() error {
Expand Down Expand Up @@ -65,5 +67,13 @@ func (s *OS) IsValid() error {
}
}

if err := s.Services.IsValid(); err != nil {
return err
}

if err := s.Modules.IsValid(); err != nil {
return err
}

return nil
}
4 changes: 2 additions & 2 deletions toolkit/tools/pkg/imagecustomizerlib/customizeos.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func doOsCustomizations(buildDir string, baseConfigPath string, config *imagecus
return err
}

err = enableOrDisableServices(config.OS.Services, imageChroot)
err = EnableOrDisableServices(config.OS.Services, imageChroot)
if err != nil {
return err
}

err = loadOrDisableModules(config.OS.Modules, imageChroot.RootDir())
err = LoadOrDisableModules(config.OS.Modules, imageChroot.RootDir())
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/microsoft/azurelinux/toolkit/tools/internal/systemd"
)

func enableOrDisableServices(services imagecustomizerapi.Services, imageChroot *safechroot.Chroot) error {
func EnableOrDisableServices(services imagecustomizerapi.Services, imageChroot safechroot.ChrootInterface) error {
var err error

// Handle enabling services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
moduleOptionsPath = modprobeConfigDir + "/" + moduleOptionsFileName
)

func loadOrDisableModules(modules []imagecustomizerapi.Module, rootDir string) error {
func LoadOrDisableModules(modules imagecustomizerapi.ModuleList, rootDir string) error {
var err error
var modulesToLoad []string
var modulesToDisable []string
Expand Down
31 changes: 16 additions & 15 deletions toolkit/tools/pkg/imagecustomizerlib/kernelmoduleutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ import (

func TestLoadOrDisableModules(t *testing.T) {
rootDir := filepath.Join(tmpDir, "TestLoadOrDisableModules")
modules := []imagecustomizerapi.Module{
{
modules := imagecustomizerapi.ModuleList{
imagecustomizerapi.Module{
Name: "module1",
LoadMode: imagecustomizerapi.ModuleLoadModeAlways,
Options: map[string]string{"option1": "value1"},
},
{
imagecustomizerapi.Module{
Name: "module2",
LoadMode: imagecustomizerapi.ModuleLoadModeDisable,
},
{
imagecustomizerapi.Module{
Name: "module3",
LoadMode: imagecustomizerapi.ModuleLoadModeAuto,
Options: map[string]string{"option3_1": "value3_1", "option3_2": "value3_2"},
},
}

err := loadOrDisableModules(modules, rootDir)
err := LoadOrDisableModules(modules, rootDir)
assert.NoError(t, err)

moduleLoadFilePath := filepath.Join(rootDir, moduleLoadPath)
Expand Down Expand Up @@ -61,29 +61,30 @@ func TestLoadOrDisableModules(t *testing.T) {
assert.Contains(t, string(moduleOptionsContent), "option3_2=value3_2")

// Test add options for module2 which was disabled
modules = []imagecustomizerapi.Module{
{
modules = imagecustomizerapi.ModuleList{
imagecustomizerapi.Module{
Name: "module2",
Options: map[string]string{"option2": "value2"},
},
}

err = loadOrDisableModules(modules, rootDir)
err = LoadOrDisableModules(modules, rootDir)
assert.Contains(t, err.Error(), "cannot add options for disabled module (module2)")

// Test updating module2's loadmode and module3's option
modules = []imagecustomizerapi.Module{
{
modules = imagecustomizerapi.ModuleList{
imagecustomizerapi.Module{
Name: "module2",
LoadMode: imagecustomizerapi.ModuleLoadModeAuto,
Options: map[string]string{"option2": "value2"},
},
{
imagecustomizerapi.Module{
Name: "module3",
Options: map[string]string{"option3_1": "new_value3_1", "option3_3": "new_value3_3"},
},
}
err = loadOrDisableModules(modules, rootDir)

err = LoadOrDisableModules(modules, rootDir)
assert.NoError(t, err)

moduleDisableContent, _ = os.ReadFile(moduleDisableFilePath)
Expand All @@ -96,15 +97,15 @@ func TestLoadOrDisableModules(t *testing.T) {
assert.Contains(t, string(moduleOptionsContent), "option3_3=new_value3_3")

// Test case where a module was already set to load at boot
modules = []imagecustomizerapi.Module{
{
modules = imagecustomizerapi.ModuleList{
imagecustomizerapi.Module{
Name: "module1",
LoadMode: imagecustomizerapi.ModuleLoadModeAlways,
Options: map[string]string{"option1": "value1"},
},
}

err = loadOrDisableModules(modules, rootDir)
err = LoadOrDisableModules(modules, rootDir)
assert.NoError(t, err)

moduleLoadContent, _ = os.ReadFile(moduleLoadFilePath)
Expand Down
10 changes: 10 additions & 0 deletions toolkit/tools/pkg/osmodifierlib/modifierutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ func doModifications(baseConfigPath string, osConfig *osmodifierapi.OS) error {
return err
}

err = imagecustomizerlib.EnableOrDisableServices(osConfig.Services, dummyChroot)
if err != nil {
return err
}

err = imagecustomizerlib.LoadOrDisableModules(osConfig.Modules, dummyChroot.RootDir())
if err != nil {
return err
}

if osConfig.Overlays != nil {
bootCustomizer, err := imagecustomizerlib.NewBootCustomizer(dummyChroot)
if err != nil {
Expand Down
Loading