2019-05-09 17:06:46 +00:00
|
|
|
// +build linux
|
|
|
|
|
2019-04-15 20:03:47 +00:00
|
|
|
package libpod
|
|
|
|
|
|
|
|
import (
|
2019-05-01 19:07:30 +00:00
|
|
|
"context"
|
2019-05-08 11:44:06 +00:00
|
|
|
"fmt"
|
2019-05-09 17:06:46 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-04-15 20:03:47 +00:00
|
|
|
"path/filepath"
|
2019-05-09 17:06:46 +00:00
|
|
|
"strconv"
|
|
|
|
"syscall"
|
2019-04-15 20:03:47 +00:00
|
|
|
|
2020-07-06 13:38:20 +00:00
|
|
|
"github.com/containers/libpod/v2/libpod/define"
|
|
|
|
"github.com/containers/libpod/v2/pkg/rootless"
|
|
|
|
"github.com/containers/libpod/v2/pkg/util"
|
2019-04-15 20:03:47 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2019-05-09 17:06:46 +00:00
|
|
|
func stopPauseProcess() error {
|
|
|
|
if rootless.IsRootless() {
|
|
|
|
pausePidPath, err := util.GetRootlessPauseProcessPidPath()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get pause process pid file path")
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(pausePidPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Wrapf(err, "cannot read pause process pid file %s", pausePidPath)
|
|
|
|
}
|
|
|
|
pausePid, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "cannot parse pause pid file %s", pausePidPath)
|
|
|
|
}
|
|
|
|
if err := os.Remove(pausePidPath); err != nil {
|
|
|
|
return errors.Wrapf(err, "cannot delete pause pid file %s", pausePidPath)
|
|
|
|
}
|
2019-07-03 20:37:17 +00:00
|
|
|
if err := syscall.Kill(pausePid, syscall.SIGKILL); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-09 17:06:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-01 19:07:30 +00:00
|
|
|
func (r *Runtime) migrate(ctx context.Context) error {
|
2019-04-15 20:03:47 +00:00
|
|
|
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 {
|
2019-05-08 11:44:06 +00:00
|
|
|
fmt.Printf("stopped %s\n", ctr.ID())
|
2019-04-15 20:03:47 +00:00
|
|
|
if err := ctr.Stop(); err != nil {
|
|
|
|
return errors.Wrapf(err, "cannot stop container %s", ctr.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 18:12:58 +00:00
|
|
|
// Did the user request a new runtime?
|
|
|
|
runtimeChangeRequested := r.migrateRuntime != ""
|
|
|
|
requestedRuntime, runtimeExists := r.ociRuntimes[r.migrateRuntime]
|
|
|
|
if !runtimeExists && runtimeChangeRequested {
|
|
|
|
return errors.Wrapf(define.ErrInvalidArg, "change to runtime %q requested but no such runtime is defined", r.migrateRuntime)
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:03:47 +00:00
|
|
|
for _, ctr := range allCtrs {
|
2019-10-08 18:12:58 +00:00
|
|
|
needsWrite := false
|
|
|
|
|
|
|
|
// Reset pause process location
|
2019-04-15 20:03:47 +00:00
|
|
|
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")
|
2019-10-08 18:12:58 +00:00
|
|
|
needsWrite = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset runtime
|
|
|
|
if runtimeChangeRequested {
|
|
|
|
logrus.Infof("Resetting container %s runtime to runtime %s", ctr.ID(), r.migrateRuntime)
|
|
|
|
ctr.config.OCIRuntime = r.migrateRuntime
|
|
|
|
ctr.ociRuntime = requestedRuntime
|
|
|
|
|
|
|
|
needsWrite = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if needsWrite {
|
2019-04-15 20:03:47 +00:00
|
|
|
if err := r.state.RewriteContainerConfig(ctr, ctr.config); err != nil {
|
|
|
|
return errors.Wrapf(err, "error rewriting config for container %s", ctr.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:06:46 +00:00
|
|
|
return stopPauseProcess()
|
2019-04-15 20:03:47 +00:00
|
|
|
}
|