Skip to content

Commit

Permalink
Make none driver work as regular user (use sudo on demand)
Browse files Browse the repository at this point in the history
  • Loading branch information
Git-Jiro committed Oct 2, 2020
1 parent 0fc0c82 commit 8f76416
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
26 changes: 21 additions & 5 deletions pkg/minikube/command/exec_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -85,11 +86,11 @@ func (*execRunner) RunCmd(cmd *exec.Cmd) (*RunResult, error) {
}

// Copy copies a file and its permissions
func (*execRunner) Copy(f assets.CopyableFile) error {
func (e *execRunner) Copy(f assets.CopyableFile) error {
dst := path.Join(f.GetTargetDir(), f.GetTargetName())
if _, err := os.Stat(dst); err == nil {
glog.Infof("found %s, removing ...", dst)
if err := os.Remove(dst); err != nil {
if _, err := e.RunCmd(exec.Command("sudo", "rm", "-f", dst)); err != nil {
return errors.Wrapf(err, "error removing file %s", dst)
}
}
Expand All @@ -105,12 +106,27 @@ func (*execRunner) Copy(f assets.CopyableFile) error {
return errors.Wrapf(err, "error converting permissions %s to integer", f.GetPermissions())
}

return writeFile(dst, f, os.FileMode(perms))
// write to TMP location ...
tmpfile, err := ioutil.TempFile("", "minikube")
if err != nil {
return errors.Wrapf(err, "error creating tempfile")
}
defer os.Remove(tmpfile.Name())
err = writeFile(tmpfile.Name(), f, os.FileMode(perms))
if err != nil {
return errors.Wrapf(err, "error writing to tempfile %s", tmpfile.Name())
}
// ... then use SUDO to move to target
// then sudo cp -a src dst
_, err = e.RunCmd(exec.Command("sudo", "cp", "-a", tmpfile.Name(), dst))

return err
}

// Remove removes a file
func (*execRunner) Remove(f assets.CopyableFile) error {
func (e *execRunner) Remove(f assets.CopyableFile) error {
dst := filepath.Join(f.GetTargetDir(), f.GetTargetName())
glog.Infof("rm: %s", dst)
return os.Remove(dst)
_, err := e.RunCmd(exec.Command("sudo", "rm", "-f", dst))
return err
}
2 changes: 1 addition & 1 deletion pkg/minikube/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func BareMetal(name string) bool {

// NeedsRoot returns true if driver needs to run with root privileges
func NeedsRoot(name string) bool {
return name == None
return false
}

// NeedsPortForward returns true if driver is unable provide direct IP connectivity
Expand Down
7 changes: 1 addition & 6 deletions pkg/minikube/registry/drvs/none/none.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package none
import (
"fmt"
"os/exec"
"os/user"

"github.com/docker/machine/libmachine/drivers"
"k8s.io/minikube/pkg/drivers/none"
Expand Down Expand Up @@ -61,14 +60,10 @@ func status() registry.State {
return registry.State{Running: true, Error: err, Installed: false, Fix: "Install docker", Doc: "https://minikube.sigs.k8s.io/docs/reference/drivers/none/"}
}

u, err := user.Current()
_, err := user.Current()
if err != nil {
return registry.State{Running: true, Error: err, Healthy: false, Doc: "https://minikube.sigs.k8s.io/docs/reference/drivers/none/"}
}

if u.Uid != "0" {
return registry.State{Error: fmt.Errorf("the 'none' driver must be run as the root user"), Healthy: false, Fix: "For non-root usage, try the newer 'docker' driver", Installed: true}
}

return registry.State{Installed: true, Healthy: true}
}

0 comments on commit 8f76416

Please sign in to comment.