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

Unify Lima user home inside guest for all platforms #3378

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 1 addition & 4 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ func TestFillDefault(t *testing.T) {
limaHome, err := dirnames.LimaDir()
assert.NilError(t, err)
user := osutil.LimaUser("0.0.0", false)
if runtime.GOOS != "windows" {
// manual template expansion for "/home/{{.User}}.linux" (done by FillDefault)
user.HomeDir = fmt.Sprintf("/home/%s.linux", user.Username)
}
user.HomeDir = fmt.Sprintf("/home/%s.linux", user.Username)
uid, err := strconv.ParseUint(user.Uid, 10, 32)
assert.NilError(t, err)

Expand Down
43 changes: 2 additions & 41 deletions pkg/osutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import (
"fmt"
"os/exec"
"os/user"
"path"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"

"github.com/lima-vm/lima/pkg/ioutilx"
. "github.com/lima-vm/lima/pkg/must"
"github.com/lima-vm/lima/pkg/version/versionutil"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -45,9 +42,6 @@ var (
// names to the fallback user as well, so the regex does not allow them.
var regexUsername = regexp.MustCompile("^[a-z_][a-z0-9_-]*$")

// regexPath detects valid Linux path.
var regexPath = regexp.MustCompile("^[/a-zA-Z0-9_-]+$")

func LookupUser(name string) (User, error) {
if users == nil {
users = make(map[string]User)
Expand Down Expand Up @@ -115,9 +109,8 @@ func LimaUser(limaVersion string, warn bool) *user.User {
warnings = append(warnings, warning)
limaUser.Username = fallbackUser
}
if runtime.GOOS != "windows" {
limaUser.HomeDir = "/home/{{.User}}.linux"
} else {
limaUser.HomeDir = "/home/{{.User}}.linux"
if runtime.GOOS == "windows" {
idu, err := call([]string{"id", "-u"})
if err != nil {
logrus.Debug(err)
Expand Down Expand Up @@ -146,38 +139,6 @@ func LimaUser(limaVersion string, warn bool) *user.User {
warnings = append(warnings, warning)
limaUser.Gid = formatUidGid(gid)
}
home, err := ioutilx.WindowsSubsystemPath(limaUser.HomeDir)
if err != nil {
logrus.Debug(err)
} else {
// Trim mount prefix within Subsystem
// cygwin/msys2 cygpath could have prefix for drive mounts configured via /etc/fstab
// wsl wslpath could have prefix for drive mounts configured via [automount] section in wsl.conf
drivePath, err := ioutilx.WindowsSubsystemPath(filepath.VolumeName(limaUser.HomeDir) + "/")
if err != nil {
logrus.Debug(err)
} else {
prefix := path.Dir(strings.TrimSuffix(drivePath, "/"))
if prefix != "/" {
home = strings.TrimPrefix(home, prefix)
}
home += ".linux"
}
}
if home == "" {
drive := filepath.VolumeName(limaUser.HomeDir)
home = filepath.ToSlash(limaUser.HomeDir)
// replace C: with /c
prefix := strings.ToLower(fmt.Sprintf("/%c", drive[0]))
home = strings.Replace(home, drive, prefix, 1)
home += ".linux"
}
if !regexPath.MatchString(limaUser.HomeDir) {
warning := fmt.Sprintf("local home %q is not a valid Linux path (must match %q); using %q home instead",
limaUser.HomeDir, regexPath.String(), home)
warnings = append(warnings, warning)
limaUser.HomeDir = home
}
}
})
if warn {
Expand Down
Loading