Merge pull request #3201 from jpoimboe/libvirt-prereq-network
Set hostname and IP address from dockerinit
This commit is contained in:
commit
f9328ad9cc
|
@ -574,7 +574,11 @@ func (container *Container) Start() (err error) {
|
|||
|
||||
// Networking
|
||||
if !container.Config.NetworkDisabled {
|
||||
params = append(params, "-g", container.network.Gateway.String())
|
||||
network := container.NetworkSettings
|
||||
params = append(params,
|
||||
"-g", network.Gateway,
|
||||
"-i", fmt.Sprintf("%s/%d", network.IPAddress, network.IPPrefixLen),
|
||||
)
|
||||
}
|
||||
|
||||
// User
|
||||
|
|
|
@ -6,24 +6,15 @@ 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
|
||||
{{else}}
|
||||
# network configuration
|
||||
lxc.network.type = veth
|
||||
lxc.network.flags = up
|
||||
lxc.network.link = {{.NetworkSettings.Bridge}}
|
||||
lxc.network.name = eth0
|
||||
lxc.network.mtu = 1500
|
||||
lxc.network.ipv4 = {{.NetworkSettings.IPAddress}}/{{.NetworkSettings.IPPrefixLen}}
|
||||
{{end}}
|
||||
|
||||
# root filesystem
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -20,26 +20,59 @@ import (
|
|||
type DockerInitArgs struct {
|
||||
user string
|
||||
gateway string
|
||||
ip string
|
||||
workDir string
|
||||
privileged bool
|
||||
env []string
|
||||
args []string
|
||||
}
|
||||
|
||||
// Setup networking
|
||||
func setupNetworking(args *DockerInitArgs) error {
|
||||
if args.gateway == "" {
|
||||
func setupHostname(args *DockerInitArgs) error {
|
||||
hostname := getEnv(args, "HOSTNAME")
|
||||
if hostname == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
ip := net.ParseIP(args.gateway)
|
||||
if ip == nil {
|
||||
return fmt.Errorf("Unable to set up networking, %s is not a valid IP", args.gateway)
|
||||
return syscall.Sethostname([]byte(hostname))
|
||||
}
|
||||
|
||||
if err := netlink.AddDefaultGw(ip); err != nil {
|
||||
// Setup networking
|
||||
func setupNetworking(args *DockerInitArgs) error {
|
||||
if args.ip != "" {
|
||||
// eth0
|
||||
iface, err := net.InterfaceByName("eth0")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to set up networking: %v", err)
|
||||
}
|
||||
ip, ipNet, err := net.ParseCIDR(args.ip)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to set up networking: %v", err)
|
||||
}
|
||||
if err := netlink.NetworkLinkAddIp(iface, ip, ipNet); err != nil {
|
||||
return fmt.Errorf("Unable to set up networking: %v", err)
|
||||
}
|
||||
if err := netlink.NetworkLinkUp(iface); err != nil {
|
||||
return fmt.Errorf("Unable to set up networking: %v", err)
|
||||
}
|
||||
|
||||
// loopback
|
||||
iface, err = net.InterfaceByName("lo")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to set up networking: %v", err)
|
||||
}
|
||||
if err := netlink.NetworkLinkUp(iface); err != nil {
|
||||
return fmt.Errorf("Unable to set up networking: %v", err)
|
||||
}
|
||||
}
|
||||
if args.gateway != "" {
|
||||
gw := net.ParseIP(args.gateway)
|
||||
if gw == nil {
|
||||
return fmt.Errorf("Unable to set up networking, %s is not a valid gateway IP", args.gateway)
|
||||
}
|
||||
|
||||
if err := netlink.AddDefaultGw(gw); err != nil {
|
||||
return fmt.Errorf("Unable to set up networking: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -132,9 +165,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
|
||||
}
|
||||
|
@ -177,6 +224,7 @@ func SysInit() {
|
|||
// Get cmdline arguments
|
||||
user := flag.String("u", "", "username or uid")
|
||||
gateway := flag.String("g", "", "gateway address")
|
||||
ip := flag.String("i", "", "ip address")
|
||||
workDir := flag.String("w", "", "workdir")
|
||||
privileged := flag.Bool("privileged", false, "privileged mode")
|
||||
flag.Parse()
|
||||
|
@ -197,6 +245,7 @@ func SysInit() {
|
|||
args := &DockerInitArgs{
|
||||
user: *user,
|
||||
gateway: *gateway,
|
||||
ip: *ip,
|
||||
workDir: *workDir,
|
||||
privileged: *privileged,
|
||||
env: env,
|
||||
|
|
Loading…
Reference in New Issue