2014-10-23 04:45:23 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package libcontainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
configFilename = "config.json"
|
|
|
|
stateFilename = "state.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a linux based container factory based in the root directory.
|
2014-10-23 07:27:06 +08:00
|
|
|
func New(root string) (Factory, error) {
|
2014-10-23 04:45:23 +08:00
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &linuxFactory{
|
|
|
|
root: root,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// linuxFactory implements the default factory interface for linux based systems.
|
|
|
|
type linuxFactory struct {
|
|
|
|
// root is the root directory
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (l *linuxFactory) Create(id string, config *Config) (Container, error) {
|
2014-10-23 04:45:23 +08:00
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (l *linuxFactory) Load(id string) (ContainerInfo, error) {
|
2014-10-23 04:45:23 +08:00
|
|
|
containerRoot := filepath.Join(l.root, id)
|
|
|
|
config, err := l.loadContainerConfig(containerRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := l.loadContainerState(containerRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &linuxContainer{
|
2014-10-23 07:53:28 +08:00
|
|
|
id: id,
|
|
|
|
root: containerRoot,
|
|
|
|
config: config,
|
|
|
|
state: state,
|
|
|
|
cgroupManager: newCgroupsManager(),
|
2014-10-23 04:45:23 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (l *linuxFactory) loadContainerConfig(root string) (*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()
|
|
|
|
|
|
|
|
var config *Config
|
|
|
|
if err := json.NewDecoder(f).Decode(&config); err != nil {
|
|
|
|
return nil, newGenericError(err, ConfigInvalid)
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:27:06 +08:00
|
|
|
func (l *linuxFactory) loadContainerState(root string) (*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()
|
|
|
|
|
|
|
|
var state *State
|
|
|
|
if err := json.NewDecoder(f).Decode(&state); err != nil {
|
|
|
|
return nil, newGenericError(err, SystemError)
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|