2015-06-30 07:49:13 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-04-23 21:39:42 +08:00
|
|
|
"fmt"
|
2015-06-22 10:31:12 +08:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
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{
|
2017-03-02 16:02:15 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "console-socket",
|
|
|
|
Value: "",
|
|
|
|
Usage: "path to an AF_UNIX socket which will receive a file descriptor referencing the master end of the console's pseudoterminal",
|
|
|
|
},
|
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: "",
|
2016-05-28 13:33:57 +08:00
|
|
|
Usage: "cgroups mode: 'soft' (default), 'full' and 'strict'",
|
2015-08-06 23:14:59 +08:00
|
|
|
},
|
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",
|
|
|
|
},
|
2016-03-15 06:46:08 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-subreaper",
|
|
|
|
Usage: "disable the use of the subreaper used to reap reparented processes",
|
|
|
|
},
|
2016-03-31 02:12:03 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-pivot",
|
|
|
|
Usage: "do not use pivot root to jail process inside rootfs. This should be used whenever the rootfs is on top of a ramdisk",
|
|
|
|
},
|
2016-06-08 01:22:40 +08:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "empty-ns",
|
2017-04-21 10:41:02 +08:00
|
|
|
Usage: "create a namespace, but don't restore its properties",
|
2016-06-08 01:22:40 +08:00
|
|
|
},
|
2017-08-18 22:19:21 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "auto-dedup",
|
|
|
|
Usage: "enable auto deduplication of memory images",
|
|
|
|
},
|
2017-07-24 23:43:14 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "lazy-pages",
|
|
|
|
Usage: "use userfaultfd to lazily restore memory pages",
|
|
|
|
},
|
2015-06-22 10:31:12 +08:00
|
|
|
},
|
2016-05-10 13:58:09 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2016-10-28 23:43:10 +08:00
|
|
|
if err := checkArgs(context, 1, exactArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-23 21:39:42 +08:00
|
|
|
// XXX: Currently this is untested with rootless containers.
|
|
|
|
if isRootless() {
|
|
|
|
return fmt.Errorf("runc restore requires root")
|
|
|
|
}
|
|
|
|
|
2017-03-02 15:48:00 +08:00
|
|
|
spec, err := setupSpec(context)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
2017-03-02 15:48:00 +08:00
|
|
|
options := criuOptions(context)
|
|
|
|
status, err := startContainer(context, spec, CT_ACT_RESTORE, options)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
2017-03-02 15:48:00 +08:00
|
|
|
// exit with the container's exit status so any external supervisor is
|
|
|
|
// notified of the exit with the correct exit status.
|
|
|
|
os.Exit(status)
|
|
|
|
return nil
|
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"),
|
2016-08-24 17:48:56 +08:00
|
|
|
ParentImage: context.String("parent-path"),
|
2015-06-22 10:31:12 +08:00
|
|
|
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"),
|
2016-08-24 17:48:56 +08:00
|
|
|
PreDump: context.Bool("pre-dump"),
|
2017-08-18 22:19:21 +08:00
|
|
|
AutoDedup: context.Bool("auto-dedup"),
|
2017-07-24 23:43:14 +08:00
|
|
|
LazyPages: context.Bool("lazy-pages"),
|
|
|
|
StatusFd: context.String("status-fd"),
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
}
|