Skip to content

Commit

Permalink
chore: mess around
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpig committed Sep 21, 2024
1 parent 5069dd2 commit 10b2ef8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func forkCmd(log *zerolog.Logger) *cobra.Command {
fork := &cobra.Command{
Use: "fork [flags] CONTAINER_ID INIT_SOCK_ADDR CONTAINER_SOCK_ADDR",
Short: "Fork container process\n\n \033[31m ⚠ FOR INTERNAL USE ONLY - DO NOT RUN DIRECTLY ⚠ \033[0m",
Args: cobra.ExactArgs(4),
Args: cobra.ExactArgs(5),
Example: "\n -- FOR INTERNAL USE ONLY --",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -160,12 +160,14 @@ func forkCmd(log *zerolog.Logger) *cobra.Command {
if err != nil {
return fmt.Errorf("convert pid string to int: %w", err)
}
consoleSocket := args[4]

opts := &commands.ForkOpts{
ID: containerID,
InitSockAddr: initSockAddr,
ContainerSockAddr: containerSockAddr,
PID: ipid,
ConsoleSocket: consoleSocket,
}

return commands.Fork(opts, log)
Expand Down
7 changes: 7 additions & 0 deletions internal/commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func Create(opts *CreateOpts, log *zerolog.Logger) error {
initSockAddr,
containerSockAddr,
strconv.Itoa(state.Pid),
opts.ConsoleSocket,
}...)

var cloneFlags uintptr
Expand Down Expand Up @@ -234,6 +235,12 @@ func Create(opts *CreateOpts, log *zerolog.Logger) error {
return err
}

// write pid to file if provided
if opts.PIDFile != "" {
pid := strconv.Itoa(state.Pid)
os.WriteFile(opts.PIDFile, []byte(pid), 0666)
}

initConn, err := listener.Accept()
if err != nil {
return err
Expand Down
45 changes: 38 additions & 7 deletions internal/commands/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"syscall"
Expand Down Expand Up @@ -75,6 +74,7 @@ type ForkOpts struct {
InitSockAddr string
ContainerSockAddr string
PID int
ConsoleSocket string
}

func Fork(opts *ForkOpts, log *zerolog.Logger) error {
Expand Down Expand Up @@ -176,12 +176,8 @@ func Fork(opts *ForkOpts, log *zerolog.Logger) error {
flags,
data,
); err != nil {
c, _ := user.Current()
log.Error().Err(err).
Str("source", mount.Source).
Str("dest", dest).
Str("options", data).
Any("user", c).
Any("mount", mount).
Msg("mount spec mount")
return err
}
Expand All @@ -200,12 +196,20 @@ func Fork(opts *ForkOpts, log *zerolog.Logger) error {
f, err := os.Create(absPath)
if err != nil && !os.IsExist(err) {
log.Error().Err(err).Msg("create default device mount point")
return err
}
if f != nil {
f.Close()
}
if err := syscall.Mount(dev.Path, absPath, "bind", syscall.MS_BIND, ""); err != nil {
if err := syscall.Mount(
dev.Path,
absPath,
"bind",
syscall.MS_BIND,
"",
); err != nil {
log.Error().Err(err).Msg("bind mount default devices")
return err
}
}
}
Expand Down Expand Up @@ -242,6 +246,31 @@ func Fork(opts *ForkOpts, log *zerolog.Logger) error {
}
}

if opts.ConsoleSocket != "" {
f, err := os.Create(filepath.Join(containerRootfs, "dev/console"))
if err != nil && !os.IsExist(err) {
log.Error().Err(err).Msg("create /dev/console")
return err
}
if f != nil {
if err := f.Chmod(0666); err != nil {
log.Error().Err(err).Msg("set permission of /dev/console")
return err
}
f.Close()
}
if err := syscall.Mount(
filepath.Join(containerRootfs, "dev/console"),
opts.ConsoleSocket,
"bind",
syscall.MS_BIND,
"",
); err != nil {
log.Error().Err(err).Msg("bind mount /dev/console")
return err
}
}

if err := syscall.Sethostname([]byte(spec.Hostname)); err != nil {
log.Error().Err(err).Str("hostname", spec.Hostname).Msg("set hostname")
return err
Expand Down Expand Up @@ -345,6 +374,8 @@ func Fork(opts *ForkOpts, log *zerolog.Logger) error {
return err
}

initConn.Close()

for {
containerConn, err := listener.Accept()
if err != nil {
Expand Down

0 comments on commit 10b2ef8

Please sign in to comment.