Move exec and checkpoint to process state

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-02-11 11:30:25 -08:00
parent cf28969328
commit d317f71ac2
5 changed files with 26 additions and 31 deletions

View file

@ -12,17 +12,6 @@ import (
"github.com/docker/docker/pkg/term" "github.com/docker/docker/pkg/term"
) )
var (
fexec bool
fcheckpoint string
)
func init() {
flag.BoolVar(&fexec, "exec", false, "exec a process instead of starting the init")
flag.StringVar(&fcheckpoint, "checkpoint", "", "start container from an existing checkpoint")
flag.Parse()
}
func setupLogger() { func setupLogger() {
f, err := os.OpenFile("/tmp/shim.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755) f, err := os.OpenFile("/tmp/shim.log", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0755)
if err != nil { if err != nil {
@ -37,6 +26,7 @@ func setupLogger() {
// the cwd of the shim should be the bundle for the container. Arg1 should be the path // the cwd of the shim should be the bundle for the container. Arg1 should be the path
// to the state directory where the shim can locate fifos and other information. // to the state directory where the shim can locate fifos and other information.
func main() { func main() {
flag.Parse()
// start handling signals as soon as possible so that things are properly reaped // start handling signals as soon as possible so that things are properly reaped
// or if runc exits before we hit the handler // or if runc exits before we hit the handler
signals := make(chan os.Signal, 2048) signals := make(chan os.Signal, 2048)
@ -56,7 +46,7 @@ func main() {
logrus.WithField("error", err).Fatal("shim: open control pipe") logrus.WithField("error", err).Fatal("shim: open control pipe")
} }
defer control.Close() defer control.Close()
p, err := newProcess(flag.Arg(0), flag.Arg(1), fexec, fcheckpoint) p, err := newProcess(flag.Arg(0), flag.Arg(1))
if err != nil { if err != nil {
logrus.WithField("error", err).Fatal("shim: create new process") logrus.WithField("error", err).Fatal("shim: create new process")
} }

View file

@ -27,19 +27,18 @@ type process struct {
state *runtime.ProcessState state *runtime.ProcessState
} }
func newProcess(id, bundle string, exec bool, checkpoint string) (*process, error) { func newProcess(id, bundle string) (*process, error) {
p := &process{ p := &process{
id: id, id: id,
bundle: bundle, bundle: bundle,
exec: exec,
} }
s, err := loadProcess() s, err := loadProcess()
if err != nil { if err != nil {
return nil, err return nil, err
} }
p.state = s p.state = s
if checkpoint != "" { if s.Checkpoint != "" {
cpt, err := loadCheckpoint(bundle, checkpoint) cpt, err := loadCheckpoint(bundle, s.Checkpoint)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -83,7 +82,7 @@ func (p *process) start() error {
return err return err
} }
args := []string{} args := []string{}
if p.exec { if p.state.Exec {
args = append(args, "exec", args = append(args, "exec",
"--process", filepath.Join(cwd, "process.json"), "--process", filepath.Join(cwd, "process.json"),
"--console", p.consolePath, "--console", p.consolePath,
@ -146,7 +145,7 @@ func (p *process) pid() int {
} }
func (p *process) delete() error { func (p *process) delete() error {
if !p.exec { if !p.state.Exec {
return exec.Command("runc", "delete", p.id).Run() return exec.Command("runc", "delete", p.id).Run()
} }
return nil return nil

View file

@ -150,7 +150,6 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
return nil, err return nil, err
} }
cmd := exec.Command("containerd-shim", cmd := exec.Command("containerd-shim",
"-checkpoint", checkpoint,
c.id, c.bundle, c.id, c.bundle,
) )
cmd.Dir = processRoot cmd.Dir = processRoot
@ -162,6 +161,7 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
return nil, err return nil, err
} }
config := &processConfig{ config := &processConfig{
checkpoint: checkpoint,
root: processRoot, root: processRoot,
id: InitProcessID, id: InitProcessID,
c: c, c: c,
@ -189,7 +189,6 @@ func (c *container) Exec(pid string, spec specs.Process, s Stdio) (Process, erro
return nil, err return nil, err
} }
cmd := exec.Command("containerd-shim", cmd := exec.Command("containerd-shim",
"-exec",
c.id, c.bundle, c.id, c.bundle,
) )
cmd.Dir = processRoot cmd.Dir = processRoot
@ -197,6 +196,7 @@ func (c *container) Exec(pid string, spec specs.Process, s Stdio) (Process, erro
Setpgid: true, Setpgid: true,
} }
config := &processConfig{ config := &processConfig{
exec: true,
id: pid, id: pid,
root: processRoot, root: processRoot,
c: c, c: c,

View file

@ -47,6 +47,8 @@ type processConfig struct {
spec *specs.LinuxSpec spec *specs.LinuxSpec
c *container c *container
stdio Stdio stdio Stdio
exec bool
checkpoint string
} }
func newProcess(config *processConfig) (*process, error) { func newProcess(config *processConfig) (*process, error) {
@ -67,12 +69,14 @@ func newProcess(config *processConfig) (*process, error) {
} }
defer f.Close() defer f.Close()
if err := json.NewEncoder(f).Encode(ProcessState{ if err := json.NewEncoder(f).Encode(ProcessState{
Process: config.processSpec, Process: config.processSpec,
RootUID: uid, Exec: config.exec,
RootGID: gid, Checkpoint: config.checkpoint,
Stdin: config.stdio.Stdin, RootUID: uid,
Stdout: config.stdio.Stdout, RootGID: gid,
Stderr: config.stdio.Stderr, Stdin: config.stdio.Stdin,
Stdout: config.stdio.Stdout,
Stderr: config.stdio.Stderr,
}); err != nil { }); err != nil {
return nil, err return nil, err
} }

View file

@ -44,11 +44,13 @@ type state struct {
type ProcessState struct { type ProcessState struct {
specs.Process specs.Process
RootUID int `json:"rootUID"` Exec bool `json:"exec"`
RootGID int `json:"rootGID"` Checkpoint string `json:"checkpoint"`
Stdin string `json:"containerdStdin"` RootUID int `json:"rootUID"`
Stdout string `json:"containerdStdout"` RootGID int `json:"rootGID"`
Stderr string `json:"containerdStderr"` Stdin string `json:"containerdStdin"`
Stdout string `json:"containerdStdout"`
Stderr string `json:"containerdStderr"`
} }
type Stat struct { type Stat struct {