From 5cecd548cd48cec8967f7ad0b0b42b30fa3ec7a0 Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Wed, 20 Feb 2013 17:47:09 -0800 Subject: [PATCH] Basic networking support with hardcoded addresses. Work in progress. --- container.go | 13 +++++++++++++ lxc_template.go | 14 +++++++------- network.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 network.go diff --git a/container.go b/container.go index 57ec531737..50ac5e715e 100644 --- a/container.go +++ b/container.go @@ -33,6 +33,7 @@ type Container struct { Config *Config Filesystem *Filesystem + Network *NetworkInterface State *State SysInitPath string @@ -87,6 +88,10 @@ func createContainer(id string, root string, command string, args []string, laye if err := container.Filesystem.createMountPoints(); err != nil { return nil, err } + var err error + if container.Network, err = allocateNetwork(); err != nil { + return nil, err + } if err := container.save(); err != nil { return nil, err } @@ -272,11 +277,19 @@ func (container *Container) Start() error { "--", "/sbin/init", } + + // Networking + params = append(params, "-g", container.Network.Gateway.String()) + + // User if container.Config.User != "" { params = append(params, "-u", container.Config.User) } + + // Program params = append(params, "--", container.Path) params = append(params, container.Args...) + container.cmd = exec.Command("/usr/bin/lxc-start", params...) var err error diff --git a/lxc_template.go b/lxc_template.go index 4ac72da273..48e6dc732b 100755 --- a/lxc_template.go +++ b/lxc_template.go @@ -14,12 +14,12 @@ lxc.utsname = {{.Id}} #lxc.aa_profile = unconfined # 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 {{$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) -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 {{if .Config.Ram}} diff --git a/network.go b/network.go new file mode 100644 index 0000000000..234086c64c --- /dev/null +++ b/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 +}