提交 b7012aef 编写于 作者: M Matt Rickard

Use mac instead of hostname to find IP

上级 9dd3d9bc
......@@ -18,7 +18,9 @@ package kvm
import (
"bytes"
"crypto/rand"
"fmt"
"net"
"text/template"
libvirt "github.com/libvirt/libvirt-go"
......@@ -54,10 +56,12 @@ const domainTmpl = `
</disk>
<interface type='network'>
<source network='{{.Network}}'/>
<mac address='{{.MAC}}'/>
<model type='virtio'/>
</interface>
<interface type='network'>
<source network='{{.PrivateNetwork}}'/>
<mac address='{{.MAC}}'/>
<model type='virtio'/>
</interface>
<serial type='pty'>
......@@ -99,6 +103,25 @@ $ newgrp libvirt
Visit https://github.com/kubernetes/minikube/blob/master/docs/drivers.md#kvm-driver for more information.
`
func randomMAC() (net.HardwareAddr, error) {
buf := make([]byte, 6)
_, err := rand.Read(buf)
if err != nil {
return nil, err
}
// We unset the first and second least significant bits (LSB) of the MAC
//
// The LSB of the first octet
// 0 for unicast
// 1 for multicast
//
// The second LSB of the first octet
// 0 for universally administered addresses
// 1 for locally administered addresses
buf[0] = buf[0] & 0xfc
return buf, nil
}
func (d *Driver) getDomain() (*libvirt.Domain, *libvirt.Connect, error) {
conn, err := getConnection()
if err != nil {
......
......@@ -66,8 +66,14 @@ type Driver struct {
// The location of the iso to boot from
ISO string
// The randomly generated MAC Address
// If empty, a random MAC will be generated.
MAC string
}
const defaultNetworkName = "minikube-net"
func NewDriver(hostName, storePath string) *Driver {
return &Driver{
BaseDriver: &drivers.BaseDriver{
......
......@@ -42,9 +42,14 @@ const networkTmpl = `
</network>
`
const defaultNetworkName = "minikube-net"
func (d *Driver) createNetwork() error {
if d.MAC == "" {
mac, err := randomMAC()
if err != nil {
return errors.Wrap(err, "generating mac address")
}
d.MAC = mac.String()
}
conn, err := getConnection()
if err != nil {
return errors.Wrap(err, "getting libvirt connection")
......@@ -154,7 +159,7 @@ func (d *Driver) lookupIPFromLeasesFile() (string, error) {
if len(entry) != 5 {
return "", fmt.Errorf("Malformed leases entry: %s", entry)
}
if entry[3] == d.MachineName {
if entry[1] == d.MAC {
ipAddress = entry[2]
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册