From 8f76416ce6a3d87f40924e792dd6c1687464490b Mon Sep 17 00:00:00 2001 From: Martin Schimandl Date: Fri, 2 Oct 2020 12:13:08 +0200 Subject: [PATCH] Make none driver work as regular user (use sudo on demand) --- pkg/minikube/command/exec_runner.go | 26 ++++++++++++++++++++----- pkg/minikube/driver/driver.go | 2 +- pkg/minikube/registry/drvs/none/none.go | 7 +------ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pkg/minikube/command/exec_runner.go b/pkg/minikube/command/exec_runner.go index 43b49c59f25c..8b94d276e357 100644 --- a/pkg/minikube/command/exec_runner.go +++ b/pkg/minikube/command/exec_runner.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "os" "os/exec" "path" @@ -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) } } @@ -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 } diff --git a/pkg/minikube/driver/driver.go b/pkg/minikube/driver/driver.go index 2002ea3afdea..12d5246ee83b 100644 --- a/pkg/minikube/driver/driver.go +++ b/pkg/minikube/driver/driver.go @@ -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 diff --git a/pkg/minikube/registry/drvs/none/none.go b/pkg/minikube/registry/drvs/none/none.go index 7afae87987df..9d3bd48abb9f 100644 --- a/pkg/minikube/registry/drvs/none/none.go +++ b/pkg/minikube/registry/drvs/none/none.go @@ -21,7 +21,6 @@ package none import ( "fmt" "os/exec" - "os/user" "github.com/docker/machine/libmachine/drivers" "k8s.io/minikube/pkg/drivers/none" @@ -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} }