2015-04-27 18:53:21 -04:00
|
|
|
// +build linux
|
|
|
|
|
2014-01-08 21:47:57 -05:00
|
|
|
package lxc
|
|
|
|
|
|
|
|
import (
|
2014-05-05 17:45:14 -04:00
|
|
|
"encoding/json"
|
2014-09-23 17:47:33 -04:00
|
|
|
"errors"
|
2014-01-09 18:04:45 -05:00
|
|
|
"fmt"
|
2014-07-16 18:37:10 -04:00
|
|
|
"io"
|
2014-01-10 21:21:41 -05:00
|
|
|
"io/ioutil"
|
2014-01-10 21:09:07 -05:00
|
|
|
"os"
|
2014-01-08 21:47:57 -05:00
|
|
|
"os/exec"
|
2014-01-10 21:09:07 -05:00
|
|
|
"path"
|
2014-01-28 10:17:51 -05:00
|
|
|
"path/filepath"
|
2014-01-08 21:47:57 -05:00
|
|
|
"strconv"
|
2014-01-09 18:04:45 -05:00
|
|
|
"strings"
|
2015-01-30 12:29:46 -05:00
|
|
|
"sync"
|
2014-01-13 21:36:59 -05:00
|
|
|
"syscall"
|
2014-01-09 18:04:45 -05:00
|
|
|
"time"
|
2014-05-01 13:08:18 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-10-24 18:11:48 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-03-29 17:17:23 -04:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2015-01-30 12:29:46 -05:00
|
|
|
sysinfo "github.com/docker/docker/pkg/system"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2015-02-03 06:41:21 -05:00
|
|
|
"github.com/docker/docker/pkg/version"
|
2015-01-30 12:29:46 -05:00
|
|
|
"github.com/docker/libcontainer"
|
2014-07-24 18:25:29 -04:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2015-03-05 12:55:14 -05:00
|
|
|
"github.com/docker/libcontainer/configs"
|
|
|
|
"github.com/docker/libcontainer/system"
|
|
|
|
"github.com/docker/libcontainer/user"
|
2015-01-30 12:29:46 -05:00
|
|
|
"github.com/kr/pty"
|
2014-01-08 21:47:57 -05:00
|
|
|
)
|
|
|
|
|
2014-01-13 21:36:59 -05:00
|
|
|
const DriverName = "lxc"
|
|
|
|
|
2014-09-23 17:47:33 -04:00
|
|
|
var ErrExec = errors.New("Unsupported: Exec is not supported by the lxc driver")
|
|
|
|
|
2014-01-08 21:47:57 -05:00
|
|
|
type driver struct {
|
2015-01-30 12:29:46 -05:00
|
|
|
root string // root path for the driver to use
|
2015-03-24 20:11:49 -04:00
|
|
|
libPath string
|
2015-01-30 12:29:46 -05:00
|
|
|
initPath string
|
|
|
|
apparmor bool
|
|
|
|
sharedRoot bool
|
|
|
|
activeContainers map[string]*activeContainer
|
|
|
|
machineMemory int64
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type activeContainer struct {
|
2015-03-05 12:55:14 -05:00
|
|
|
container *configs.Config
|
2015-01-30 12:29:46 -05:00
|
|
|
cmd *exec.Cmd
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2015-03-24 20:11:49 -04:00
|
|
|
func NewDriver(root, libPath, initPath string, apparmor bool) (*driver, error) {
|
2015-03-24 18:53:23 -04:00
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-01-08 21:47:57 -05:00
|
|
|
// setup unconfined symlink
|
2014-01-10 21:09:07 -05:00
|
|
|
if err := linkLxcStart(root); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-30 12:29:46 -05:00
|
|
|
meminfo, err := sysinfo.ReadMemInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-01-09 18:04:45 -05:00
|
|
|
return &driver{
|
2015-01-30 12:29:46 -05:00
|
|
|
apparmor: apparmor,
|
|
|
|
root: root,
|
2015-03-24 20:11:49 -04:00
|
|
|
libPath: libPath,
|
2015-01-30 12:29:46 -05:00
|
|
|
initPath: initPath,
|
|
|
|
sharedRoot: rootIsShared(),
|
|
|
|
activeContainers: make(map[string]*activeContainer),
|
|
|
|
machineMemory: meminfo.MemTotal,
|
2014-01-09 18:04:45 -05:00
|
|
|
}, nil
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-01-13 19:10:23 -05:00
|
|
|
func (d *driver) Name() string {
|
2014-01-16 14:59:46 -05:00
|
|
|
version := d.version()
|
|
|
|
return fmt.Sprintf("%s-%s", DriverName, version)
|
2014-01-13 14:13:49 -05:00
|
|
|
}
|
|
|
|
|
2014-10-30 19:06:54 -04:00
|
|
|
func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (execdriver.ExitStatus, error) {
|
2014-07-16 18:37:10 -04:00
|
|
|
var (
|
2015-01-30 12:29:46 -05:00
|
|
|
term execdriver.Terminal
|
|
|
|
err error
|
|
|
|
dataPath = d.containerDir(c.ID)
|
2014-07-16 18:37:10 -04:00
|
|
|
)
|
|
|
|
|
2015-04-16 05:36:45 -04:00
|
|
|
container, err := d.createContainer(c)
|
|
|
|
if err != nil {
|
|
|
|
return execdriver.ExitStatus{ExitCode: -1}, err
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
if c.ProcessConfig.Tty {
|
|
|
|
term, err = NewTtyConsole(&c.ProcessConfig, pipes)
|
2014-07-16 18:37:10 -04:00
|
|
|
} else {
|
2014-08-26 18:05:37 -04:00
|
|
|
term, err = execdriver.NewStdConsole(&c.ProcessConfig, pipes)
|
2014-02-21 15:32:14 -05:00
|
|
|
}
|
2015-01-30 12:29:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return execdriver.ExitStatus{ExitCode: -1}, err
|
|
|
|
}
|
2015-04-16 05:36:45 -04:00
|
|
|
c.ProcessConfig.Terminal = term
|
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
d.Lock()
|
|
|
|
d.activeContainers[c.ID] = &activeContainer{
|
|
|
|
container: container,
|
|
|
|
cmd: &c.ProcessConfig.Cmd,
|
|
|
|
}
|
|
|
|
d.Unlock()
|
2014-07-16 18:37:10 -04:00
|
|
|
|
2014-08-13 03:37:30 -04:00
|
|
|
c.Mounts = append(c.Mounts, execdriver.Mount{
|
|
|
|
Source: d.initPath,
|
|
|
|
Destination: c.InitPath,
|
|
|
|
Writable: false,
|
|
|
|
Private: true,
|
|
|
|
})
|
2014-07-16 18:37:10 -04:00
|
|
|
|
2014-05-05 17:45:14 -04:00
|
|
|
if err := d.generateEnvConfig(c); err != nil {
|
2014-12-12 13:38:48 -05:00
|
|
|
return execdriver.ExitStatus{ExitCode: -1}, err
|
2014-05-05 17:45:14 -04:00
|
|
|
}
|
2014-01-15 20:26:04 -05:00
|
|
|
configPath, err := d.generateLXCConfig(c)
|
|
|
|
if err != nil {
|
2014-12-12 13:38:48 -05:00
|
|
|
return execdriver.ExitStatus{ExitCode: -1}, err
|
2014-01-15 20:26:04 -05:00
|
|
|
}
|
2014-01-08 21:47:57 -05:00
|
|
|
params := []string{
|
2014-01-13 19:18:46 -05:00
|
|
|
"lxc-start",
|
2014-01-10 17:26:29 -05:00
|
|
|
"-n", c.ID,
|
2014-01-15 20:26:04 -05:00
|
|
|
"-f", configPath,
|
2014-11-11 03:52:41 -05:00
|
|
|
}
|
2015-02-03 06:41:21 -05:00
|
|
|
|
|
|
|
// From lxc>=1.1 the default behavior is to daemonize containers after start
|
|
|
|
lxcVersion := version.Version(d.version())
|
|
|
|
if lxcVersion.GreaterThanOrEqualTo(version.Version("1.1")) {
|
|
|
|
params = append(params, "-F")
|
|
|
|
}
|
|
|
|
|
2014-11-11 03:52:41 -05:00
|
|
|
if c.Network.ContainerID != "" {
|
|
|
|
params = append(params,
|
|
|
|
"--share-net", c.Network.ContainerID,
|
|
|
|
)
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
2015-01-19 19:32:29 -05:00
|
|
|
if c.Ipc != nil {
|
|
|
|
if c.Ipc.ContainerID != "" {
|
|
|
|
params = append(params,
|
|
|
|
"--share-ipc", c.Ipc.ContainerID,
|
|
|
|
)
|
|
|
|
} else if c.Ipc.HostIpc {
|
|
|
|
params = append(params,
|
|
|
|
"--share-ipc", "1",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2014-01-08 21:47:57 -05:00
|
|
|
|
2014-11-11 03:52:41 -05:00
|
|
|
params = append(params,
|
|
|
|
"--",
|
|
|
|
c.InitPath,
|
|
|
|
)
|
2015-02-10 19:04:09 -05:00
|
|
|
if c.Network.Interface != nil {
|
|
|
|
params = append(params,
|
|
|
|
"-g", c.Network.Interface.Gateway,
|
|
|
|
"-i", fmt.Sprintf("%s/%d", c.Network.Interface.IPAddress, c.Network.Interface.IPPrefixLen),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
params = append(params,
|
|
|
|
"-mtu", strconv.Itoa(c.Network.Mtu),
|
|
|
|
)
|
2014-01-08 21:47:57 -05:00
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
if c.ProcessConfig.User != "" {
|
|
|
|
params = append(params, "-u", c.ProcessConfig.User)
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
if c.ProcessConfig.Privileged {
|
2014-01-10 21:09:07 -05:00
|
|
|
if d.apparmor {
|
|
|
|
params[0] = path.Join(d.root, "lxc-start-unconfined")
|
|
|
|
|
|
|
|
}
|
2014-01-08 21:47:57 -05:00
|
|
|
params = append(params, "-privileged")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.WorkingDir != "" {
|
|
|
|
params = append(params, "-w", c.WorkingDir)
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
params = append(params, "--", c.ProcessConfig.Entrypoint)
|
|
|
|
params = append(params, c.ProcessConfig.Arguments...)
|
2014-01-21 04:19:12 -05:00
|
|
|
|
2014-01-10 21:21:41 -05:00
|
|
|
if d.sharedRoot {
|
|
|
|
// lxc-start really needs / to be non-shared, or all kinds of stuff break
|
|
|
|
// when lxc-start unmount things and those unmounts propagate to the main
|
|
|
|
// mount namespace.
|
|
|
|
// What we really want is to clone into a new namespace and then
|
|
|
|
// mount / MS_REC|MS_SLAVE, but since we can't really clone or fork
|
|
|
|
// without exec in go we have to do this horrible shell hack...
|
|
|
|
shellString :=
|
|
|
|
"mount --make-rslave /; exec " +
|
2015-03-29 17:17:23 -04:00
|
|
|
stringutils.ShellQuoteArguments(params)
|
2014-01-10 21:21:41 -05:00
|
|
|
|
|
|
|
params = []string{
|
|
|
|
"unshare", "-m", "--", "/bin/sh", "-c", shellString,
|
|
|
|
}
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("lxc params %s", params)
|
2014-01-10 14:44:35 -05:00
|
|
|
var (
|
|
|
|
name = params[0]
|
|
|
|
arg = params[1:]
|
|
|
|
)
|
|
|
|
aname, err := exec.LookPath(name)
|
|
|
|
if err != nil {
|
|
|
|
aname = name
|
2014-01-09 18:04:45 -05:00
|
|
|
}
|
2014-08-26 18:05:37 -04:00
|
|
|
c.ProcessConfig.Path = aname
|
|
|
|
c.ProcessConfig.Args = append([]string{name}, arg...)
|
2014-01-10 14:44:35 -05:00
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
if err := createDeviceNodes(c.Rootfs, c.AutoCreatedDevices); err != nil {
|
2014-12-12 13:38:48 -05:00
|
|
|
return execdriver.ExitStatus{ExitCode: -1}, err
|
2014-02-17 18:14:30 -05:00
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
if err := c.ProcessConfig.Start(); err != nil {
|
2014-12-12 13:38:48 -05:00
|
|
|
return execdriver.ExitStatus{ExitCode: -1}, err
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-01-13 18:02:12 -05:00
|
|
|
var (
|
|
|
|
waitErr error
|
|
|
|
waitLock = make(chan struct{})
|
|
|
|
)
|
2014-05-27 16:52:05 -04:00
|
|
|
|
2014-01-10 18:06:16 -05:00
|
|
|
go func() {
|
2014-08-26 18:05:37 -04:00
|
|
|
if err := c.ProcessConfig.Wait(); err != nil {
|
2014-01-30 17:59:21 -05:00
|
|
|
if _, ok := err.(*exec.ExitError); !ok { // Do not propagate the error if it's simply a status code != 0
|
|
|
|
waitErr = err
|
|
|
|
}
|
2014-01-10 18:06:16 -05:00
|
|
|
}
|
2014-01-13 18:02:12 -05:00
|
|
|
close(waitLock)
|
2014-01-10 18:06:16 -05:00
|
|
|
}()
|
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
terminate := func(terr error) (execdriver.ExitStatus, error) {
|
2014-08-26 18:05:37 -04:00
|
|
|
if c.ProcessConfig.Process != nil {
|
|
|
|
c.ProcessConfig.Process.Kill()
|
|
|
|
c.ProcessConfig.Wait()
|
2014-03-11 14:39:28 -04:00
|
|
|
}
|
2015-01-30 12:29:46 -05:00
|
|
|
return execdriver.ExitStatus{ExitCode: -1}, terr
|
|
|
|
}
|
|
|
|
// Poll lxc for RUNNING status
|
|
|
|
pid, err := d.waitForStart(c, waitLock)
|
|
|
|
if err != nil {
|
|
|
|
return terminate(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroupPaths, err := cgroupPaths(c.ID)
|
|
|
|
if err != nil {
|
|
|
|
return terminate(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state := &libcontainer.State{
|
2015-03-05 12:55:14 -05:00
|
|
|
InitProcessPid: pid,
|
|
|
|
CgroupPaths: cgroupPaths,
|
2015-01-30 12:29:46 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
f, err := os.Create(filepath.Join(dataPath, "state.json"))
|
|
|
|
if err != nil {
|
|
|
|
return terminate(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := json.NewEncoder(f).Encode(state); err != nil {
|
2015-01-30 12:29:46 -05:00
|
|
|
return terminate(err)
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
2014-05-27 16:52:05 -04:00
|
|
|
|
2014-08-26 18:44:00 -04:00
|
|
|
c.ContainerPid = pid
|
2014-01-13 18:02:12 -05:00
|
|
|
|
|
|
|
if startCallback != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Invoking startCallback")
|
2014-08-26 18:44:00 -04:00
|
|
|
startCallback(&c.ProcessConfig, pid)
|
2014-01-13 18:02:12 -05:00
|
|
|
}
|
2015-03-05 12:55:14 -05:00
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
oomKill := false
|
2015-03-05 12:55:14 -05:00
|
|
|
oomKillNotification, err := notifyOnOOM(cgroupPaths)
|
2015-03-10 16:45:13 -04:00
|
|
|
|
|
|
|
<-waitLock
|
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
if err == nil {
|
|
|
|
_, oomKill = <-oomKillNotification
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("oomKill error %s waitErr %s", oomKill, waitErr)
|
2015-01-30 12:29:46 -05:00
|
|
|
} else {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Warnf("Your kernel does not support OOM notifications: %s", err)
|
2015-01-30 12:29:46 -05:00
|
|
|
}
|
2014-01-13 18:02:12 -05:00
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
// check oom error
|
|
|
|
exitCode := getExitCode(c)
|
|
|
|
if oomKill {
|
|
|
|
exitCode = 137
|
|
|
|
}
|
|
|
|
return execdriver.ExitStatus{ExitCode: exitCode, OOMKilled: oomKill}, waitErr
|
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
// copy from libcontainer
|
|
|
|
func notifyOnOOM(paths map[string]string) (<-chan struct{}, error) {
|
|
|
|
dir := paths["memory"]
|
|
|
|
if dir == "" {
|
|
|
|
return nil, fmt.Errorf("There is no path for %q in state", "memory")
|
|
|
|
}
|
|
|
|
oomControl, err := os.Open(filepath.Join(dir, "memory.oom_control"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fd, _, syserr := syscall.RawSyscall(syscall.SYS_EVENTFD2, 0, syscall.FD_CLOEXEC, 0)
|
|
|
|
if syserr != 0 {
|
|
|
|
oomControl.Close()
|
|
|
|
return nil, syserr
|
|
|
|
}
|
|
|
|
|
|
|
|
eventfd := os.NewFile(fd, "eventfd")
|
|
|
|
|
|
|
|
eventControlPath := filepath.Join(dir, "cgroup.event_control")
|
|
|
|
data := fmt.Sprintf("%d %d", eventfd.Fd(), oomControl.Fd())
|
|
|
|
if err := ioutil.WriteFile(eventControlPath, []byte(data), 0700); err != nil {
|
|
|
|
eventfd.Close()
|
|
|
|
oomControl.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
close(ch)
|
|
|
|
eventfd.Close()
|
|
|
|
oomControl.Close()
|
|
|
|
}()
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
for {
|
|
|
|
if _, err := eventfd.Read(buf); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// When a cgroup is destroyed, an event is sent to eventfd.
|
|
|
|
// So if the control path is gone, return instead of notifying.
|
|
|
|
if _, err := os.Lstat(eventControlPath); os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- struct{}{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
// createContainer populates and configures the container type with the
|
|
|
|
// data provided by the execdriver.Command
|
2015-03-05 12:55:14 -05:00
|
|
|
func (d *driver) createContainer(c *execdriver.Command) (*configs.Config, error) {
|
2015-01-30 12:29:46 -05:00
|
|
|
container := execdriver.InitContainer(c)
|
|
|
|
if err := execdriver.SetupCgroups(container, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return container, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an map of susbystem -> container cgroup
|
|
|
|
func cgroupPaths(containerId string) (map[string]string, error) {
|
|
|
|
subsystems, err := cgroups.GetAllSubsystems()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("subsystems: %s", subsystems)
|
2015-01-30 12:29:46 -05:00
|
|
|
paths := make(map[string]string)
|
|
|
|
for _, subsystem := range subsystems {
|
|
|
|
cgroupRoot, cgroupDir, err := findCgroupRootAndDir(subsystem)
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("cgroup path %s %s", cgroupRoot, cgroupDir)
|
2015-01-30 12:29:46 -05:00
|
|
|
if err != nil {
|
|
|
|
//unsupported subystem
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
path := filepath.Join(cgroupRoot, cgroupDir, "lxc", containerId)
|
|
|
|
paths[subsystem] = path
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths, nil
|
2014-01-28 05:16:02 -05:00
|
|
|
}
|
|
|
|
|
2015-03-05 12:55:14 -05:00
|
|
|
// this is copy from old libcontainer nodes.go
|
|
|
|
func createDeviceNodes(rootfs string, nodesToCreate []*configs.Device) error {
|
|
|
|
oldMask := syscall.Umask(0000)
|
|
|
|
defer syscall.Umask(oldMask)
|
|
|
|
|
|
|
|
for _, node := range nodesToCreate {
|
|
|
|
if err := createDeviceNode(rootfs, node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates the device node in the rootfs of the container.
|
|
|
|
func createDeviceNode(rootfs string, node *configs.Device) error {
|
|
|
|
var (
|
|
|
|
dest = filepath.Join(rootfs, node.Path)
|
|
|
|
parent = filepath.Dir(dest)
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(parent, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileMode := node.FileMode
|
|
|
|
switch node.Type {
|
|
|
|
case 'c':
|
|
|
|
fileMode |= syscall.S_IFCHR
|
|
|
|
case 'b':
|
|
|
|
fileMode |= syscall.S_IFBLK
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%c is not a valid device type for device %s", node.Type, node.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Mknod(dest, uint32(fileMode), node.Mkdev()); err != nil && !os.IsExist(err) {
|
|
|
|
return fmt.Errorf("mknod %s %s", node.Path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := syscall.Chown(dest, int(node.Uid), int(node.Gid)); err != nil {
|
|
|
|
return fmt.Errorf("chown %s to %d:%d", node.Path, node.Uid, node.Gid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupUser changes the groups, gid, and uid for the user inside the container
|
|
|
|
// copy from libcontainer, cause not it's private
|
|
|
|
func setupUser(userSpec string) error {
|
|
|
|
// Set up defaults.
|
|
|
|
defaultExecUser := user.ExecUser{
|
|
|
|
Uid: syscall.Getuid(),
|
|
|
|
Gid: syscall.Getgid(),
|
|
|
|
Home: "/",
|
|
|
|
}
|
|
|
|
passwdPath, err := user.GetPasswdPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
groupPath, err := user.GetGroupPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
execUser, err := user.GetExecUserPath(userSpec, &defaultExecUser, passwdPath, groupPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-10 12:43:11 -04:00
|
|
|
if err := syscall.Setgroups(execUser.Sgids); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-05 12:55:14 -05:00
|
|
|
if err := system.Setgid(execUser.Gid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := system.Setuid(execUser.Uid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// if we didn't get HOME already, set it based on the user's HOME
|
|
|
|
if envHome := os.Getenv("HOME"); envHome == "" {
|
|
|
|
if err := os.Setenv("HOME", execUser.Home); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-28 05:16:02 -05:00
|
|
|
/// Return the exit code of the process
|
|
|
|
// if the process has not exited -1 will be returned
|
|
|
|
func getExitCode(c *execdriver.Command) int {
|
2014-08-26 18:05:37 -04:00
|
|
|
if c.ProcessConfig.ProcessState == nil {
|
2014-01-28 05:16:02 -05:00
|
|
|
return -1
|
|
|
|
}
|
2014-08-26 18:05:37 -04:00
|
|
|
return c.ProcessConfig.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-01-20 19:05:07 -05:00
|
|
|
func (d *driver) Kill(c *execdriver.Command, sig int) error {
|
2014-03-06 17:14:25 -05:00
|
|
|
return KillLxc(c.ID, sig)
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-05-21 17:06:18 -04:00
|
|
|
func (d *driver) Pause(c *execdriver.Command) error {
|
|
|
|
_, err := exec.LookPath("lxc-freeze")
|
|
|
|
if err == nil {
|
|
|
|
output, errExec := exec.Command("lxc-freeze", "-n", c.ID).CombinedOutput()
|
|
|
|
if errExec != nil {
|
|
|
|
return fmt.Errorf("Err: %s Output: %s", errExec, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) Unpause(c *execdriver.Command) error {
|
|
|
|
_, err := exec.LookPath("lxc-unfreeze")
|
|
|
|
if err == nil {
|
|
|
|
output, errExec := exec.Command("lxc-unfreeze", "-n", c.ID).CombinedOutput()
|
|
|
|
if errExec != nil {
|
|
|
|
return fmt.Errorf("Err: %s Output: %s", errExec, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-26 02:48:16 -04:00
|
|
|
func (d *driver) Terminate(c *execdriver.Command) error {
|
|
|
|
return KillLxc(c.ID, 9)
|
|
|
|
}
|
|
|
|
|
2014-01-16 14:59:46 -05:00
|
|
|
func (d *driver) version() string {
|
2014-02-27 19:32:58 -05:00
|
|
|
var (
|
|
|
|
version string
|
|
|
|
output []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if _, errPath := exec.LookPath("lxc-version"); errPath == nil {
|
|
|
|
output, err = exec.Command("lxc-version").CombinedOutput()
|
|
|
|
} else {
|
|
|
|
output, err = exec.Command("lxc-start", "--version").CombinedOutput()
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
version = strings.TrimSpace(string(output))
|
|
|
|
if parts := strings.SplitN(version, ":", 2); len(parts) == 2 {
|
|
|
|
version = strings.TrimSpace(parts[1])
|
2014-01-10 21:09:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2014-03-06 17:14:25 -05:00
|
|
|
func KillLxc(id string, sig int) error {
|
2014-01-30 12:42:28 -05:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
output []byte
|
|
|
|
)
|
|
|
|
_, err = exec.LookPath("lxc-kill")
|
|
|
|
if err == nil {
|
2014-03-06 17:14:25 -05:00
|
|
|
output, err = exec.Command("lxc-kill", "-n", id, strconv.Itoa(sig)).CombinedOutput()
|
2014-01-30 12:42:28 -05:00
|
|
|
} else {
|
2014-03-06 17:14:25 -05:00
|
|
|
output, err = exec.Command("lxc-stop", "-k", "-n", id, strconv.Itoa(sig)).CombinedOutput()
|
2014-01-30 12:42:28 -05:00
|
|
|
}
|
2014-01-10 18:34:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Err: %s Output: %s", err, output)
|
|
|
|
}
|
|
|
|
return nil
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 20:57:27 -05:00
|
|
|
// wait for the process to start and return the pid for the process
|
|
|
|
func (d *driver) waitForStart(c *execdriver.Command, waitLock chan struct{}) (int, error) {
|
2014-01-10 17:26:29 -05:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
output []byte
|
|
|
|
)
|
2014-01-09 18:04:45 -05:00
|
|
|
// We wait for the container to be fully running.
|
|
|
|
// Timeout after 5 seconds. In case of broken pipe, just retry.
|
|
|
|
// Note: The container can run and finish correctly before
|
|
|
|
// the end of this loop
|
|
|
|
for now := time.Now(); time.Since(now) < 5*time.Second; {
|
2014-01-10 19:11:19 -05:00
|
|
|
select {
|
2014-01-13 18:02:12 -05:00
|
|
|
case <-waitLock:
|
2014-01-10 19:11:19 -05:00
|
|
|
// If the process dies while waiting for it, just return
|
2014-03-05 20:57:27 -05:00
|
|
|
return -1, nil
|
2014-01-10 19:11:19 -05:00
|
|
|
default:
|
2014-01-10 17:26:29 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 14:46:25 -05:00
|
|
|
output, err = d.getInfo(c.ID)
|
2014-05-10 18:05:02 -04:00
|
|
|
if err == nil {
|
|
|
|
info, err := parseLxcInfo(string(output))
|
2014-01-09 18:04:45 -05:00
|
|
|
if err != nil {
|
2014-03-05 20:57:27 -05:00
|
|
|
return -1, err
|
2014-01-09 18:04:45 -05:00
|
|
|
}
|
2014-05-10 18:05:02 -04:00
|
|
|
if info.Running {
|
|
|
|
return info.Pid, nil
|
|
|
|
}
|
2014-01-09 18:04:45 -05:00
|
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
2014-03-05 20:57:27 -05:00
|
|
|
return -1, execdriver.ErrNotRunning
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 14:46:25 -05:00
|
|
|
func (d *driver) getInfo(id string) ([]byte, error) {
|
2014-03-05 20:57:27 -05:00
|
|
|
return exec.Command("lxc-info", "-n", id).CombinedOutput()
|
2014-01-15 14:46:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type info struct {
|
|
|
|
ID string
|
|
|
|
driver *driver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *info) IsRunning() bool {
|
|
|
|
var running bool
|
|
|
|
|
|
|
|
output, err := i.driver.getInfo(i.ID)
|
|
|
|
if err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("Error getting info for lxc container %s: %s (%s)", i.ID, err, output)
|
2014-02-07 22:09:52 -05:00
|
|
|
return false
|
2014-01-15 14:46:25 -05:00
|
|
|
}
|
|
|
|
if strings.Contains(string(output), "RUNNING") {
|
|
|
|
running = true
|
|
|
|
}
|
|
|
|
return running
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) Info(id string) execdriver.Info {
|
|
|
|
return &info{
|
|
|
|
ID: id,
|
|
|
|
driver: d,
|
|
|
|
}
|
2014-01-10 17:26:29 -05:00
|
|
|
}
|
2014-01-10 21:09:07 -05:00
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
func findCgroupRootAndDir(subsystem string) (string, string, error) {
|
2014-01-28 10:17:51 -05:00
|
|
|
cgroupRoot, err := cgroups.FindCgroupMountpoint(subsystem)
|
|
|
|
if err != nil {
|
2015-01-30 12:29:46 -05:00
|
|
|
return "", "", err
|
2014-01-28 10:17:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cgroupDir, err := cgroups.GetThisCgroupDir(subsystem)
|
2015-01-30 12:29:46 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return cgroupRoot, cgroupDir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) GetPidsForContainer(id string) ([]int, error) {
|
|
|
|
pids := []int{}
|
|
|
|
|
|
|
|
// cpu is chosen because it is the only non optional subsystem in cgroups
|
|
|
|
subsystem := "cpu"
|
|
|
|
cgroupRoot, cgroupDir, err := findCgroupRootAndDir(subsystem)
|
2014-01-28 10:17:51 -05:00
|
|
|
if err != nil {
|
|
|
|
return pids, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Join(cgroupRoot, cgroupDir, id, "tasks")
|
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
|
|
// With more recent lxc versions use, cgroup will be in lxc/
|
|
|
|
filename = filepath.Join(cgroupRoot, cgroupDir, "lxc", id, "tasks")
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return pids, err
|
|
|
|
}
|
|
|
|
for _, p := range strings.Split(string(output), "\n") {
|
|
|
|
if len(p) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pid, err := strconv.Atoi(p)
|
|
|
|
if err != nil {
|
|
|
|
return pids, fmt.Errorf("Invalid pid '%s': %s", p, err)
|
|
|
|
}
|
|
|
|
pids = append(pids, pid)
|
|
|
|
}
|
|
|
|
return pids, nil
|
|
|
|
}
|
|
|
|
|
2014-01-10 21:09:07 -05:00
|
|
|
func linkLxcStart(root string) error {
|
|
|
|
sourcePath, err := exec.LookPath("lxc-start")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
targetPath := path.Join(root, "lxc-start-unconfined")
|
|
|
|
|
|
|
|
if _, err := os.Lstat(targetPath); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
} else if err == nil {
|
|
|
|
if err := os.Remove(targetPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os.Symlink(sourcePath, targetPath)
|
|
|
|
}
|
2014-01-10 21:21:41 -05:00
|
|
|
|
2014-01-13 18:09:41 -05:00
|
|
|
// TODO: This can be moved to the mountinfo reader in the mount pkg
|
2014-01-10 21:21:41 -05:00
|
|
|
func rootIsShared() bool {
|
|
|
|
if data, err := ioutil.ReadFile("/proc/self/mountinfo"); err == nil {
|
|
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
|
|
cols := strings.Split(line, " ")
|
|
|
|
if len(cols) >= 6 && cols[4] == "/" {
|
|
|
|
return strings.HasPrefix(cols[6], "shared")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No idea, probably safe to assume so
|
|
|
|
return true
|
|
|
|
}
|
2014-01-15 20:26:04 -05:00
|
|
|
|
2015-01-30 12:29:46 -05:00
|
|
|
func (d *driver) containerDir(containerId string) string {
|
2015-03-24 20:11:49 -04:00
|
|
|
return path.Join(d.libPath, "containers", containerId)
|
2015-01-30 12:29:46 -05:00
|
|
|
}
|
|
|
|
|
2014-01-20 19:05:07 -05:00
|
|
|
func (d *driver) generateLXCConfig(c *execdriver.Command) (string, error) {
|
2015-01-30 12:29:46 -05:00
|
|
|
root := path.Join(d.containerDir(c.ID), "config.lxc")
|
2014-09-29 18:40:26 -04:00
|
|
|
|
2014-01-15 20:26:04 -05:00
|
|
|
fo, err := os.Create(root)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer fo.Close()
|
|
|
|
|
|
|
|
if err := LxcTemplateCompiled.Execute(fo, struct {
|
2014-01-20 19:05:07 -05:00
|
|
|
*execdriver.Command
|
2014-09-29 18:40:26 -04:00
|
|
|
AppArmor bool
|
2014-01-15 20:26:04 -05:00
|
|
|
}{
|
2014-09-29 18:40:26 -04:00
|
|
|
Command: c,
|
|
|
|
AppArmor: d.apparmor,
|
2014-01-15 20:26:04 -05:00
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-09-29 18:40:26 -04:00
|
|
|
|
2014-01-15 20:26:04 -05:00
|
|
|
return root, nil
|
|
|
|
}
|
2014-05-05 17:45:14 -04:00
|
|
|
|
|
|
|
func (d *driver) generateEnvConfig(c *execdriver.Command) error {
|
2014-08-26 18:05:37 -04:00
|
|
|
data, err := json.Marshal(c.ProcessConfig.Env)
|
2014-05-05 17:45:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-24 20:11:49 -04:00
|
|
|
p := path.Join(d.libPath, "containers", c.ID, "config.env")
|
2014-08-13 03:37:30 -04:00
|
|
|
c.Mounts = append(c.Mounts, execdriver.Mount{
|
|
|
|
Source: p,
|
|
|
|
Destination: "/.dockerenv",
|
|
|
|
Writable: false,
|
|
|
|
Private: true,
|
|
|
|
})
|
2014-05-05 17:45:14 -04:00
|
|
|
|
|
|
|
return ioutil.WriteFile(p, data, 0600)
|
|
|
|
}
|
2014-07-16 18:37:10 -04:00
|
|
|
|
2014-09-10 03:34:38 -04:00
|
|
|
// Clean not implemented for lxc
|
|
|
|
func (d *driver) Clean(id string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-16 18:37:10 -04:00
|
|
|
type TtyConsole struct {
|
|
|
|
MasterPty *os.File
|
|
|
|
SlavePty *os.File
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
func NewTtyConsole(processConfig *execdriver.ProcessConfig, pipes *execdriver.Pipes) (*TtyConsole, error) {
|
2014-07-16 18:37:10 -04:00
|
|
|
// lxc is special in that we cannot create the master outside of the container without
|
|
|
|
// opening the slave because we have nothing to provide to the cmd. We have to open both then do
|
|
|
|
// the crazy setup on command right now instead of passing the console path to lxc and telling it
|
|
|
|
// to open up that console. we save a couple of openfiles in the native driver because we can do
|
|
|
|
// this.
|
|
|
|
ptyMaster, ptySlave, err := pty.Open()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tty := &TtyConsole{
|
|
|
|
MasterPty: ptyMaster,
|
|
|
|
SlavePty: ptySlave,
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
if err := tty.AttachPipes(&processConfig.Cmd, pipes); err != nil {
|
2014-07-16 18:37:10 -04:00
|
|
|
tty.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:05:37 -04:00
|
|
|
processConfig.Console = tty.SlavePty.Name()
|
2014-07-16 18:37:10 -04:00
|
|
|
|
|
|
|
return tty, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TtyConsole) Master() *os.File {
|
|
|
|
return t.MasterPty
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TtyConsole) Resize(h, w int) error {
|
|
|
|
return term.SetWinsize(t.MasterPty.Fd(), &term.Winsize{Height: uint16(h), Width: uint16(w)})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TtyConsole) AttachPipes(command *exec.Cmd, pipes *execdriver.Pipes) error {
|
|
|
|
command.Stdout = t.SlavePty
|
|
|
|
command.Stderr = t.SlavePty
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if wb, ok := pipes.Stdout.(interface {
|
|
|
|
CloseWriters() error
|
|
|
|
}); ok {
|
|
|
|
defer wb.CloseWriters()
|
|
|
|
}
|
|
|
|
|
|
|
|
io.Copy(pipes.Stdout, t.MasterPty)
|
|
|
|
}()
|
|
|
|
|
|
|
|
if pipes.Stdin != nil {
|
|
|
|
command.Stdin = t.SlavePty
|
|
|
|
command.SysProcAttr.Setctty = true
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
io.Copy(t.MasterPty, pipes.Stdin)
|
|
|
|
|
|
|
|
pipes.Stdin.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TtyConsole) Close() error {
|
|
|
|
t.SlavePty.Close()
|
|
|
|
return t.MasterPty.Close()
|
|
|
|
}
|
2014-09-04 01:29:19 -04:00
|
|
|
|
|
|
|
func (d *driver) Exec(c *execdriver.Command, processConfig *execdriver.ProcessConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
|
2014-09-23 17:47:33 -04:00
|
|
|
return -1, ErrExec
|
2014-09-04 01:29:19 -04:00
|
|
|
}
|
2015-01-07 17:43:04 -05:00
|
|
|
|
|
|
|
func (d *driver) Stats(id string) (*execdriver.ResourceStats, error) {
|
2015-03-24 20:11:49 -04:00
|
|
|
if _, ok := d.activeContainers[id]; !ok {
|
|
|
|
return nil, fmt.Errorf("%s is not a key in active containers", id)
|
|
|
|
}
|
2015-01-30 12:29:46 -05:00
|
|
|
return execdriver.Stats(d.containerDir(id), d.activeContainers[id].container.Cgroups.Memory, d.machineMemory)
|
2015-01-07 17:43:04 -05:00
|
|
|
}
|