2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-05-12 14:52:00 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-22 13:52:41 +00:00
|
|
|
"context"
|
2016-05-12 14:52:00 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-09-06 15:35:06 +00:00
|
|
|
"github.com/docker/docker/daemon/names"
|
2016-11-23 19:45:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-09-06 15:35:06 +00:00
|
|
|
validCheckpointNameChars = names.RestrictedNameChars
|
|
|
|
validCheckpointNamePattern = names.RestrictedNamePattern
|
2016-05-12 14:52:00 +00:00
|
|
|
)
|
|
|
|
|
2016-12-03 12:21:48 +00:00
|
|
|
// getCheckpointDir verifies checkpoint directory for create,remove, list options and checks if checkpoint already exists
|
2017-09-22 13:52:41 +00:00
|
|
|
func getCheckpointDir(checkDir, checkpointID, ctrName, ctrID, ctrCheckpointDir string, create bool) (string, error) {
|
2016-12-03 12:21:48 +00:00
|
|
|
var checkpointDir string
|
|
|
|
var err2 error
|
|
|
|
if checkDir != "" {
|
2017-12-04 19:24:35 +00:00
|
|
|
checkpointDir = checkDir
|
2016-12-03 12:21:48 +00:00
|
|
|
} 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):
|
2017-09-22 13:52:41 +00:00
|
|
|
err2 = os.MkdirAll(checkpointAbsDir, 0700)
|
2016-12-03 12:21:48 +00:00
|
|
|
case err != nil:
|
|
|
|
err2 = err
|
2019-08-05 23:59:33 +00:00
|
|
|
default:
|
2016-12-03 12:21:48 +00:00
|
|
|
err2 = fmt.Errorf("%s exists and is not a directory", checkpointAbsDir)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
2018-12-20 21:52:54 +00:00
|
|
|
err2 = fmt.Errorf("checkpoint %s does not exist for container %s", checkpointID, ctrName)
|
2019-08-05 23:59:33 +00:00
|
|
|
case stat.IsDir():
|
2016-12-03 12:21:48 +00:00
|
|
|
err2 = nil
|
2019-08-05 23:59:33 +00:00
|
|
|
default:
|
2016-12-03 12:21:48 +00:00
|
|
|
err2 = fmt.Errorf("%s exists and is not a directory", checkpointAbsDir)
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 13:52:41 +00:00
|
|
|
return checkpointAbsDir, err2
|
2016-12-03 12:21:48 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 14:52:00 +00: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 19:45:35 +00:00
|
|
|
if !validCheckpointNamePattern.MatchString(config.CheckpointID) {
|
|
|
|
return fmt.Errorf("Invalid checkpoint ID (%s), only %s are allowed", config.CheckpointID, validCheckpointNameChars)
|
|
|
|
}
|
|
|
|
|
2016-12-03 12:21:48 +00: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)
|
|
|
|
}
|
|
|
|
|
2017-09-22 13:52:41 +00:00
|
|
|
err = daemon.containerd.CreateCheckpoint(context.Background(), container.ID, checkpointDir, config.Exit)
|
2016-05-12 14:52:00 +00:00
|
|
|
if err != nil {
|
2017-09-22 13:52:41 +00:00
|
|
|
os.RemoveAll(checkpointDir)
|
2016-05-12 14:52:00 +00:00
|
|
|
return fmt.Errorf("Cannot checkpoint container %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon.LogContainerEvent(container, "checkpoint")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckpointDelete deletes the specified checkpoint
|
2016-09-19 16:01:16 +00:00
|
|
|
func (daemon *Daemon) CheckpointDelete(name string, config types.CheckpointDeleteOptions) error {
|
2016-05-12 14:52:00 +00:00
|
|
|
container, err := daemon.GetContainer(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-03 12:21:48 +00:00
|
|
|
checkpointDir, err := getCheckpointDir(config.CheckpointDir, config.CheckpointID, name, container.ID, container.CheckpointDir(), false)
|
|
|
|
if err == nil {
|
2018-10-12 11:08:28 +00:00
|
|
|
return os.RemoveAll(checkpointDir)
|
2016-09-19 16:01:16 +00:00
|
|
|
}
|
2016-12-03 12:21:48 +00:00
|
|
|
return err
|
2016-05-12 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 04:09:54 +00:00
|
|
|
// CheckpointList lists all checkpoints of the specified container
|
2016-09-19 16:01:16 +00:00
|
|
|
func (daemon *Daemon) CheckpointList(name string, config types.CheckpointListOptions) ([]types.Checkpoint, error) {
|
2016-09-12 04:09:54 +00:00
|
|
|
var out []types.Checkpoint
|
2016-05-12 14:52:00 +00:00
|
|
|
|
|
|
|
container, err := daemon.GetContainer(name)
|
|
|
|
if err != nil {
|
2016-09-12 04:09:54 +00:00
|
|
|
return nil, err
|
2016-05-12 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 02:16:19 +00:00
|
|
|
checkpointDir, err := getCheckpointDir(config.CheckpointDir, "", name, container.ID, container.CheckpointDir(), false)
|
2017-05-05 06:03:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 16:01:16 +00:00
|
|
|
|
2016-05-12 14:52:00 +00:00
|
|
|
if err := os.MkdirAll(checkpointDir, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
dirs, err := os.ReadDir(checkpointDir)
|
2016-05-12 14:52:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dirs {
|
|
|
|
if !d.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
2018-12-29 18:20:48 +00:00
|
|
|
cpt := types.Checkpoint{Name: d.Name()}
|
2016-05-12 14:52:00 +00:00
|
|
|
out = append(out, cpt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|