Add dynamic veth name

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:54:53 -08:00
parent 8430fbf11e
commit ab6864d0c0
4 changed files with 48 additions and 73 deletions

View file

@ -1,41 +0,0 @@
package network
import (
"fmt"
"github.com/dotcloud/docker/pkg/libcontainer"
)
// SetupVeth sets up an existing network namespace with the specified
// network configuration.
func SetupVeth(config *libcontainer.Network, tempVethName string) error {
if err := InterfaceDown(tempVethName); err != nil {
return fmt.Errorf("interface down %s %s", tempVethName, err)
}
if err := ChangeInterfaceName(tempVethName, "eth0"); err != nil {
return fmt.Errorf("change %s to eth0 %s", tempVethName, err)
}
if err := SetInterfaceIp("eth0", config.IP); err != nil {
return fmt.Errorf("set eth0 ip %s", err)
}
if err := SetMtu("eth0", config.Mtu); err != nil {
return fmt.Errorf("set eth0 mtu to %d %s", config.Mtu, err)
}
if err := InterfaceUp("eth0"); err != nil {
return fmt.Errorf("eth0 up %s", err)
}
if err := SetMtu("lo", config.Mtu); err != nil {
return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
}
if err := InterfaceUp("lo"); err != nil {
return fmt.Errorf("lo up %s", err)
}
if config.Gateway != "" {
if err := SetDefaultGateway(config.Gateway); err != nil {
return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
}
}
return nil
}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/dotcloud/docker/pkg/libcontainer"
"github.com/dotcloud/docker/pkg/libcontainer/network"
"github.com/dotcloud/docker/pkg/libcontainer/utils"
"github.com/dotcloud/docker/pkg/system"
"github.com/dotcloud/docker/pkg/term"
"io"
@ -105,7 +106,14 @@ func createMasterAndConsole() (*os.File, string, error) {
}
func createVethPair() (name1 string, name2 string, err error) {
name1, name2 = "veth001", "veth002"
name1, err = utils.GenerateRandomName("dock", 4)
if err != nil {
return
}
name2, err = utils.GenerateRandomName("dock", 4)
if err != nil {
return
}
if err = network.CreateVethPair(name1, name2); err != nil {
return
}

View file

@ -52,11 +52,14 @@ func main() {
log.Fatal(err)
}
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error reading from stdin %s", err)
var tempVethName string
if container.Network != nil {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("error reading from stdin %s", err)
}
tempVethName = string(data)
}
tempVethName := string(data)
// close pipes so that we can replace it with the pty
os.Stdin.Close()
@ -73,7 +76,6 @@ func main() {
if err := dupSlave(slave); err != nil {
log.Fatalf("dup2 slave %s", err)
}
if _, err := system.Setsid(); err != nil {
log.Fatalf("setsid %s", err)
}
@ -83,13 +85,11 @@ func main() {
if err := system.ParentDeathSignal(); err != nil {
log.Fatalf("parent deth signal %s", err)
}
if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
log.Fatalf("setup mount namespace %s", err)
}
if container.Network != nil {
if err := setupNetworking(container, tempVethName); err != nil {
if err := setupNetworking(container.Network, tempVethName); err != nil {
log.Fatalf("setup networking %s", err)
}
}
@ -174,6 +174,32 @@ func setLogFile(container *libcontainer.Container) error {
return nil
}
func setupNetworking(container *libcontainer.Container, tempVethName string) error {
return network.SetupVeth(container.Network, tempVethName)
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)
}
}
return nil
}

View file

@ -4,30 +4,12 @@ import (
"crypto/rand"
"encoding/hex"
"io"
"os"
"syscall"
)
func WaitOnPid(pid int) (exitcode int, err error) {
child, err := os.FindProcess(pid)
if err != nil {
return -1, err
}
state, err := child.Wait()
if err != nil {
return -1, err
}
return getExitCode(state), nil
}
func getExitCode(state *os.ProcessState) int {
return state.Sys().(syscall.WaitStatus).ExitStatus()
}
func GenerateRandomName(size int) (string, error) {
id := make([]byte, size)
func GenerateRandomName(prefix string, size int) (string, error) {
id := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, id); err != nil {
return "", err
}
return hex.EncodeToString(id), nil
return prefix + hex.EncodeToString(id)[:size], nil
}