github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/restore.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podman/cliconfig"
     5  	"github.com/containers/libpod/pkg/adapter"
     6  	"github.com/containers/libpod/pkg/rootless"
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var (
    12  	restoreCommand     cliconfig.RestoreValues
    13  	restoreDescription = `
    14     podman container restore
    15  
    16     Restores a container from a checkpoint. The container name or ID can be used.
    17  `
    18  	_restoreCommand = &cobra.Command{
    19  		Use:   "restore [flags] CONTAINER [CONTAINER...]",
    20  		Short: "Restores one or more containers from a checkpoint",
    21  		Long:  restoreDescription,
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			restoreCommand.InputArgs = args
    24  			restoreCommand.GlobalFlags = MainGlobalOpts
    25  			restoreCommand.Remote = remoteclient
    26  			return restoreCmd(&restoreCommand, cmd)
    27  		},
    28  		Args: func(cmd *cobra.Command, args []string) error {
    29  			return checkAllLatestAndCIDFile(cmd, args, true, false)
    30  		},
    31  		Example: `podman container restore ctrID
    32    podman container restore --latest
    33    podman container restore --all`,
    34  	}
    35  )
    36  
    37  func init() {
    38  	restoreCommand.Command = _restoreCommand
    39  	restoreCommand.SetHelpTemplate(HelpTemplate())
    40  	restoreCommand.SetUsageTemplate(UsageTemplate())
    41  	flags := restoreCommand.Flags()
    42  	flags.BoolVarP(&restoreCommand.All, "all", "a", false, "Restore all checkpointed containers")
    43  	flags.BoolVarP(&restoreCommand.Keep, "keep", "k", false, "Keep all temporary checkpoint files")
    44  	flags.BoolVarP(&restoreCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
    45  	flags.BoolVar(&restoreCommand.TcpEstablished, "tcp-established", false, "Restore a container with established TCP connections")
    46  	flags.StringVarP(&restoreCommand.Import, "import", "i", "", "Restore from exported checkpoint archive (tar.gz)")
    47  	flags.StringVarP(&restoreCommand.Name, "name", "n", "", "Specify new name for container restored from exported checkpoint (only works with --import)")
    48  	flags.BoolVar(&restoreCommand.IgnoreRootfs, "ignore-rootfs", false, "Do not apply root file-system changes when importing from exported checkpoint")
    49  	flags.BoolVar(&restoreCommand.IgnoreStaticIP, "ignore-static-ip", false, "Ignore IP address set via --static-ip")
    50  	flags.BoolVar(&restoreCommand.IgnoreStaticMAC, "ignore-static-mac", false, "Ignore MAC address set via --mac-address")
    51  
    52  	markFlagHiddenForRemoteClient("latest", flags)
    53  }
    54  
    55  func restoreCmd(c *cliconfig.RestoreValues, cmd *cobra.Command) error {
    56  	if rootless.IsRootless() {
    57  		return errors.New("restoring a container requires root")
    58  	}
    59  
    60  	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
    61  	if err != nil {
    62  		return errors.Wrapf(err, "could not get runtime")
    63  	}
    64  	defer runtime.DeferredShutdown(false)
    65  
    66  	if c.Import == "" && c.IgnoreRootfs {
    67  		return errors.Errorf("--ignore-rootfs can only be used with --import")
    68  	}
    69  
    70  	if c.Import == "" && c.Name != "" {
    71  		return errors.Errorf("--name can only be used with --import")
    72  	}
    73  
    74  	if c.Name != "" && c.TcpEstablished {
    75  		return errors.Errorf("--tcp-established cannot be used with --name")
    76  	}
    77  
    78  	argLen := len(c.InputArgs)
    79  	if c.Import != "" {
    80  		if c.All || c.Latest {
    81  			return errors.Errorf("Cannot use --import with --all or --latest")
    82  		}
    83  		if argLen > 0 {
    84  			return errors.Errorf("Cannot use --import with positional arguments")
    85  		}
    86  	}
    87  
    88  	if (c.All || c.Latest) && argLen > 0 {
    89  		return errors.Errorf("no arguments are needed with --all or --latest")
    90  	}
    91  	if argLen < 1 && !c.All && !c.Latest && c.Import == "" {
    92  		return errors.Errorf("you must provide at least one name or id")
    93  	}
    94  
    95  	return runtime.Restore(getContext(), c)
    96  }