Skip to content

Commit 4503dbd

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 c9452b1 commit 4503dbd

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
@@ -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)