2014-01-08 21:47:57 -05:00
|
|
|
package lxc
|
|
|
|
|
|
|
|
import (
|
2014-01-09 18:04:45 -05:00
|
|
|
"fmt"
|
2014-01-08 21:47:57 -05:00
|
|
|
"github.com/dotcloud/docker/execdriver"
|
2014-01-28 10:17:51 -05:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups"
|
2014-01-10 21:21:41 -05:00
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
|
"io/ioutil"
|
2014-01-13 21:36:59 -05:00
|
|
|
"log"
|
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"
|
2014-01-13 21:36:59 -05:00
|
|
|
"syscall"
|
2014-01-09 18:04:45 -05:00
|
|
|
"time"
|
2014-01-08 21:47:57 -05:00
|
|
|
)
|
|
|
|
|
2014-01-13 21:36:59 -05:00
|
|
|
const DriverName = "lxc"
|
|
|
|
|
|
|
|
func init() {
|
2014-01-15 16:57:07 -05:00
|
|
|
execdriver.RegisterInitFunc(DriverName, func(args *execdriver.InitArgs) error {
|
2014-01-13 21:36:59 -05:00
|
|
|
if err := setupHostname(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setupNetworking(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setupCapabilities(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-20 19:05:07 -05:00
|
|
|
|
2014-01-13 21:36:59 -05:00
|
|
|
if err := setupWorkingDirectory(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := changeUser(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-20 19:05:07 -05:00
|
|
|
|
2014-01-13 21:36:59 -05:00
|
|
|
path, err := exec.LookPath(args.Args[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to locate %v", args.Args[0])
|
|
|
|
os.Exit(127)
|
|
|
|
}
|
|
|
|
if err := syscall.Exec(path, args.Args, os.Environ()); err != nil {
|
2014-01-17 19:53:13 -05:00
|
|
|
return fmt.Errorf("dockerinit unable to execute %s - %s", path, err)
|
2014-01-13 21:36:59 -05:00
|
|
|
}
|
|
|
|
panic("Unreachable")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:47:57 -05:00
|
|
|
type driver struct {
|
2014-01-10 21:21:41 -05:00
|
|
|
root string // root path for the driver to use
|
|
|
|
apparmor bool
|
|
|
|
sharedRoot bool
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-01-13 19:10:23 -05:00
|
|
|
func NewDriver(root string, apparmor bool) (*driver, error) {
|
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
|
|
|
|
}
|
2014-01-09 18:04:45 -05:00
|
|
|
return &driver{
|
2014-01-10 21:21:41 -05:00
|
|
|
apparmor: apparmor,
|
|
|
|
root: root,
|
|
|
|
sharedRoot: rootIsShared(),
|
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-02-21 15:32:14 -05:00
|
|
|
func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
|
2014-02-21 16:27:15 -05:00
|
|
|
if err := SetTerminal(c, pipes); err != nil {
|
2014-02-21 15:42:37 -05:00
|
|
|
return -1, err
|
2014-02-21 15:32:14 -05:00
|
|
|
}
|
2014-01-15 20:26:04 -05:00
|
|
|
configPath, err := d.generateLXCConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
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-01-08 21:47:57 -05:00
|
|
|
"--",
|
|
|
|
c.InitPath,
|
2014-01-13 14:13:49 -05:00
|
|
|
"-driver",
|
2014-01-16 18:09:00 -05:00
|
|
|
DriverName,
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Network != nil {
|
|
|
|
params = append(params,
|
|
|
|
"-g", c.Network.Gateway,
|
2014-01-09 18:04:45 -05:00
|
|
|
"-i", fmt.Sprintf("%s/%d", c.Network.IPAddress, c.Network.IPPrefixLen),
|
|
|
|
"-mtu", strconv.Itoa(c.Network.Mtu),
|
2014-01-08 21:47:57 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.User != "" {
|
|
|
|
params = append(params, "-u", c.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.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-01-21 04:19:12 -05:00
|
|
|
params = append(params, "--", c.Entrypoint)
|
|
|
|
params = append(params, c.Arguments...)
|
|
|
|
|
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 " +
|
|
|
|
utils.ShellQuoteArguments(params)
|
|
|
|
|
|
|
|
params = []string{
|
|
|
|
"unshare", "-m", "--", "/bin/sh", "-c", shellString,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-10 14:44:35 -05:00
|
|
|
c.Path = aname
|
|
|
|
c.Args = append([]string{name}, arg...)
|
|
|
|
|
|
|
|
if err := c.Start(); err != nil {
|
2014-01-13 18:02:12 -05:00
|
|
|
return -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-01-10 18:06:16 -05:00
|
|
|
go func() {
|
|
|
|
if err := c.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
|
|
|
}()
|
|
|
|
|
2014-01-13 18:02:12 -05:00
|
|
|
// Poll lxc for RUNNING status
|
|
|
|
if err := d.waitForStart(c, waitLock); err != nil {
|
|
|
|
return -1, err
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
2014-01-13 18:02:12 -05:00
|
|
|
|
|
|
|
if startCallback != nil {
|
|
|
|
startCallback(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-waitLock
|
|
|
|
|
2014-01-28 05:16:02 -05:00
|
|
|
return getExitCode(c), waitErr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the exit code of the process
|
|
|
|
// if the process has not exited -1 will be returned
|
|
|
|
func getExitCode(c *execdriver.Command) int {
|
|
|
|
if c.ProcessState == nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return c.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-01-09 18:04:45 -05:00
|
|
|
return d.kill(c, sig)
|
|
|
|
}
|
2014-01-08 21:47:57 -05:00
|
|
|
|
2014-01-20 21:27:36 -05:00
|
|
|
func (d *driver) Restore(c *execdriver.Command) error {
|
2014-01-13 20:17:59 -05:00
|
|
|
for {
|
2014-01-20 21:27:36 -05:00
|
|
|
output, err := exec.Command("lxc-info", "-n", c.ID).CombinedOutput()
|
2014-01-13 20:17:59 -05:00
|
|
|
if err != nil {
|
2014-01-09 18:04:45 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-01-13 20:17:59 -05:00
|
|
|
if !strings.Contains(string(output), "RUNNING") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2014-01-09 18:04:45 -05:00
|
|
|
}
|
2014-01-08 21:47:57 -05:00
|
|
|
}
|
|
|
|
|
2014-01-16 14:59:46 -05:00
|
|
|
func (d *driver) version() string {
|
2014-01-10 21:09:07 -05:00
|
|
|
version := ""
|
|
|
|
if output, err := exec.Command("lxc-version").CombinedOutput(); err == nil {
|
|
|
|
outputStr := string(output)
|
|
|
|
if len(strings.SplitN(outputStr, ":", 2)) == 2 {
|
|
|
|
version = strings.TrimSpace(strings.SplitN(outputStr, ":", 2)[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2014-01-20 19:05:07 -05:00
|
|
|
func (d *driver) kill(c *execdriver.Command, sig int) error {
|
2014-01-30 12:42:28 -05:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
output []byte
|
|
|
|
)
|
|
|
|
_, err = exec.LookPath("lxc-kill")
|
|
|
|
if err == nil {
|
|
|
|
output, err = exec.Command("lxc-kill", "-n", c.ID, strconv.Itoa(sig)).CombinedOutput()
|
|
|
|
} else {
|
|
|
|
output, err = exec.Command("lxc-stop", "-k", "-n", c.ID, strconv.Itoa(sig)).CombinedOutput()
|
|
|
|
}
|
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-01-20 19:05:07 -05:00
|
|
|
func (d *driver) waitForStart(c *execdriver.Command, waitLock chan struct{}) 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-01-13 19:10:23 -05:00
|
|
|
return nil
|
2014-01-10 19:11:19 -05:00
|
|
|
if c.ProcessState != nil && c.ProcessState.Exited() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
2014-01-10 17:26:29 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 14:46:25 -05:00
|
|
|
output, err = d.getInfo(c.ID)
|
2014-01-09 18:04:45 -05:00
|
|
|
if err != nil {
|
2014-01-15 14:46:25 -05:00
|
|
|
output, err = d.getInfo(c.ID)
|
2014-01-09 18:04:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.Contains(string(output), "RUNNING") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
2014-01-13 19:18:46 -05:00
|
|
|
return 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) {
|
|
|
|
return exec.Command("lxc-info", "-s", "-n", id).CombinedOutput()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2014-02-07 22:09:52 -05:00
|
|
|
utils.Errorf("Error getting info for lxc container %s: %s (%s)", i.ID, err, output)
|
|
|
|
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
|
|
|
|
2014-01-28 10:17:51 -05:00
|
|
|
func (d *driver) GetPidsForContainer(id string) ([]int, error) {
|
|
|
|
pids := []int{}
|
|
|
|
|
2014-02-24 14:08:20 -05:00
|
|
|
// cpu is chosen because it is the only non optional subsystem in cgroups
|
|
|
|
subsystem := "cpu"
|
2014-01-28 10:17:51 -05:00
|
|
|
cgroupRoot, err := cgroups.FindCgroupMountpoint(subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return pids, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroupDir, err := cgroups.GetThisCgroupDir(subsystem)
|
|
|
|
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
|
|
|
|
2014-01-20 19:05:07 -05:00
|
|
|
func (d *driver) generateLXCConfig(c *execdriver.Command) (string, error) {
|
|
|
|
root := path.Join(d.root, "containers", c.ID, "config.lxc")
|
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-01-15 20:26:04 -05:00
|
|
|
AppArmor bool
|
|
|
|
}{
|
2014-01-20 19:05:07 -05:00
|
|
|
Command: c,
|
2014-01-15 20:26:04 -05:00
|
|
|
AppArmor: d.apparmor,
|
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return root, nil
|
|
|
|
}
|