1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Implement init veth creation

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
Michael Crosby 2014-02-19 15:33:44 -08:00
parent 7bc3c01250
commit 34671f2010
6 changed files with 63 additions and 67 deletions

View file

@ -22,5 +22,4 @@ type Network struct {
Gateway string `json:"gateway,omitempty"` Gateway string `json:"gateway,omitempty"`
Bridge string `json:"bridge,omitempty"` Bridge string `json:"bridge,omitempty"`
Mtu int `json:"mtu,omitempty"` Mtu int `json:"mtu,omitempty"`
TempVethName string `json:"temp_veth,omitempty"`
} }

View file

@ -1,6 +1,6 @@
{ {
"id": "koye", "id": "koye",
"namespace_pid": 3117, "log_file": "/root/logs",
"command": { "command": {
"args": [ "args": [
"/bin/bash" "/bin/bash"
@ -12,12 +12,12 @@
"TERM=xterm" "TERM=xterm"
] ]
}, },
"rootfs": "/var/lib/docker/containers/ee76122136d691d63e09d24168a91ddb2ef9fdcf210b4de5c50aa76354892f4b/root",
"namespaces": [ "namespaces": [
"NEWIPC", "NEWIPC",
"NEWNS", "NEWNS",
"NEWPID", "NEWPID",
"NEWUTS" "NEWUTS",
"NEWNET"
], ],
"capabilities": [ "capabilities": [
"SETPCAP", "SETPCAP",
@ -34,5 +34,11 @@
"AUDIT_CONTROL", "AUDIT_CONTROL",
"MAC_OVERRIDE", "MAC_OVERRIDE",
"MAC_ADMIN" "MAC_ADMIN"
] ],
"network": {
"ip": "172.17.0.100/16",
"gateway": "172.17.42.1",
"bridge": "docker0",
"mtu": 1500
}
} }

View file

