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

bib: add --filesystems commandline option for basic customizations #283

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion bib/cmd/bootc-image-builder/export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

var CanChownInPath = canChownInPath
var (
CanChownInPath = canChownInPath
NewFilesystemCustomizationFromString = newFilesystemCustomizationFromString
)

func MockOsGetuid(new func() int) (restore func()) {
saved := osGetuid
Expand Down
6 changes: 5 additions & 1 deletion bib/cmd/bootc-image-builder/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type ManifestConfig struct {

// Use a local container from the host rather than a repository
Local bool

// The default filesystem size
// TODO: support type as well
Filesystems []blueprint.FilesystemCustomization
}

func Manifest(c *ManifestConfig) (*manifest.Manifest, error) {
Expand Down Expand Up @@ -110,7 +114,7 @@ func manifestForDiskImage(c *ManifestConfig, rng *rand.Rand) (*manifest.Manifest
if !ok {
return nil, fmt.Errorf("pipelines: no partition tables defined for %s", c.Architecture)
}
pt, err := disk.NewPartitionTable(&basept, nil, DEFAULT_SIZE, disk.RawPartitioningMode, nil, rng)
pt, err := disk.NewPartitionTable(&basept, c.Filesystems, DEFAULT_SIZE, disk.RawPartitioningMode, nil, rng)
if err != nil {
return nil, err
}
Expand Down
48 changes: 44 additions & 4 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import (
"strconv"
"strings"

"github.com/osbuild/bootc-image-builder/bib/internal/setup"
"code.cloudfoundry.org/bytefmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"

"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/cloud/awscloud"
Expand All @@ -19,9 +23,8 @@ import (
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"

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

//go:embed fedora-eln.json
Expand Down Expand Up @@ -174,6 +177,39 @@ func saveManifest(ms manifest.OSBuildManifest, fpath string) error {
return nil
}

func newFilesystemCustomizationFromString(s string) ([]blueprint.FilesystemCustomization, error) {
var fses []blueprint.FilesystemCustomization

// a fsSpec has the form: '<mountpoint>:<size>[:<fs-type>]'
for _, fsSpec := range strings.Split(s, ",") {
l := strings.Split(fsSpec, ":")
if len(l) != 2 {
return nil, fmt.Errorf("cannot parse %q: need a mountpoint and a size", fsSpec)
}
mountPoint := l[0]
sizeStr := l[1]
size, err := bytefmt.ToBytes(sizeStr)
if err != nil {
return nil, fmt.Errorf("cannot parse size in: %q: %v", fsSpec, err)
}
// only "/" supported today
if mountPoint != "/" {
return nil, fmt.Errorf("cannot customize %q: only '/' is supported right now", mountPoint)
}
// TODO: cross check that the size of the FS is not too small
// for the image, maybe a bootc print-configuration could
// provide an optional minimal size (even auto-calculated
// from the rootfs size and some buffer?)

fses = append(fses, blueprint.FilesystemCustomization{
Mountpoint: mountPoint,
MinSize: size,
})
}

return fses, nil
}

func manifestFromCobra(cmd *cobra.Command, args []string) ([]byte, error) {
buildArch := arch.Current()
repos, err := loadRepos(buildArch.String())
Expand All @@ -188,6 +224,8 @@ func manifestFromCobra(cmd *cobra.Command, args []string) ([]byte, error) {
targetArch, _ := cmd.Flags().GetString("target-arch")
tlsVerify, _ := cmd.Flags().GetBool("tls-verify")
localStorage, _ := cmd.Flags().GetBool("local")
filesystems, _ := cmd.Flags().GetString("filesystems")
fses, _ := newFilesystemCustomizationFromString(filesystems)

if targetArch != "" {
// TODO: detect if binfmt_misc for target arch is
Expand Down Expand Up @@ -226,6 +264,7 @@ func manifestFromCobra(cmd *cobra.Command, args []string) ([]byte, error) {
Repos: repos,
TLSVerify: tlsVerify,
Local: localStorage,
Filesystems: fses,
}
return makeManifest(manifestConfig, rpmCacheRoot)
}
Expand Down Expand Up @@ -409,6 +448,7 @@ func run() error {
manifestCmd.Flags().String("target-arch", "", "build for the given target architecture (experimental)")
manifestCmd.Flags().StringArray("type", []string{"qcow2"}, "image types to build [qcow2, ami, iso, raw]")
manifestCmd.Flags().Bool("local", false, "use a local container rather than a container from a registry")
manifestCmd.Flags().String("filesystems", "", "Specify filesystem sizes, e.g. '/:20G'")

logrus.SetLevel(logrus.ErrorLevel)
buildCmd.Flags().AddFlagSet(manifestCmd.Flags())
Expand Down
26 changes: 25 additions & 1 deletion bib/cmd/bootc-image-builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

main "github.com/osbuild/bootc-image-builder/bib/cmd/bootc-image-builder"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/rpmmd"

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

func TestCanChownInPathHappy(t *testing.T) {
Expand Down Expand Up @@ -527,3 +528,26 @@ func TestBuildType(t *testing.T) {
})
}
}

func TestFilesystemSizeFromCmdlineHappy(t *testing.T) {
mps, err := main.NewFilesystemCustomizationFromString("/:20G")
assert.NoError(t, err)
assert.Equal(t, mps, []blueprint.FilesystemCustomization{
{Mountpoint: "/", MinSize: 20 * 1024 * 1024 * 1024},
})
}

func TestFilesystemSizeFromCmdlineSad(t *testing.T) {
for _, tc := range []struct {
fsSpec string
expectedErr string
}{
{"x", `cannot parse "x": need a mountpoint and a size`},
{"x:y:z", `cannot parse "x:y:z": need a mountpoint and a size`},
{"/:not-size", `cannot parse size in: "/:not-size": byte quantity must be a positive integer with a unit of measurement like M, MB, MiB, G, GiB, or GB`},
{"/opt:10G", `cannot customize "/opt": only '/' is supported right now`},
} {
_, err := main.NewFilesystemCustomizationFromString(tc.fsSpec)
assert.Equal(t, tc.expectedErr, err.Error())
}
}
5 changes: 3 additions & 2 deletions bib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
)

require (
code.cloudfoundry.org/bytefmt v0.0.0-20240318154019-470077afc6dc // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
Expand Down Expand Up @@ -105,12 +106,12 @@ require (
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.14.0 // indirect
golang.org/x/tools v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240304161311-37d4d3c04a78 // indirect
google.golang.org/grpc v1.62.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions bib/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
code.cloudfoundry.org/bytefmt v0.0.0-20240318154019-470077afc6dc h1:KTXwvKQ94jsSsKbT23FO3/wb1nNRZTOH0js9/PfSel0=
code.cloudfoundry.org/bytefmt v0.0.0-20240318154019-470077afc6dc/go.mod h1:JJ+p2lgTjXP0zB6fShYUwYHuGnDvxEjJRcpqIESTSao=
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/14rcole/gopopulate v0.0.0-20180821133914-b175b219e774 h1:SCbEWT58NSt7d2mcFdvxC9uyrdcTfvBbPLThhkDmXzg=
Expand Down Expand Up @@ -401,6 +403,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -469,6 +473,8 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
2 changes: 2 additions & 0 deletions test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def build_images(shared_tmpdir, build_container, request, force_aws_upload):
*creds_args,
build_container,
container_ref,
"--filesystems", "/:12G",
"--config", "/output/config.json",
*types_arg,
*upload_args,
Expand Down Expand Up @@ -324,6 +325,7 @@ def test_image_boots(image_type):
exit_status, output = test_vm.run("echo hello", user=image_type.username, password=image_type.password)
assert exit_status == 0
assert "hello" in output
# TODO: check the root FS size here to see that we really got 12G


@pytest.mark.parametrize("image_type", gen_testcases("ami-boot"), indirect=["image_type"])
Expand Down
Loading