2015-06-30 07:49:13 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2016-02-02 07:00:09 +08:00
|
|
|
"syscall"
|
2015-06-22 10:31:12 +08:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2016-03-11 06:18:39 +08:00
|
|
|
"github.com/opencontainers/specs/specs-go"
|
2015-06-22 10:31:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var restoreCommand = cli.Command{
|
|
|
|
Name: "restore",
|
|
|
|
Usage: "restore a container from a previous checkpoint",
|
2016-02-11 01:30:06 +08:00
|
|
|
ArgsUsage: `<container-id>
|
|
|
|
|
|
|
|
Where "<container-id>" is the name for the instance of the container to be
|
|
|
|
restored.`,
|
|
|
|
Description: `Restores the saved state of the container instance that was previously saved
|
|
|
|
using the runc checkpoint command.`,
|
2015-06-22 10:31:12 +08:00
|
|
|
Flags: []cli.Flag{
|
2015-09-02 00:32:29 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "image-path",
|
|
|
|
Value: "",
|
|
|
|
Usage: "path to criu image files for restoring",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "work-path",
|
|
|
|
Value: "",
|
|
|
|
Usage: "path for saving work files and logs",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "tcp-established",
|
|
|
|
Usage: "allow open tcp connections",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "ext-unix-sk",
|
|
|
|
Usage: "allow external unix sockets",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "shell-job",
|
|
|
|
Usage: "allow shell jobs",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "file-locks",
|
|
|
|
Usage: "handle file locks, for safety",
|
|
|
|
},
|
2015-08-06 23:14:59 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "manage-cgroups-mode",
|
|
|
|
Value: "",
|
|
|
|
Usage: "cgroups mode: 'soft' (default), 'full' and 'strict'.",
|
|
|
|
},
|
2015-09-02 00:32:29 +08:00
|
|
|
cli.StringFlag{
|
2015-10-28 03:23:44 +08:00
|
|
|
Name: "bundle, b",
|
|
|
|
Value: "",
|
|
|
|
Usage: "path to the root of the bundle directory",
|
2015-09-02 00:32:29 +08:00
|
|
|
},
|
2016-02-02 07:00:09 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "detach,d",
|
|
|
|
Usage: "detach from the container's process",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pid-file",
|
|
|
|
Value: "",
|
|
|
|
Usage: "specify the file to write the process id to",
|
|
|
|
},
|
2015-06-22 10:31:12 +08:00
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
imagePath := context.String("image-path")
|
2016-02-09 06:25:03 +08:00
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
|
|
|
fatal(errEmptyID)
|
|
|
|
}
|
2015-06-22 10:31:12 +08:00
|
|
|
if imagePath == "" {
|
|
|
|
imagePath = getDefaultImagePath(context)
|
|
|
|
}
|
2015-10-28 03:23:44 +08:00
|
|
|
bundle := context.String("bundle")
|
|
|
|
if bundle != "" {
|
|
|
|
if err := os.Chdir(bundle); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 02:46:12 +08:00
|
|
|
spec, err := loadSpec(specConfig)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2016-02-09 06:25:03 +08:00
|
|
|
config, err := createLibcontainerConfig(id, spec)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
status, err := restoreContainer(context, spec, config, imagePath)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
os.Exit(status)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-11 06:18:39 +08:00
|
|
|
func restoreContainer(context *cli.Context, spec *specs.Spec, config *configs.Config, imagePath string) (code int, err error) {
|
2016-02-09 06:25:03 +08:00
|
|
|
var (
|
|
|
|
rootuid = 0
|
|
|
|
id = context.Args().First()
|
|
|
|
)
|
2015-06-22 10:31:12 +08:00
|
|
|
factory, err := loadFactory(context)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-02-09 06:25:03 +08:00
|
|
|
container, err := factory.Load(id)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
2016-02-09 06:25:03 +08:00
|
|
|
container, err = factory.Create(id, config)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
options := criuOptions(context)
|
2015-08-06 23:14:59 +08:00
|
|
|
|
2015-09-01 00:28:14 +08:00
|
|
|
status, err := container.Status()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
if status == libcontainer.Running {
|
2016-03-09 10:05:50 +08:00
|
|
|
fatalf("Container with id %s already running", id)
|
2015-09-01 00:28:14 +08:00
|
|
|
}
|
2015-08-06 23:14:59 +08:00
|
|
|
|
|
|
|
setManageCgroupsMode(context, options)
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
// ensure that the container is always removed if we were the process
|
|
|
|
// that created it.
|
2016-02-02 07:00:09 +08:00
|
|
|
detach := context.Bool("detach")
|
|
|
|
if !detach {
|
|
|
|
defer destroy(container)
|
2015-10-03 02:16:50 +08:00
|
|
|
}
|
2016-02-02 07:00:09 +08:00
|
|
|
process := &libcontainer.Process{}
|
|
|
|
tty, err := setupIO(process, rootuid, "", false, detach)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-02-27 04:14:47 +08:00
|
|
|
defer tty.Close()
|
2016-02-16 17:59:16 +08:00
|
|
|
handler := newSignalHandler(tty)
|
|
|
|
defer handler.Close()
|
2015-06-22 10:31:12 +08:00
|
|
|
if err := container.Restore(process, options); err != nil {
|
2016-02-27 04:14:47 +08:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if err := tty.ClosePostStart(); err != nil {
|
2015-06-22 10:31:12 +08:00
|
|
|
return -1, err
|
|
|
|
}
|
2016-02-02 07:00:09 +08:00
|
|
|
if pidFile := context.String("pid-file"); pidFile != "" {
|
|
|
|
if err := createPidFile(pidFile, process); err != nil {
|
|
|
|
process.Signal(syscall.SIGKILL)
|
|
|
|
process.Wait()
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if detach {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2015-08-04 07:27:56 +08:00
|
|
|
return handler.forward(process)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func criuOptions(context *cli.Context) *libcontainer.CriuOpts {
|
|
|
|
imagePath := getCheckpointImagePath(context)
|
|
|
|
if err := os.MkdirAll(imagePath, 0655); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
return &libcontainer.CriuOpts{
|
|
|
|
ImagesDirectory: imagePath,
|
|
|
|
WorkDirectory: context.String("work-path"),
|
|
|
|
LeaveRunning: context.Bool("leave-running"),
|
2015-08-22 06:59:43 +08:00
|
|
|
TcpEstablished: context.Bool("tcp-established"),
|
2015-06-22 10:31:12 +08:00
|
|
|
ExternalUnixConnections: context.Bool("ext-unix-sk"),
|
|
|
|
ShellJob: context.Bool("shell-job"),
|
2015-06-27 17:56:24 +08:00
|
|
|
FileLocks: context.Bool("file-locks"),
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
}
|