@ -3,18 +3,16 @@ package network
import ( import (
"fmt" "fmt"
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"
"os"
"syscall"
) )
// SetupVeth sets up an existing network namespace with the specified // SetupVeth sets up an existing network namespace with the specified
// network configuration. // network configuration.
func SetupVeth(config *libcontainer.Network) error { func SetupVeth(config *libcontainer.Network, tempVethName string) error {
if err := InterfaceDown(config.TempVethName); err != nil { if err := InterfaceDown(tempVethName); err != nil {
return fmt.Errorf("interface down %s %s", config.TempVethName, err) return fmt.Errorf("interface down %s %s", tempVethName, err)
} }
if err := ChangeInterfaceName(config.TempVethName, "eth0"); err != nil { if err := ChangeInterfaceName(tempVethName, "eth0"); err != nil {
return fmt.Errorf("change %s to eth0 %s", config.TempVethName, err) return fmt.Errorf("change %s to eth0 %s", tempVethName, err)
} }
if err := SetInterfaceIp("eth0", config.IP); err != nil { if err := SetInterfaceIp("eth0", config.IP); err != nil {
return fmt.Errorf("set eth0 ip %s", err) return fmt.Errorf("set eth0 ip %s", err)
@ -41,29 +39,3 @@ func SetupVeth(config *libcontainer.Network) error {
} }
return nil return nil
} }
// SetupNamespaceMountDir prepares a new root for use as a mount
// source for bind mounting namespace fd to an outside path
func SetupNamespaceMountDir(root string) error {
if err := os.MkdirAll(root, 0666); err != nil {
return err
}
// make sure mounts are not unmounted by other mnt namespaces
if err := syscall.Mount("", root, "none", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil && err != syscall.EINVAL {
return err
}
if err := syscall.Mount(root, root, "none", syscall.MS_BIND, ""); err != nil {
return err
}
return nil
}
// DeleteNetworkNamespace unmounts the binding path and removes the
// file so that no references to the fd are present and the network
// namespace is automatically cleaned up
func DeleteNetworkNamespace(bindingPath string) error {
if err := syscall.Unmount(bindingPath, 0); err != nil {
return err
}
return os.Remove(bindingPath)
}

View file

@ -1,7 +1,9 @@
package main package main
import ( import (
"fmt"
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"
"github.com/dotcloud/docker/pkg/libcontainer/network"
"github.com/dotcloud/docker/pkg/system" "github.com/dotcloud/docker/pkg/system"
"github.com/dotcloud/docker/pkg/term" "github.com/dotcloud/docker/pkg/term"
"io" "io"
@ -25,11 +27,34 @@ func execCommand(container *libcontainer.Container) (pid int, err error) {
Cloneflags: flag, Cloneflags: flag,
} }
inPipe, err := command.StdinPipe()
if err != nil {
return -1, err
}
if err := command.Start(); err != nil { if err := command.Start(); err != nil {
return -1, err return -1, err
} }
pid = command.Process.Pid pid = command.Process.Pid
if container.Network != nil {
name1, name2, err := createVethPair()
if err != nil {
log.Fatal(err)
}
if err := network.SetInterfaceMaster(name1, container.Network.Bridge); err != nil {
log.Fatal(err)
}
if err := network.InterfaceUp(name1); err != nil {
log.Fatal(err)
}
if err := network.SetInterfaceInNamespacePid(name2, pid); err != nil {
log.Fatal(err)
}
fmt.Fprint(inPipe, name2)
inPipe.Close()
}
go func() { go func() {
if _, err := io.Copy(os.Stdout, master); err != nil { if _, err := io.Copy(os.Stdout, master); err != nil {
log.Println(err) log.Println(err)
@ -78,3 +103,11 @@ func createMasterAndConsole() (*os.File, string, error) {
} }
return master, console, nil return master, console, nil
} }
func createVethPair() (name1 string, name2 string, err error) {
name1, name2 = "veth001", "veth002"
if err = network.CreateVethPair(name1, name2); err != nil {
return
}
return
}

View file

@ -5,7 +5,9 @@ import (
"fmt" "fmt"
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"
"github.com/dotcloud/docker/pkg/libcontainer/capabilities" "github.com/dotcloud/docker/pkg/libcontainer/capabilities"
"github.com/dotcloud/docker/pkg/libcontainer/network"
"github.com/dotcloud/docker/pkg/system" "github.com/dotcloud/docker/pkg/system"
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -50,6 +52,12 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error reading from stdin %s", err)
}
tempVethName := string(data)
// close pipes so that we can replace it with the pty // close pipes so that we can replace it with the pty
os.Stdin.Close() os.Stdin.Close()
os.Stdout.Close() os.Stdout.Close()
@ -81,7 +89,7 @@ func main() {
} }
if container.Network != nil { if container.Network != nil {
if err := setupNetworking(container); err != nil { if err := setupNetworking(container, tempVethName); err != nil {
log.Fatalf("setup networking %s", err) log.Fatalf("setup networking %s", err)
} }
} }
@ -166,6 +174,6 @@ func setLogFile(container *libcontainer.Container) error {
return nil return nil
} }
func setupNetworking(conatiner *libcontainer.Container) error { func setupNetworking(container *libcontainer.Container, tempVethName string) error {
return nil return network.SetupVeth(container.Network, tempVethName)
} }

View file

@ -1,22 +0,0 @@
{
"id": "koye",
"namespace_pid": 3745,
"command": {
"args": [
"/sbin/init"
],
"environment": [
"HOME=/",
"PATH=PATH=$PATH:/bin:/usr/bin:/sbin:/usr/sbin",
"container=docker",
"TERM=xterm"
]
},
"rootfs": "/var/lib/docker/btrfs/subvolumes/7c0f15df1ad2e2fe04d7a6e079aec17406e9465a6a37dd16cb0dd754fc0167b3",
"namespaces": [
"NEWIPC",
"NEWNS",
"NEWPID",
"NEWUTS"
]
}