github.com/lalkh/containerd@v1.4.3/cmd/ctr/commands/containers/checkpoint.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  	"fmt"
    21  
    22  	"github.com/containerd/containerd"
    23  	"github.com/containerd/containerd/cmd/ctr/commands"
    24  	"github.com/containerd/containerd/errdefs"
    25  	"github.com/pkg/errors"
    26  	"github.com/urfave/cli"
    27  )
    28  
    29  var checkpointCommand = cli.Command{
    30  	Name:      "checkpoint",
    31  	Usage:     "checkpoint a container",
    32  	ArgsUsage: "CONTAINER REF",
    33  	Flags: []cli.Flag{
    34  		cli.BoolFlag{
    35  			Name:  "rw",
    36  			Usage: "include the rw layer in the checkpoint",
    37  		},
    38  		cli.BoolFlag{
    39  			Name:  "image",
    40  			Usage: "include the image in the checkpoint",
    41  		},
    42  		cli.BoolFlag{
    43  			Name:  "task",
    44  			Usage: "checkpoint container task",
    45  		},
    46  	},
    47  	Action: func(context *cli.Context) error {
    48  		id := context.Args().First()
    49  		if id == "" {
    50  			return errors.New("container id must be provided")
    51  		}
    52  		ref := context.Args().Get(1)
    53  		if ref == "" {
    54  			return errors.New("ref must be provided")
    55  		}
    56  		client, ctx, cancel, err := commands.NewClient(context)
    57  		if err != nil {
    58  			return err
    59  		}
    60  		defer cancel()
    61  		opts := []containerd.CheckpointOpts{
    62  			containerd.WithCheckpointRuntime,
    63  		}
    64  
    65  		if context.Bool("image") {
    66  			opts = append(opts, containerd.WithCheckpointImage)
    67  		}
    68  		if context.Bool("rw") {
    69  			opts = append(opts, containerd.WithCheckpointRW)
    70  		}
    71  		if context.Bool("task") {
    72  			opts = append(opts, containerd.WithCheckpointTask)
    73  		}
    74  		container, err := client.LoadContainer(ctx, id)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		task, err := container.Task(ctx, nil)
    79  		if err != nil {
    80  			if !errdefs.IsNotFound(err) {
    81  				return err
    82  			}
    83  		}
    84  		// pause if running
    85  		if task != nil {
    86  			if err := task.Pause(ctx); err != nil {
    87  				return err
    88  			}
    89  			defer func() {
    90  				if err := task.Resume(ctx); err != nil {
    91  					fmt.Println(errors.Wrap(err, "error resuming task"))
    92  				}
    93  			}()
    94  		}
    95  
    96  		if _, err := container.Checkpoint(ctx, ref, opts...); err != nil {
    97  			return err
    98  		}
    99  
   100  		return nil
   101  	},
   102  }