2014-10-23 04:45:23 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
2014-10-31 06:08:28 +08:00
|
|
|
import (
|
2015-10-17 23:35:36 +08:00
|
|
|
"bytes"
|
2015-02-12 08:45:23 +08:00
|
|
|
"encoding/json"
|
2014-12-15 23:05:11 +08:00
|
|
|
"fmt"
|
2015-10-17 23:35:36 +08:00
|
|
|
"io"
|
2015-04-29 00:49:44 +08:00
|
|
|
"io/ioutil"
|
2014-12-15 23:05:11 +08:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-02-12 08:45:23 +08:00
|
|
|
"path/filepath"
|
2015-08-31 19:34:14 +08:00
|
|
|
"reflect"
|
2015-03-19 11:22:21 +08:00
|
|
|
"strings"
|
2015-02-14 06:41:37 +08:00
|
|
|
"sync"
|
2014-12-15 23:05:11 +08:00
|
|
|
"syscall"
|
|
|
|
|
2015-05-06 21:14:04 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-06-24 06:55:02 +08:00
|
|
|
"github.com/golang/protobuf/proto"
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/criurpc"
|
2015-10-17 23:35:36 +08:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2014-10-31 06:08:28 +08:00
|
|
|
)
|
2014-10-23 04:45:23 +08:00
|
|
|
|
2015-04-09 05:14:51 +08:00
|
|
|
const stdioFdCount = 3
|
|
|
|
|
2014-10-23 04:45:23 +08:00
|
|
|
type linuxContainer struct {
|
2014-10-23 07:53:28 +08:00
|
|
|
id string
|
|
|
|
root string
|
2014-12-17 17:12:23 +08:00
|
|
|
config *configs.Config
|
2015-01-13 05:54:00 +08:00
|
|
|
cgroupManager cgroups.Manager
|
2015-02-25 09:53:11 +08:00
|
|
|
initPath string
|
2014-12-15 23:05:11 +08:00
|
|
|
initArgs []string
|
2015-02-07 13:12:27 +08:00
|
|
|
initProcess parentProcess
|
2015-03-07 03:21:02 +08:00
|
|
|
criuPath string
|
2015-02-14 06:41:37 +08:00
|
|
|
m sync.Mutex
|
2015-08-31 19:34:14 +08:00
|
|
|
criuVersion int
|
2015-10-03 02:16:50 +08:00
|
|
|
state containerState
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-10-24 00:22:48 +08:00
|
|
|
// State represents a running container's state
|
|
|
|
type State struct {
|
|
|
|
BaseState
|
|
|
|
|
|
|
|
// Platform specific fields below here
|
|
|
|
|
|
|
|
// Path to all the cgroups setup for a container. Key is cgroup subsystem name
|
|
|
|
// with the value as the path.
|
|
|
|
CgroupPaths map[string]string `json:"cgroup_paths"`
|
|
|
|
|
|
|
|
// NamespacePaths are filepaths to the container's namespaces. Key is the namespace type
|
|
|
|
// with the value as the path.
|
|
|
|
NamespacePaths map[configs.NamespaceType]string `json:"namespace_paths"`
|
|
|
|
|
|
|
|
// Container's standard descriptors (std{in,out,err}), needed for checkpoint and restore
|
|
|
|
ExternalDescriptors []string `json:"external_descriptors,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-10-24 00:30:32 +08:00
|
|
|
// A libcontainer container object.
|
|
|
|
//
|
|
|
|
// Each container is thread-safe within the same process. Since a container can
|
|
|
|
// be destroyed by a separate process, any function may return that the container
|
|
|
|
// was not found.
|
|
|
|
type Container interface {
|
|
|
|
BaseContainer
|
|
|
|
|
|
|
|
// Methods below here are platform specific
|
|
|
|
|
|
|
|
// Checkpoint checkpoints the running container's state to disk using the criu(8) utility.
|
|
|
|
//
|
|
|
|
// errors:
|
|
|
|
// Systemerror - System error.
|
|
|
|
Checkpoint(criuOpts *CriuOpts) error
|
|
|
|
|
|
|
|
// Restore restores the checkpointed container to a running state using the criu(8) utiity.
|
|
|
|
//
|
|
|
|
// errors:
|
|
|
|
// Systemerror - System error.
|
|
|
|
Restore(process *Process, criuOpts *CriuOpts) error
|
|
|
|
|
|
|
|
// If the Container state is RUNNING or PAUSING, sets the Container state to PAUSING and pauses
|
|
|
|
// the execution of any user processes. Asynchronously, when the container finished being paused the
|
|
|
|
// state is changed to PAUSED.
|
|
|
|
// If the Container state is PAUSED, do nothing.
|
|
|
|
//
|
|
|
|
// errors:
|
|
|
|
// ContainerDestroyed - Container no longer exists,
|
|
|
|
// Systemerror - System error.
|
|
|
|
Pause() error
|
|
|
|
|
|
|
|
// If the Container state is PAUSED, resumes the execution of any user processes in the
|
|
|
|
// Container before setting the Container state to RUNNING.
|
|
|
|
// If the Container state is RUNNING, do nothing.
|
|
|
|
//
|
|
|
|
// errors:
|
|
|
|
// ContainerDestroyed - Container no longer exists,
|
|
|
|
// Systemerror - System error.
|
|
|
|
Resume() error
|
|
|
|
|
|
|
|
// NotifyOOM returns a read-only channel signaling when the container receives an OOM notification.
|
|
|
|
//
|
|
|
|
// errors:
|
|
|
|
// Systemerror - System error.
|
|
|
|
NotifyOOM() (<-chan struct{}, error)
|
2015-12-08 23:33:47 +08:00
|
|
|
|
|
|
|
// NotifyMemoryPressure returns a read-only channel signaling when the container reaches a given pressure level
|
|
|
|
//
|
|
|
|
// errors:
|
|
|
|
// Systemerror - System error.
|
|
|
|
NotifyMemoryPressure(level PressureLevel) (<-chan struct{}, error)
|
2015-10-24 00:30:32 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 13:21:06 +08:00
|
|
|
// ID returns the container's unique ID
|
2014-10-23 04:45:23 +08:00
|
|
|
func (c *linuxContainer) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2015-02-01 13:21:06 +08:00
|
|
|
// Config returns the container's configuration
|
|
|
|
func (c *linuxContainer) Config() configs.Config {
|
|
|
|
return *c.config
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 08:45:23 +08:00
|
|
|
func (c *linuxContainer) Status() (Status, error) {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
return c.currentStatus()
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 06:45:07 +08:00
|
|
|
func (c *linuxContainer) State() (*State, error) {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
return c.currentState()
|
2015-02-12 06:45:07 +08:00
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (c *linuxContainer) Processes() ([]int, error) {
|
2016-01-09 03:37:18 +08:00
|
|
|
pids, err := c.cgroupManager.GetAllPids()
|
2014-10-23 04:45:23 +08:00
|
|
|
if err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return nil, newSystemError(err)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
return pids, nil
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func (c *linuxContainer) Stats() (*Stats, error) {
|
2014-10-23 04:45:23 +08:00
|
|
|
var (
|
|
|
|
err error
|
2015-02-01 11:56:27 +08:00
|
|
|
stats = &Stats{}
|
2014-10-23 04:45:23 +08:00
|
|
|
)
|
2014-12-06 09:02:49 +08:00
|
|
|
if stats.CgroupStats, err = c.cgroupManager.GetStats(); err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return stats, newSystemError(err)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
for _, iface := range c.config.Networks {
|
2015-02-10 07:16:27 +08:00
|
|
|
switch iface.Type {
|
|
|
|
case "veth":
|
2015-02-11 03:51:45 +08:00
|
|
|
istats, err := getNetworkInterfaceStats(iface.HostInterfaceName)
|
2015-02-10 07:16:27 +08:00
|
|
|
if err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return stats, newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-10 07:16:27 +08:00
|
|
|
stats.Interfaces = append(stats.Interfaces, istats)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
return stats, nil
|
|
|
|
}
|
2014-10-28 08:51:14 +08:00
|
|
|
|
2015-03-11 16:46:54 +08:00
|
|
|
func (c *linuxContainer) Set(config configs.Config) error {
|
2015-02-27 12:09:42 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2015-03-11 16:46:54 +08:00
|
|
|
c.config = &config
|
2015-02-27 12:09:42 +08:00
|
|
|
return c.cgroupManager.Set(c.config)
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:26:43 +08:00
|
|
|
func (c *linuxContainer) Start(process *Process) error {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
status, err := c.currentStatus()
|
2014-12-15 23:05:11 +08:00
|
|
|
if err != nil {
|
2015-02-23 17:26:43 +08:00
|
|
|
return err
|
2014-12-15 23:05:11 +08:00
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
doInit := status == Destroyed
|
2015-02-07 13:12:27 +08:00
|
|
|
parent, err := c.newParentProcess(process, doInit)
|
|
|
|
if err != nil {
|
2015-02-23 17:26:43 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
if err := parent.start(); err != nil {
|
|
|
|
// terminate the process to ensure that it properly is reaped.
|
|
|
|
if err := parent.terminate(); err != nil {
|
2015-05-06 21:14:04 +08:00
|
|
|
logrus.Warn(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
return newSystemError(err)
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2016-01-22 08:43:33 +08:00
|
|
|
c.state = &runningState{
|
|
|
|
c: c,
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
if doInit {
|
2015-10-03 02:16:50 +08:00
|
|
|
if err := c.updateState(parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-22 08:43:33 +08:00
|
|
|
if c.config.Hooks != nil {
|
|
|
|
s := configs.HookState{
|
|
|
|
Version: c.config.Version,
|
|
|
|
ID: c.id,
|
|
|
|
Pid: parent.pid(),
|
|
|
|
Root: c.config.Rootfs,
|
|
|
|
}
|
|
|
|
for _, hook := range c.config.Hooks.Poststart {
|
|
|
|
if err := hook.Run(s); err != nil {
|
|
|
|
if err := parent.terminate(); err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
return newSystemError(err)
|
2015-11-07 07:03:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 17:26:43 +08:00
|
|
|
return nil
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 07:48:19 +08:00
|
|
|
func (c *linuxContainer) Signal(s os.Signal) error {
|
|
|
|
if err := c.initProcess.signal(s); err != nil {
|
|
|
|
return newSystemError(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-07 13:12:27 +08:00
|
|
|
func (c *linuxContainer) newParentProcess(p *Process, doInit bool) (parentProcess, error) {
|
|
|
|
parentPipe, childPipe, err := newPipe()
|
|
|
|
if err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return nil, newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
cmd, err := c.commandTemplate(p, childPipe)
|
|
|
|
if err != nil {
|
2015-02-12 08:45:23 +08:00
|
|
|
return nil, newSystemError(err)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
if !doInit {
|
2015-10-17 23:14:26 +08:00
|
|
|
return c.newSetnsProcess(p, cmd, parentPipe, childPipe)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
2015-02-13 04:58:40 +08:00
|
|
|
return c.newInitProcess(p, cmd, parentPipe, childPipe)
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) commandTemplate(p *Process, childPipe *os.File) (*exec.Cmd, error) {
|
2015-02-25 09:53:11 +08:00
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: c.initPath,
|
|
|
|
Args: c.initArgs,
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
cmd.Stdin = p.Stdin
|
|
|
|
cmd.Stdout = p.Stdout
|
|
|
|
cmd.Stderr = p.Stderr
|
2015-02-04 09:44:58 +08:00
|
|
|
cmd.Dir = c.config.Rootfs
|
2014-12-23 06:06:22 +08:00
|
|
|
if cmd.SysProcAttr == nil {
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
}
|
2015-04-09 05:14:51 +08:00
|
|
|
cmd.ExtraFiles = append(p.ExtraFiles, childPipe)
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("_LIBCONTAINER_INITPIPE=%d", stdioFdCount+len(cmd.ExtraFiles)-1))
|
2015-04-03 04:55:55 +08:00
|
|
|
// NOTE: when running a container with no PID namespace and the parent process spawning the container is
|
|
|
|
// PID1 the pdeathsig is being delivered to the container's init process by the kernel for some reason
|
|
|
|
// even with the parent still running.
|
2015-02-12 08:45:23 +08:00
|
|
|
if c.config.ParentDeathSignal > 0 {
|
|
|
|
cmd.SysProcAttr.Pdeathsig = syscall.Signal(c.config.ParentDeathSignal)
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
return cmd, nil
|
2014-12-15 23:05:11 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 04:58:40 +08:00
|
|
|
func (c *linuxContainer) newInitProcess(p *Process, cmd *exec.Cmd, parentPipe, childPipe *os.File) (*initProcess, error) {
|
2016-01-20 11:02:31 +08:00
|
|
|
t := "_LIBCONTAINER_INITTYPE=" + string(initStandard)
|
2015-02-07 13:12:27 +08:00
|
|
|
cloneFlags := c.config.Namespaces.CloneFlags()
|
|
|
|
if cloneFlags&syscall.CLONE_NEWUSER != 0 {
|
2015-02-13 04:58:40 +08:00
|
|
|
if err := c.addUidGidMappings(cmd.SysProcAttr); err != nil {
|
2015-02-14 08:06:17 +08:00
|
|
|
// user mappings are not supported
|
2015-02-13 04:58:40 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-08-01 04:33:31 +08:00
|
|
|
enableSetgroups(cmd.SysProcAttr)
|
2015-02-07 13:12:27 +08:00
|
|
|
// Default to root user when user namespaces are enabled.
|
|
|
|
if cmd.SysProcAttr.Credential == nil {
|
|
|
|
cmd.SysProcAttr.Credential = &syscall.Credential{}
|
2015-02-07 04:48:57 +08:00
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2015-02-11 03:51:45 +08:00
|
|
|
cmd.Env = append(cmd.Env, t)
|
2015-02-07 13:12:27 +08:00
|
|
|
cmd.SysProcAttr.Cloneflags = cloneFlags
|
|
|
|
return &initProcess{
|
|
|
|
cmd: cmd,
|
|
|
|
childPipe: childPipe,
|
|
|
|
parentPipe: parentPipe,
|
|
|
|
manager: c.cgroupManager,
|
|
|
|
config: c.newInitConfig(p),
|
2015-09-11 09:15:00 +08:00
|
|
|
container: c,
|
2015-11-07 08:49:06 +08:00
|
|
|
process: p,
|
2015-02-13 04:58:40 +08:00
|
|
|
}, nil
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
2015-10-17 23:14:26 +08:00
|
|
|
func (c *linuxContainer) newSetnsProcess(p *Process, cmd *exec.Cmd, parentPipe, childPipe *os.File) (*setnsProcess, error) {
|
2016-01-20 11:02:31 +08:00
|
|
|
cmd.Env = append(cmd.Env, "_LIBCONTAINER_INITTYPE="+string(initSetns))
|
2015-10-17 23:35:36 +08:00
|
|
|
// for setns process, we dont have to set cloneflags as the process namespaces
|
|
|
|
// will only be set via setns syscall
|
|
|
|
data, err := c.bootstrapData(0, c.initProcess.pid(), p.consolePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-03-05 08:04:20 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
// TODO: set on container for process management
|
|
|
|
return &setnsProcess{
|
2015-10-17 23:35:36 +08:00
|
|
|
cmd: cmd,
|
|
|
|
cgroupPaths: c.cgroupManager.GetPaths(),
|
|
|
|
childPipe: childPipe,
|
|
|
|
parentPipe: parentPipe,
|
|
|
|
config: c.newInitConfig(p),
|
|
|
|
process: p,
|
|
|
|
bootstrapData: data,
|
2015-10-17 23:14:26 +08:00
|
|
|
}, nil
|
2015-02-07 13:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) newInitConfig(process *Process) *initConfig {
|
|
|
|
return &initConfig{
|
2015-04-01 05:40:05 +08:00
|
|
|
Config: c.config,
|
|
|
|
Args: process.Args,
|
|
|
|
Env: process.Env,
|
|
|
|
User: process.User,
|
|
|
|
Cwd: process.Cwd,
|
|
|
|
Console: process.consolePath,
|
|
|
|
Capabilities: process.Capabilities,
|
|
|
|
PassedFilesCount: len(process.ExtraFiles),
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2014-12-15 23:05:11 +08:00
|
|
|
}
|
|
|
|
|
2015-02-07 13:12:27 +08:00
|
|
|
func newPipe() (parent *os.File, child *os.File, err error) {
|
|
|
|
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
2015-02-01 13:21:06 +08:00
|
|
|
if err != nil {
|
2015-02-07 13:12:27 +08:00
|
|
|
return nil, nil, err
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
return os.NewFile(uintptr(fds[1]), "parent"), os.NewFile(uintptr(fds[0]), "child"), nil
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Destroy() error {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2015-10-03 02:16:50 +08:00
|
|
|
return c.state.destroy()
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Pause() error {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2016-01-22 08:43:33 +08:00
|
|
|
status, err := c.currentStatus()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if status != Running {
|
|
|
|
return newGenericError(fmt.Errorf("container not running"), ContainerNotRunning)
|
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
if err := c.cgroupManager.Freeze(configs.Frozen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.state.transition(&pausedState{
|
|
|
|
c: c,
|
|
|
|
})
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) Resume() error {
|
2015-02-14 06:41:37 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2016-01-22 08:43:33 +08:00
|
|
|
status, err := c.currentStatus()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if status != Paused {
|
|
|
|
return newGenericError(fmt.Errorf("container not paused"), ContainerNotPaused)
|
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
if err := c.cgroupManager.Freeze(configs.Thawed); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.state.transition(&runningState{
|
|
|
|
c: c,
|
|
|
|
})
|
2014-10-28 08:51:14 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 07:09:54 +08:00
|
|
|
func (c *linuxContainer) NotifyOOM() (<-chan struct{}, error) {
|
2015-02-12 09:12:03 +08:00
|
|
|
return notifyOnOOM(c.cgroupManager.GetPaths())
|
2015-02-01 13:21:06 +08:00
|
|
|
}
|
2015-02-12 08:45:23 +08:00
|
|
|
|
2015-12-08 23:33:47 +08:00
|
|
|
func (c *linuxContainer) NotifyMemoryPressure(level PressureLevel) (<-chan struct{}, error) {
|
|
|
|
return notifyMemoryPressure(c.cgroupManager.GetPaths(), level)
|
|
|
|
}
|
|
|
|
|
2015-03-19 11:22:21 +08:00
|
|
|
// XXX debug support, remove when debugging done.
|
|
|
|
func addArgsFromEnv(evar string, args *[]string) {
|
|
|
|
if e := os.Getenv(evar); e != "" {
|
|
|
|
for _, f := range strings.Fields(e) {
|
|
|
|
*args = append(*args, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Printf(">>> criu %v\n", *args)
|
|
|
|
}
|
|
|
|
|
2015-08-06 23:14:59 +08:00
|
|
|
// check Criu version greater than or equal to min_version
|
|
|
|
func (c *linuxContainer) checkCriuVersion(min_version string) error {
|
|
|
|
var x, y, z, versionReq int
|
2015-04-01 23:15:00 +08:00
|
|
|
|
2015-08-06 23:14:59 +08:00
|
|
|
_, err := fmt.Sscanf(min_version, "%d.%d.%d\n", &x, &y, &z) // 1.5.2
|
2015-04-01 23:15:00 +08:00
|
|
|
if err != nil {
|
2015-08-06 23:14:59 +08:00
|
|
|
_, err = fmt.Sscanf(min_version, "Version: %d.%d\n", &x, &y) // 1.6
|
2015-04-01 23:15:00 +08:00
|
|
|
}
|
2015-08-06 23:14:59 +08:00
|
|
|
versionReq = x*10000 + y*100 + z
|
2015-04-01 23:15:00 +08:00
|
|
|
|
2015-08-06 23:14:59 +08:00
|
|
|
out, err := exec.Command(c.criuPath, "-V").Output()
|
2015-05-19 19:48:59 +08:00
|
|
|
if err != nil {
|
2015-08-06 23:14:59 +08:00
|
|
|
return fmt.Errorf("Unable to execute CRIU command: %s", c.criuPath)
|
2015-05-19 19:48:59 +08:00
|
|
|
}
|
2015-08-06 23:14:59 +08:00
|
|
|
|
|
|
|
x = 0
|
|
|
|
y = 0
|
|
|
|
z = 0
|
|
|
|
if ep := strings.Index(string(out), "-"); ep >= 0 {
|
|
|
|
// criu Git version format
|
|
|
|
var version string
|
|
|
|
if sp := strings.Index(string(out), "GitID"); sp > 0 {
|
|
|
|
version = string(out)[sp:ep]
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Unable to parse the CRIU version: %s", c.criuPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := fmt.Sscanf(string(version), "GitID: v%d.%d.%d", &x, &y, &z) // 1.5.2
|
|
|
|
if err != nil {
|
|
|
|
n, err = fmt.Sscanf(string(version), "GitID: v%d.%d", &x, &y) // 1.6
|
|
|
|
y++
|
|
|
|
} else {
|
|
|
|
z++
|
|
|
|
}
|
|
|
|
if n < 2 || err != nil {
|
|
|
|
return fmt.Errorf("Unable to parse the CRIU version: %s %d %s", version, n, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// criu release version format
|
|
|
|
n, err := fmt.Sscanf(string(out), "Version: %d.%d.%d\n", &x, &y, &z) // 1.5.2
|
|
|
|
if err != nil {
|
|
|
|
n, err = fmt.Sscanf(string(out), "Version: %d.%d\n", &x, &y) // 1.6
|
|
|
|
}
|
|
|
|
if n < 2 || err != nil {
|
|
|
|
return fmt.Errorf("Unable to parse the CRIU version: %s %d %s", out, n, err)
|
|
|
|
}
|
2015-04-01 23:15:00 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 19:34:14 +08:00
|
|
|
c.criuVersion = x*10000 + y*100 + z
|
|
|
|
|
|
|
|
if c.criuVersion < versionReq {
|
2015-08-06 23:14:59 +08:00
|
|
|
return fmt.Errorf("CRIU version must be %s or higher", min_version)
|
2015-04-01 23:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-05 05:44:45 +08:00
|
|
|
const descriptorsFilename = "descriptors.json"
|
2015-05-05 02:25:43 +08:00
|
|
|
|
2015-07-21 02:25:22 +08:00
|
|
|
func (c *linuxContainer) addCriuDumpMount(req *criurpc.CriuReq, m *configs.Mount) {
|
|
|
|
mountDest := m.Destination
|
|
|
|
if strings.HasPrefix(mountDest, c.config.Rootfs) {
|
|
|
|
mountDest = mountDest[len(c.config.Rootfs):]
|
|
|
|
}
|
|
|
|
|
|
|
|
extMnt := &criurpc.ExtMountMap{
|
|
|
|
Key: proto.String(mountDest),
|
|
|
|
Val: proto.String(mountDest),
|
|
|
|
}
|
|
|
|
req.Opts.ExtMnt = append(req.Opts.ExtMnt, extMnt)
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:28:40 +08:00
|
|
|
func (c *linuxContainer) Checkpoint(criuOpts *CriuOpts) error {
|
2015-03-13 12:45:43 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2015-04-01 23:15:00 +08:00
|
|
|
|
2015-08-06 23:14:59 +08:00
|
|
|
if err := c.checkCriuVersion("1.5.2"); err != nil {
|
2015-04-01 23:15:00 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:28:40 +08:00
|
|
|
if criuOpts.ImagesDirectory == "" {
|
2015-10-03 02:16:50 +08:00
|
|
|
return fmt.Errorf("invalid directory to save checkpoint")
|
2015-04-19 09:28:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Since a container can be C/R'ed multiple times,
|
|
|
|
// the checkpoint directory may already exist.
|
|
|
|
if err := os.Mkdir(criuOpts.ImagesDirectory, 0755); err != nil && !os.IsExist(err) {
|
2015-03-07 03:32:16 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-04-10 23:19:14 +08:00
|
|
|
|
2015-04-19 09:28:40 +08:00
|
|
|
if criuOpts.WorkDirectory == "" {
|
|
|
|
criuOpts.WorkDirectory = filepath.Join(c.root, "criu.work")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Mkdir(criuOpts.WorkDirectory, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
workDir, err := os.Open(criuOpts.WorkDirectory)
|
2015-04-10 23:19:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer workDir.Close()
|
|
|
|
|
2015-04-19 09:28:40 +08:00
|
|
|
imageDir, err := os.Open(criuOpts.ImagesDirectory)
|
2015-04-10 23:19:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer imageDir.Close()
|
2015-04-19 09:28:40 +08:00
|
|
|
|
|
|
|
rpcOpts := criurpc.CriuOpts{
|
|
|
|
ImagesDirFd: proto.Int32(int32(imageDir.Fd())),
|
|
|
|
WorkDirFd: proto.Int32(int32(workDir.Fd())),
|
|
|
|
LogLevel: proto.Int32(4),
|
|
|
|
LogFile: proto.String("dump.log"),
|
|
|
|
Root: proto.String(c.config.Rootfs),
|
|
|
|
ManageCgroups: proto.Bool(true),
|
|
|
|
NotifyScripts: proto.Bool(true),
|
|
|
|
Pid: proto.Int32(int32(c.initProcess.pid())),
|
|
|
|
ShellJob: proto.Bool(criuOpts.ShellJob),
|
|
|
|
LeaveRunning: proto.Bool(criuOpts.LeaveRunning),
|
|
|
|
TcpEstablished: proto.Bool(criuOpts.TcpEstablished),
|
|
|
|
ExtUnixSk: proto.Bool(criuOpts.ExternalUnixConnections),
|
2015-06-27 17:56:24 +08:00
|
|
|
FileLocks: proto.Bool(criuOpts.FileLocks),
|
2015-04-19 09:28:40 +08:00
|
|
|
}
|
|
|
|
|
2015-04-24 04:16:47 +08:00
|
|
|
// append optional criu opts, e.g., page-server and port
|
2015-04-24 23:13:15 +08:00
|
|
|
if criuOpts.PageServer.Address != "" && criuOpts.PageServer.Port != 0 {
|
2015-04-24 04:16:47 +08:00
|
|
|
rpcOpts.Ps = &criurpc.CriuPageServerInfo{
|
2015-04-24 23:13:15 +08:00
|
|
|
Address: proto.String(criuOpts.PageServer.Address),
|
|
|
|
Port: proto.Int32(criuOpts.PageServer.Port),
|
2015-04-24 04:16:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 23:14:59 +08:00
|
|
|
// append optional manage cgroups mode
|
|
|
|
if criuOpts.ManageCgroupsMode != 0 {
|
|
|
|
if err := c.checkCriuVersion("1.7"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rpcOpts.ManageCgroupsMode = proto.Uint32(uint32(criuOpts.ManageCgroupsMode))
|
|
|
|
}
|
|
|
|
|
2015-04-10 23:19:14 +08:00
|
|
|
t := criurpc.CriuReqType_DUMP
|
2015-07-21 02:25:22 +08:00
|
|
|
req := &criurpc.CriuReq{
|
2015-04-10 23:19:14 +08:00
|
|
|
Type: &t,
|
2015-04-19 09:28:40 +08:00
|
|
|
Opts: &rpcOpts,
|
2015-03-07 03:21:02 +08:00
|
|
|
}
|
2015-04-19 09:28:40 +08:00
|
|
|
|
2015-03-07 03:21:02 +08:00
|
|
|
for _, m := range c.config.Mounts {
|
2015-07-21 02:25:22 +08:00
|
|
|
switch m.Device {
|
|
|
|
case "bind":
|
|
|
|
c.addCriuDumpMount(req, m)
|
|
|
|
break
|
|
|
|
case "cgroup":
|
|
|
|
binds, err := getCgroupMounts(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-19 09:37:39 +08:00
|
|
|
}
|
2015-07-21 02:25:22 +08:00
|
|
|
for _, b := range binds {
|
|
|
|
c.addCriuDumpMount(req, b)
|
|
|
|
}
|
|
|
|
break
|
2015-03-07 03:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-10 23:19:14 +08:00
|
|
|
|
2015-04-29 00:49:44 +08:00
|
|
|
// Write the FD info to a file in the image directory
|
|
|
|
|
2015-04-29 19:35:21 +08:00
|
|
|
fdsJSON, err := json.Marshal(c.initProcess.externalDescriptors())
|
2015-04-29 00:49:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-05 05:44:45 +08:00
|
|
|
err = ioutil.WriteFile(filepath.Join(criuOpts.ImagesDirectory, descriptorsFilename), fdsJSON, 0655)
|
2015-04-29 00:49:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-08 17:02:08 +08:00
|
|
|
err = c.criuSwrk(nil, req, criuOpts, false)
|
2015-04-10 20:48:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-13 12:45:43 +08:00
|
|
|
return nil
|
2015-03-07 03:21:02 +08:00
|
|
|
}
|
|
|
|
|
2015-07-21 02:25:22 +08:00
|
|
|
func (c *linuxContainer) addCriuRestoreMount(req *criurpc.CriuReq, m *configs.Mount) {
|
|
|
|
mountDest := m.Destination
|
|
|
|
if strings.HasPrefix(mountDest, c.config.Rootfs) {
|
|
|
|
mountDest = mountDest[len(c.config.Rootfs):]
|
|
|
|
}
|
|
|
|
|
|
|
|
extMnt := &criurpc.ExtMountMap{
|
|
|
|
Key: proto.String(mountDest),
|
|
|
|
Val: proto.String(m.Source),
|
|
|
|
}
|
|
|
|
req.Opts.ExtMnt = append(req.Opts.ExtMnt, extMnt)
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:28:40 +08:00
|
|
|
func (c *linuxContainer) Restore(process *Process, criuOpts *CriuOpts) error {
|
2015-03-13 12:45:43 +08:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2015-08-06 23:14:59 +08:00
|
|
|
if err := c.checkCriuVersion("1.5.2"); err != nil {
|
2015-04-01 23:15:00 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-04-19 09:28:40 +08:00
|
|
|
if criuOpts.WorkDirectory == "" {
|
|
|
|
criuOpts.WorkDirectory = filepath.Join(c.root, "criu.work")
|
|
|
|
}
|
2015-04-02 14:54:02 +08:00
|
|
|
// Since a container can be C/R'ed multiple times,
|
|
|
|
// the work directory may already exist.
|
2015-04-19 09:28:40 +08:00
|
|
|
if err := os.Mkdir(criuOpts.WorkDirectory, 0655); err != nil && !os.IsExist(err) {
|
2015-04-02 14:54:02 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-04-19 09:28:40 +08:00
|
|
|
workDir, err := os.Open(criuOpts.WorkDirectory)
|
2015-04-02 14:54:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer workDir.Close()
|
2015-04-19 09:28:40 +08:00
|
|
|
if criuOpts.ImagesDirectory == "" {
|
2015-10-03 02:16:50 +08:00
|
|
|
return fmt.Errorf("invalid directory to restore checkpoint")
|
2015-04-19 09:28:40 +08:00
|
|
|
}
|
|
|
|
imageDir, err := os.Open(criuOpts.ImagesDirectory)
|
2015-03-26 19:20:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer imageDir.Close()
|
2015-04-22 13:12:41 +08:00
|
|
|
// CRIU has a few requirements for a root directory:
|
|
|
|
// * it must be a mount point
|
|
|
|
// * its parent must not be overmounted
|
|
|
|
// c.config.Rootfs is bind-mounted to a temporary directory
|
|
|
|
// to satisfy these requirements.
|
2015-04-16 19:15:02 +08:00
|
|
|
root := filepath.Join(c.root, "criu-root")
|
|
|
|
if err := os.Mkdir(root, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.Remove(root)
|
|
|
|
root, err = filepath.EvalSymlinks(root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = syscall.Mount(c.config.Rootfs, root, "", syscall.MS_BIND|syscall.MS_REC, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer syscall.Unmount(root, syscall.MNT_DETACH)
|
2015-03-26 19:20:59 +08:00
|
|
|
t := criurpc.CriuReqType_RESTORE
|
2015-07-21 02:25:22 +08:00
|
|
|
req := &criurpc.CriuReq{
|
2015-03-26 19:20:59 +08:00
|
|
|
Type: &t,
|
|
|
|
Opts: &criurpc.CriuOpts{
|
|
|
|
ImagesDirFd: proto.Int32(int32(imageDir.Fd())),
|
2015-04-02 14:54:02 +08:00
|
|
|
WorkDirFd: proto.Int32(int32(workDir.Fd())),
|
2015-03-26 19:20:59 +08:00
|
|
|
EvasiveDevices: proto.Bool(true),
|
|
|
|
LogLevel: proto.Int32(4),
|
|
|
|
LogFile: proto.String("restore.log"),
|
|
|
|
RstSibling: proto.Bool(true),
|
2015-04-16 19:15:02 +08:00
|
|
|
Root: proto.String(root),
|
2015-03-26 19:20:59 +08:00
|
|
|
ManageCgroups: proto.Bool(true),
|
|
|
|
NotifyScripts: proto.Bool(true),
|
2015-04-19 09:28:40 +08:00
|
|
|
ShellJob: proto.Bool(criuOpts.ShellJob),
|
|
|
|
ExtUnixSk: proto.Bool(criuOpts.ExternalUnixConnections),
|
|
|
|
TcpEstablished: proto.Bool(criuOpts.TcpEstablished),
|
2015-06-27 17:56:24 +08:00
|
|
|
FileLocks: proto.Bool(criuOpts.FileLocks),
|
2015-03-26 19:20:59 +08:00
|
|
|
},
|
2015-03-07 03:21:02 +08:00
|
|
|
}
|
2015-09-08 17:02:08 +08:00
|
|
|
|
2015-03-07 03:21:02 +08:00
|
|
|
for _, m := range c.config.Mounts {
|
2015-07-21 02:25:22 +08:00
|
|
|
switch m.Device {
|
|
|
|
case "bind":
|
|
|
|
c.addCriuRestoreMount(req, m)
|
|
|
|
break
|
|
|
|
case "cgroup":
|
|
|
|
binds, err := getCgroupMounts(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-29 00:49:44 +08:00
|
|
|
}
|
2015-07-21 02:25:22 +08:00
|
|
|
for _, b := range binds {
|
|
|
|
c.addCriuRestoreMount(req, b)
|
|
|
|
}
|
|
|
|
break
|
2015-03-07 03:21:02 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-20 16:24:50 +08:00
|
|
|
for _, iface := range c.config.Networks {
|
|
|
|
switch iface.Type {
|
|
|
|
case "veth":
|
|
|
|
veth := new(criurpc.CriuVethPair)
|
|
|
|
veth.IfOut = proto.String(iface.HostInterfaceName)
|
|
|
|
veth.IfIn = proto.String(iface.Name)
|
|
|
|
req.Opts.Veths = append(req.Opts.Veths, veth)
|
|
|
|
break
|
|
|
|
case "loopback":
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-08-25 00:26:39 +08:00
|
|
|
for _, i := range criuOpts.VethPairs {
|
|
|
|
veth := new(criurpc.CriuVethPair)
|
|
|
|
veth.IfOut = proto.String(i.HostInterfaceName)
|
|
|
|
veth.IfIn = proto.String(i.ContainerInterfaceName)
|
|
|
|
req.Opts.Veths = append(req.Opts.Veths, veth)
|
|
|
|
}
|
2015-04-29 00:49:44 +08:00
|
|
|
|
2015-08-06 23:14:59 +08:00
|
|
|
// append optional manage cgroups mode
|
|
|
|
if criuOpts.ManageCgroupsMode != 0 {
|
|
|
|
if err := c.checkCriuVersion("1.7"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Opts.ManageCgroupsMode = proto.Uint32(uint32(criuOpts.ManageCgroupsMode))
|
|
|
|
}
|
|
|
|
|
2015-05-05 02:25:43 +08:00
|
|
|
var (
|
|
|
|
fds []string
|
|
|
|
fdJSON []byte
|
|
|
|
)
|
2015-08-05 05:44:45 +08:00
|
|
|
if fdJSON, err = ioutil.ReadFile(filepath.Join(criuOpts.ImagesDirectory, descriptorsFilename)); err != nil {
|
2015-04-29 00:49:44 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-03 02:16:50 +08:00
|
|
|
if err := json.Unmarshal(fdJSON, &fds); err != nil {
|
2015-04-29 00:49:44 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-04-29 23:26:18 +08:00
|
|
|
for i := range fds {
|
2015-04-29 00:49:44 +08:00
|
|
|
if s := fds[i]; strings.Contains(s, "pipe:") {
|
2015-03-26 19:20:59 +08:00
|
|
|
inheritFd := new(criurpc.InheritFd)
|
|
|
|
inheritFd.Key = proto.String(s)
|
2015-04-29 23:26:18 +08:00
|
|
|
inheritFd.Fd = proto.Int32(int32(i))
|
2015-03-26 19:20:59 +08:00
|
|
|
req.Opts.InheritFd = append(req.Opts.InheritFd, inheritFd)
|
2015-03-19 11:22:21 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
return c.criuSwrk(process, req, criuOpts, true)
|
2015-04-10 23:18:16 +08:00
|
|
|
}
|
|
|
|
|
2015-09-08 17:02:08 +08:00
|
|
|
func (c *linuxContainer) criuApplyCgroups(pid int, req *criurpc.CriuReq) error {
|
|
|
|
if err := c.cgroupManager.Apply(pid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("/proc/%d/cgroup", pid)
|
|
|
|
cgroupsPaths, err := cgroups.ParseCgroupFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for c, p := range cgroupsPaths {
|
|
|
|
cgroupRoot := &criurpc.CgroupRoot{
|
|
|
|
Ctrl: proto.String(c),
|
|
|
|
Path: proto.String(p),
|
|
|
|
}
|
|
|
|
req.Opts.CgRoot = append(req.Opts.CgRoot, cgroupRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) criuSwrk(process *Process, req *criurpc.CriuReq, opts *CriuOpts, applyCgroups bool) error {
|
2015-04-10 23:18:16 +08:00
|
|
|
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_SEQPACKET|syscall.SOCK_CLOEXEC, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-22 05:20:59 +08:00
|
|
|
logPath := filepath.Join(opts.WorkDirectory, req.GetOpts().GetLogFile())
|
2015-04-10 23:18:16 +08:00
|
|
|
criuClient := os.NewFile(uintptr(fds[0]), "criu-transport-client")
|
|
|
|
criuServer := os.NewFile(uintptr(fds[1]), "criu-transport-server")
|
|
|
|
defer criuClient.Close()
|
|
|
|
defer criuServer.Close()
|
|
|
|
|
2015-03-26 19:20:59 +08:00
|
|
|
args := []string{"swrk", "3"}
|
2015-08-31 19:34:14 +08:00
|
|
|
logrus.Debugf("Using CRIU %d at: %s", c.criuVersion, c.criuPath)
|
|
|
|
logrus.Debugf("Using CRIU with following args: %s", args)
|
2015-03-13 12:45:43 +08:00
|
|
|
cmd := exec.Command(c.criuPath, args...)
|
2015-04-10 23:19:14 +08:00
|
|
|
if process != nil {
|
|
|
|
cmd.Stdin = process.Stdin
|
|
|
|
cmd.Stdout = process.Stdout
|
|
|
|
cmd.Stderr = process.Stderr
|
|
|
|
}
|
2015-03-26 19:20:59 +08:00
|
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, criuServer)
|
|
|
|
|
2015-03-13 12:45:43 +08:00
|
|
|
if err := cmd.Start(); err != nil {
|
2015-03-19 11:22:21 +08:00
|
|
|
return err
|
2015-03-13 12:45:43 +08:00
|
|
|
}
|
2015-03-26 19:20:59 +08:00
|
|
|
criuServer.Close()
|
2015-03-25 21:21:44 +08:00
|
|
|
|
2015-03-26 19:20:59 +08:00
|
|
|
defer func() {
|
|
|
|
criuClient.Close()
|
2015-05-21 06:48:07 +08:00
|
|
|
_, err := cmd.Process.Wait()
|
2015-03-26 19:20:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-08 17:02:08 +08:00
|
|
|
if applyCgroups {
|
|
|
|
err := c.criuApplyCgroups(cmd.Process.Pid, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 17:42:21 +08:00
|
|
|
var extFds []string
|
2015-04-29 04:54:03 +08:00
|
|
|
if process != nil {
|
2015-05-14 17:42:21 +08:00
|
|
|
extFds, err = getPipeFds(cmd.Process.Pid)
|
2015-04-29 04:54:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:34:14 +08:00
|
|
|
logrus.Debugf("Using CRIU in %s mode", req.GetType().String())
|
|
|
|
val := reflect.ValueOf(req.GetOpts())
|
|
|
|
v := reflect.Indirect(val)
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
st := v.Type()
|
|
|
|
name := st.Field(i).Name
|
|
|
|
if strings.HasPrefix(name, "XXX_") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
value := val.MethodByName("Get" + name).Call([]reflect.Value{})
|
|
|
|
logrus.Debugf("CRIU option %s with value %v", name, value[0])
|
|
|
|
}
|
2015-04-10 23:18:16 +08:00
|
|
|
data, err := proto.Marshal(req)
|
2015-03-26 19:20:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-03-25 21:21:44 +08:00
|
|
|
}
|
2015-03-26 19:20:59 +08:00
|
|
|
_, err = criuClient.Write(data)
|
2015-03-07 03:21:02 +08:00
|
|
|
if err != nil {
|
2015-03-19 11:22:21 +08:00
|
|
|
return err
|
2015-03-07 03:21:02 +08:00
|
|
|
}
|
2015-03-25 21:21:44 +08:00
|
|
|
|
2015-03-26 19:20:59 +08:00
|
|
|
buf := make([]byte, 10*4096)
|
|
|
|
for true {
|
|
|
|
n, err := criuClient.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return fmt.Errorf("unexpected EOF")
|
|
|
|
}
|
|
|
|
if n == len(buf) {
|
|
|
|
return fmt.Errorf("buffer is too small")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := new(criurpc.CriuResp)
|
|
|
|
err = proto.Unmarshal(buf[:n], resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !resp.GetSuccess() {
|
2015-08-22 05:20:59 +08:00
|
|
|
typeString := req.GetType().String()
|
|
|
|
return fmt.Errorf("criu failed: type %s errno %d\nlog file: %s", typeString, resp.GetCrErrno(), logPath)
|
2015-03-26 19:20:59 +08:00
|
|
|
}
|
|
|
|
|
2015-04-10 23:18:16 +08:00
|
|
|
t := resp.GetType()
|
2015-03-26 19:20:59 +08:00
|
|
|
switch {
|
|
|
|
case t == criurpc.CriuReqType_NOTIFY:
|
2015-05-14 17:42:21 +08:00
|
|
|
if err := c.criuNotifications(resp, process, opts, extFds); err != nil {
|
2015-04-10 23:03:23 +08:00
|
|
|
return err
|
2015-03-26 19:20:59 +08:00
|
|
|
}
|
|
|
|
t = criurpc.CriuReqType_NOTIFY
|
2015-04-10 23:18:16 +08:00
|
|
|
req = &criurpc.CriuReq{
|
2015-03-26 19:20:59 +08:00
|
|
|
Type: &t,
|
|
|
|
NotifySuccess: proto.Bool(true),
|
|
|
|
}
|
2015-04-10 23:18:16 +08:00
|
|
|
data, err = proto.Marshal(req)
|
2015-03-26 19:20:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n, err = criuClient.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
case t == criurpc.CriuReqType_RESTORE:
|
2015-04-10 23:19:14 +08:00
|
|
|
case t == criurpc.CriuReqType_DUMP:
|
2015-04-10 23:18:16 +08:00
|
|
|
break
|
2015-03-26 19:20:59 +08:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unable to parse the response %s", resp.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmd.Wait() waits cmd.goroutines which are used for proxying file descriptors.
|
|
|
|
// Here we want to wait only the CRIU process.
|
|
|
|
st, err := cmd.Process.Wait()
|
|
|
|
if err != nil {
|
2015-03-19 11:22:21 +08:00
|
|
|
return err
|
2015-03-13 12:45:43 +08:00
|
|
|
}
|
2015-03-26 19:20:59 +08:00
|
|
|
if !st.Success() {
|
2015-08-22 05:20:59 +08:00
|
|
|
return fmt.Errorf("criu failed: %s\nlog file: %s", st.String(), logPath)
|
2015-03-26 19:20:59 +08:00
|
|
|
}
|
2015-03-19 11:22:21 +08:00
|
|
|
return nil
|
2015-03-07 03:21:02 +08:00
|
|
|
}
|
|
|
|
|
2015-04-20 16:24:50 +08:00
|
|
|
// block any external network activity
|
|
|
|
func lockNetwork(config *configs.Config) error {
|
|
|
|
for _, config := range config.Networks {
|
|
|
|
strategy, err := getStrategy(config.Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := strategy.detach(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unlockNetwork(config *configs.Config) error {
|
|
|
|
for _, config := range config.Networks {
|
|
|
|
strategy, err := getStrategy(config.Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = strategy.attach(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-14 17:42:21 +08:00
|
|
|
func (c *linuxContainer) criuNotifications(resp *criurpc.CriuResp, process *Process, opts *CriuOpts, fds []string) error {
|
2015-04-10 23:03:23 +08:00
|
|
|
notify := resp.GetNotify()
|
|
|
|
if notify == nil {
|
|
|
|
return fmt.Errorf("invalid response: %s", resp.String())
|
|
|
|
}
|
|
|
|
switch {
|
2015-04-10 23:19:14 +08:00
|
|
|
case notify.GetScript() == "post-dump":
|
2015-10-03 02:16:50 +08:00
|
|
|
f, err := os.Create(filepath.Join(c.root, "checkpoint"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-04-10 23:19:14 +08:00
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
f.Close()
|
2015-04-20 16:24:50 +08:00
|
|
|
case notify.GetScript() == "network-unlock":
|
|
|
|
if err := unlockNetwork(c.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case notify.GetScript() == "network-lock":
|
|
|
|
if err := lockNetwork(c.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-10 23:03:23 +08:00
|
|
|
case notify.GetScript() == "post-restore":
|
|
|
|
pid := notify.GetPid()
|
2015-05-14 17:42:21 +08:00
|
|
|
r, err := newRestoredProcess(int(pid), fds)
|
2015-04-10 23:03:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
process.ops = r
|
|
|
|
if err := c.state.transition(&restoredState{
|
|
|
|
imageDir: opts.ImagesDirectory,
|
|
|
|
c: c,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-10 23:03:23 +08:00
|
|
|
if err := c.updateState(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
if err := os.Remove(filepath.Join(c.root, "checkpoint")); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 23:03:23 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 08:45:23 +08:00
|
|
|
func (c *linuxContainer) updateState(process parentProcess) error {
|
|
|
|
c.initProcess = process
|
2015-02-14 06:41:37 +08:00
|
|
|
state, err := c.currentState()
|
2015-02-12 08:45:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
return c.saveState(state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) saveState(s *State) error {
|
2015-02-12 08:45:23 +08:00
|
|
|
f, err := os.Create(filepath.Join(c.root, stateFilename))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2015-10-03 02:16:50 +08:00
|
|
|
return json.NewEncoder(f).Encode(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) deleteState() error {
|
|
|
|
return os.Remove(filepath.Join(c.root, stateFilename))
|
2015-02-12 08:45:23 +08:00
|
|
|
}
|
2015-02-14 06:41:37 +08:00
|
|
|
|
|
|
|
func (c *linuxContainer) currentStatus() (Status, error) {
|
2015-10-03 02:16:50 +08:00
|
|
|
if err := c.refreshState(); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return c.state.status(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// refreshState needs to be called to verify that the current state on the
|
|
|
|
// container is what is true. Because consumers of libcontainer can use it
|
|
|
|
// out of process we need to verify the container's status based on runtime
|
|
|
|
// information and not rely on our in process info.
|
|
|
|
func (c *linuxContainer) refreshState() error {
|
|
|
|
paused, err := c.isPaused()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if paused {
|
|
|
|
return c.state.transition(&pausedState{c: c})
|
2015-04-10 20:47:37 +08:00
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
running, err := c.isRunning()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if running {
|
|
|
|
return c.state.transition(&runningState{c: c})
|
|
|
|
}
|
|
|
|
return c.state.transition(&stoppedState{c: c})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) isRunning() (bool, error) {
|
2015-02-14 06:41:37 +08:00
|
|
|
if c.initProcess == nil {
|
2015-10-03 02:16:50 +08:00
|
|
|
return false, nil
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
|
|
|
// return Running if the init process is alive
|
|
|
|
if err := syscall.Kill(c.initProcess.pid(), 0); err != nil {
|
|
|
|
if err == syscall.ESRCH {
|
2015-10-03 02:16:50 +08:00
|
|
|
return false, nil
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
return false, newSystemError(err)
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) isPaused() (bool, error) {
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(c.cgroupManager.GetPaths()["freezer"], "freezer.state"))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, newSystemError(err)
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
2015-10-03 02:16:50 +08:00
|
|
|
return bytes.Equal(bytes.TrimSpace(data), []byte("FROZEN")), nil
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *linuxContainer) currentState() (*State, error) {
|
2016-01-22 08:43:33 +08:00
|
|
|
var (
|
|
|
|
startTime string
|
|
|
|
externalDescriptors []string
|
|
|
|
pid = -1
|
|
|
|
)
|
|
|
|
if c.initProcess != nil {
|
|
|
|
pid = c.initProcess.pid()
|
|
|
|
startTime, _ = c.initProcess.startTime()
|
|
|
|
externalDescriptors = c.initProcess.externalDescriptors()
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
|
|
|
state := &State{
|
2015-10-24 00:22:48 +08:00
|
|
|
BaseState: BaseState{
|
|
|
|
ID: c.ID(),
|
|
|
|
Config: *c.config,
|
2016-01-22 08:43:33 +08:00
|
|
|
InitProcessPid: pid,
|
2015-10-24 00:22:48 +08:00
|
|
|
InitProcessStartTime: startTime,
|
|
|
|
},
|
|
|
|
CgroupPaths: c.cgroupManager.GetPaths(),
|
|
|
|
NamespacePaths: make(map[configs.NamespaceType]string),
|
2016-01-22 08:43:33 +08:00
|
|
|
ExternalDescriptors: externalDescriptors,
|
2015-02-14 06:41:37 +08:00
|
|
|
}
|
2016-01-22 08:43:33 +08:00
|
|
|
if pid > 0 {
|
|
|
|
for _, ns := range c.config.Namespaces {
|
|
|
|
state.NamespacePaths[ns.Type] = ns.GetPath(pid)
|
|
|
|
}
|
|
|
|
for _, nsType := range configs.NamespaceTypes() {
|
|
|
|
if _, ok := state.NamespacePaths[nsType]; !ok {
|
|
|
|
ns := configs.Namespace{Type: nsType}
|
|
|
|
state.NamespacePaths[ns.Type] = ns.GetPath(pid)
|
|
|
|
}
|
2015-04-08 05:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-14 06:41:37 +08:00
|
|
|
return state, nil
|
|
|
|
}
|
2015-10-17 23:35:36 +08:00
|
|
|
|
|
|
|
// bootstrapData encodes the necessary data in netlink binary format as a io.Reader.
|
|
|
|
// Consumer can write the data to a bootstrap program such as one that uses
|
|
|
|
// nsenter package to bootstrap the container's init process correctly, i.e. with
|
|
|
|
// correct namespaces, uid/gid mapping etc.
|
|
|
|
func (c *linuxContainer) bootstrapData(cloneFlags uintptr, pid int, consolePath string) (io.Reader, error) {
|
|
|
|
// create the netlink message
|
|
|
|
r := nl.NewNetlinkRequest(int(InitMsg), 0)
|
|
|
|
// write pid
|
|
|
|
r.AddData(&Int32msg{
|
|
|
|
Type: PidAttr,
|
|
|
|
Value: uint32(pid),
|
|
|
|
})
|
|
|
|
// write console path
|
|
|
|
if consolePath != "" {
|
|
|
|
r.AddData(&Bytemsg{
|
|
|
|
Type: ConsolePathAttr,
|
|
|
|
Value: []byte(consolePath),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return bytes.NewReader(r.Serialize()), nil
|
|
|
|
}
|