Skip to content

Commit

Permalink
Use argv0 as reexec implementation for dockerinit
Browse files Browse the repository at this point in the history
This changes the way the exec drivers work by not specifing a -driver
flag on reexec.  For each of the exec  drivers they register their own
functions that will be matched aginst the argv 0 on exec and called if
they match.

This also allows any functionality to be added to docker so that the
binary can be reexec'd and any type of function can be called.  I moved
the flag parsing on docker exec to the specific initializers so that the
implementations do not bleed into one another.  This also allows for
more flexability within reexec initializers to specify their own flags
and options.

Signed-off-by: Michael Crosby <michael@docker.com>
  • Loading branch information
Michael Crosby committed Aug 11, 2014
1 parent e033425 commit 7321067
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 185 deletions.
44 changes: 1 addition & 43 deletions daemon/execdriver/driver.go
Expand Up @@ -20,49 +20,7 @@ var (
ErrDriverNotFound = errors.New("The requested docker init has not been found")
)

var dockerInitFcts map[string]InitFunc

type (
StartCallback func(*Command)
InitFunc func(i *InitArgs) error
)

func RegisterInitFunc(name string, fct InitFunc) error {
if dockerInitFcts == nil {
dockerInitFcts = make(map[string]InitFunc)
}
if _, ok := dockerInitFcts[name]; ok {
return ErrDriverAlreadyRegistered
}
dockerInitFcts[name] = fct
return nil
}

func GetInitFunc(name string) (InitFunc, error) {
fct, ok := dockerInitFcts[name]
if !ok {
return nil, ErrDriverNotFound
}
return fct, nil
}

// Args provided to the init function for a driver
type InitArgs struct {
User string
Gateway string
Ip string
WorkDir string
Privileged bool
Env []string
Args []string
Mtu int
Driver string
Console string
Pipe int
Root string
CapAdd string
CapDrop string
}
type StartCallback func(*Command)

