From 1fe5a251c4fb0fb35478b8b1fd16277cb417ccb9 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 9 Nov 2017 11:20:58 -0500 Subject: [PATCH] Move Exec creation to init process Signed-off-by: Michael Crosby --- cmd/containerd-shim/main_unix.go | 3 ++- linux/proc/exec.go | 32 -------------------------------- linux/proc/init.go | 29 +++++++++++++++++++++++++++++ linux/runtime.go | 6 +++--- linux/shim/service.go | 2 +- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/cmd/containerd-shim/main_unix.go b/cmd/containerd-shim/main_unix.go index 6801cd04d..1f51b1aea 100644 --- a/cmd/containerd-shim/main_unix.go +++ b/cmd/containerd-shim/main_unix.go @@ -18,6 +18,7 @@ import ( "github.com/containerd/containerd/dialer" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/events" + "github.com/containerd/containerd/linux/proc" "github.com/containerd/containerd/linux/shim" shimapi "github.com/containerd/containerd/linux/shim/v1" "github.com/containerd/containerd/reaper" @@ -56,7 +57,7 @@ func init() { flag.StringVar(&socketFlag, "socket", "", "abstract socket path to serve") flag.StringVar(&addressFlag, "address", "", "grpc address back to main containerd") flag.StringVar(&workdirFlag, "workdir", "", "path used to storge large temporary data") - flag.StringVar(&runtimeRootFlag, "runtime-root", shim.RuncRoot, "root directory for the runtime") + flag.StringVar(&runtimeRootFlag, "runtime-root", proc.RuncRoot, "root directory for the runtime") flag.StringVar(&criuFlag, "criu", "", "path to criu binary") flag.BoolVar(&systemdCgroupFlag, "systemd-cgroup", false, "set runtime to use systemd-cgroup") flag.Parse() diff --git a/linux/proc/exec.go b/linux/proc/exec.go index cf335a2d6..8154af550 100644 --- a/linux/proc/exec.go +++ b/linux/proc/exec.go @@ -4,7 +4,6 @@ package proc import ( "context" - "encoding/json" "fmt" "io" "os" @@ -16,8 +15,6 @@ import ( "golang.org/x/sys/unix" "github.com/containerd/console" - "github.com/containerd/containerd/identifiers" - shimapi "github.com/containerd/containerd/linux/shim/v1" "github.com/containerd/fifo" runc "github.com/containerd/go-runc" specs "github.com/opencontainers/runtime-spec/specs-go" @@ -46,35 +43,6 @@ type execProcess struct { waitBlock chan struct{} } -// NewExec returns a new exec'd process -func NewExec(context context.Context, path string, r *shimapi.ExecProcessRequest, parent *Init, id string) (Process, error) { - if err := identifiers.Validate(id); err != nil { - return nil, errors.Wrapf(err, "invalid exec id") - } - // process exec request - var spec specs.Process - if err := json.Unmarshal(r.Spec.Value, &spec); err != nil { - return nil, err - } - spec.Terminal = r.Terminal - - e := &execProcess{ - id: id, - path: path, - parent: parent, - spec: spec, - stdio: Stdio{ - Stdin: r.Stdin, - Stdout: r.Stdout, - Stderr: r.Stderr, - Terminal: r.Terminal, - }, - waitBlock: make(chan struct{}), - } - e.State = &execCreatedState{p: e} - return e, nil -} - func (e *execProcess) Wait() { <-e.waitBlock } diff --git a/linux/proc/init.go b/linux/proc/init.go index cf4783708..0d88e9bc5 100644 --- a/linux/proc/init.go +++ b/linux/proc/init.go @@ -337,6 +337,35 @@ func (p *Init) Runtime() *runc.Runc { return p.runtime } +// Exec returns a new exec'd process +func (p *Init) Exec(context context.Context, path string, r *shimapi.ExecProcessRequest) (Process, error) { + if err := identifiers.Validate(r.ID); err != nil { + return nil, errors.Wrapf(err, "invalid exec id") + } + // process exec request + var spec specs.Process + if err := json.Unmarshal(r.Spec.Value, &spec); err != nil { + return nil, err + } + spec.Terminal = r.Terminal + + e := &execProcess{ + id: r.ID, + path: path, + parent: p, + spec: spec, + stdio: Stdio{ + Stdin: r.Stdin, + Stdout: r.Stdout, + Stderr: r.Stderr, + Terminal: r.Terminal, + }, + waitBlock: make(chan struct{}), + } + e.State = &execCreatedState{p: e} + return e, nil +} + func (p *Init) checkpoint(context context.Context, r *shimapi.CheckpointTaskRequest) error { var options runctypes.CheckpointOptions if r.Options != nil { diff --git a/linux/runtime.go b/linux/runtime.go index a7fd9dd43..605a8dae2 100644 --- a/linux/runtime.go +++ b/linux/runtime.go @@ -17,8 +17,8 @@ import ( "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/events/exchange" "github.com/containerd/containerd/identifiers" + "github.com/containerd/containerd/linux/proc" "github.com/containerd/containerd/linux/runctypes" - client "github.com/containerd/containerd/linux/shim" shim "github.com/containerd/containerd/linux/shim/v1" "github.com/containerd/containerd/log" "github.com/containerd/containerd/metadata" @@ -376,7 +376,7 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) { filepath.Join(r.state, ns, id), filepath.Join(r.root, ns, id), ) - pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, client.InitPidFile)) + pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile)) s, err := bundle.NewShimClient(ctx, ns, ShimConnect(), nil) if err != nil { log.G(ctx).WithError(err).WithFields(logrus.Fields{ @@ -474,7 +474,7 @@ func (r *Runtime) getRuntime(ctx context.Context, ns, id string) (*runc.Runc, er var ( cmd = r.config.Runtime - root = client.RuncRoot + root = proc.RuncRoot ) if ropts != nil { if ropts.Runtime != "" { diff --git a/linux/shim/service.go b/linux/shim/service.go index 3822dd1cc..dd1ca62c8 100644 --- a/linux/shim/service.go +++ b/linux/shim/service.go @@ -218,7 +218,7 @@ func (s *Service) Exec(ctx context.Context, r *shimapi.ExecProcessRequest) (*goo return nil, errdefs.ToGRPCf(errdefs.ErrFailedPrecondition, "container must be created") } - process, err := proc.NewExec(ctx, s.config.Path, r, p.(*proc.Init), r.ID) + process, err := p.(*proc.Init).Exec(ctx, s.config.Path, r) if err != nil { return nil, errdefs.ToGRPC(err) }