Skip to content

Commit 61f1e83

Browse files
committed
Improve Subsystem paths handling on Windows hosts
Signed-off-by: Arthur Sengileyev <[email protected]>
1 parent 47ace06 commit 61f1e83

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

cmd/limactl/shell.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
"fmt"
99
"os"
1010
"os/exec"
11-
"path"
1211
"runtime"
1312
"strconv"
1413
"strings"
1514

1615
"al.essio.dev/pkg/shellescape"
1716
"github.com/coreos/go-semver/semver"
1817
"github.com/lima-vm/lima/pkg/ioutilx"
18+
"github.com/lima-vm/lima/pkg/limayaml"
1919
"github.com/lima-vm/lima/pkg/sshutil"
2020
"github.com/lima-vm/lima/pkg/store"
2121
"github.com/mattn/go-isatty"
@@ -93,10 +93,10 @@ func shellAction(cmd *cobra.Command, args []string) error {
9393
if workDir != "" {
9494
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
9595
// FIXME: check whether y.Mounts contains the home, not just len > 0
96-
} else if len(inst.Config.Mounts) > 0 {
96+
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limayaml.WSL2 {
9797
hostCurrentDir, err := os.Getwd()
9898
if err == nil && runtime.GOOS == "windows" {
99-
hostCurrentDir, err = mountDirFromWindowsDir(hostCurrentDir)
99+
hostCurrentDir, err = mountDirFromWindowsDir(inst, hostCurrentDir)
100100
}
101101
if err == nil {
102102
changeDirCmd = fmt.Sprintf("cd %s", shellescape.Quote(hostCurrentDir))
@@ -106,7 +106,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
106106
}
107107
hostHomeDir, err := os.UserHomeDir()
108108
if err == nil && runtime.GOOS == "windows" {
109-
hostHomeDir, err = mountDirFromWindowsDir(hostHomeDir)
109+
hostHomeDir, err = mountDirFromWindowsDir(inst, hostHomeDir)
110110
}
111111
if err == nil {
112112
changeDirCmd = fmt.Sprintf("%s || cd %s", changeDirCmd, shellescape.Quote(hostHomeDir))
@@ -198,12 +198,12 @@ func shellAction(cmd *cobra.Command, args []string) error {
198198
return sshCmd.Run()
199199
}
200200

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

209209
func shellBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

pkg/ioutilx/ioutilx.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,16 @@ func WindowsSubsystemPath(orig string) (string, error) {
5353
out, err := exec.Command("cygpath", filepath.ToSlash(orig)).CombinedOutput()
5454
if err != nil {
5555
logrus.WithError(err).Errorf("failed to convert path to mingw, maybe not using Git ssh?")
56-
return orig, err
56+
return "", err
57+
}
58+
return strings.TrimSpace(string(out)), nil
59+
}
60+
61+
func WindowsSubsystemPathForLinux(orig, distro string) (string, error) {
62+
out, err := exec.Command("wsl", "-d", distro, "--exec", "wslpath", filepath.ToSlash(orig)).CombinedOutput()
63+
if err != nil {
64+
logrus.WithError(err).Errorf("failed to convert path to mingw, maybe wsl command is not operational?")
65+
return "", err
5766
}
5867
return strings.TrimSpace(string(out)), nil
5968
}

pkg/osutil/user.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os/exec"
99
"os/user"
10+
"path"
1011
"path/filepath"
1112
"regexp"
1213
"runtime"
@@ -149,7 +150,19 @@ func LimaUser(limaVersion string, warn bool) *user.User {
149150
if err != nil {
150151
logrus.Debug(err)
151152
} else {
152-
home += ".linux"
153+
// Trim mount prefix within Subsystem
154+
// cygwin/msys2 cygpath could have prefix for drive mounts configured via /etc/fstab
155+
// wsl wslpath could have prefix for drive mounts configured via [automount] section in wsl.conf
156+
drivePath, err := ioutilx.WindowsSubsystemPath(filepath.VolumeName(limaUser.HomeDir) + "/")
157+
if err != nil {
158+
logrus.Debug(err)
159+
} else {
160+
prefix := path.Dir(strings.TrimSuffix(drivePath, "/"))
161+
if prefix != "/" {
162+
home = strings.TrimPrefix(home, prefix)
163+
}
164+
home += ".linux"
165+
}
153166
}
154167
if home == "" {
155168
drive := filepath.VolumeName(limaUser.HomeDir)
@@ -160,10 +173,6 @@ func LimaUser(limaVersion string, warn bool) *user.User {
160173
home += ".linux"
161174
}
162175
if !regexPath.MatchString(limaUser.HomeDir) {
163-
// Trim prefix of well known default mounts
164-
if strings.HasPrefix(home, "/mnt/") {
165-
home = strings.TrimPrefix(home, "/mnt")
166-
}
167176
warning := fmt.Sprintf("local home %q is not a valid Linux path (must match %q); using %q home instead",
168177
limaUser.HomeDir, regexPath.String(), home)
169178
warnings = append(warnings, warning)

0 commit comments

Comments
 (0)