diff --git a/container.go b/container.go index ac8c81f1..30a87153 100644 --- a/container.go +++ b/container.go @@ -21,6 +21,9 @@ const ( // The container exists, but all its processes are paused. Paused + // The container exists, but its state is saved on disk + Checkpointed + // The container does not exist. Destroyed ) diff --git a/container_linux.go b/container_linux.go index 76f5dd5a..9bee7546 100644 --- a/container_linux.go +++ b/container_linux.go @@ -223,12 +223,6 @@ func newPipe() (parent *os.File, child *os.File, err error) { func (c *linuxContainer) Destroy() error { c.m.Lock() defer c.m.Unlock() - // Since the state.json and CRIU image files are in the c.root - // directory, we should not remove it after checkpoint. Also, - // when CRIU exits after restore, we should not kill the processes. - if _, err := os.Stat(filepath.Join(c.root, "checkpoint")); err == nil { - return nil - } status, err := c.currentStatus() if err != nil { return err @@ -569,10 +563,14 @@ func (c *linuxContainer) updateState(process parentProcess) error { return err } defer f.Close() + os.RemoveAll(filepath.Join(c.root, "checkpoint")) return json.NewEncoder(f).Encode(state) } func (c *linuxContainer) currentStatus() (Status, error) { + if _, err := os.Stat(filepath.Join(c.root, "checkpoint")); err == nil { + return Checkpointed, nil + } if c.initProcess == nil { return Destroyed, nil } diff --git a/nsinit/exec.go b/nsinit/exec.go index cf40a595..223493af 100644 --- a/nsinit/exec.go +++ b/nsinit/exec.go @@ -93,10 +93,17 @@ func execAction(context *cli.Context) { } } if created { - if err := container.Destroy(); err != nil { + status, err := container.Status() + if err != nil { tty.Close() fatal(err) } + if status != libcontainer.Checkpointed { + if err := container.Destroy(); err != nil { + tty.Close() + fatal(err) + } + } } tty.Close() os.Exit(utils.ExitStatus(status.Sys().(syscall.WaitStatus))) diff --git a/nsinit/restore.go b/nsinit/restore.go index d31401fb..dd0953dc 100644 --- a/nsinit/restore.go +++ b/nsinit/restore.go @@ -53,8 +53,11 @@ var restoreCommand = cli.Command{ fatal(err) } } - if err := container.Destroy(); err != nil { - fatal(err) + ctStatus, err := container.Status() + if ctStatus == libcontainer.Destroyed { + if err := container.Destroy(); err != nil { + fatal(err) + } } os.Exit(utils.ExitStatus(status.Sys().(syscall.WaitStatus))) },