Skip to content

Commit 08327ff

Browse files
authored
Merge pull request #3304 from arixmkii/fix-hardcoded-prefix
Improve Subsystem paths handling on Windows hosts
2 parents ce8061f + 848f704 commit 08327ff

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,7 +8,6 @@ import (
88
"fmt"
99
"os"
1010
"os/exec"
11-
"path"
1211
"runtime"
1312
"strconv"
1413
"strings"
@@ -17,6 +16,7 @@ import (
1716
"github.com/coreos/go-semver/semver"
1817
"github.com/lima-vm/lima/pkg/instance"
1918
"github.com/lima-vm/lima/pkg/ioutilx"
19+
"github.com/lima-vm/lima/pkg/limayaml"
2020
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
2121
"github.com/lima-vm/lima/pkg/sshutil"
2222
"github.com/lima-vm/lima/pkg/store"
@@ -139,10 +139,10 @@ func shellAction(cmd *cobra.Command, args []string) error {
139139
if workDir != "" {
140140
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
141141
// FIXME: check whether y.Mounts contains the home, not just len > 0
142-
} else if len(inst.Config.Mounts) > 0 {
142+
} else if len(inst.Config.Mounts) > 0 || inst.VMType == limayaml.WSL2 {
143143
hostCurrentDir, err := os.Getwd()
144144
if err == nil && runtime.GOOS == "windows" {
145-
hostCurrentDir, err = mountDirFromWindowsDir(hostCurrentDir)
145+
hostCurrentDir, err = mountDirFromWindowsDir(inst, hostCurrentDir)
146146
}
147147
if err == nil {
148148
changeDirCmd = fmt.Sprintf("cd %s", shellescape.Quote(hostCurrentDir))
@@ -152,7 +152,7 @@ func shellAction(cmd *cobra.Command, args []string) error {
152152
}
153153
hostHomeDir, err := os.UserHomeDir()
154154
if err == nil && runtime.GOOS == "windows" {
155-
hostHomeDir, err = mountDirFromWindowsDir(hostHomeDir)
155+
hostHomeDir, err = mountDirFromWindowsDir(inst, hostHomeDir)
156156
}
157157
if err == nil {
158158
changeDirCmd = fmt.Sprintf("%s || cd %s", changeDirCmd, shellescape.Quote(hostHomeDir))
@@ -244,12 +244,12 @@ func shellAction(cmd *cobra.Command, args []string) error {
244244
return sshCmd.Run()
245245
}
246246

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

255255
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)