Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable port for minikube mount #11979

Merged
merged 4 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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