Skip to content

Commit

Permalink
refactor: move out fs ops from reexec to filesystem package
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Dec 15, 2024
1 parent b33f7fb commit 72076be
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 50 deletions.
4 changes: 4 additions & 0 deletions capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ var Capabilities = map[string]capability.Cap{
}

func SetCapabilities(caps *specs.LinuxCapabilities) error {
if caps == nil {
return nil
}

c, err := capability.NewPid2(0)
if err != nil {
return fmt.Errorf("initialise capabilities object: %w", err)
Expand Down
17 changes: 8 additions & 9 deletions container/container_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,15 @@ func (c *Container) Init(reexec string, arg string) error {
}
defer conn.Close()

b := make([]byte, 1024)
for {
n, err := conn.Read(b)
if err != nil || n == 0 {
continue
}
b := make([]byte, 128)
n, err := conn.Read(b)
if err != nil {
return fmt.Errorf("read from init socket: %w", err)
}

if string(b[:n]) == "ready" {
break
}
msg := string(b[:n])
if msg != "ready" {
return fmt.Errorf("expecting 'ready', received '%s'", msg)
}

// after receiving "ready"
Expand Down
65 changes: 24 additions & 41 deletions container/container_reexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Container) Reexec() error {
if err := os.RemoveAll(
filepath.Join(containerRootDir, c.ID(), containerSockFilename),
); err != nil {
return fmt.Errorf("remove socket before creating: %w", err)
return fmt.Errorf("remove any existing container socket: %w", err)
}

listener, err := net.Listen(
Expand All @@ -54,16 +54,15 @@ func (c *Container) Reexec() error {
return err
}

b := make([]byte, 1024)
for {
n, err := containerConn.Read(b)
if err != nil || n == 0 {
continue
}
b := make([]byte, 128)
n, err := containerConn.Read(b)
if err != nil {
return fmt.Errorf("read from container socket: %w", err)
}

if string(b[:n]) == "start" {
break
}
msg := string(b[:n])
if msg != "start" {
return fmt.Errorf("expecting 'start', received '%s'", msg)
}

// close as soon as we're done so they don't leak into the container
Expand Down Expand Up @@ -91,28 +90,16 @@ func (c *Container) Reexec() error {
return err
}

if c.Spec.Linux.RootfsPropagation != "" {
if err := syscall.Mount(
"",
"/",
"",
filesystem.MountOptions[c.Spec.Linux.RootfsPropagation].Flag,
"",
); err != nil {
return err
}
if err := filesystem.SetRootfsMountPropagation(
c.Spec.Linux.RootfsPropagation,
); err != nil {
return err
}

if c.Spec.Root.Readonly {
if err := syscall.Mount(
"",
"/",
"",
syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY,
"",
); err != nil {
return err
}
if err := filesystem.MountRootReadonly(
c.Spec.Root.Readonly,
); err != nil {
return err
}

if slices.ContainsFunc(
Expand All @@ -130,18 +117,14 @@ func (c *Container) Reexec() error {
}
}

if c.Spec.Process.Rlimits != nil {
if err := cgroups.SetRlimits(c.Spec.Process.Rlimits); err != nil {
return err
}
if err := cgroups.SetRlimits(c.Spec.Process.Rlimits); err != nil {
return err
}

if c.Spec.Process.Capabilities != nil {
if err := capabilities.SetCapabilities(
c.Spec.Process.Capabilities,
); err != nil {
return err
}
if err := capabilities.SetCapabilities(
c.Spec.Process.Capabilities,
); err != nil {
return err
}

cmd := exec.Command(c.Spec.Process.Args[0], c.Spec.Process.Args[1:]...)
Expand Down Expand Up @@ -187,7 +170,7 @@ func (c *Container) Reexec() error {
return fmt.Errorf("execute startContainer hooks: %w", err)
}

// we can't get logs or anything past this point
// point of no return
if err := cmd.Run(); err != nil {
return err
}
Expand Down
37 changes: 37 additions & 0 deletions filesystem/rootfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filesystem

import (
"fmt"
"syscall"

"github.com/opencontainers/runtime-spec/specs-go"
)
Expand Down Expand Up @@ -41,3 +42,39 @@ func PivotRoot(rootfs string) error {

return nil
}

func SetRootfsMountPropagation(prop string) error {
if prop == "" {
return nil
}

if err := syscall.Mount(
"",
"/",
"",
MountOptions[prop].Flag,
"",
); err != nil {
return fmt.Errorf("set rootfs mount propagation (%s): %w", prop, err)
}

return nil
}

func MountRootReadonly(ro bool) error {
if !ro {
return nil
}

if err := syscall.Mount(
"",
"/",
"",
syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY,
"",
); err != nil {
return fmt.Errorf("remount root as readonly: %w", err)
}

return nil
}

0 comments on commit 72076be

Please sign in to comment.