Skip to content

Commit

Permalink
Merge pull request #11979 from andriyDev/MountPort
Browse files Browse the repository at this point in the history
Add configurable port for minikube mount
  • Loading branch information
medyagh authored Jul 26, 2021
2 parents 7e0c46c + 36cc7e0 commit c6ff620
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 119 deletions.
9 changes: 7 additions & 2 deletions cmd/minikube/cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
// placeholders for flag values
var (
mountIP string
mountPort uint16
mountVersion string
mountType string
isKill bool
Expand Down Expand Up @@ -191,6 +192,9 @@ var mountCmd = &cobra.Command{

err = cluster.Mount(co.CP.Runner, ip.String(), vmPath, cfg)
if err != nil {
if rtErr, ok := err.(*cluster.MountError); ok && rtErr.ErrorType == cluster.MountErrorConnect {
exit.Error(reason.GuestMountCouldNotConnect, "mount could not connect", rtErr)
}
exit.Error(reason.GuestMount, "mount failed", err)
}
out.Step(style.Success, "Successfully mounted {{.sourcePath}} to {{.destinationPath}}", out.V{"sourcePath": hostPath, "destinationPath": vmPath})
Expand All @@ -202,6 +206,7 @@ var mountCmd = &cobra.Command{

func init() {
mountCmd.Flags().StringVar(&mountIP, "ip", "", "Specify the ip that the mount should be setup on")
mountCmd.Flags().Uint16Var(&mountPort, "port", 0, "Specify the port that the mount should be setup on, where 0 means any free port.")
mountCmd.Flags().StringVar(&mountType, "type", nineP, "Specify the mount filesystem type (supported types: 9p)")
mountCmd.Flags().StringVar(&mountVersion, "9p-version", defaultMountVersion, "Specify the 9p version that the mount should use")
mountCmd.Flags().BoolVar(&isKill, "kill", false, "Kill the mount process spawned by minikube start")
Expand All @@ -212,9 +217,9 @@ func init() {
mountCmd.Flags().IntVar(&mSize, "msize", defaultMsize, "The number of bytes to use for 9p packet payload")
}

// getPort asks the kernel for a free open port that is ready to use
// getPort uses the requested port or asks the kernel for a free open port that is ready to use
func getPort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", mountPort))
if err != nil {
panic(err)
}
Expand Down
28 changes: 25 additions & 3 deletions pkg/minikube/cluster/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,41 @@ type mountRunner interface {
RunCmd(*exec.Cmd) (*command.RunResult, error)
}

const (
// MountErrorUnknown failed with unknown error
MountErrorUnknown = iota
// MountErrorConnect
MountErrorConnect
)

// MountError wrapper around errors in the `Mount` function
type MountError struct {
// ErrorType enum for more info about the error
ErrorType int
// UnderlyingError the error being wrapped
UnderlyingError error
}

func (m *MountError) Error() string {
return m.UnderlyingError.Error()
}

// Mount runs the mount command from the 9p client on the VM to the 9p server on the host
func Mount(r mountRunner, source string, target string, c *MountConfig) error {
if err := Unmount(r, target); err != nil {
return errors.Wrap(err, "umount")
return &MountError{ErrorType: MountErrorUnknown, UnderlyingError: errors.Wrap(err, "umount")}
}

if _, err := r.RunCmd(exec.Command("/bin/bash", "-c", fmt.Sprintf("sudo mkdir -m %o -p %s", c.Mode, target))); err != nil {
return errors.Wrap(err, "create folder pre-mount")
return &MountError{ErrorType: MountErrorUnknown, UnderlyingError: errors.Wrap(err, "create folder pre-mount")}
}

rr, err := r.RunCmd(exec.Command("/bin/bash", "-c", mntCmd(source, target, c)))
if err != nil {
return errors.Wrapf(err, "mount with cmd %s ", rr.Command())
if strings.Contains(rr.Stderr.String(), "Connection timed out") {
return &MountError{ErrorType: MountErrorConnect, UnderlyingError: err}
}
return &MountError{ErrorType: MountErrorUnknown, UnderlyingError: errors.Wrapf(err, "mount with cmd %s ", rr.Command())}
}

klog.Infof("mount successful: %q", rr.Output())
Expand Down
9 changes: 9 additions & 0 deletions pkg/minikube/reason/reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ var (
GuestLoadHost = Kind{ID: "GUEST_LOAD_HOST", ExitCode: ExGuestError}
// minkube failed to create a mount
GuestMount = Kind{ID: "GUEST_MOUNT", ExitCode: ExGuestError}
// mount on guest was unable to connect to host mount server
GuestMountCouldNotConnect = Kind{
ID: "GUEST_MOUNT_COULD_NOT_CONNECT",
ExitCode: ExGuestError,
Advice: `If the host has a firewall:
1. Allow a port through the firewall
2. Specify "--port=<port_number>" for "minikube mount"`,
}
// minkube failed to update a mount
GuestMountConflict = Kind{ID: "GUEST_MOUNT_CONFLICT", ExitCode: ExGuestConflict}
// minikube failed to add a node to the cluster
Expand Down
Loading

0 comments on commit c6ff620

Please sign in to comment.