Skip to content

Commit cfc9d88

Browse files
committed
Add --build-container to run build pipeline and dnf in a custom container
The automotive project wants to build minimal bootc images which will not contain tools like dnf, mkfs.ext, etc. We support this by allowing the container used in the build pipeline to come from another (but related) container image. This depends on osbuild/images#1507
1 parent b0c50fa commit cfc9d88

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

bib/cmd/bootc-image-builder/image.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const DEFAULT_SIZE = uint64(10 * GibiByte)
3939

4040
type ManifestConfig struct {
4141
// OCI image path (without the transport, that is always docker://)
42-
Imgref string
42+
Imgref string
43+
BuildImgref string
4344

4445
ImageTypes imagetypes.ImageTypes
4546

@@ -57,7 +58,8 @@ type ManifestConfig struct {
5758
DistroDefPaths []string
5859

5960
// Extracted information about the source container image
60-
SourceInfo *source.Info
61+
SourceInfo *source.Info
62+
BuildSourceInfo *source.Info
6163

6264
// RootFSType specifies the filesystem type for the root partition
6365
RootFSType string
@@ -323,16 +325,25 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
323325
Name: c.Imgref,
324326
Local: true,
325327
}
328+
buildContainerSource := container.SourceSpec{
329+
Source: c.BuildImgref,
330+
Name: c.BuildImgref,
331+
Local: true,
332+
}
326333

327334
var customizations *blueprint.Customizations
328335
if c.Config != nil {
329336
customizations = c.Config.Customizations
330337
}
331338

332-
img := image.NewBootcDiskImage(containerSource)
339+
img := image.NewBootcDiskImage(containerSource, buildContainerSource)
333340
img.Users = users.UsersFromBP(customizations.GetUsers())
334341
img.Groups = users.GroupsFromBP(customizations.GetGroups())
335342
img.SELinux = c.SourceInfo.SELinuxPolicy
343+
img.BuildSELinux = img.SELinux
344+
if c.BuildSourceInfo != nil {
345+
img.BuildSELinux = c.BuildSourceInfo.SELinuxPolicy
346+
}
336347

337348
img.KernelOptionsAppend = []string{
338349
"rw",
@@ -410,7 +421,9 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
410421
mf.Distro = manifest.DISTRO_FEDORA
411422
runner := &runner.Linux{}
412423

413-
if err := img.InstantiateManifestFromContainers(&mf, []container.SourceSpec{containerSource}, runner, rng); err != nil {
424+
if err := img.InstantiateManifestFromContainers(&mf,
425+
[]container.SourceSpec{containerSource},
426+
[]container.SourceSpec{buildContainerSource}, runner, rng); err != nil {
414427
return nil, err
415428
}
416429

bib/cmd/bootc-image-builder/main.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ func manifestFromCobra(cmd *cobra.Command, args []string, pbar progress.Progress
203203
rpmCacheRoot, _ := cmd.Flags().GetString("rpmmd")
204204
targetArch, _ := cmd.Flags().GetString("target-arch")
205205
rootFs, _ := cmd.Flags().GetString("rootfs")
206+
buildImgref, _ := cmd.Flags().GetString("build-container")
206207
useLibrepo, _ := cmd.Flags().GetBool("use-librepo")
207208

208209
// If --local was given, warn in the case of --local or --local=true (true is the default), error in the case of --local=false
@@ -297,26 +298,55 @@ func manifestFromCobra(cmd *cobra.Command, args []string, pbar progress.Progress
297298
return nil, nil, err
298299
}
299300

301+
buildContainer := container
302+
buildSourceinfo := sourceinfo
303+
startedBuildContainer := false
304+
defer func() {
305+
if startedBuildContainer {
306+
if err := buildContainer.Stop(); err != nil {
307+
logrus.Warnf("error stopping container: %v", err)
308+
}
309+
}
310+
}()
311+
312+
if buildImgref != "" {
313+
buildContainer, err = podman_container.New(buildImgref)
314+
if err != nil {
315+
return nil, nil, err
316+
}
317+
startedBuildContainer = true
318+
319+
// Gather some data from the containers distro
320+
buildSourceinfo, err = source.LoadInfo(buildContainer.Root())
321+
if err != nil {
322+
return nil, nil, err
323+
}
324+
} else {
325+
buildImgref = imgref
326+
}
327+
300328
// This is needed just for RHEL and RHSM in most cases, but let's run it every time in case
301329
// the image has some non-standard dnf plugins.
302-
if err := container.InitDNF(); err != nil {
330+
if err := buildContainer.InitDNF(); err != nil {
303331
return nil, nil, err
304332
}
305-
solver, err := container.NewContainerSolver(rpmCacheRoot, cntArch, sourceinfo)
333+
solver, err := buildContainer.NewContainerSolver(rpmCacheRoot, cntArch, sourceinfo)
306334
if err != nil {
307335
return nil, nil, err
308336
}
309337

310338
manifestConfig := &ManifestConfig{
311-
Architecture: cntArch,
312-
Config: config,
313-
ImageTypes: imageTypes,
314-
Imgref: imgref,
315-
RootfsMinsize: cntSize * containerSizeToDiskSizeMultiplier,
316-
DistroDefPaths: distroDefPaths,
317-
SourceInfo: sourceinfo,
318-
RootFSType: rootfsType,
319-
UseLibrepo: useLibrepo,
339+
Architecture: cntArch,
340+
Config: config,
341+
ImageTypes: imageTypes,
342+
Imgref: imgref,
343+
BuildImgref: buildImgref,
344+
RootfsMinsize: cntSize * containerSizeToDiskSizeMultiplier,
345+
DistroDefPaths: distroDefPaths,
346+
SourceInfo: sourceinfo,
347+
BuildSourceInfo: buildSourceinfo,
348+
RootFSType: rootfsType,
349+
UseLibrepo: useLibrepo,
320350
}
321351

322352
manifest, repos, err := makeManifest(manifestConfig, solver, rpmCacheRoot)
@@ -656,6 +686,7 @@ func buildCobraCmdline() (*cobra.Command, error) {
656686
}
657687
manifestCmd.Flags().String("rpmmd", "/rpmmd", "rpm metadata cache directory")
658688
manifestCmd.Flags().String("target-arch", "", "build for the given target architecture (experimental)")
689+
manifestCmd.Flags().String("build-container", "", "Use a custom container for the image build")
659690
manifestCmd.Flags().StringArray("type", []string{"qcow2"}, fmt.Sprintf("image types to build [%s]", imagetypes.Available()))
660691
manifestCmd.Flags().Bool("local", true, "DEPRECATED: --local is now the default behavior, make sure to pull the container image before running bootc-image-builder")
661692
if err := manifestCmd.Flags().MarkHidden("local"); err != nil {

0 commit comments

Comments
 (0)