Browse code

dockerinit: set hostname

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.

Josh Poimboeuf authored on 2013/10/30 03:31:00
Showing 3 changed files
... ...
@@ -6,13 +6,6 @@ import (
6 6
 )
7 7
 
8 8
 const LxcTemplate = `
9
-# hostname
10
-{{if .Config.Hostname}}
11
-lxc.utsname = {{.Config.Hostname}}
12
-{{else}}
13
-lxc.utsname = {{.Id}}
14
-{{end}}
15
-
16 9
 {{if .Config.NetworkDisabled}}
17 10
 # network is disabled (-n=false)
18 11
 lxc.network.type = empty
... ...
@@ -29,7 +29,6 @@ func TestLXCConfig(t *testing.T) {
29 29
 	container := &Container{
30 30
 		root: root,
31 31
 		Config: &Config{
32
-			Hostname:        "foobar",
33 32
 			Memory:          int64(mem),
34 33
 			CpuShares:       int64(cpu),
35 34
 			NetworkDisabled: true,
... ...
@@ -41,7 +40,6 @@ func TestLXCConfig(t *testing.T) {
41 41
 	if err := container.generateLXCConfig(); err != nil {
42 42
 		t.Fatal(err)
43 43
 	}
44
-	grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
45 44
 	grepFile(t, container.lxcConfigPath(),
46 45
 		fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
47 46
 	grepFile(t, container.lxcConfigPath(),
... ...
@@ -26,6 +26,14 @@ type DockerInitArgs struct {
26 26
 	args       []string
27 27
 }
28 28
 
29
+func setupHostname(args *DockerInitArgs) error {
30
+	hostname := getEnv(args, "HOSTNAME")
31
+	if hostname == "" {
32
+		return nil
33
+	}
34
+	return syscall.Sethostname([]byte(hostname))
35
+}
36
+
29 37
 // Setup networking
30 38
 func setupNetworking(args *DockerInitArgs) error {
31 39
 	if args.gateway == "" {
... ...
@@ -132,9 +140,23 @@ func setupEnv(args *DockerInitArgs) {
132 132
 	}
133 133
 }
134 134
 
135
+func getEnv(args *DockerInitArgs, key string) string {
136
+	for _, kv := range args.env {
137
+		parts := strings.SplitN(kv, "=", 2)
138
+		if parts[0] == key && len(parts) == 2 {
139
+			return parts[1]
140
+		}
141
+	}
142
+	return ""
143
+}
144
+
135 145
 func executeProgram(args *DockerInitArgs) error {
136 146
 	setupEnv(args)
137 147
 
148
+	if err := setupHostname(args); err != nil {
149
+		return err
150
+	}
151
+
138 152
 	if err := setupNetworking(args); err != nil {
139 153
 		return err
140 154
 	}