Browse Source

Basic networking support with hardcoded addresses. Work in progress.

Andrea Luzzardi 12 years ago
parent
commit
5cecd548cd
3 changed files with 49 additions and 7 deletions
  1. 13 0
      container.go
  2. 7 7
      lxc_template.go
  3. 29 0
      network.go

+ 13 - 0
container.go

@@ -33,6 +33,7 @@ type Container struct {
 
 
 	Config     *Config
 	Config     *Config
 	Filesystem *Filesystem
 	Filesystem *Filesystem
+	Network    *NetworkInterface
 	State      *State
 	State      *State
 
 
 	SysInitPath   string
 	SysInitPath   string
@@ -87,6 +88,10 @@ func createContainer(id string, root string, command string, args []string, laye
 	if err := container.Filesystem.createMountPoints(); err != nil {
 	if err := container.Filesystem.createMountPoints(); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
+	var err error
+	if container.Network, err = allocateNetwork(); err != nil {
+		return nil, err
+	}
 	if err := container.save(); err != nil {
 	if err := container.save(); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -272,11 +277,19 @@ func (container *Container) Start() error {
 		"--",
 		"--",
 		"/sbin/init",
 		"/sbin/init",
 	}
 	}
+
+	// Networking
+	params = append(params, "-g", container.Network.Gateway.String())
+
+	// User
 	if container.Config.User != "" {
 	if container.Config.User != "" {
 		params = append(params, "-u", container.Config.User)
 		params = append(params, "-u", container.Config.User)
 	}
 	}
+
+	// Program
 	params = append(params, "--", container.Path)
 	params = append(params, "--", container.Path)
 	params = append(params, container.Args...)
 	params = append(params, container.Args...)
+
 	container.cmd = exec.Command("/usr/bin/lxc-start", params...)
 	container.cmd = exec.Command("/usr/bin/lxc-start", params...)
 
 
 	var err error
 	var err error

+ 7 - 7
lxc_template.go

@@ -14,12 +14,12 @@ lxc.utsname = {{.Id}}
 #lxc.aa_profile = unconfined
 #lxc.aa_profile = unconfined
 
 
 # network configuration
 # network configuration
-#lxc.network.type = veth
-#lxc.network.flags = up
-#lxc.network.link = br0
-#lxc.network.name = eth0  # Internal container network interface name
-#lxc.network.mtu = 1500
-#lxc.network.ipv4 = {ip_address}/{ip_prefix_len}
+lxc.network.type = veth
+lxc.network.flags = up
+lxc.network.link = lxcbr0
+lxc.network.name = eth0
+lxc.network.mtu = 1500
+lxc.network.ipv4 = {{.Network.IpAddress}}/{{.Network.IpPrefixLen}}
 
 
 # root filesystem
 # root filesystem
 {{$ROOTFS := .Filesystem.RootFS}}
 {{$ROOTFS := .Filesystem.RootFS}}
@@ -82,7 +82,7 @@ lxc.mount.entry = /etc/resolv.conf {{$ROOTFS}}/etc/resolv.conf none bind,ro 0 0
 
 
 
 
 # drop linux capabilities (apply mainly to the user root in the container)
 # drop linux capabilities (apply mainly to the user root in the container)
-lxc.cap.drop = audit_control audit_write mac_admin mac_override mknod net_raw setfcap setpcap sys_admin sys_boot sys_module sys_nice sys_pacct sys_rawio sys_resource sys_time sys_tty_config
+#lxc.cap.drop = audit_control audit_write mac_admin mac_override mknod net_raw setfcap setpcap sys_admin sys_boot sys_module sys_nice sys_pacct sys_rawio sys_resource sys_time sys_tty_config
 
 
 # limits
 # limits
 {{if .Config.Ram}}
 {{if .Config.Ram}}

+ 29 - 0
network.go

@@ -0,0 +1,29 @@
+package docker
+
+import (
+	"net"
+)
+
+const (
+	networkGateway   = "10.0.3.1"
+	networkPrefixLen = 24
+)
+
+type NetworkInterface struct {
+	IpAddress   string
+	IpPrefixLen int
+	Gateway     net.IP
+}
+
+func allocateIPAddress() string {
+	return "10.0.3.2"
+}
+
+func allocateNetwork() (*NetworkInterface, error) {
+	iface := &NetworkInterface{
+		IpAddress:   allocateIPAddress(),
+		IpPrefixLen: networkPrefixLen,
+		Gateway:     net.ParseIP(networkGateway),
+	}
+	return iface, nil
+}