Skip to content

Commit

Permalink
feat: use specified rootfs
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Nov 1, 2024
1 parent 16979e2 commit 37d70de
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 23 deletions.
13 changes: 13 additions & 0 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/nixpig/brownie/container/lifecycle"
"github.com/nixpig/brownie/pkg"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions container/container_fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions container/container_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -49,7 +49,7 @@ func (c *Container) Init(exe string, arg string) error {
}

forkCmd := exec.Command(
exe,
reexec,
[]string{
arg,
c.ID(),
Expand Down
9 changes: 1 addition & 8 deletions container/filesystem/rootfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 0 additions & 1 deletion internal/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions internal/commands/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion internal/commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions internal/commands/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions pkg/config.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 37d70de

Please sign in to comment.