From 37d70de5619d947db1f4680ab22d902c4831c570 Mon Sep 17 00:00:00 2001 From: nixpig <143995476+nixpig@users.noreply.github.com> Date: Fri, 1 Nov 2024 05:55:29 +0000 Subject: [PATCH] feat: use specified rootfs --- container/container.go | 13 +++++++++++++ container/container_fork.go | 4 ++-- container/container_init.go | 4 ++-- container/filesystem/rootfs.go | 9 +-------- internal/commands/delete.go | 1 - internal/commands/kill.go | 2 -- internal/commands/start.go | 1 - internal/commands/state.go | 3 --- pkg/config.go | 6 ++---- 9 files changed, 20 insertions(+), 23 deletions(-) diff --git a/container/container.go b/container/container.go index fdabba1..ab66247 100644 --- a/container/container.go +++ b/container/container.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/nixpig/brownie/container/lifecycle" "github.com/nixpig/brownie/pkg" @@ -65,6 +66,10 @@ func New( return nil, errors.New("only linux containers are supported") } + if spec.Root == nil { + return nil, errors.New("root is required") + } + absBundlePath, err := filepath.Abs(bundle) if err != nil { return nil, fmt.Errorf("construct absolute bundle path: %w", err) @@ -227,3 +232,11 @@ func (c *Container) SetID(id string) { func (c *Container) ID() string { return c.State.ID } + +func (c *Container) Rootfs() string { + if strings.Index(c.Spec.Root.Path, "/") == 0 { + return c.Spec.Root.Path + } + + return filepath.Join(c.Bundle(), c.Spec.Root.Path) +} diff --git a/container/container_fork.go b/container/container_fork.go index 9d8b9e2..8f7d975 100644 --- a/container/container_fork.go +++ b/container/container_fork.go @@ -64,7 +64,7 @@ func (c *Container) Fork() error { } defer listCloser() - if err := filesystem.SetupRootfs(c.Bundle(), c.Spec); err != nil { + if err := filesystem.SetupRootfs(c.Rootfs(), c.Spec); err != nil { return err } @@ -81,7 +81,7 @@ func (c *Container) Fork() error { c.initIPC.ch <- []byte("ready") startErr := ipc.WaitForMsg(listCh, "start", func() error { - if err := filesystem.PivotRoot(c.Bundle()); err != nil { + if err := filesystem.PivotRoot(c.Rootfs()); err != nil { return err } diff --git a/container/container_init.go b/container/container_init.go index 203d886..1d4c060 100644 --- a/container/container_init.go +++ b/container/container_init.go @@ -15,7 +15,7 @@ import ( "github.com/opencontainers/runtime-spec/specs-go" ) -func (c *Container) Init(exe string, arg string) error { +func (c *Container) Init(reexec string, arg string) error { if err := c.ExecHooks("createRuntime"); err != nil { return fmt.Errorf("execute createruntime hooks: %w", err) } @@ -49,7 +49,7 @@ func (c *Container) Init(exe string, arg string) error { } forkCmd := exec.Command( - exe, + reexec, []string{ arg, c.ID(), diff --git a/container/filesystem/rootfs.go b/container/filesystem/rootfs.go index 8055a3b..67dc7f7 100644 --- a/container/filesystem/rootfs.go +++ b/container/filesystem/rootfs.go @@ -2,18 +2,11 @@ package filesystem import ( "fmt" - "path/filepath" "github.com/opencontainers/runtime-spec/specs-go" ) -func SetupRootfs(root string, spec *specs.Spec) error { - rootfs := root - - if spec.Root != nil { - rootfs = filepath.Join(root, spec.Root.Path) - } - +func SetupRootfs(rootfs string, spec *specs.Spec) error { if err := mountRootfs(rootfs); err != nil { return fmt.Errorf("mount rootfs: %w", err) } diff --git a/internal/commands/delete.go b/internal/commands/delete.go index 318d609..0a28066 100644 --- a/internal/commands/delete.go +++ b/internal/commands/delete.go @@ -17,7 +17,6 @@ func Delete(opts *DeleteOpts, log *zerolog.Logger, db *database.DB) error { return err } - log.Info().Msg("loading container") cntr, err := container.Load(bundle) if err != nil { return err diff --git a/internal/commands/kill.go b/internal/commands/kill.go index 27f36d7..7632707 100644 --- a/internal/commands/kill.go +++ b/internal/commands/kill.go @@ -20,12 +20,10 @@ func Kill(opts *KillOpts, log *zerolog.Logger, db *database.DB) error { return err } - log.Info().Msg("loading container") cntr, err := container.Load(bundle) if err != nil { return err } - log.Info().Str("container id", opts.ID).Msg("killing container...") s, err := signal.FromString(opts.Signal) if err != nil { diff --git a/internal/commands/start.go b/internal/commands/start.go index ed6bd0f..aa4d8ec 100644 --- a/internal/commands/start.go +++ b/internal/commands/start.go @@ -16,7 +16,6 @@ func Start(opts *StartOpts, log *zerolog.Logger, db *database.DB) error { return err } - log.Info().Msg("loading container") cntr, err := container.Load(bundle) if err != nil { return err diff --git a/internal/commands/state.go b/internal/commands/state.go index 80b5267..c57033c 100644 --- a/internal/commands/state.go +++ b/internal/commands/state.go @@ -14,14 +14,11 @@ type StateOpts struct { } func State(opts *StateOpts, log *zerolog.Logger, db *database.DB) (string, error) { - log.Info().Msg("get state...") - bundle, err := db.GetBundleFromID(opts.ID) if err != nil { return "", err } - log.Info().Msg("loading container") cntr, err := container.Load(bundle) if err != nil { return "", err diff --git a/pkg/config.go b/pkg/config.go index 1278f54..cb5f10c 100644 --- a/pkg/config.go +++ b/pkg/config.go @@ -1,8 +1,6 @@ package pkg const ( - OCIVersion = "1.0.1-dev" - BrownieRootDir = "/var/lib/brownie" - DefaultRootfs = "rootfs" - BrownieContainerDir = BrownieRootDir + "/containers" + OCIVersion = "1.0.1-dev" + BrownieRootDir = "/var/lib/brownie" )