Skip to content

Commit

Permalink
dockerinit: set hostname
Browse files Browse the repository at this point in the history
Set the hostname in dockerinit instead of with lxc utils.  libvirt-lxc
doesn't have a way to do this, so do it in a common place.
  • Loading branch information
jpoimboe committed Dec 13, 2013
1 parent 8224e13 commit f7c7f79
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
7 changes: 0 additions & 7 deletions lxc_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
)

const LxcTemplate = `
# hostname
{{if .Config.Hostname}}
lxc.utsname = {{.Config.Hostname}}
{{else}}
lxc.utsname = {{.Id}}
{{end}}
{{if .Config.NetworkDisabled}}
# network is disabled (-n=false)
lxc.network.type = empty
Expand Down
2 changes: 0 additions & 2 deletions lxc_template_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestLXCConfig(t *testing.T) {
container := &Container{
root: root,
Config: &Config{
Hostname: "foobar",
Memory: int64(mem),
CpuShares: int64(cpu),
NetworkDisabled: true,
Expand All @@ -41,7 +40,6 @@ func TestLXCConfig(t *testing.T) {
if err := container.generateLXCConfig(); err != nil {
t.Fatal(err)
}
grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
grepFile(t, container.lxcConfigPath(),
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
grepFile(t, container.lxcConfigPath(),
Expand Down
22 changes: 22 additions & 0 deletions sysinit/sysinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ type DockerInitArgs struct {
args []string
}

func setupHostname(args *DockerInitArgs) error {
hostname := getEnv(args, "HOSTNAME")
if hostname == "" {
return nil
}
return syscall.Sethostname([]byte(hostname))
}

// Setup networking
func setupNetworking(args *DockerInitArgs) error {
if args.gateway == "" {
Expand Down Expand Up @@ -132,9 +140,23 @@ func setupEnv(args *DockerInitArgs) {
}
}

func getEnv(args *DockerInitArgs, key string) string {
for _, kv := range args.env {
parts := strings.SplitN(kv, "=", 2)
if parts[0] == key && len(parts) == 2 {
return parts[1]
}
}
return ""
}

func executeProgram(args *DockerInitArgs) error {
setupEnv(args)

if err := setupHostname(args); err != nil {
return err
}

if err := setupNetworking(args); err != nil {
return err
}
Expand Down

0 comments on commit f7c7f79

Please sign in to comment.