提交 b4936d02 编写于 作者: A Aaron Prindle

Fixed mount command to bind on specific VM ip address instead of all interfaces

上级 83f77bb4
......@@ -18,6 +18,7 @@ package cmd
import (
"fmt"
"net"
"os"
"sync"
......@@ -26,6 +27,7 @@ import (
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/third_party/go9p/ufs"
......@@ -79,13 +81,23 @@ var mountCmd = &cobra.Command{
os.Exit(1)
}
defer api.Close()
host, err := api.Load(config.GetMachineName())
if err != nil {
glog.Errorln("Error loading api: ", err)
os.Exit(1)
}
ip, err := cluster.GetVMHostIP(host)
if err != nil {
glog.Errorln("Error getting the host IP address to use from within the VM: ", err)
os.Exit(1)
}
fmt.Printf("Mounting %s into %s on the minikubeVM\n", hostPath, vmPath)
fmt.Println("This daemon process needs to stay alive for the mount to still be accessible...")
var wg sync.WaitGroup
wg.Add(1)
go func() {
ufs.StartServer(constants.DefaultUfsAddress, debugVal, hostPath)
ufs.StartServer(net.JoinHostPort(ip.String(), constants.DefaultUfsPort), debugVal, hostPath)
wg.Done()
}()
err = cluster.MountHost(api, vmPath)
......
......@@ -23,6 +23,7 @@ import (
"net"
"os"
"path/filepath"
"regexp"
"strings"
"time"
......@@ -405,7 +406,7 @@ func MountHost(api libmachine.API, path string) error {
if err != nil {
return errors.Wrap(err, "Error checking that api exists and loading it")
}
ip, err := getVMHostIP(host)
ip, err := GetVMHostIP(host)
if err != nil {
return errors.Wrap(err, "Error getting the host IP address to use from within the VM")
}
......@@ -421,6 +422,47 @@ func MountHost(api libmachine.API, path string) error {
return nil
}
// GetVMHostIP gets the ip address to be used for mapping host -> VM and VM -> host
func GetVMHostIP(host *host.Host) (net.IP, error) {
switch host.DriverName {
case "kvm":
return net.ParseIP("192.168.42.1"), nil
case "hyperv":
re := regexp.MustCompile("\"VSwitch\": \"(.*?)\",")
// TODO(aprindle) Change this to deserialize the driver instead
hypervVirtualSwitch := re.FindStringSubmatch(string(host.RawDriver))[1]
ip, err := getIPForInterface(fmt.Sprintf("vEthernet (%s)", hypervVirtualSwitch))
if err != nil {
return []byte{}, errors.Wrap(err, "Error getting VM/Host IP address")
}
return ip, nil
case "virtualbox":
ip, err := getIPForInterface("vboxnet0")
if err != nil {
return []byte{}, errors.Wrap(err, "Error getting VM/Host IP address")
}
return ip, nil
case "xhyve":
return net.ParseIP("192.168.64.1"), nil
default:
return []byte{}, errors.New("Error, attempted to get host ip address for unsupported driver")
}
}
// Based on code from http://stackoverflow.com/questions/23529663/how-to-get-all-addresses-and-masks-from-local-interfaces-in-go
func getIPForInterface(name string) (net.IP, error) {
i, _ := net.InterfaceByName(name)
addrs, _ := i.Addrs()
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok {
if ip := ipnet.IP.To4(); ip != nil {
return ip, nil
}
}
}
return nil, errors.Errorf("Error finding IPV4 address for %s", name)
}
func CheckIfApiExistsAndLoad(api libmachine.API) (*host.Host, error) {
exists, err := api.Exists(cfg.GetMachineName())
if err != nil {
......
......@@ -17,12 +17,8 @@ limitations under the License.
package cluster
import (
"errors"
"net"
"github.com/docker/machine/drivers/vmwarefusion"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
)
......@@ -72,14 +68,3 @@ func createXhyveHost(config MachineConfig) *xhyveDriver {
Virtio9pFolder: "/Users",
}
}
func getVMHostIP(host *host.Host) (net.IP, error) {
switch host.DriverName {
case "virtualbox":
return net.ParseIP("10.0.2.2"), nil
case "xhyve":
return net.ParseIP("192.168.64.1"), nil
default:
return []byte{}, errors.New("Error, attempted to get host ip address for unsupported driver")
}
}
......@@ -17,13 +17,10 @@ limitations under the License.
package cluster
import (
"errors"
"fmt"
"net"
"path/filepath"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
)
......@@ -61,14 +58,3 @@ func createKVMHost(config MachineConfig) *kvmDriver {
IOMode: "threads",
}
}
func getVMHostIP(host *host.Host) (net.IP, error) {
switch host.DriverName {
case "virtualbox":
return net.ParseIP("10.0.2.2"), nil
case "kvm":
return net.ParseIP("192.168.42.1"), nil
default:
return []byte{}, errors.New("Error, attempted to get host ip address for unsupported driver")
}
}
......@@ -17,15 +17,8 @@ limitations under the License.
package cluster
import (
"fmt"
"net"
"regexp"
"strings"
"github.com/docker/machine/drivers/hyperv"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/host"
"github.com/pkg/errors"
cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
)
......@@ -40,41 +33,3 @@ func createHypervHost(config MachineConfig) drivers.Driver {
d.SSHUser = "docker"
return d
}
func getVMHostIP(host *host.Host) (net.IP, error) {
switch host.DriverName {
case "virtualbox":
return net.ParseIP("10.0.2.2"), nil
case "hyperv":
re := regexp.MustCompile("\"VSwitch\": \"(.*?)\",")
hypervVirtualSwitch := re.FindStringSubmatch(string(host.RawDriver))[1]
ip, err := getWindowsHostIpFromHyperV(hypervVirtualSwitch)
if err != nil {
return []byte{}, errors.Wrap(err, "Error getting 9p mount command")
}
return ip, nil
default:
return []byte{}, errors.New("Error, attempted to get host ip address for unsupported driver")
}
}
func getWindowsHostIpFromHyperV(hypervVirtualSwitch string) (net.IP, error) {
virtualSwitchTemplate := "vEthernet (%s)"
i, _ := net.InterfaceByName(fmt.Sprintf(virtualSwitchTemplate, hypervVirtualSwitch))
addrs, _ := i.Addrs()
for _, a := range addrs {
switch a.(type) {
case *net.IPNet:
ip := a.String()
if strings.Contains(ip, ".") {
vmIP := net.ParseIP(strings.Split(ip, "/")[0])
if vmIP.String() == "" {
return nil, errors.Errorf("Error finding IPV4 address for virtual switch %s", hypervVirtualSwitch)
}
return vmIP, nil
}
}
}
return nil, errors.Errorf("Error finding IPV4 address for virtual switch %s", hypervVirtualSwitch)
}
......@@ -125,6 +125,6 @@ const (
)
const (
DefaultUfsAddress = ":5640"
DefaultUfsPort = "5640"
DefaultUfsDebugLvl = 0
)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册