2016-05-12 10:52:00 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2016-12-21 16:42:47 -05:00
|
|
|
"github.com/docker/docker/api"
|
2016-05-12 10:52:00 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-11-23 14:45:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-12-21 16:42:47 -05:00
|
|
|
validCheckpointNameChars = api.RestrictedNameChars
|
|
|
|
validCheckpointNamePattern = api.RestrictedNamePattern
|
2016-05-12 10:52:00 -04:00
|
|
|
)
|
|
|
|
|
2016-12-03 07:21:48 -05:00
|
|
|
// getCheckpointDir verifies checkpoint directory for create,remove, list options and checks if checkpoint already exists
|
|
|
|
func getCheckpointDir(checkDir, checkpointID string, ctrName string, ctrID string, ctrCheckpointDir string, create bool) (string, error) {
|
|
|
|
var checkpointDir string
|
|
|
|
var err2 error
|
|
|
|
if checkDir != "" {
|
|
|
|
checkpointDir = filepath.Join(checkDir, ctrID, "checkpoints")
|
|
|
|
} else {
|
|
|
|
checkpointDir = ctrCheckpointDir
|
|
|
|
}
|
|
|
|
checkpointAbsDir := filepath.Join(checkpointDir, checkpointID)
|
|
|
|
stat, err := os.Stat(checkpointAbsDir)
|
|
|
|
if create {
|
|
|
|
switch {
|
|
|
|
case err == nil && stat.IsDir():
|
|
|
|
err2 = fmt.Errorf("checkpoint with name %s already exists for container %s", checkpointID, ctrName)
|
|
|
|
case err != nil && os.IsNotExist(err):
|
|
|
|
err2 = nil
|
|
|
|
case err != nil:
|
|
|
|
err2 = err
|
|
|
|
case err == nil:
|
|
|
|
err2 = fmt.Errorf("%s exists and is not a directory", checkpointAbsDir)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
err2 = fmt.Errorf("checkpoint %s does not exists for container %s", checkpointID, ctrName)
|
|
|
|
case err == nil && stat.IsDir():
|
|
|
|
err2 = nil
|
|
|
|
case err == nil:
|
|
|
|
err2 = fmt.Errorf("%s exists and is not a directory", checkpointAbsDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return checkpointDir, err2
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:52:00 -04:00
|
|
|
// CheckpointCreate checkpoints the process running in a container with CRIU
|
|
|
|
func (daemon *Daemon) CheckpointCreate(name string, config types.CheckpointCreateOptions) error {
|
|
|
|
container, err := daemon.GetContainer(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.IsRunning() {
|
|
|
|
return fmt.Errorf("Container %s not running", name)
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:45:35 -05:00
|
|
|
if !validCheckpointNamePattern.MatchString(config.CheckpointID) {
|
|
|
|
return fmt.Errorf("Invalid checkpoint ID (%s), only %s are allowed", config.CheckpointID, validCheckpointNameChars)
|
|
|
|
}
|
|
|
|
|
2016-12-03 07:21:48 -05:00
|
|
|
checkpointDir, err := getCheckpointDir(config.CheckpointDir, config.CheckpointID, name, container.ID, container.CheckpointDir(), true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot checkpoint container %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2016-09-19 12:01:16 -04:00
|
|
|
err = daemon.containerd.CreateCheckpoint(container.ID, config.CheckpointID, checkpointDir, config.Exit)
|
2016-05-12 10:52:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Cannot checkpoint container %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon.LogContainerEvent(container, "checkpoint")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckpointDelete deletes the specified checkpoint
|
2016-09-19 12:01:16 -04:00
|
|
|
func (daemon *Daemon) CheckpointDelete(name string, config types.CheckpointDeleteOptions) error {
|
2016-05-12 10:52:00 -04:00
|
|
|
container, err := daemon.GetContainer(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-03 07:21:48 -05:00
|
|
|
checkpointDir, err := getCheckpointDir(config.CheckpointDir, config.CheckpointID, name, container.ID, container.CheckpointDir(), false)
|
|
|
|
if err == nil {
|
|
|
|
return os.RemoveAll(filepath.Join(checkpointDir, config.CheckpointID))
|
2016-09-19 12:01:16 -04:00
|
|
|
}
|
2016-12-03 07:21:48 -05:00
|
|
|
return err
|
2016-05-12 10:52:00 -04:00
|
|
|
}
|
|
|
|
|
2016-09-12 00:09:54 -04:00
|
|
|
// CheckpointList lists all checkpoints of the specified container
|
2016-09-19 12:01:16 -04:00
|
|
|
func (daemon *Daemon) CheckpointList(name string, config types.CheckpointListOptions) ([]types.Checkpoint, error) {
|
2016-09-12 00:09:54 -04:00
|
|
|
var out []types.Checkpoint
|
2016-05-12 10:52:00 -04:00
|
|
|
|
|
|
|
container, err := daemon.GetContainer(name)
|
|
|
|
if err != nil {
|
2016-09-12 00:09:54 -04:00
|
|
|
return nil, err
|
2016-05-12 10:52:00 -04:00
|
|
|
}
|
|
|
|
|
2017-05-17 22:16:19 -04:00
|
|
|
checkpointDir, err := getCheckpointDir(config.CheckpointDir, "", name, container.ID, container.CheckpointDir(), false)
|
2017-05-05 02:03:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 12:01:16 -04:00
|
|
|
|
2016-05-12 10:52:00 -04:00
|
|
|
if err := os.MkdirAll(checkpointDir, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dirs, err := ioutil.ReadDir(checkpointDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dirs {
|
|
|
|
if !d.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path := filepath.Join(checkpointDir, d.Name(), "config.json")
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var cpt types.Checkpoint
|
|
|
|
if err := json.Unmarshal(data, &cpt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = append(out, cpt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|