Skip to content
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
18 changes: 9 additions & 9 deletions cmd/limactl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/coreos/go-semver/semver"
"github.com/lima-vm/lima/pkg/instance"
"github.com/lima-vm/lima/pkg/ioutilx"
"github.com/lima-vm/lima/pkg/limayaml"
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store"
Expand Down Expand Up @@ -139,10 +139,10 @@ func shellAction(cmd *cobra.Command, args []string) error {
if workDir != "" {
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
// FIXME: check whether y.Mounts contains the home, not just len > 0
} else if len(inst.Config.Mounts) > 0 {
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limayaml.WSL2 {
hostCurrentDir, err := os.Getwd()
if err == nil && runtime.GOOS == "windows" {
hostCurrentDir, err = mountDirFromWindowsDir(hostCurrentDir)
hostCurrentDir, err = mountDirFromWindowsDir(inst, hostCurrentDir)
}
if err == nil {
changeDirCmd = fmt.Sprintf("cd %s", shellescape.Quote(hostCurrentDir))
Expand All @@ -152,7 +152,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
}
hostHomeDir, err := os.UserHomeDir()
if err == nil && runtime.GOOS == "windows" {
hostHomeDir, err = mountDirFromWindowsDir(hostHomeDir)
hostHomeDir, err = mountDirFromWindowsDir(inst, hostHomeDir)
}
if err == nil {
changeDirCmd = fmt.Sprintf("%s || cd %s", changeDirCmd, shellescape.Quote(hostHomeDir))
Expand Down Expand Up @@ -244,12 +244,12 @@ func shellAction(cmd *cobra.Command, args []string) error {
return sshCmd.Run()
}

func mountDirFromWindowsDir(dir string) (string, error) {
dir, err := ioutilx.WindowsSubsystemPath(dir)
if err == nil && !strings.HasPrefix(dir, "/mnt/") {
dir = path.Join("/mnt", dir)
func mountDirFromWindowsDir(inst *store.Instance, dir string) (string, error) {
if inst.VMType == limayaml.WSL2 {
distroName := "lima-" + inst.Name
return ioutilx.WindowsSubsystemPathForLinux(dir, distroName)
}
return dir, err
return ioutilx.WindowsSubsystemPath(dir)
}

func shellBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
Expand Down
11 changes: 10 additions & 1 deletion pkg/ioutilx/ioutilx.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ func WindowsSubsystemPath(orig string) (string, error) {
out, err := exec.Command("cygpath", filepath.ToSlash(orig)).CombinedOutput()
if err != nil {
logrus.WithError(err).Errorf("failed to convert path to mingw, maybe not using Git ssh?")
return orig, err
return "", err
}
return strings.TrimSpace(string(out)), nil
}

func WindowsSubsystemPathForLinux(orig, distro string) (string, error) {
out, err := exec.Command("wsl", "-d", distro, "--exec", "wslpath", filepath.ToSlash(orig)).CombinedOutput()
if err != nil {
logrus.WithError(err).Errorf("failed to convert path to mingw, maybe wsl command is not operational?")
return "", err
}
return strings.TrimSpace(string(out)), nil
}
19 changes: 14 additions & 5 deletions pkg/osutil/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os/exec"
"os/user"
"path"
"path/filepath"
"regexp"
"runtime"
Expand Down Expand Up @@ -149,7 +150,19 @@ func LimaUser(limaVersion string, warn bool) *user.User {
if err != nil {
logrus.Debug(err)
} else {
home += ".linux"
// 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"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This complicated logic should have unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to write a test for this one, but this have some heavy dependencies. It needs cygpath, which is mostly fine for Windows specific cases, but for wslpath - one needs WSL distribution as it is available only inside container.

At some point I had idea to remove this part entirely and rely only on hand coded cygpath alternative in the code below this (the second conditional branch), as for the purpose of creating home directory it was good enough.

I will create a ticket, where I summarize what options are available to improve situation.

}
if home == "" {
drive := filepath.VolumeName(limaUser.HomeDir)
Expand All @@ -160,10 +173,6 @@ func LimaUser(limaVersion string, warn bool) *user.User {
home += ".linux"
}
if !regexPath.MatchString(limaUser.HomeDir) {
// Trim prefix of well known default mounts
if strings.HasPrefix(home, "/mnt/") {
home = strings.TrimPrefix(home, "/mnt")
}
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)
Expand Down
Loading