diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2019-04-26 15:28:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-26 15:28:15 -0700 |
commit | fe3acddcbe02cfa258170707791bd096dc909022 (patch) | |
tree | 9a68b96d5f85adcc67189e97e1c788786c20b32c /libpod | |
parent | b6e2cbad0a4581a8fe4954ff4a9202461a526781 (diff) | |
parent | f49e0c19ede56b1cc6b1d44ea9ba8b336cd22658 (diff) | |
download | podman-fe3acddcbe02cfa258170707791bd096dc909022.tar.gz podman-fe3acddcbe02cfa258170707791bd096dc909022.tar.bz2 podman-fe3acddcbe02cfa258170707791bd096dc909022.zip |
Merge pull request #2950 from giuseppe/podman-system-migrate
system: add new subcommand "migrate"
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/options.go | 30 | ||||
-rw-r--r-- | libpod/runtime.go | 23 | ||||
-rw-r--r-- | libpod/runtime_migrate.go | 47 |
3 files changed, 100 insertions, 0 deletions
diff --git a/libpod/options.go b/libpod/options.go index 8038f1935..9932d5453 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -1,6 +1,7 @@ package libpod import ( + "context" "net" "os" "path/filepath" @@ -436,6 +437,22 @@ func WithRenumber() RuntimeOption { } } +// WithMigrate instructs libpod to perform a lock migrateing while +// initializing. This will handle migrations from early versions of libpod with +// file locks to newer versions with SHM locking, as well as changes in the +// number of configured locks. +func WithMigrate() RuntimeOption { + return func(rt *Runtime) error { + if rt.valid { + return ErrRuntimeFinalized + } + + rt.doMigrate = true + + return nil + } +} + // Container Creation Options // WithShmDir sets the directory that should be mounted on /dev/shm. @@ -450,6 +467,19 @@ func WithShmDir(dir string) CtrCreateOption { } } +// WithContext sets the context to use. +func WithContext(ctx context.Context) RuntimeOption { + return func(rt *Runtime) error { + if rt.valid { + return ErrRuntimeFinalized + } + + rt.ctx = ctx + + return nil + } +} + // WithSystemd turns on systemd mode in the container func WithSystemd() CtrCreateOption { return func(ctr *Container) error { diff --git a/libpod/runtime.go b/libpod/runtime.go index 69cc10389..e85242028 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -1,6 +1,7 @@ package libpod import ( + "context" "fmt" "io/ioutil" "os" @@ -100,6 +101,8 @@ type Runtime struct { // unused. doRenumber bool + doMigrate bool + // valid indicates whether the runtime is ready to use. // valid is set to true when a runtime is returned from GetRuntime(), // and remains true until the runtime is shut down (rendering its @@ -109,6 +112,8 @@ type Runtime struct { // mechanism to read and write even logs eventer events.Eventer + + ctx context.Context } // OCIRuntimePath contains information about an OCI runtime. @@ -962,6 +967,24 @@ func makeRuntime(runtime *Runtime) (err error) { // further runtime.valid = true + if runtime.doMigrate { + if os.Geteuid() != 0 { + aliveLock.Unlock() + locked = false + + became, ret, err := rootless.BecomeRootInUserNS() + if err != nil { + return err + } + if became { + os.Exit(ret) + } + } + if err := runtime.migrate(); err != nil { + return err + } + } + return nil } diff --git a/libpod/runtime_migrate.go b/libpod/runtime_migrate.go new file mode 100644 index 000000000..a084df289 --- /dev/null +++ b/libpod/runtime_migrate.go @@ -0,0 +1,47 @@ +package libpod + +import ( + "path/filepath" + + "github.com/pkg/errors" + "github.com/sirupsen/logrus" +) + +func (r *Runtime) migrate() error { + runningContainers, err := r.GetRunningContainers() + if err != nil { + return err + } + + allCtrs, err := r.state.AllContainers() + if err != nil { + return err + } + + logrus.Infof("stopping all containers") + for _, ctr := range runningContainers { + logrus.Infof("stopping %s", ctr.ID()) + if err := ctr.Stop(); err != nil { + return errors.Wrapf(err, "cannot stop container %s", ctr.ID()) + } + } + + for _, ctr := range allCtrs { + 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") + if err := r.state.RewriteContainerConfig(ctr, ctr.config); err != nil { + return errors.Wrapf(err, "error rewriting config for container %s", ctr.ID()) + } + } + } + + for _, ctr := range runningContainers { + if err := ctr.Start(r.ctx, true); err != nil { + logrus.Errorf("error restarting container %s", ctr.ID()) + } + } + + return nil +} |