Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Sep 22, 2024
1 parent 0f700b1 commit d8b0e3b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 112 deletions.
2 changes: 1 addition & 1 deletion internal/commands/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func Fork(opts *ForkOpts, log *zerolog.Logger) error {

var dataOptions []string
for _, opt := range mount.Options {
o, ok := pkg.MountOptions[opt]
o, ok := filesystem.MountOptions[opt]
if !ok {
if !strings.HasPrefix(opt, "gid=") &&
!strings.HasPrefix(opt, "uid=") &&
Expand Down
78 changes: 78 additions & 0 deletions internal/filesystem/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package filesystem

import (
"os"

"github.com/opencontainers/runtime-spec/specs-go"
)

var (
defaultFileMode = os.FileMode(0666)
defaultUID uint32 = 0
defaultGID uint32 = 0
)

var DefaultSymlinks = map[string]string{
"/proc/self/fd": "dev/fd",
"/proc/self/fd/0": "dev/stdin",
"/proc/self/fd/1": "dev/stdout",
"/proc/self/fd/2": "dev/stderr",
"pts/ptmx": "dev/ptmx",
}

var DefaultDevices = []specs.LinuxDevice{
{
Path: "/dev/null",
Type: CharDevice,
Major: 1,
Minor: 3,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/zero",
Major: 1,
Minor: 5,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/full",
Major: 1,
Minor: 7,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/random",
Major: 1,
Minor: 8,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/urandom",
Major: 1,
Minor: 9,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/tty",
Major: 5,
Minor: 0,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
}
9 changes: 9 additions & 0 deletions internal/filesystem/devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package filesystem

var (
AllDevices = "a"
BlockDevice = "b"
CharDevice = "c"
UnbufferedCharDevice = "u"
FifoDevice = "p"
)
107 changes: 0 additions & 107 deletions internal/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,83 +8,6 @@ import (
"github.com/opencontainers/runtime-spec/specs-go"
)

var (
defaultFileMode = os.FileMode(0666)
defaultUID uint32 = 0
defaultGID uint32 = 0

AllDevices = "a"
BlockDevice = "b"
CharDevice = "c"
UnbufferedCharDevice = "u"
FifoDevice = "p"
)

var DefaultSymlinks = map[string]string{
"/proc/self/fd": "dev/fd",
"/proc/self/fd/0": "dev/stdin",
"/proc/self/fd/1": "dev/stdout",
"/proc/self/fd/2": "dev/stderr",
"pts/ptmx": "dev/ptmx",
}

var DefaultDevices = []specs.LinuxDevice{
{
Path: "/dev/null",
Type: CharDevice,
Major: 1,
Minor: 3,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/zero",
Major: 1,
Minor: 5,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/full",
Major: 1,
Minor: 7,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/random",
Major: 1,
Minor: 8,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/urandom",
Major: 1,
Minor: 9,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
{
Type: CharDevice,
Path: "/dev/tty",
Major: 5,
Minor: 0,
FileMode: &defaultFileMode,
UID: &defaultUID,
GID: &defaultGID,
},
}

func MountProc(containerRootfs string) error {
containerPath := filepath.Join(containerRootfs, "proc")

Expand All @@ -108,10 +31,6 @@ func MountProc(containerRootfs string) error {
return nil
}

func UnmountProc() error {
return syscall.Unmount("proc", 0)
}

func MountRootfs(containerRootfs string) error {
if err := syscall.Mount(
containerRootfs,
Expand All @@ -126,32 +45,6 @@ func MountRootfs(containerRootfs string) error {
return nil
}

func PivotRootfs(containerRootfs string) error {
oldroot := filepath.Join(containerRootfs, "oldroot")

if err := os.MkdirAll(oldroot, 0700); err != nil {
return err
}

if err := syscall.PivotRoot(containerRootfs, oldroot); err != nil {
return err
}

if err := os.Chdir("/"); err != nil {
return err
}

if err := syscall.Unmount("oldroot", syscall.MNT_DETACH); err != nil {
return err
}

if err := os.RemoveAll("oldroot"); err != nil {
return err
}

return nil
}

func DevInSpec(mounts []specs.Mount, dev string) bool {
for _, mount := range mounts {
if mount.Destination == dev {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package pkg
package filesystem

import "golang.org/x/sys/unix"

// MountOptions maps the supported mount option string to the corresponding
// flag, per MUST in spec:
// https://github.com/opencontainers/runtime-spec/blob/main/config.md#linux-mount-options
var MountOptions = map[string]struct {
No bool
Flag uintptr
Expand Down
34 changes: 34 additions & 0 deletions internal/filesystem/pivot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package filesystem

import (
"fmt"
"os"
"path/filepath"
"syscall"
)

func PivotRootfs(containerRootfs string) error {
oldroot := filepath.Join(containerRootfs, "oldroot")

if err := os.MkdirAll(oldroot, 0700); err != nil {
return fmt.Errorf("make old root dir: %w", err)
}

if err := syscall.PivotRoot(containerRootfs, oldroot); err != nil {
return fmt.Errorf("pivot to new root: %w", err)
}

if err := os.Chdir("/"); err != nil {
return fmt.Errorf("chdir to new root: %w", err)
}

if err := syscall.Unmount("oldroot", syscall.MNT_DETACH); err != nil {
return fmt.Errorf("unmount old root: %w", err)
}

if err := os.RemoveAll("oldroot"); err != nil {
return fmt.Errorf("remove old root: %w", err)
}

return nil
}

0 comments on commit d8b0e3b

Please sign in to comment.