mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									8224e13bd2
								
							
						
					
					
						commit
						f7c7f7978c
					
				
					 3 changed files with 22 additions and 9 deletions
				
			
		| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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,
 | 
			
		||||
| 
						 | 
				
			
			@ -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(),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 == "" {
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue