Browse code

Add dynamic veth name Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)

Michael Crosby authored on 2014/02/20 08:54:53
Showing 4 changed files
1 1
deleted file mode 100644
... ...
@@ -1,41 +0,0 @@
1
-package network
2
-
3
-import (
4
-	"fmt"
5
-	"github.com/dotcloud/docker/pkg/libcontainer"
6
-)
7
-
8
-// SetupVeth sets up an existing network namespace with the specified
9
-// network configuration.
10
-func SetupVeth(config *libcontainer.Network, tempVethName string) error {
11
-	if err := InterfaceDown(tempVethName); err != nil {
12
-		return fmt.Errorf("interface down %s %s", tempVethName, err)
13
-	}
14
-	if err := ChangeInterfaceName(tempVethName, "eth0"); err != nil {
15
-		return fmt.Errorf("change %s to eth0 %s", tempVethName, err)
16
-	}
17
-	if err := SetInterfaceIp("eth0", config.IP); err != nil {
18
-		return fmt.Errorf("set eth0 ip %s", err)
19
-	}
20
-
21
-	if err := SetMtu("eth0", config.Mtu); err != nil {
22
-		return fmt.Errorf("set eth0 mtu to %d %s", config.Mtu, err)
23
-	}
24
-	if err := InterfaceUp("eth0"); err != nil {
25
-		return fmt.Errorf("eth0 up %s", err)
26
-	}
27
-
28
-	if err := SetMtu("lo", config.Mtu); err != nil {
29
-		return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
30
-	}
31
-	if err := InterfaceUp("lo"); err != nil {
32
-		return fmt.Errorf("lo up %s", err)
33
-	}
34
-
35
-	if config.Gateway != "" {
36
-		if err := SetDefaultGateway(config.Gateway); err != nil {
37
-			return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
38
-		}
39
-	}
40
-	return nil
41
-}
... ...
@@ -4,6 +4,7 @@ import (
4 4
 	"fmt"
5 5
 	"github.com/dotcloud/docker/pkg/libcontainer"
6 6
 	"github.com/dotcloud/docker/pkg/libcontainer/network"
7
+	"github.com/dotcloud/docker/pkg/libcontainer/utils"
7 8
 	"github.com/dotcloud/docker/pkg/system"
8 9
 	"github.com/dotcloud/docker/pkg/term"
9 10
 	"io"
... ...
@@ -105,7 +106,14 @@ func createMasterAndConsole() (*os.File, string, error) {
105 105
 }
106 106
 
107 107
 func createVethPair() (name1 string, name2 string, err error) {
108
-	name1, name2 = "veth001", "veth002"
108
+	name1, err = utils.GenerateRandomName("dock", 4)
109
+	if err != nil {
110
+		return
111
+	}
112
+	name2, err = utils.GenerateRandomName("dock", 4)
113
+	if err != nil {
114
+		return
115
+	}
109 116
 	if err = network.CreateVethPair(name1, name2); err != nil {
110 117
 		return
111 118
 	}
... ...
@@ -52,11 +52,14 @@ func main() {
52 52
 		log.Fatal(err)
53 53
 	}
54 54
 
55
-	data, err := ioutil.ReadAll(os.Stdin)
56
-	if err != nil {
57
-		log.Fatalf("error reading from stdin %s", err)
55
+	var tempVethName string
56
+	if container.Network != nil {
57
+		data, err := ioutil.ReadAll(os.Stdin)
58
+		if err != nil {
59
+			log.Fatalf("error reading from stdin %s", err)
60
+		}
61
+		tempVethName = string(data)
58 62
 	}
59
-	tempVethName := string(data)
60 63
 
61 64
 	// close pipes so that we can replace it with the pty
62 65
 	os.Stdin.Close()
... ...
@@ -73,7 +76,6 @@ func main() {
73 73
 	if err := dupSlave(slave); err != nil {
74 74
 		log.Fatalf("dup2 slave %s", err)
75 75
 	}
76
-
77 76
 	if _, err := system.Setsid(); err != nil {
78 77
 		log.Fatalf("setsid %s", err)
79 78
 	}
... ...
@@ -83,13 +85,11 @@ func main() {
83 83
 	if err := system.ParentDeathSignal(); err != nil {
84 84
 		log.Fatalf("parent deth signal %s", err)
85 85
 	}
86
-
87 86
 	if err := setupNewMountNamespace(rootfs, console, container.ReadonlyFs); err != nil {
88 87
 		log.Fatalf("setup mount namespace %s", err)
89 88
 	}
90
-
91 89
 	if container.Network != nil {
92
-		if err := setupNetworking(container, tempVethName); err != nil {
90
+		if err := setupNetworking(container.Network, tempVethName); err != nil {
93 91
 			log.Fatalf("setup networking %s", err)
94 92
 		}
95 93
 	}
... ...
@@ -174,6 +174,32 @@ func setLogFile(container *libcontainer.Container) error {
174 174
 	return nil
175 175
 }
176 176
 
177
-func setupNetworking(container *libcontainer.Container, tempVethName string) error {
178
-	return network.SetupVeth(container.Network, tempVethName)
177
+func setupNetworking(config *libcontainer.Network, tempVethName string) error {
178
+	if err := network.InterfaceDown(tempVethName); err != nil {
179
+		return fmt.Errorf("interface down %s %s", tempVethName, err)
180
+	}
181
+	if err := network.ChangeInterfaceName(tempVethName, "eth0"); err != nil {
182
+		return fmt.Errorf("change %s to eth0 %s", tempVethName, err)
183
+	}
184
+	if err := network.SetInterfaceIp("eth0", config.IP); err != nil {
185
+		return fmt.Errorf("set eth0 ip %s", err)
186
+	}
187
+	if err := network.SetMtu("eth0", config.Mtu); err != nil {
188
+		return fmt.Errorf("set eth0 mtu to %d %s", config.Mtu, err)
189
+	}
190
+	if err := network.InterfaceUp("eth0"); err != nil {
191
+		return fmt.Errorf("eth0 up %s", err)
192
+	}
193
+	if err := network.SetMtu("lo", config.Mtu); err != nil {
194
+		return fmt.Errorf("set lo mtu to %d %s", config.Mtu, err)
195
+	}
196
+	if err := network.InterfaceUp("lo"); err != nil {
197
+		return fmt.Errorf("lo up %s", err)
198
+	}
199
+	if config.Gateway != "" {
200
+		if err := network.SetDefaultGateway(config.Gateway); err != nil {
201
+			return fmt.Errorf("set gateway to %s %s", config.Gateway, err)
202
+		}
203
+	}
204
+	return nil
179 205
 }
... ...
@@ -4,30 +4,12 @@ import (
4 4
 	"crypto/rand"
5 5
 	"encoding/hex"
6 6
 	"io"
7
-	"os"
8
-	"syscall"
9 7
 )
10 8
 
11
-func WaitOnPid(pid int) (exitcode int, err error) {
12
-	child, err := os.FindProcess(pid)
13
-	if err != nil {
14
-		return -1, err
15
-	}
16
-	state, err := child.Wait()
17
-	if err != nil {
18
-		return -1, err
19
-	}
20
-	return getExitCode(state), nil
21
-}
22
-
23
-func getExitCode(state *os.ProcessState) int {
24
-	return state.Sys().(syscall.WaitStatus).ExitStatus()
25
-}
26
-
27
-func GenerateRandomName(size int) (string, error) {
28
-	id := make([]byte, size)
9
+func GenerateRandomName(prefix string, size int) (string, error) {
10
+	id := make([]byte, 32)
29 11
 	if _, err := io.ReadFull(rand.Reader, id); err != nil {
30 12
 		return "", err
31 13
 	}
32
-	return hex.EncodeToString(id), nil
14
+	return prefix + hex.EncodeToString(id)[:size], nil
33 15
 }