From 1f9f5eed5d004395886b8e93cec9389332d8fded Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 10 Apr 2013 18:23:34 -0700 Subject: [PATCH 1/2] Put the resolv.conf path in a variable instead of being hardcoded within lxc --- container.go | 13 +++++++------ lxc_template.go | 2 +- runtime.go | 6 +++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/container.go b/container.go index f180c7559b..a930cbd3e3 100644 --- a/container.go +++ b/container.go @@ -33,13 +33,14 @@ type Container struct { network *NetworkInterface NetworkSettings *NetworkSettings - SysInitPath string - cmd *exec.Cmd - stdout *writeBroadcaster - stderr *writeBroadcaster - stdin io.ReadCloser - stdinPipe io.WriteCloser + SysInitPath string + ResolvConfPath string + cmd *exec.Cmd + stdout *writeBroadcaster + stderr *writeBroadcaster + stdin io.ReadCloser + stdinPipe io.WriteCloser ptyMaster io.Closer runtime *Runtime diff --git a/lxc_template.go b/lxc_template.go index c6849cb0df..5ac62f52af 100644 --- a/lxc_template.go +++ b/lxc_template.go @@ -78,7 +78,7 @@ lxc.mount.entry = devpts {{$ROOTFS}}/dev/pts devpts newinstance,ptmxmode=0666,no lxc.mount.entry = {{.SysInitPath}} {{$ROOTFS}}/sbin/init none bind,ro 0 0 # In order to get a working DNS environment, mount bind (ro) the host's /etc/resolv.conf into the container -lxc.mount.entry = /etc/resolv.conf {{$ROOTFS}}/etc/resolv.conf none bind,ro 0 0 +lxc.mount.entry = {{.ResolvConfPath}} {{$ROOTFS}}/etc/resolv.conf none bind,ro 0 0 # drop linux capabilities (apply mainly to the user root in the container) diff --git a/runtime.go b/runtime.go index 24194f74ad..2dd419868f 100644 --- a/runtime.go +++ b/runtime.go @@ -82,6 +82,9 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) { if config.Hostname == "" { config.Hostname = id[:12] } + + resolvConfPath := "/etc/resolv.conf" + container := &Container{ // FIXME: we should generate the ID here instead of receiving it as an argument Id: id, @@ -92,7 +95,8 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) { Image: img.Id, // Always use the resolved image id NetworkSettings: &NetworkSettings{}, // FIXME: do we need to store this in the container? - SysInitPath: sysInitPath, + SysInitPath: sysInitPath, + ResolvConfPath: resolvConfPath, } container.root = runtime.containerRoot(container.Id) // Step 1: create the container directory. From 7673afc8438a9e46a1d11aff0f8c0f7eca169809 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 10 Apr 2013 19:02:23 -0700 Subject: [PATCH 2/2] Allow use to set his own dns via -dns --- container.go | 5 +++++ runtime.go | 23 +++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/container.go b/container.go index a930cbd3e3..848c2ae215 100644 --- a/container.go +++ b/container.go @@ -62,6 +62,7 @@ type Config struct { StdinOnce bool // If true, close stdin after the 1 attached client disconnects. Env []string Cmd []string + Dns []string Image string // Name of the image as it was passed by the operator (eg. could be symbolic) } @@ -86,6 +87,9 @@ func ParseRun(args []string, stdout io.Writer) (*Config, error) { var flEnv ListOpts cmd.Var(&flEnv, "e", "Set environment variables") + var flDns ListOpts + cmd.Var(&flDns, "dns", "Set custom dns servers") + if err := cmd.Parse(args); err != nil { return nil, err } @@ -123,6 +127,7 @@ func ParseRun(args []string, stdout io.Writer) (*Config, error) { AttachStderr: flAttach.Get("stderr"), Env: flEnv, Cmd: runCmd, + Dns: flDns, Image: image, } // When allocating stdin in attached mode, close stdin at client disconnect diff --git a/runtime.go b/runtime.go index 2dd419868f..e66b32f31a 100644 --- a/runtime.go +++ b/runtime.go @@ -83,8 +83,6 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) { config.Hostname = id[:12] } - resolvConfPath := "/etc/resolv.conf" - container := &Container{ // FIXME: we should generate the ID here instead of receiving it as an argument Id: id, @@ -95,8 +93,7 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) { Image: img.Id, // Always use the resolved image id NetworkSettings: &NetworkSettings{}, // FIXME: do we need to store this in the container? - SysInitPath: sysInitPath, - ResolvConfPath: resolvConfPath, + SysInitPath: sysInitPath, } container.root = runtime.containerRoot(container.Id) // Step 1: create the container directory. @@ -104,6 +101,24 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) { if err := os.Mkdir(container.root, 0700); err != nil { return nil, err } + + // If custom dns exists, then create a resolv.conf for the container + if len(config.Dns) > 0 { + container.ResolvConfPath = path.Join(container.root, "resolv.conf") + f, err := os.Create(container.ResolvConfPath) + if err != nil { + return nil, err + } + defer f.Close() + for _, dns := range config.Dns { + if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil { + return nil, err + } + } + } else { + container.ResolvConfPath = "/etc/resolv.conf" + } + // Step 2: save the container json if err := container.ToDisk(); err != nil { return nil, err