2019-04-29 21:25:05 +00:00
|
|
|
// Copyright 2018 The gVisor Authors.
|
2018-06-06 19:31:01 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-11-28 22:00:54 +00:00
|
|
|
"context"
|
2018-06-22 16:40:21 +00:00
|
|
|
"path/filepath"
|
2018-06-21 16:57:33 +00:00
|
|
|
"syscall"
|
|
|
|
|
2018-06-06 19:31:01 +00:00
|
|
|
"flag"
|
|
|
|
"github.com/google/subcommands"
|
2018-06-21 16:57:33 +00:00
|
|
|
"gvisor.googlesource.com/gvisor/runsc/boot"
|
|
|
|
"gvisor.googlesource.com/gvisor/runsc/container"
|
|
|
|
"gvisor.googlesource.com/gvisor/runsc/specutils"
|
2018-06-06 19:31:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Restore implements subcommands.Command for the "restore" command.
|
|
|
|
type Restore struct {
|
2018-06-21 16:57:33 +00:00
|
|
|
// Restore flags are a super-set of those for Create.
|
|
|
|
Create
|
|
|
|
|
|
|
|
// imagePath is the path to the saved container image
|
|
|
|
imagePath string
|
2019-05-04 04:40:48 +00:00
|
|
|
|
|
|
|
// detach indicates that runsc has to start a process and exit without waiting it.
|
|
|
|
detach bool
|
2018-06-06 19:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name implements subcommands.Command.Name.
|
|
|
|
func (*Restore) Name() string {
|
|
|
|
return "restore"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synopsis implements subcommands.Command.Synopsis.
|
|
|
|
func (*Restore) Synopsis() string {
|
2018-06-29 21:46:45 +00:00
|
|
|
return "restore a saved state of container (experimental)"
|
2018-06-06 19:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Usage implements subcommands.Command.Usage.
|
|
|
|
func (*Restore) Usage() string {
|
2018-06-21 16:57:33 +00:00
|
|
|
return `restore [flags] <container id> - restore saved state of container.
|
2018-06-06 19:31:01 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFlags implements subcommands.Command.SetFlags.
|
|
|
|
func (r *Restore) SetFlags(f *flag.FlagSet) {
|
2018-06-21 16:57:33 +00:00
|
|
|
r.Create.SetFlags(f)
|
2018-06-22 16:40:21 +00:00
|
|
|
f.StringVar(&r.imagePath, "image-path", "", "directory path to saved container image")
|
2019-05-04 04:40:48 +00:00
|
|
|
f.BoolVar(&r.detach, "detach", false, "detach from the container's process")
|
2018-06-22 16:40:21 +00:00
|
|
|
|
|
|
|
// Unimplemented flags necessary for compatibility with docker.
|
|
|
|
|
|
|
|
var nsr bool
|
|
|
|
f.BoolVar(&nsr, "no-subreaper", false, "ignored")
|
|
|
|
|
|
|
|
var wp string
|
|
|
|
f.StringVar(&wp, "work-path", "", "ignored")
|
2018-06-06 19:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute implements subcommands.Command.Execute.
|
|
|
|
func (r *Restore) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
|
2018-06-21 16:57:33 +00:00
|
|
|
if f.NArg() != 1 {
|
|
|
|
f.Usage()
|
|
|
|
return subcommands.ExitUsageError
|
|
|
|
}
|
|
|
|
|
|
|
|
id := f.Arg(0)
|
|
|
|
conf := args[0].(*boot.Config)
|
|
|
|
waitStatus := args[1].(*syscall.WaitStatus)
|
|
|
|
|
|
|
|
bundleDir := r.bundleDir
|
|
|
|
if bundleDir == "" {
|
|
|
|
bundleDir = getwdOrDie()
|
|
|
|
}
|
|
|
|
spec, err := specutils.ReadSpec(bundleDir)
|
|
|
|
if err != nil {
|
2019-01-19 01:35:09 +00:00
|
|
|
Fatalf("reading spec: %v", err)
|
2018-06-21 16:57:33 +00:00
|
|
|
}
|
|
|
|
specutils.LogSpec(spec)
|
|
|
|
|
|
|
|
if r.imagePath == "" {
|
|
|
|
Fatalf("image-path flag must be provided")
|
|
|
|
}
|
|
|
|
|
2019-05-04 04:40:48 +00:00
|
|
|
conf.RestoreFile = filepath.Join(r.imagePath, checkpointFileName)
|
2018-06-21 16:57:33 +00:00
|
|
|
|
2019-05-04 04:40:48 +00:00
|
|
|
ws, err := container.Run(id, spec, conf, bundleDir, r.consoleSocket, r.pidFile, r.userLog, r.detach)
|
2018-06-21 16:57:33 +00:00
|
|
|
if err != nil {
|
2019-01-19 01:35:09 +00:00
|
|
|
Fatalf("running container: %v", err)
|
2018-06-21 16:57:33 +00:00
|
|
|
}
|
|
|
|
*waitStatus = ws
|
|
|
|
|
|
|
|
return subcommands.ExitSuccess
|
2018-06-06 19:31:01 +00:00
|
|
|
}
|