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

     1  package main
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podman/cliconfig"
     5  	"github.com/containers/libpod/cmd/podman/libpodruntime"
     6  	"github.com/pkg/errors"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  var (
    11  	migrateCommand     cliconfig.SystemMigrateValues
    12  	migrateDescription = `
    13          podman system migrate
    14  
    15          Migrate existing containers to a new version of Podman.
    16  `
    17  
    18  	_migrateCommand = &cobra.Command{
    19  		Use:   "migrate",
    20  		Args:  noSubArgs,
    21  		Short: "Migrate containers",
    22  		Long:  migrateDescription,
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			migrateCommand.InputArgs = args
    25  			migrateCommand.GlobalFlags = MainGlobalOpts
    26  			return migrateCmd(&migrateCommand)
    27  		},
    28  	}
    29  )
    30  
    31  func init() {
    32  	migrateCommand.Command = _migrateCommand
    33  	migrateCommand.SetHelpTemplate(HelpTemplate())
    34  	migrateCommand.SetUsageTemplate(UsageTemplate())
    35  	flags := migrateCommand.Flags()
    36  	flags.StringVar(&migrateCommand.NewRuntime, "new-runtime", "", "Specify a new runtime for all containers")
    37  }
    38  
    39  func migrateCmd(c *cliconfig.SystemMigrateValues) error {
    40  	// We need to pass one extra option to NewRuntime.
    41  	// This will inform the OCI runtime to start a migrate.
    42  	// That's controlled by the last argument to GetRuntime.
    43  	r, err := libpodruntime.GetRuntimeMigrate(getContext(), &c.PodmanCommand, c.NewRuntime)
    44  	if err != nil {
    45  		return errors.Wrapf(err, "error migrating containers")
    46  	}
    47  	if err := r.Shutdown(false); err != nil {
    48  		return err
    49  	}
    50  
    51  	return nil
    52  }