Skip to content

Commit 5bae3b6

Browse files
alexlarssonachilleas-k
authored andcommitted
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 ba98e1b commit 5bae3b6

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
@@ -335,16 +337,25 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
335337
Name: c.Imgref,
336338
Local: true,
337339
}
340+
buildContainerSource := container.SourceSpec{
341+
Source: c.BuildImgref,
342+
Name: c.BuildImgref,
343+
Local: true,
344+
}
338345

339346
var customizations *blueprint.Customizations
340347
if c.Config != nil {
341348
customizations = c.Config.Customizations
342349
}
343350

344-
img := image.NewBootcDiskImage(containerSource)
351+
img := image.NewBootcDiskImage(containerSource, buildContainerSource)
345352
img.Users = users.UsersFromBP(customizations.GetUsers())
346353
img.Groups = users.GroupsFromBP(customizations.GetGroups())
347354
img.SELinux = c.SourceInfo.SELinuxPolicy
355+
img.BuildSELinux = img.SELinux
356+
if c.BuildSourceInfo != nil {
357+
img.BuildSELinux = c.BuildSourceInfo.SELinuxPolicy
358+
}
348359

349360
img.KernelOptionsAppend = []string{
350361
"rw",
@@ -422,7 +433,9 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
422433
mf.Distro = manifest.DISTRO_FEDORA
423434
runner := &runner.Linux{}
424435

425-
if err := img.InstantiateManifestFromContainers(&mf, []container.SourceSpec{containerSource}, runner, rng); err != nil {
436+
if err := img.InstantiateManifestFromContainers(&mf,
437+
[]container.SourceSpec{containerSource},
438+
[]container.SourceSpec{buildContainerSource}, runner, rng); err != nil {
426439
return nil, err
427440
}
428441

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
@@ -291,26 +292,55 @@ func manifestFromCobra(cmd *cobra.Command, args []string, pbar progress.Progress
291292
return nil, nil, err
292293
}
293294

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

304332
manifestConfig := &ManifestConfig{
305-
Architecture: cntArch,
306-
Config: config,
307-
ImageTypes: imageTypes,
308-
Imgref: imgref,
309-
RootfsMinsize: cntSize * containerSizeToDiskSizeMultiplier,
310-
DistroDefPaths: distroDefPaths,
311-
SourceInfo: sourceinfo,
312-
RootFSType: rootfsType,
313-
UseLibrepo: useLibrepo,
333+
Architecture: cntArch,
334+
Config: config,
335+
ImageTypes: imageTypes,
336+
Imgref: imgref,
337+
BuildImgref: buildImgref,
338+
RootfsMinsize: cntSize * containerSizeToDiskSizeMultiplier,
339+
DistroDefPaths: distroDefPaths,
340+
SourceInfo: sourceinfo,
341+
BuildSourceInfo: buildSourceinfo,
342+
RootFSType: rootfsType,
343+
UseLibrepo: useLibrepo,
314344
}
315345

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

0 commit comments

Comments
 (0)