2014-10-23 04:45:23 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-11-01 04:56:53 +08:00
|
|
|
"fmt"
|
2015-02-01 13:21:06 +08:00
|
|
|
"io/ioutil"
|
2014-10-23 04:45:23 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-11-01 04:56:53 +08:00
|
|
|
"regexp"
|
2014-10-31 06:08:28 +08:00
|
|
|
|
2014-12-06 09:06:58 +08:00
|
|
|
"github.com/golang/glog"
|
2014-12-17 17:12:23 +08:00
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
cgroups "github.com/docker/libcontainer/cgroups/manager"
|
2014-12-17 17:12:23 +08:00
|
|
|
"github.com/docker/libcontainer/configs"
|
2015-02-07 04:48:57 +08:00
|
|
|
"github.com/docker/libcontainer/configs/validate"
|
2014-10-23 04:45:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configFilename = "config.json"
|
|
|
|
stateFilename = "state.json"
|
|
|
|
)
|
|
|
|
|
2014-11-01 04:56:53 +08:00
|
|
|
var (
|
2014-11-04 01:42:20 +08:00
|
|
|
idRegex = regexp.MustCompile(`^[\w_]+$`)
|
|
|
|
maxIdLen = 1024
|
2014-11-01 04:56:53 +08:00
|
|
|
)
|
|
|
|
|
2014-10-23 04:45:23 +08:00
|
|
|
// New returns a linux based container factory based in the root directory.
|
2014-12-15 23:05:11 +08:00
|
|
|
func New(root string, initArgs []string) (Factory, error) {
|
|
|
|
if root != "" {
|
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
return &linuxFactory{
|
2015-02-07 04:48:57 +08:00
|
|
|
root: root,
|
|
|
|
initArgs: initArgs,
|
|
|
|
validator: validate.New(),
|
2014-10-23 04:45:23 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// linuxFactory implements the default factory interface for linux based systems.
|
|
|
|
type linuxFactory struct {
|
|
|
|
// root is the root directory
|
2015-02-07 04:48:57 +08:00
|
|
|
root string
|
|
|
|
initArgs []string
|
|
|
|
validator validate.Validator
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 17:12:23 +08:00
|
|
|
func (l *linuxFactory) Create(id string, config *configs.Config) (Container, error) {
|
2014-12-15 23:05:11 +08:00
|
|
|
if l.root == "" {
|
|
|
|
return nil, newGenericError(fmt.Errorf("invalid root"), ConfigInvalid)
|
|
|
|
}
|
2015-02-01 12:51:12 +08:00
|
|
|
if err := l.validateID(id); err != nil {
|
|
|
|
return nil, err
|
2014-11-04 01:42:20 +08:00
|
|
|
}
|
2015-02-07 04:48:57 +08:00
|
|
|
if err := l.validator.Validate(config); err != nil {
|
|
|
|
return nil, newGenericError(err, ConfigInvalid)
|
|
|
|
}
|
2014-11-01 06:34:50 +08:00
|
|
|
containerRoot := filepath.Join(l.root, id)
|
2015-02-01 12:51:12 +08:00
|
|
|
if _, err := os.Stat(containerRoot); err == nil {
|
2014-11-01 06:34:50 +08:00
|
|
|
return nil, newGenericError(fmt.Errorf("Container with id exists: %v", id), IdInUse)
|
2014-11-04 01:42:20 +08:00
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return nil, newGenericError(err, SystemError)
|
2014-11-01 04:56:53 +08:00
|
|
|
}
|
2014-12-15 23:05:11 +08:00
|
|
|
data, err := json.MarshalIndent(config, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(containerRoot, 0700); err != nil {
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
f, err := os.Create(filepath.Join(containerRoot, configFilename))
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(containerRoot)
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2015-02-01 12:51:12 +08:00
|
|
|
if _, err := f.Write(data); err != nil {
|
2014-12-15 23:05:11 +08:00
|
|
|
os.RemoveAll(containerRoot)
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
return &linuxContainer{
|
|
|
|
id: id,
|
|
|
|
root: containerRoot,
|
|
|
|
config: config,
|
|
|
|
initArgs: l.initArgs,
|
2015-02-07 04:48:57 +08:00
|
|
|
cgroupManager: cgroups.NewCgroupManager(config.Cgroups),
|
2014-12-15 23:05:11 +08:00
|
|
|
}, nil
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 08:51:14 +08:00
|
|
|
func (l *linuxFactory) Load(id string) (Container, error) {
|
2014-12-15 23:05:11 +08:00
|
|
|
if l.root == "" {
|
|
|
|
return nil, newGenericError(fmt.Errorf("invalid root"), ConfigInvalid)
|
|
|
|
}
|
2014-10-23 04:45:23 +08:00
|
|
|
containerRoot := filepath.Join(l.root, id)
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Infof("loading container config from %s", containerRoot)
|
2014-10-23 04:45:23 +08:00
|
|
|
config, err := l.loadContainerConfig(containerRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Infof("loading container state from %s", containerRoot)
|
2014-10-23 04:45:23 +08:00
|
|
|
state, err := l.loadContainerState(containerRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
r := &restoredProcess{
|
|
|
|
processPid: state.InitPid,
|
|
|
|
processStartTime: state.InitStartTime,
|
|
|
|
}
|
2015-01-14 23:47:26 +08:00
|
|
|
cgroupManager := cgroups.LoadCgroupManager(config.Cgroups, state.CgroupPaths)
|
2014-12-06 09:06:58 +08:00
|
|
|
glog.Infof("using %s as cgroup manager", cgroupManager)
|
2014-10-23 04:45:23 +08:00
|
|
|
return &linuxContainer{
|
2015-02-07 13:12:27 +08:00
|
|
|
initProcess: r,
|
2014-10-23 07:53:28 +08:00
|
|
|
id: id,
|
|
|
|
config: config,
|
2014-12-15 23:05:11 +08:00
|
|
|
initArgs: l.initArgs,
|
2015-02-07 13:12:27 +08:00
|
|
|
cgroupManager: cgroupManager,
|
|
|
|
root: containerRoot,
|
2014-10-23 04:45:23 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-02-01 12:51:12 +08:00
|
|
|
// StartInitialization loads a container by opening the pipe fd from the parent to read the configuration and state
|
|
|
|
// This is a low level implementation detail of the reexec and should not be consumed externally
|
2015-02-01 13:21:06 +08:00
|
|
|
func (l *linuxFactory) StartInitialization(pipefd uintptr) (err error) {
|
2015-02-07 04:48:57 +08:00
|
|
|
var (
|
|
|
|
pipe = os.NewFile(uintptr(pipefd), "pipe")
|
|
|
|
it = initType(os.Getenv("_LIBCONTAINER_INITTYPE"))
|
|
|
|
)
|
|
|
|
// clear the current process's environment to clean any libcontainer
|
|
|
|
// specific env vars.
|
|
|
|
os.Clearenv()
|
2015-02-01 13:21:06 +08:00
|
|
|
defer func() {
|
|
|
|
// if we have an error during the initialization of the container's init then send it back to the
|
|
|
|
// parent process in the form of an initError.
|
|
|
|
if err != nil {
|
|
|
|
// ensure that any data sent from the parent is consumed so it doesn't
|
|
|
|
// receive ECONNRESET when the child writes to the pipe.
|
|
|
|
ioutil.ReadAll(pipe)
|
|
|
|
if err := json.NewEncoder(pipe).Encode(initError{
|
|
|
|
Message: err.Error(),
|
|
|
|
}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ensure that this pipe is always closed
|
|
|
|
pipe.Close()
|
|
|
|
}()
|
2015-02-07 04:48:57 +08:00
|
|
|
i, err := newContainerInit(it, pipe)
|
2015-02-01 13:21:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-07 04:48:57 +08:00
|
|
|
return i.Init()
|
2015-02-01 12:51:12 +08:00
|
|
|
}
|
|
|
|
|
2014-12-17 17:12:23 +08:00
|
|
|
func (l *linuxFactory) loadContainerConfig(root string) (*configs.Config, error) {
|
2014-10-23 04:45:23 +08:00
|
|
|
f, err := os.Open(filepath.Join(root, configFilename))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2014-10-23 07:27:06 +08:00
|
|
|
return nil, newGenericError(err, ContainerNotExists)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2014-12-17 17:12:23 +08:00
|
|
|
var config *configs.Config
|
2014-10-23 04:45:23 +08:00
|
|
|
if err := json.NewDecoder(f).Decode(&config); err != nil {
|
|
|
|
return nil, newGenericError(err, ConfigInvalid)
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2014-12-17 17:30:52 +08:00
|
|
|
func (l *linuxFactory) loadContainerState(root string) (*configs.State, error) {
|
2014-10-23 04:45:23 +08:00
|
|
|
f, err := os.Open(filepath.Join(root, stateFilename))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2014-10-23 07:27:06 +08:00
|
|
|
return nil, newGenericError(err, ContainerNotExists)
|
2014-10-23 04:45:23 +08:00
|
|
|
}
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2014-12-17 17:30:52 +08:00
|
|
|
var state *configs.State
|
2014-10-23 04:45:23 +08:00
|
|
|
if err := json.NewDecoder(f).Decode(&state); err != nil {
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
2014-12-15 23:05:11 +08:00
|
|
|
|
2015-02-01 12:51:12 +08:00
|
|
|
func (l *linuxFactory) validateID(id string) error {
|
|
|
|
if !idRegex.MatchString(id) {
|
|
|
|
return newGenericError(fmt.Errorf("Invalid id format: %v", id), InvalidIdFormat)
|
2014-12-23 06:06:22 +08:00
|
|
|
}
|
2015-02-01 12:51:12 +08:00
|
|
|
if len(id) > maxIdLen {
|
|
|
|
return newGenericError(fmt.Errorf("Invalid id format: %v", id), InvalidIdFormat)
|
|
|
|
}
|
|
|
|
return nil
|
2014-12-15 23:05:11 +08:00
|
|
|
}
|
2015-02-07 13:12:27 +08:00
|
|
|
|
|
|
|
// restoredProcess represents a process where the calling process may or may not be
|
|
|
|
// the parent process. This process is created when a factory loads a container from
|
|
|
|
// a persisted state.
|
|
|
|
type restoredProcess struct {
|
|
|
|
processPid int
|
|
|
|
processStartTime string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) start() error {
|
|
|
|
return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) pid() int {
|
|
|
|
return p.processPid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) terminate() error {
|
|
|
|
return newGenericError(fmt.Errorf("restored process cannot be terminated"), SystemError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) wait() (*os.ProcessState, error) {
|
|
|
|
return nil, newGenericError(fmt.Errorf("restored process cannot be waited on"), SystemError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *restoredProcess) startTime() (string, error) {
|
|
|
|
return p.processStartTime, nil
|
|
|
|
}
|
2015-02-07 14:33:10 +08:00
|
|
|
|
|
|
|
func (p *restoredProcess) signal(s os.Signal) error {
|
|
|
|
return newGenericError(fmt.Errorf("restored process cannot be signaled"), SystemError)
|
|
|
|
}
|