瀏覽代碼

Refactor large funcs
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby 11 年之前
父節點
當前提交
5d62916c48

+ 2 - 3
pkg/libcontainer/container.go

@@ -1,14 +1,13 @@
 package libcontainer
 
 type Container struct {
-	ID           string       `json:"id,omitempty"`
-	Command      *Command     `json:"command,omitempty"`
+	Hostname     string       `json:"hostname,omitempty"`
 	ReadonlyFs   bool         `json:"readonly_fs,omitempty"`
 	User         string       `json:"user,omitempty"`
 	WorkingDir   string       `json:"working_dir,omitempty"`
+	Command      *Command     `json:"command,omitempty"`
 	Namespaces   Namespaces   `json:"namespaces,omitempty"`
 	Capabilities Capabilities `json:"capabilities,omitempty"`
-	LogFile      string       `json:"log_file,omitempty"`
 	Network      *Network     `json:"network,omitempty"`
 }
 

+ 1 - 2
pkg/libcontainer/container.json

@@ -1,6 +1,5 @@
 {
     "id": "koye",
-    "log_file": "/root/logs",
     "command": {
         "args": [
             "/bin/bash"
@@ -9,7 +8,7 @@
             "HOME=/",
             "PATH=PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin",
             "container=docker",
-            "TERM=xterm"
+            "TERM=xterm-256color"
         ]
     },
     "namespaces": [

+ 40 - 20
pkg/libcontainer/nsinit/exec.go

@@ -27,6 +27,8 @@ func execCommand(container *libcontainer.Container) (int, error) {
 		Cloneflags: uintptr(getNamespaceFlags(container.Namespaces) | syscall.CLONE_VFORK), // we need CLONE_VFORK so we can wait on the child
 	}
 
+	// create a pipe so that we can syncronize with the namespaced process and
+	// pass the veth name to the child
 	inPipe, err := command.StdinPipe()
 	if err != nil {
 		return -1, err
@@ -39,34 +41,17 @@ func execCommand(container *libcontainer.Container) (int, error) {
 	}
 
 	if container.Network != nil {
-		name1, name2, err := createVethPair()
+		vethPair, err := setupVeth(container.Network.Bridge, command.Process.Pid)
 		if err != nil {
 			return -1, err
 		}
-		if err := network.SetInterfaceMaster(name1, container.Network.Bridge); err != nil {
-			return -1, err
-		}
-		if err := network.InterfaceUp(name1); err != nil {
-			return -1, err
-		}
-		if err := network.SetInterfaceInNamespacePid(name2, command.Process.Pid); err != nil {
-			return -1, err
-		}
-		fmt.Fprint(inPipe, name2)
-		inPipe.Close()
+		sendVethName(vethPair, inPipe)
 	}
 
 	go io.Copy(os.Stdout, master)
 	go io.Copy(master, os.Stdin)
 
-	ws, err := term.GetWinsize(os.Stdin.Fd())
-	if err != nil {
-		return -1, err
-	}
-	if err := term.SetWinsize(master.Fd(), ws); err != nil {
-		return -1, err
-	}
-	state, err := term.SetRawTerminal(os.Stdin.Fd())
+	state, err := setupWindow(master)
 	if err != nil {
 		command.Process.Kill()
 		return -1, err
@@ -81,6 +66,41 @@ func execCommand(container *libcontainer.Container) (int, error) {
 	return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
 }
 
+func sendVethName(name string, pipe io.WriteCloser) {
+	// write the veth pair name to the child's stdin then close the
+	// pipe so that the child stops waiting
+	fmt.Fprint(pipe, name)
+	pipe.Close()
+}
+
+func setupVeth(bridge string, nspid int) (string, error) {
+	name1, name2, err := createVethPair()
+	if err != nil {
+		return "", err
+	}
+	if err := network.SetInterfaceMaster(name1, bridge); err != nil {
+		return "", err
+	}
+	if err := network.InterfaceUp(name1); err != nil {
+		return "", err
+	}
+	if err := network.SetInterfaceInNamespacePid(name2, nspid); err != nil {
+		return "", err
+	}
+	return name2, nil
+}
+
+func setupWindow(master *os.File) (*term.State, error) {
+	ws, err := term.GetWinsize(os.Stdin.Fd())
+	if err != nil {
+		return nil, err
+	}
+	if err := term.SetWinsize(master.Fd(), ws); err != nil {
+		return nil, err
+	}
+	return term.SetRawTerminal(os.Stdin.Fd())
+}
+
 func createMasterAndConsole() (*os.File, string, error) {
 	master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
 	if err != nil {

+ 40 - 47
pkg/libcontainer/nsinit/init.go

@@ -9,17 +9,12 @@ import (
 	"github.com/dotcloud/docker/pkg/libcontainer/network"
 	"github.com/dotcloud/docker/pkg/system"
 	"io/ioutil"
-	"log"
 	"os"
 	"path/filepath"
 	"syscall"
 )
 
 func initCommand(container *libcontainer.Container, console string) error {
-	if err := setLogFile(container); err != nil {
-		return err
-	}
-
 	rootfs, err := resolveRootfs()
 	if err != nil {
 		return err
@@ -27,11 +22,10 @@ func initCommand(container *libcontainer.Container, console string) error {
 
 	var tempVethName string
 	if container.Network != nil {
-		data, err := ioutil.ReadAll(os.Stdin)
+		tempVethName, err = getVethName()
 		if err != nil {
-			return fmt.Errorf("error reading from stdin %s", err)
+			return err
 		}
-		tempVethName = string(data)
 	}
 
 	// close pipes so that we can replace it with the pty
@@ -61,13 +55,10 @@ func initCommand(container *libcontainer.Container, console string) error {
 	if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
 		return fmt.Errorf("setup mount namespace %s", err)
 	}
-	if container.Network != nil {
-		if err := setupNetworking(container.Network, tempVethName); err != nil {
-			return fmt.Errorf("setup networking %s", err)
-		}
+	if err := setupNetworking(container.Network, tempVethName); err != nil {
+		return fmt.Errorf("setup networking %s", err)
 	}
-
-	if err := system.Sethostname(container.ID); err != nil {
+	if err := system.Sethostname(container.Hostname); err != nil {
 		return fmt.Errorf("sethostname %s", err)
 	}
 	if err := capabilities.DropCapabilities(container); err != nil {
@@ -136,43 +127,45 @@ func openTerminal(name string, flag int) (*os.File, error) {
 	return os.NewFile(uintptr(r), name), nil
 }
 
-func setLogFile(container *libcontainer.Container) error {
-	if container.LogFile != "" {
-		f, err := os.OpenFile(container.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0655)
-		if err != nil {
-			return err
+func setupNetworking(config *libcontainer.Network, tempVethName string) error {
+	if config != nil {
+		if err := network.InterfaceDown(tempVethName); err != nil {
+			return fmt.Errorf("interface down %s %s", tempVethName, err)
+		}
+		if err := network.ChangeInterfaceName(tempVethName, "eth0"); err != nil {
+			return fmt.Errorf("change %s to eth0 %s", tempVethName, err)
+		}
+		if err := network.SetInterfaceIp("eth0", config.IP); err != nil {
+			return fmt.Errorf("set eth0 ip %s", err)
+		}
+		if err := network.SetMtu("eth0", config.Mtu); err != nil {
+			return fmt.Errorf("set eth0 mtu to %d %s", config.Mtu, err)
+		}
+		if err := network.InterfaceUp("eth0"); err != nil {
+			return fmt.Errorf("eth0 up %s", err)
+		}
+		if err := network.SetMtu("lo", config.Mtu); err != nil {
+			return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
+		}
+		if err := network.InterfaceUp("lo"); err != nil {
+			return fmt.Errorf("lo up %s", err)
+		}
+		if config.Gateway != "" {
+			if err := network.SetDefaultGateway(config.Gateway); err != nil {
+				return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
+			}
 		}
-		log.SetOutput(f)
 	}
 	return nil
 }
 
-func setupNetworking(config *libcontainer.Network, tempVethName string) error {
-	if err := network.InterfaceDown(tempVethName); err != nil {
-		return fmt.Errorf("interface down %s %s", tempVethName, err)
-	}
-	if err := network.ChangeInterfaceName(tempVethName, "eth0"); err != nil {
-		return fmt.Errorf("change %s to eth0 %s", tempVethName, err)
-	}
-	if err := network.SetInterfaceIp("eth0", config.IP); err != nil {
-		return fmt.Errorf("set eth0 ip %s", err)
-	}
-	if err := network.SetMtu("eth0", config.Mtu); err != nil {
-		return fmt.Errorf("set eth0 mtu to %d %s", config.Mtu, err)
-	}
-	if err := network.InterfaceUp("eth0"); err != nil {
-		return fmt.Errorf("eth0 up %s", err)
-	}
-	if err := network.SetMtu("lo", config.Mtu); err != nil {
-		return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
-	}
-	if err := network.InterfaceUp("lo"); err != nil {
-		return fmt.Errorf("lo up %s", err)
-	}
-	if config.Gateway != "" {
-		if err := network.SetDefaultGateway(config.Gateway); err != nil {
-			return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
-		}
+// getVethName reads from Stdin the temp veth name
+// sent by the parent processes after the veth pair
+// has been created and setup
+func getVethName() (string, error) {
+	data, err := ioutil.ReadAll(os.Stdin)
+	if err != nil {
+		return "", fmt.Errorf("error reading from stdin %s", err)
 	}
-	return nil
+	return string(data), nil
 }

+ 24 - 24
pkg/libcontainer/types.go

@@ -1,29 +1,5 @@
 package libcontainer
 
-type Namespace string
-type Namespaces []Namespace
-
-func (n Namespaces) Contains(ns Namespace) bool {
-	for _, nns := range n {
-		if nns == ns {
-			return true
-		}
-	}
-	return false
-}
-
-type Capability string
-type Capabilities []Capability
-
-func (c Capabilities) Contains(capp Capability) bool {
-	for _, cc := range c {
-		if cc == capp {
-			return true
-		}
-	}
-	return false
-}
-
 const (
 	CAP_SETPCAP        Capability = "SETPCAP"
 	CAP_SYS_MODULE     Capability = "SYS_MODULE"
@@ -47,3 +23,27 @@ const (
 	CLONE_NEWPID  Namespace = "NEWPID"  // pid
 	CLONE_NEWNET  Namespace = "NEWNET"  // network
 )
+
+type Namespace string
+type Namespaces []Namespace
+
+func (n Namespaces) Contains(ns Namespace) bool {
+	for _, nns := range n {
+		if nns == ns {
+			return true
+		}
+	}
+	return false
+}
+
+type Capability string
+type Capabilities []Capability
+
+func (c Capabilities) Contains(capp Capability) bool {
+	for _, cc := range c {
+		if cc == capp {
+			return true
+		}
+	}
+	return false
+}