github.com/lalkh/containerd@v1.4.3/cmd/ctr/commands/containers/restore.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package containers 18 19 import ( 20 "github.com/containerd/containerd" 21 "github.com/containerd/containerd/cio" 22 "github.com/containerd/containerd/cmd/ctr/commands" 23 "github.com/containerd/containerd/errdefs" 24 "github.com/pkg/errors" 25 "github.com/urfave/cli" 26 ) 27 28 var restoreCommand = cli.Command{ 29 Name: "restore", 30 Usage: "restore a container from checkpoint", 31 ArgsUsage: "CONTAINER REF", 32 Flags: []cli.Flag{ 33 cli.BoolFlag{ 34 Name: "rw", 35 Usage: "restore the rw layer from the checkpoint", 36 }, 37 cli.BoolFlag{ 38 Name: "live", 39 Usage: "restore the runtime and memory data from the checkpoint", 40 }, 41 }, 42 Action: func(context *cli.Context) error { 43 id := context.Args().First() 44 if id == "" { 45 return errors.New("container id must be provided") 46 } 47 ref := context.Args().Get(1) 48 if ref == "" { 49 return errors.New("ref must be provided") 50 } 51 client, ctx, cancel, err := commands.NewClient(context) 52 if err != nil { 53 return err 54 } 55 defer cancel() 56 57 checkpoint, err := client.GetImage(ctx, ref) 58 if err != nil { 59 if !errdefs.IsNotFound(err) { 60 return err 61 } 62 // TODO (ehazlett): consider other options (always/never fetch) 63 ck, err := client.Fetch(ctx, ref) 64 if err != nil { 65 return err 66 } 67 checkpoint = containerd.NewImage(client, ck) 68 } 69 70 opts := []containerd.RestoreOpts{ 71 containerd.WithRestoreImage, 72 containerd.WithRestoreSpec, 73 containerd.WithRestoreRuntime, 74 } 75 if context.Bool("rw") { 76 opts = append(opts, containerd.WithRestoreRW) 77 } 78 79 ctr, err := client.Restore(ctx, id, checkpoint, opts...) 80 if err != nil { 81 return err 82 } 83 84 topts := []containerd.NewTaskOpts{} 85 if context.Bool("live") { 86 topts = append(topts, containerd.WithTaskCheckpoint(checkpoint)) 87 } 88 89 task, err := ctr.NewTask(ctx, cio.NewCreator(cio.WithStdio), topts...) 90 if err != nil { 91 return err 92 } 93 94 return task.Start(ctx) 95 }, 96 }