Exemplo n.º 1
0
func getDynamicIP(t Netlink, link netlink.Link, endpoint *NetworkEndpoint) (client.Client, error) {
	var ack *dhcp.Packet
	var err error

	// use dhcp to acquire address
	dc, err := client.NewClient(link.Attrs().Index, link.Attrs().HardwareAddr)
	if err != nil {
		return nil, err
	}

	params := []byte{byte(dhcp4.OptionSubnetMask)}
	if ip.IsUnspecifiedIP(endpoint.Network.Gateway.IP) {
		params = append(params, byte(dhcp4.OptionRouter))
	}
	if len(endpoint.Network.Nameservers) == 0 {
		params = append(params, byte(dhcp4.OptionDomainNameServer))
	}

	dc.SetParameterRequestList(params...)

	err = dc.Request()
	if err != nil {
		log.Errorf("error sending dhcp request: %s", err)
		return nil, err
	}

	ack = dc.LastAck()
	if ack.YourIP() == nil || ack.SubnetMask() == nil {
		err = fmt.Errorf("dhcp assigned nil ip or subnet mask")
		log.Error(err)
		return nil, err
	}

	log.Infof("DHCP response: IP=%s, SubnetMask=%s, Gateway=%s, DNS=%s, Lease Time=%s", ack.YourIP(), ack.SubnetMask(), ack.Gateway(), ack.DNS(), ack.LeaseTime())
	defer func() {
		if err != nil && ack != nil {
			dc.Release()
		}
	}()

	return dc, nil
}
Exemplo n.º 2
0
func (t *BaseOperations) Setup(config Config) error {
	c, err := client.NewClient()
	if err != nil {
		return err
	}

	h := etcconf.NewHosts(hostsFile)
	if err = h.Load(); err != nil {
		return err
	}

	// make sure localhost entries are present
	entries := []struct {
		hostname string
		addr     net.IP
	}{
		{"localhost", net.ParseIP("127.0.0.1")},
		{"ip6-localhost", net.ParseIP("::1")},
		{"ip6-loopback", net.ParseIP("::1")},
		{"ip6-localnet", net.ParseIP("fe00::0")},
		{"ip6-mcastprefix", net.ParseIP("ff00::0")},
		{"ip6-allnodes", net.ParseIP("ff02::1")},
		{"ip6-allrouters", net.ParseIP("ff02::2")},
	}

	for _, e := range entries {
		h.SetHost(e.hostname, e.addr)
	}

	if err = h.Save(); err != nil {
		return err
	}

	// start with empty resolv.conf
	os.Remove(resolvFile)

	rc := etcconf.NewResolvConf(resolvFile)

	t.dynEndpoints = make(map[string][]*NetworkEndpoint)
	t.dhcpLoops = make(map[string]chan bool)
	t.dhcpClient = c
	t.hosts = h
	t.resolvConf = rc
	t.config = config

	// support the df command (#1642)
	if err = os.Symlink("/proc/mounts", "/etc/mtab"); err != nil {
		return err
	}

	mounted, err := mount.Mounted(runMountPoint)
	if err != nil {
		return err
	}
	if mounted {
		// unmount /run - https://github.com/vmware/vic/issues/1643
		if err := syscall.Unmount(runMountPoint, syscall.MNT_DETACH); err != nil {
			return fmt.Errorf("unmount %s failed with %q", runMountPoint, err)
		}
	}

	return nil
}