Skip to content

Commit 3232c46

Browse files
committed
Add commandline switch output-iso-initrd-self-contained
1 parent 406e557 commit 3232c46

File tree

4 files changed

+47
-46
lines changed

4 files changed

+47
-46
lines changed

toolkit/tools/imagecustomizer/main.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
)
1919

2020
type CustomizeCmd struct {
21-
BuildDir string `name:"build-dir" help:"Directory to run build out of." required:""`
22-
InputImageFile string `name:"image-file" help:"Path of the base Azure Linux image which the customization will be applied to."`
23-
OutputImageFile string `name:"output-image-file" help:"Path to write the customized image to."`
24-
OutputImageFormat string `name:"output-image-format" placeholder:"(vhd|vhd-fixed|vhdx|qcow2|raw|iso|cosi)" help:"Format of output image." enum:"${imageformat}" default:""`
25-
ConfigFile string `name:"config-file" help:"Path of the image customization config file." required:""`
26-
RpmSources []string `name:"rpm-source" help:"Path to a RPM repo config file or a directory containing RPMs."`
27-
DisableBaseImageRpmRepos bool `name:"disable-base-image-rpm-repos" help:"Disable the base image's RPM repos as an RPM source."`
28-
OutputPXEArtifactsDir string `name:"output-pxe-artifacts-dir" help:"Create a directory with customized image PXE booting artifacts. '--output-image-format' must be set to 'iso'."`
21+
BuildDir string `name:"build-dir" help:"Directory to run build out of." required:""`
22+
InputImageFile string `name:"image-file" help:"Path of the base Azure Linux image which the customization will be applied to."`
23+
OutputImageFile string `name:"output-image-file" help:"Path to write the customized image to."`
24+
OutputImageFormat string `name:"output-image-format" placeholder:"(vhd|vhd-fixed|vhdx|qcow2|raw|iso|cosi)" help:"Format of output image." enum:"${imageformat}" default:""`
25+
ConfigFile string `name:"config-file" help:"Path of the image customization config file." required:""`
26+
RpmSources []string `name:"rpm-source" help:"Path to a RPM repo config file or a directory containing RPMs."`
27+
DisableBaseImageRpmRepos bool `name:"disable-base-image-rpm-repos" help:"Disable the base image's RPM repos as an RPM source."`
28+
OutputIsoInitrdSelfContained bool `name:"output-iso-initrd-self-contained" help:"Output a self-conftained initrd for the ISO/PXE scenarios."`
29+
OutputPXEArtifactsDir string `name:"output-pxe-artifacts-dir" help:"Create a directory with customized image PXE booting artifacts. '--output-image-format' must be set to 'iso'."`
2930
}
3031

