Skip to content

Commit

Permalink
images: add new image.BootcDiskImage type for bootc-image-builder
Browse files Browse the repository at this point in the history
Add a new `BootcDiskImage` type that is more opinionated then
OSTreeDiskImage as the two are more and more diverging.

This will be used in `bootc-image-builder`.
  • Loading branch information
mvo5 committed Jan 8, 2024
1 parent a8fce0c commit 2bf1c9b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
72 changes: 72 additions & 0 deletions pkg/image/bootc_disk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package image

import (
"fmt"
"math/rand"

"github.com/osbuild/images/pkg/artifact"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/platform"
"github.com/osbuild/images/pkg/runner"
)

type BootcDiskImage struct {
*OSTreeDiskImage
}

func NewBootcDiskImage(container container.SourceSpec) *BootcDiskImage {
// XXX: hardcoded for now
ref := "ostree/1/1/0"

return &BootcDiskImage{
&OSTreeDiskImage{
Base: NewBase("bootc-raw-image"),
ContainerSource: &container,
Ref: ref,
OSName: "default",
},
}
}

func (img *BootcDiskImage) InstantiateManifestFromContainers(m *manifest.Manifest,
containers []container.SourceSpec,
runner runner.Runner,
rng *rand.Rand) (*artifact.Artifact, error) {

buildPipeline := manifest.NewBuildFromContainersSourceSpec(m, runner, containers, &manifest.BuildOptions{ContainerBuildable: true})
buildPipeline.Checkpoint()

// don't support compressing non-raw images
imgFormat := img.Platform.GetImageFormat()
if imgFormat == platform.FORMAT_UNSET {
// treat unset as raw for this check
imgFormat = platform.FORMAT_RAW
}
if imgFormat != platform.FORMAT_RAW && img.Compression != "" {
panic(fmt.Sprintf("no compression is allowed with %q format for %q", imgFormat, img.name))
}

baseImage := baseRawOstreeImage(img.OSTreeDiskImage, buildPipeline)
switch imgFormat {
case platform.FORMAT_QCOW2:
// TODO: create new build pipeline here that uses "bib" itself
// as the buildroot to get access to tooling like "qemu-img"
qcow2Pipeline := manifest.NewQCOW2(buildPipeline, baseImage)
qcow2Pipeline.Compat = img.Platform.GetQCOW2Compat()
qcow2Pipeline.SetFilename(img.Filename)
return qcow2Pipeline.Export(), nil
}

switch img.Compression {
case "xz":
compressedImage := manifest.NewXZ(buildPipeline, baseImage)
compressedImage.SetFilename(img.Filename)
return compressedImage.Export(), nil
case "":
baseImage.SetFilename(img.Filename)
return baseImage.Export(), nil
default:
panic(fmt.Sprintf("unsupported compression type %q on %q", img.Compression, img.name))
}
}
22 changes: 22 additions & 0 deletions pkg/image/bootc_disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package image_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/image"
)

func TestBootcDiskImageNew(t *testing.T) {
containerSource := container.SourceSpec{
Source: "source-spec",
Name: "name",
}

img := image.NewBootcDiskImage(containerSource)
require.NotNil(t, img)
assert.Equal(t, img.OSTreeDiskImage.Base.Name(), "bootc-raw-image")
}
7 changes: 4 additions & 3 deletions pkg/image/ostree_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ func baseRawOstreeImage(img *OSTreeDiskImage, buildPipeline *manifest.Build) *ma
osPipeline.LockRoot = img.LockRoot

// other image types (e.g. live) pass the workload to the pipeline.
osPipeline.EnabledServices = img.Workload.GetServices()
osPipeline.DisabledServices = img.Workload.GetDisabledServices()

if img.Workload != nil {
osPipeline.EnabledServices = img.Workload.GetServices()
osPipeline.DisabledServices = img.Workload.GetDisabledServices()
}
return manifest.NewRawOStreeImage(buildPipeline, osPipeline, img.Platform)
}

Expand Down

0 comments on commit 2bf1c9b

Please sign in to comment.