diff --git a/libpod/runtime_migrate.go b/libpod/runtime_migrate.go new file mode 100644 index 0000000000..6b2f921898 --- /dev/null +++ b/libpod/runtime_migrate.go @@ -0,0 +1,88 @@ +//go:build !remote + +package libpod + +import ( + "fmt" + "path/filepath" + + "github.com/containers/podman/v5/libpod/define" + "github.com/sirupsen/logrus" +) + +// Migrate stops the rootless pause process and performs any necessary database +// migrations that are required. It can also migrate all containers to a new OCI +// runtime, if requested. +func (r *Runtime) Migrate(newRuntime string) error { + // Acquire the alive lock and hold it. + // Ensures that we don't let other Podman commands run while we are + // rewriting things in the DB. + aliveLock, err := r.getRuntimeAliveLock() + if err != nil { + return fmt.Errorf("retrieving alive lock: %w", err) + } + aliveLock.Lock() + defer aliveLock.Unlock() + + if !r.valid { + return define.ErrRuntimeStopped + } + + runningContainers, err := r.GetRunningContainers() + if err != nil { + return err + } + + allCtrs, err := r.state.AllContainers(false) + if err != nil { + return err + } + + logrus.Infof("Stopping all containers") + for _, ctr := range runningContainers { + fmt.Printf("stopped %s\n", ctr.ID()) + if err := ctr.Stop(); err != nil { + return fmt.Errorf("cannot stop container %s: %w", ctr.ID(), err) + } + } + + // Did the user request a new runtime? + runtimeChangeRequested := newRuntime != "" + var requestedRuntime OCIRuntime + if runtimeChangeRequested { + runtime, exists := r.ociRuntimes[newRuntime] + if !exists { + return fmt.Errorf("change to runtime %q requested but no such runtime is defined: %w", newRuntime, define.ErrInvalidArg) + } + requestedRuntime = runtime + } + + for _, ctr := range allCtrs { + needsWrite := false + + // Reset pause process location + oldLocation := filepath.Join(ctr.state.RunDir, "conmon.pid") + if ctr.config.ConmonPidFile == oldLocation { + logrus.Infof("Changing conmon PID file for %s", ctr.ID()) + ctr.config.ConmonPidFile = filepath.Join(ctr.config.StaticDir, "conmon.pid") + needsWrite = true + } + + // Reset runtime + if runtimeChangeRequested && ctr.config.OCIRuntime != newRuntime { + logrus.Infof("Resetting container %s runtime to runtime %s", ctr.ID(), newRuntime) + ctr.config.OCIRuntime = newRuntime + ctr.ociRuntime = requestedRuntime + + needsWrite = true + } + + if needsWrite { + if err := r.state.RewriteContainerConfig(ctr, ctr.config); err != nil { + return fmt.Errorf("rewriting config for container %s: %w", ctr.ID(), err) + } + } + } + + return r.stopPauseProcess() +} diff --git a/libpod/runtime_migrate_freebsd.go b/libpod/runtime_migrate_freebsd.go new file mode 100644 index 0000000000..47c94a4d0c --- /dev/null +++ b/libpod/runtime_migrate_freebsd.go @@ -0,0 +1,7 @@ +//go:build !remote + +package libpod + +func (r *Runtime) stopPauseProcess() error { + return nil +} diff --git a/libpod/runtime_migrate_linux.go b/libpod/runtime_migrate_linux.go index 002acb09a4..162d72edf2 100644 --- a/libpod/runtime_migrate_linux.go +++ b/libpod/runtime_migrate_linux.go @@ -5,14 +5,11 @@ package libpod import ( "fmt" "os" - "path/filepath" "strconv" "syscall" - "github.com/containers/podman/v5/libpod/define" "github.com/containers/podman/v5/pkg/rootless" "github.com/containers/podman/v5/pkg/util" - "github.com/sirupsen/logrus" ) func (r *Runtime) stopPauseProcess() error { @@ -41,80 +38,3 @@ func (r *Runtime) stopPauseProcess() error { } return nil } - -// Migrate stops the rootless pause process and performs any necessary database -// migrations that are required. It can also migrate all containers to a new OCI -// runtime, if requested. -func (r *Runtime) Migrate(newRuntime string) error { - // Acquire the alive lock and hold it. - // Ensures that we don't let other Podman commands run while we are - // rewriting things in the DB. - aliveLock, err := r.getRuntimeAliveLock() - if err != nil { - return fmt.Errorf("retrieving alive lock: %w", err) - } - aliveLock.Lock() - defer aliveLock.Unlock() - - if !r.valid { - return define.ErrRuntimeStopped - } - - runningContainers, err := r.GetRunningContainers() - if err != nil { - return err - } - - allCtrs, err := r.state.AllContainers(false) - if err != nil { - return err - } - - logrus.Infof("Stopping all containers") - for _, ctr := range runningContainers { - fmt.Printf("stopped %s\n", ctr.ID()) - if err := ctr.Stop(); err != nil { - return fmt.Errorf("cannot stop container %s: %w", ctr.ID(), err) - } - } - - // Did the user request a new runtime? - runtimeChangeRequested := newRuntime != "" - var requestedRuntime OCIRuntime - if runtimeChangeRequested { - runtime, exists := r.ociRuntimes[newRuntime] - if !exists { - return fmt.Errorf("change to runtime %q requested but no such runtime is defined: %w", newRuntime, define.ErrInvalidArg) - } - requestedRuntime = runtime - } - - for _, ctr := range allCtrs { - needsWrite := false - - // Reset pause process location - oldLocation := filepath.Join(ctr.state.RunDir, "conmon.pid") - if ctr.config.ConmonPidFile == oldLocation { - logrus.Infof("Changing conmon PID file for %s", ctr.ID()) - ctr.config.ConmonPidFile = filepath.Join(ctr.config.StaticDir, "conmon.pid") - needsWrite = true - } - - // Reset runtime - if runtimeChangeRequested && ctr.config.OCIRuntime != newRuntime { - logrus.Infof("Resetting container %s runtime to runtime %s", ctr.ID(), newRuntime) - ctr.config.OCIRuntime = newRuntime - ctr.ociRuntime = requestedRuntime - - needsWrite = true - } - - if needsWrite { - if err := r.state.RewriteContainerConfig(ctr, ctr.config); err != nil { - return fmt.Errorf("rewriting config for container %s: %w", ctr.ID(), err) - } - } - } - - return r.stopPauseProcess() -} diff --git a/libpod/runtime_migrate_unsupported.go b/libpod/runtime_migrate_unsupported.go index 9a8c2fda7f..6355ab4ecb 100644 --- a/libpod/runtime_migrate_unsupported.go +++ b/libpod/runtime_migrate_unsupported.go @@ -1,4 +1,4 @@ -//go:build !remote && !linux +//go:build !remote && !linux && !freebsd package libpod @@ -9,7 +9,3 @@ import ( func (r *Runtime) stopPauseProcess() error { return errors.New("not implemented (*Runtime) stopPauseProcess") } - -func (r *Runtime) Migrate(newRuntime string) error { - return errors.New("not implemented (*Runtime) migrate") -}