3132
type InjectFilesCmd struct {
@@ -88,8 +89,8 @@ func main() {
8889

8990
func customizeImage(cmd CustomizeCmd) error {
9091
err := imagecustomizerlib.CustomizeImageWithConfigFile(cmd.BuildDir, cmd.ConfigFile, cmd.InputImageFile,
91-
cmd.RpmSources, cmd.OutputImageFile, cmd.OutputImageFormat, cmd.OutputPXEArtifactsDir,
92-
!cmd.DisableBaseImageRpmRepos)
92+
cmd.RpmSources, cmd.OutputImageFile, cmd.OutputImageFormat, cmd.OutputIsoInitrdSelfContained,
93+
cmd.OutputPXEArtifactsDir, !cmd.DisableBaseImageRpmRepos)
9394
if err != nil {
9495
return err
9596
}

toolkit/tools/pkg/imagecustomizerlib/imagecustomizer.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ type ImageCustomizerParameters struct {
6161
rawImageFile string
6262

6363
// output image
64-
outputImageFormat imagecustomizerapi.ImageFormatType
65-
outputIsIso bool
66-
outputImageFile string
67-
outputImageDir string
68-
outputImageBase string
69-
outputPXEArtifactsDir string
64+
outputImageFormat imagecustomizerapi.ImageFormatType
65+
outputIsIso bool
66+
outputImageFile string
67+
outputImageDir string
68+
outputImageBase string
69+
outputIsoInitrdSelfContained bool
70+
outputPXEArtifactsDir string
7071

7172
imageUuid [UuidSize]byte
7273
imageUuidStr string
@@ -89,9 +90,8 @@ type verityDeviceMetadata struct {
8990

9091
func createImageCustomizerParameters(buildDir string,
9192
inputImageFile string,
92-
configPath string, config *imagecustomizerapi.Config,
93-
useBaseImageRpmRepos bool, rpmsSources []string,
94-
outputImageFormat string, outputImageFile string, outputPXEArtifactsDir string,
93+
configPath string, config *imagecustomizerapi.Config, useBaseImageRpmRepos bool, rpmsSources []string,
94+
outputImageFormat string, outputImageFile string, outputIsoInitrdSelfContained bool, outputPXEArtifactsDir string,
9595
) (*ImageCustomizerParameters, error) {
9696
ic := &ImageCustomizerParameters{}
9797

@@ -155,6 +155,7 @@ func createImageCustomizerParameters(buildDir string,
155155

156156
ic.outputImageBase = strings.TrimSuffix(filepath.Base(ic.outputImageFile), filepath.Ext(ic.outputImageFile))
157157
ic.outputImageDir = filepath.Dir(ic.outputImageFile)
158+
ic.outputIsoInitrdSelfContained = outputIsoInitrdSelfContained
158159
ic.outputPXEArtifactsDir = outputPXEArtifactsDir
159160
ic.outputIsIso = ic.outputImageFormat == imagecustomizerapi.ImageFormatTypeIso
160161

@@ -182,7 +183,7 @@ func createImageCustomizerParameters(buildDir string,
182183
}
183184

184185
func CustomizeImageWithConfigFile(buildDir string, configFile string, inputImageFile string,
185-
rpmsSources []string, outputImageFile string, outputImageFormat string,
186+
rpmsSources []string, outputImageFile string, outputImageFormat string, outputIsoInitrdSelfContained bool,
186187
outputPXEArtifactsDir string, useBaseImageRpmRepos bool,
187188
) error {
188189
var err error
@@ -201,7 +202,7 @@ func CustomizeImageWithConfigFile(buildDir string, configFile string, inputImage
201202
}
202203

203204
err = CustomizeImage(buildDir, absBaseConfigPath, &config, inputImageFile, rpmsSources, outputImageFile, outputImageFormat,
204-
outputPXEArtifactsDir, useBaseImageRpmRepos)
205+
outputIsoInitrdSelfContained, outputPXEArtifactsDir, useBaseImageRpmRepos)
205206
if err != nil {
206207
return err
207208
}
@@ -219,7 +220,7 @@ func cleanUp(ic *ImageCustomizerParameters) error {
219220
}
220221

221222
func CustomizeImage(buildDir string, baseConfigPath string, config *imagecustomizerapi.Config, inputImageFile string,
222-
rpmsSources []string, outputImageFile string, outputImageFormat string,
223+
rpmsSources []string, outputImageFile string, outputImageFormat string, outputIsoInitrdSelfContained bool,
223224
outputPXEArtifactsDir string, useBaseImageRpmRepos bool,
224225
) error {
225226
err := validateConfig(baseConfigPath, config, inputImageFile, rpmsSources, outputImageFile, outputImageFormat, useBaseImageRpmRepos)
@@ -229,7 +230,7 @@ func CustomizeImage(buildDir string, baseConfigPath string, config *imagecustomi
229230

230231
imageCustomizerParameters, err := createImageCustomizerParameters(buildDir, inputImageFile,
231232
baseConfigPath, config, useBaseImageRpmRepos, rpmsSources,
232-
outputImageFormat, outputImageFile, outputPXEArtifactsDir)
233+
outputImageFormat, outputImageFile, outputIsoInitrdSelfContained, outputPXEArtifactsDir)
233234
if err != nil {
234235
return fmt.Errorf("invalid parameters:\n%w", err)
235236
}
@@ -525,13 +526,13 @@ func convertWriteableFormatToOutputImage(ic *ImageCustomizerParameters, inputIso
525526
requestedSELinuxMode = ic.config.OS.SELinux.Mode
526527
}
527528
err := createLiveOSIsoImage(ic.buildDirAbs, ic.configPath, inputIsoArtifacts, requestedSELinuxMode, ic.config.Iso, ic.config.Pxe,
528-
ic.rawImageFile, ic.outputImageFile, ic.outputPXEArtifactsDir)
529+
ic.rawImageFile, ic.outputImageFile, ic.outputIsoInitrdSelfContained, ic.outputPXEArtifactsDir)
529530
if err != nil {
530531
return fmt.Errorf("failed to create LiveOS iso image:\n%w", err)
531532
}
532533
} else {
533534
err := createImageFromUnchangedOS(ic.buildDirAbs, ic.configPath, ic.config.Iso, ic.config.Pxe,
534-
inputIsoArtifacts, ic.outputImageFile, ic.outputPXEArtifactsDir)
535+
inputIsoArtifacts, ic.outputImageFile, ic.outputIsoInitrdSelfContained, ic.outputPXEArtifactsDir)
535536
if err != nil {
536537
return fmt.Errorf("failed to create LiveOS iso image:\n%w", err)
537538
}

toolkit/tools/pkg/imagecustomizerlib/liveosisobuilder.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func populateWriteableRootfsDir(sourceDir, writeableRootfsDir string) error {
3333

3434
func createLiveOSIsoImage(buildDir, baseConfigPath string, inputArtifactsStore *IsoArtifactsStore, requestedSelinuxMode imagecustomizerapi.SELinuxMode,
3535
isoConfig *imagecustomizerapi.Iso, pxeConfig *imagecustomizerapi.Pxe, rawImageFile, outputImagePath string,
36-
outputPXEArtifactsDir string) (err error) {
36+
outputIsoInitrdSelfContained bool, outputPXEArtifactsDir string) (err error) {
3737

3838
var extraCommandLine []string
3939
var additionalIsoFiles imagecustomizerapi.AdditionalFileList
@@ -115,10 +115,8 @@ func createLiveOSIsoImage(buildDir, baseConfigPath string, inputArtifactsStore *
115115
}
116116
}
117117

118-
minimalInitrd := false
119-
120118
// Update grub.cfg
121-
err = updateGrubCfg(minimalInitrd, artifactsStore.files.isoGrubCfgPath, artifactsStore.files.pxeGrubCfgPath, disableSELinux,
119+
err = updateGrubCfg(outputIsoInitrdSelfContained, artifactsStore.files.isoGrubCfgPath, artifactsStore.files.pxeGrubCfgPath, disableSELinux,
122120
updatedSavedConfigs, filepath.Base(outputImagePath))
123121
if err != nil {
124122
return fmt.Errorf("failed to update grub.cfg:\n%w", err)
@@ -134,7 +132,14 @@ func createLiveOSIsoImage(buildDir, baseConfigPath string, inputArtifactsStore *
134132

135133
outputInitrdPath := filepath.Join(artifactsStore.files.artifactsDir, initrdImage)
136134

137-
if minimalInitrd {
135+
if outputIsoInitrdSelfContained {
136+
// Generate the initrd image
137+
err = createInitrdImage(writeableRootfsDir, outputInitrdPath)
138+
if err != nil {
139+
return fmt.Errorf("failed to create initrd image:\n%w", err)
140+
}
141+
artifactsStore.files.initrdImagePath = outputInitrdPath
142+
} else {
138143
// Generate the initrd image
139144
err = createMinimalInitrdImage(writeableRootfsDir, artifactsStore.info.kernelVersion, outputInitrdPath)
140145
if err != nil {
@@ -149,13 +154,6 @@ func createLiveOSIsoImage(buildDir, baseConfigPath string, inputArtifactsStore *
149154
return fmt.Errorf("failed to create squashfs image:\n%w", err)
150155
}
151156
artifactsStore.files.squashfsImagePath = outputSquashfsPath
152-
} else {
153-
// Generate the initrd image
154-
err = createInitrdImage(writeableRootfsDir, outputInitrdPath)
155-
if err != nil {
156-
return fmt.Errorf("failed to create initrd image:\n%w", err)
157-
}
158-
artifactsStore.files.initrdImagePath = outputInitrdPath
159157
}
160158

161159
// Generate the final iso image
@@ -168,7 +166,8 @@ func createLiveOSIsoImage(buildDir, baseConfigPath string, inputArtifactsStore *
168166
}
169167

170168
func createImageFromUnchangedOS(isoBuildDir string, baseConfigPath string, isoConfig *imagecustomizerapi.Iso,
171-
pxeConfig *imagecustomizerapi.Pxe, inputArtifactsStore *IsoArtifactsStore, outputImagePath string, outputPXEArtifactsDir string) error {
169+
pxeConfig *imagecustomizerapi.Pxe, inputArtifactsStore *IsoArtifactsStore, outputImagePath string,
170+
outputIsoInitrdSelfContained bool, outputPXEArtifactsDir string) error {
172171

173172
logger.Log.Infof("Creating LiveOS iso image using unchanged OS partitions")
174173

@@ -207,10 +206,10 @@ func createImageFromUnchangedOS(isoBuildDir string, baseConfigPath string, isoCo
207206
// selinux and not enable it either.
208207
disableSELinux := false
209208

210-
minimalInitrd := true
209+
// george: if outputIsoInitrdSelfContained != previous value -> convert.
211210

212211
// Update grub.cfg
213-
err = updateGrubCfg(minimalInitrd, inputArtifactsStore.files.isoGrubCfgPath, inputArtifactsStore.files.pxeGrubCfgPath, disableSELinux, updatedSavedConfigs, filepath.Base(outputImagePath))
212+
err = updateGrubCfg(outputIsoInitrdSelfContained, inputArtifactsStore.files.isoGrubCfgPath, inputArtifactsStore.files.pxeGrubCfgPath, disableSELinux, updatedSavedConfigs, filepath.Base(outputImagePath))
214213
if err != nil {
215214
return fmt.Errorf("failed to update grub.cfg:\n%w", err)
216215
}

toolkit/tools/pkg/imagecustomizerlib/liveosisogrub.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
liveOSImagePath = "/" + liveOSDir + "/" + liveOSImage
3030
)
3131

32-
func updateGrubCfg(minimalInitrd bool, isoGrubCfgFileName string, pxeGrubCfgFileName string,
32+
func updateGrubCfg(outputIsoInitrdSelfContained bool, isoGrubCfgFileName string, pxeGrubCfgFileName string,
3333
disableSELinux bool, savedConfigs *SavedConfigs, outputImageBase string) error {
3434

3535
inputContentString, err := file.Read(isoGrubCfgFileName)
@@ -79,18 +79,18 @@ func updateGrubCfg(minimalInitrd bool, isoGrubCfgFileName string, pxeGrubCfgFile
7979
}
8080

8181
liveosKernelArgs := ""
82-
if minimalInitrd {
83-
rootValue := fmt.Sprintf(rootValueLiveOSTemplate, isogenerator.DefaultVolumeId)
84-
inputContentString, err = replaceKernelCommandLineArgValueAll(inputContentString, "root", rootValue)
82+
if outputIsoInitrdSelfContained {
83+
inputContentString, err = replaceKernelCommandLineArgValueAll(inputContentString, "root", "/dev/ram0")
8584
if err != nil {
8685
return fmt.Errorf("failed to update the root kernel argument in the iso grub.cfg:\n%w", err)
8786
}
88-
liveosKernelArgs = fmt.Sprintf(kernelArgsLiveOSTemplate, liveOSDir, liveOSImage)
8987
} else {
90-
inputContentString, err = replaceKernelCommandLineArgValueAll(inputContentString, "root", "/dev/ram0")
88+
rootValue := fmt.Sprintf(rootValueLiveOSTemplate, isogenerator.DefaultVolumeId)
89+
inputContentString, err = replaceKernelCommandLineArgValueAll(inputContentString, "root", rootValue)
9190
if err != nil {
9291
return fmt.Errorf("failed to update the root kernel argument in the iso grub.cfg:\n%w", err)
9392
}
93+
liveosKernelArgs = fmt.Sprintf(kernelArgsLiveOSTemplate, liveOSDir, liveOSImage)
9494
}
9595

9696
if disableSELinux {

0 commit comments

Comments
 (0)