Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook up blueprint filesystem customizations #124

Merged
merged 7 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bib/cmd/bootc-image-builder/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

var CanChownInPath = canChownInPath
var (
CanChownInPath = canChownInPath
ApplyFilesystemCustomizations = applyFilesystemCustomizations
GetDistroAndRunner = getDistroAndRunner
)

func MockOsGetuid(new func() int) (restore func()) {
saved := osGetuid
Expand Down
24 changes: 24 additions & 0 deletions bib/cmd/bootc-image-builder/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
cryptorand "crypto/rand"
"fmt"
"golang.org/x/exp/slices"
"math"
"math/big"
"math/rand"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/osbuild/images/pkg/image"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/platform"
"github.com/osbuild/images/pkg/policies"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/runner"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -72,6 +74,24 @@ func Manifest(c *ManifestConfig) (*manifest.Manifest, error) {
}
}

func applyFilesystemCustomizations(customizations *blueprint.Customizations, c *ManifestConfig) error {
// Check the filesystem customizations against the policy and set
// if user configured
if err := blueprint.CheckMountpointsPolicy(customizations.GetFilesystems(), policies.OstreeMountpointPolicies); err != nil {
return err
}
if customFS := customizations.GetFilesystems(); len(customFS) != 0 {
allowedMounts := []string{"/", "/boot"}
for _, mp := range customFS {
if !slices.Contains(allowedMounts, mp.Mountpoint) {
return fmt.Errorf("cannot use custom mount points yet, found: %v", mp.Mountpoint)
}
}
c.Filesystems = customFS
}
return nil
}

func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest, error) {
if c.Imgref == "" {
return nil, fmt.Errorf("pipeline: no base image defined")
Expand Down Expand Up @@ -138,6 +158,10 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
rootFS.Type = c.RootFSType
}

if err := applyFilesystemCustomizations(customizations, c); err != nil {
return nil, err
}

pt, err := disk.NewPartitionTable(&basept, c.Filesystems, DEFAULT_SIZE, disk.RawPartitioningMode, nil, rng)
if err != nil {
return nil, err
Expand Down
58 changes: 53 additions & 5 deletions bib/cmd/bootc-image-builder/image_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package main
package main_test

import (
"fmt"
"testing"

"github.com/osbuild/bootc-image-builder/bib/internal/source"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/runner"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/runner"

bib "github.com/osbuild/bootc-image-builder/bib/cmd/bootc-image-builder"
"github.com/osbuild/bootc-image-builder/bib/internal/source"
)

func TestGetDistroAndRunner(t *testing.T) {
Expand Down Expand Up @@ -42,7 +46,7 @@ func TestGetDistroAndRunner(t *testing.T) {
ID: c.id,
VersionID: c.versionID,
}
distro, runner, err := getDistroAndRunner(osRelease)
distro, runner, err := bib.GetDistroAndRunner(osRelease)
if c.expectedErr != "" {
assert.ErrorContains(t, err, c.expectedErr)
} else {
Expand All @@ -53,3 +57,47 @@ func TestGetDistroAndRunner(t *testing.T) {
})
}
}

func TestApplyFilesystemCustomizationsValidates(t *testing.T) {
for _, tc := range []struct {
fsCust []blueprint.FilesystemCustomization
expectedErr string
}{
// happy
{
fsCust: []blueprint.FilesystemCustomization{},
expectedErr: "",
},
{
fsCust: []blueprint.FilesystemCustomization{
{Mountpoint: "/"}, {Mountpoint: "/boot"},
},
expectedErr: "",
},
// sad
{
fsCust: []blueprint.FilesystemCustomization{
{Mountpoint: "/"},
{Mountpoint: "/ostree"},
},
expectedErr: `The following custom mountpoints are not supported ["/ostree"]`,
},
{
fsCust: []blueprint.FilesystemCustomization{
{Mountpoint: "/"},
{Mountpoint: "/var/log"},
},
expectedErr: "cannot use custom mount points yet, found: /var/log",
},
} {
conf := &bib.ManifestConfig{}
cust := &blueprint.Customizations{
Filesystem: tc.fsCust,
}
if tc.expectedErr == "" {
assert.NoError(t, bib.ApplyFilesystemCustomizations(cust, conf))
} else {
assert.ErrorContains(t, bib.ApplyFilesystemCustomizations(cust, conf), tc.expectedErr)
}
}
}
15 changes: 15 additions & 0 deletions test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ def build_images(shared_tmpdir, build_container, request, force_aws_upload):
"groups": ["wheel"],
},
],
"filesystem": [
{
"mountpoint": "/",
"minsize": "12GiB"
},
],
"kernel": {
"append": kargs,
},
Expand Down Expand Up @@ -371,6 +377,15 @@ def test_image_boots(image_type):
# XXX: read the fully yaml instead?
assert f"image: {image_type.container_ref}" in output

# Figure out how big / is and make sure it is > 11bGiB
# Note that df output is in 1k blocks, not bytes
for line in output.splitlines():
fields = line.split()
if fields[0] == "/sysroot":
size = int(fields[1])
assert size > 11 * 1024 * 1024
break


@pytest.mark.parametrize("image_type", gen_testcases("ami-boot"), indirect=["image_type"])
def test_ami_boots_in_aws(image_type, force_aws_upload):
Expand Down
45 changes: 45 additions & 0 deletions test/test_manifest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import platform
import pathlib
import subprocess
import textwrap

Expand Down Expand Up @@ -224,3 +225,47 @@ def test_manifest_user_customizations_toml(tmp_path, build_container):
"key": "ssh-rsa AAA ... [email protected]",
"groups": ["wheel"],
}


def test_mount_ostree_error(tmpdir_factory, build_container):
# no need to parameterize this test, toml is the same for all containers
container_ref = "quay.io/centos-bootc/centos-bootc:stream9"

CFG = {
"blueprint": {
"customizations": {
"filesystem": [
{
"mountpoint": "/",
"minsize": "12GiB"
},
{
"mountpoint": "/var/log",
"minsize": "1GiB"
},
{
"mountpoint": "/ostree",
cgwalters marked this conversation as resolved.
Show resolved Hide resolved
"minsize": "10GiB"
},
]
},
},
}

output_path = pathlib.Path(tmpdir_factory.mktemp("data")) / "output"
output_path.mkdir(exist_ok=True)
config_json_path = output_path / "config.json"
config_json_path.write_text(json.dumps(CFG), encoding="utf-8")

with pytest.raises(subprocess.CalledProcessError) as exc:
subprocess.check_output([
"podman", "run", "--rm",
"--privileged",
"-v", "/var/lib/containers/storage:/var/lib/containers/storage",
"--security-opt", "label=type:unconfined_t",
"-v", f"{output_path}:/output",
f'--entrypoint=["/usr/bin/bootc-image-builder", "manifest", "{container_ref}"]',
build_container,
"--config", "/output/config.json",
], stderr=subprocess.PIPE, encoding="utf8")
assert 'The following custom mountpoints are not supported ["/ostree"]' in exc.value.stderr
Loading