// Driver specific information based on
// processes registered with the driver
Expand Down
33 changes: 1 addition & 32 deletions daemon/execdriver/lxc/driver.go
Expand Up @@ -5,12 +5,10 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
Expand All @@ -27,34 +25,6 @@ import (

const DriverName = "lxc"

func init() {
execdriver.RegisterInitFunc(DriverName, func(args *execdriver.InitArgs) error {
runtime.LockOSThread()
if err := setupEnv(args); err != nil {
return err
}
if err := setupHostname(args); err != nil {
return err
}
if err := setupNetworking(args); err != nil {
return err
}
if err := finalizeNamespace(args); err != nil {
return err
}

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 {
return fmt.Errorf("dockerinit unable to execute %s - %s", path, err)
}
panic("Unreachable")
})
}

type driver struct {
root string // root path for the driver to use
initPath string
Expand All @@ -67,6 +37,7 @@ func NewDriver(root, initPath string, apparmor bool) (*driver, error) {
if err := linkLxcStart(root); err != nil {
return nil, err
}

return &driver{
apparmor: apparmor,
root: root,
Expand Down Expand Up @@ -108,8 +79,6 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
"-f", configPath,
"--",
c.InitPath,
"-driver",
DriverName,
}

if c.Network.Interface != nil {
Expand Down
109 changes: 103 additions & 6 deletions daemon/execdriver/lxc/init.go
Expand Up @@ -2,19 +2,116 @@ package lxc

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"runtime"
"strings"
"syscall"

"github.com/docker/docker/daemon/execdriver"
"github.com/docker/docker/reexec"
"github.com/docker/libcontainer/netlink"
)

// Args provided to the init function for a driver
type InitArgs struct {
User string
Gateway string
Ip string
WorkDir string
Privileged bool
Env []string
Args []string
Mtu int
Console string
Pipe int
Root string
CapAdd string
CapDrop string
}

func init() {
// like always lxc requires a hack to get this to work
reexec.Register("/.dockerinit", dockerInititalizer)
}

func dockerInititalizer() {
initializer()
}

// initializer is the lxc driver's init function that is run inside the namespace to setup
// additional configurations
func initializer() {
runtime.LockOSThread()

args := getArgs()

if err := setupNamespace(args); err != nil {
log.Fatal(err)
}
}

func setupNamespace(args *InitArgs) error {
if err := setupEnv(args); err != nil {
return err
}
if err := setupHostname(args); err != nil {
return err
}
if err := setupNetworking(args); err != nil {
return err
}
if err := finalizeNamespace(args); err != nil {
return err
}

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 {
return fmt.Errorf("dockerinit unable to execute %s - %s", path, err)
}

return nil
}

func getArgs() *InitArgs {
var (
// Get cmdline arguments
user = flag.String("u", "", "username or uid")
gateway = flag.String("g", "", "gateway address")
ip = flag.String("i", "", "ip address")
workDir = flag.String("w", "", "workdir")
privileged = flag.Bool("privileged", false, "privileged mode")
mtu = flag.Int("mtu", 1500, "interface mtu")
capAdd = flag.String("cap-add", "", "capabilities to add")
capDrop = flag.String("cap-drop", "", "capabilities to drop")
)

flag.Parse()

return &InitArgs{
User: *user,
Gateway: *gateway,
Ip: *ip,
WorkDir: *workDir,
Privileged: *privileged,
Args: flag.Args(),
Mtu: *mtu,
CapAdd: *capAdd,
CapDrop: *capDrop,
}
}

// Clear environment pollution introduced by lxc-start
func setupEnv(args *execdriver.InitArgs) error {
func setupEnv(args *InitArgs) error {
// Get env
var env []string
content, err := ioutil.ReadFile(".dockerenv")
Expand All @@ -41,7 +138,7 @@ func setupEnv(args *execdriver.InitArgs) error {
return nil
}

func setupHostname(args *execdriver.InitArgs) error {
func setupHostname(args *InitArgs) error {
hostname := getEnv(args, "HOSTNAME")
if hostname == "" {
return nil
Expand All @@ -50,7 +147,7 @@ func setupHostname(args *execdriver.InitArgs) error {
}

// Setup networking
func setupNetworking(args *execdriver.InitArgs) error {
func setupNetworking(args *InitArgs) error {
if args.Ip != "" {
// eth0
iface, err := net.InterfaceByName("eth0")
Expand Down Expand Up @@ -95,7 +192,7 @@ func setupNetworking(args *execdriver.InitArgs) error {
}

// Setup working directory
func setupWorkingDirectory(args *execdriver.InitArgs) error {
func setupWorkingDirectory(args *InitArgs) error {
if args.WorkDir == "" {
return nil
}
Expand All @@ -105,7 +202,7 @@ func setupWorkingDirectory(args *execdriver.InitArgs) error {
return nil
}

func getEnv(args *execdriver.InitArgs, key string) string {
func getEnv(args *InitArgs, key string) string {
for _, kv := range args.Env {
parts := strings.SplitN(kv, "=", 2)
if parts[0] == key && len(parts) == 2 {
Expand Down
2 changes: 1 addition & 1 deletion daemon/execdriver/lxc/lxc_init_linux.go
Expand Up @@ -17,7 +17,7 @@ func setHostname(hostname string) error {
return syscall.Sethostname([]byte(hostname))
}

func finalizeNamespace(args *execdriver.InitArgs) error {
func finalizeNamespace(args *InitArgs) error {
if err := utils.CloseExecFrom(3); err != nil {
return err
}
Expand Down
39 changes: 1 addition & 38 deletions daemon/execdriver/native/driver.go
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/docker/libcontainer/cgroups/systemd"
consolepkg "github.com/docker/libcontainer/console"
"github.com/docker/libcontainer/namespaces"
"github.com/docker/libcontainer/syncpipe"
"github.com/docker/libcontainer/system"
)

Expand All @@ -31,38 +30,6 @@ const (
Version = "0.2"
)

func init() {
execdriver.RegisterInitFunc(DriverName, func(args *execdriver.InitArgs) error {
var container *libcontainer.Config
f, err := os.Open(filepath.Join(args.Root, "container.json"))
if err != nil {
return err
}

if err := json.NewDecoder(f).Decode(&container); err != nil {
f.Close()
return err
}
f.Close()

rootfs, err := os.Getwd()
if err != nil {
return err
}

syncPipe, err := syncpipe.NewSyncPipeFromFd(0, uintptr(args.Pipe))
if err != nil {
return err
}

if err := namespaces.Init(container, rootfs, args.Console, syncPipe, args.Args); err != nil {
return err
}

return nil
})
}

type activeContainer struct {
container *libcontainer.Config
cmd *exec.Cmd
Expand Down Expand Up @@ -133,13 +100,9 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
}

return namespaces.Exec(container, c.Stdin, c.Stdout, c.Stderr, c.Console, c.Rootfs, dataPath, args, func(container *libcontainer.Config, console, rootfs, dataPath, init string, child *os.File, args []string) *exec.Cmd {
// we need to join the rootfs because namespaces will setup the rootfs and chroot
initPath := filepath.Join(c.Rootfs, c.InitPath)

c.Path = d.initPath
c.Args = append([]string{
initPath,
"-driver", DriverName,
DriverName,
"-console", console,
"-pipe", "3",
"-root", filepath.Join(d.root, c.ID),
Expand Down

0 comments on commit 7321067

Please sign in to comment.