Skip to content

Commit d9e4fce

Browse files
committed
bib: Add --build-container to run the build 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 d410758 commit d9e4fce

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
@@ -285,26 +286,55 @@ func manifestFromCobra(cmd *cobra.Command, args []string, pbar progress.Progress
285286
return nil, nil, err
286287
}
287288

289+
buildContainer := container
290+
buildSourceinfo := sourceinfo
291+
startedBuildContainer := false
292+
defer func() {
293+
if startedBuildContainer {
294+
if err := buildContainer.Stop(); err != nil {
295+
logrus.Warnf("error stopping container: %v", err)
296+
}
297+
}
298+
}()
299+
300+
if buildImgref != "" {
301+
buildContainer, err = podman_container.New(buildImgref)
302+
if err != nil {
303+
return nil, nil, err
304+
}
305+
startedBuildContainer = true
306+
307+
// Gather some data from the containers distro
308+
buildSourceinfo, err = source.LoadInfo(buildContainer.Root())
309+
if err != nil {
310+
return nil, nil, err
311+
}
312+
} else {
313+
buildImgref = imgref
314+
}
315+
288316
// This is needed just for RHEL and RHSM in most cases, but let's run it every time in case
289317
// the image has some non-standard dnf plugins.
290-
if err := container.InitDNF(); err != nil {
318+
if err := buildContainer.InitDNF(); err != nil {
291319
return nil, nil, err
292320
}
293-
solver, err := container.NewContainerSolver(rpmCacheRoot, cntArch, sourceinfo)
321+
solver, err := buildContainer.NewContainerSolver(rpmCacheRoot, cntArch, sourceinfo)
294322
if err != nil {
295323
return nil, nil, err
296324
}
297325

298326
manifestConfig := &ManifestConfig{
299-
Architecture: cntArch,
300-
Config: config,
301-
ImageTypes: imageTypes,
302-
Imgref: imgref,
303-
RootfsMinsize: cntSize * containerSizeToDiskSizeMultiplier,
304-
DistroDefPaths: distroDefPaths,
305-
SourceInfo: sourceinfo,
306-
RootFSType: rootfsType,
307-
UseLibrepo: useLibrepo,
327+
Architecture: cntArch,
328+
Config: config,
329+
ImageTypes: imageTypes,
330+
Imgref: imgref,
331+
BuildImgref: buildImgref,
332+
RootfsMinsize: cntSize * containerSizeToDiskSizeMultiplier,
333+
DistroDefPaths: distroDefPaths,
334+
SourceInfo: sourceinfo,
335+
BuildSourceInfo: buildSourceinfo,
336+
RootFSType: rootfsType,
337+
UseLibrepo: useLibrepo,
308338
}
309339

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

0 commit comments

Comments
 (0)