Skip to content

Commit a4a4ce2

Browse files
committed
Fix mount support in QEMU on Windows hosts
On Windows there is native path translation needed from location to Unix path mountPoint. And another one is needed for location before passing it to Unix like tooling (sshfs). Signed-off-by: Arthur Sengileyev <[email protected]>
1 parent 01af6f9 commit a4a4ce2

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

pkg/hostagent/mount.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"errors"
88
"fmt"
99
"os"
10+
"runtime"
1011

12+
"github.com/lima-vm/lima/pkg/ioutilx"
1113
"github.com/lima-vm/lima/pkg/limayaml"
1214
"github.com/lima-vm/sshocker/pkg/reversesshfs"
1315
"github.com/sirupsen/logrus"
@@ -47,33 +49,42 @@ func (a *HostAgent) setupMount(m limayaml.Mount) (*mount, error) {
4749
}
4850
logrus.Infof("Mounting %q on %q", m.Location, *m.MountPoint)
4951

52+
resolvedLocation := m.Location
53+
if runtime.GOOS == "windows" {
54+
var err error
55+
resolvedLocation, err = ioutilx.WindowsSubsystemPath(m.Location)
56+
if err != nil {
57+
return nil, err
58+
}
59+
}
60+
5061
rsf := &reversesshfs.ReverseSSHFS{
5162
Driver: *m.SSHFS.SFTPDriver,
5263
SSHConfig: a.sshConfig,
53-
LocalPath: m.Location,
64+
LocalPath: resolvedLocation,
5465
Host: "127.0.0.1",
5566
Port: a.sshLocalPort,
5667
RemotePath: *m.MountPoint,
5768
Readonly: !(*m.Writable),
5869
SSHFSAdditionalArgs: []string{"-o", sshfsOptions},
5970
}
6071
if err := rsf.Prepare(); err != nil {
61-
return nil, fmt.Errorf("failed to prepare reverse sshfs for %q on %q: %w", m.Location, *m.MountPoint, err)
72+
return nil, fmt.Errorf("failed to prepare reverse sshfs for %q on %q: %w", resolvedLocation, *m.MountPoint, err)
6273
}
6374
if err := rsf.Start(); err != nil {
64-
logrus.WithError(err).Warnf("failed to mount reverse sshfs for %q on %q, retrying with `-o nonempty`", m.Location, *m.MountPoint)
75+
logrus.WithError(err).Warnf("failed to mount reverse sshfs for %q on %q, retrying with `-o nonempty`", resolvedLocation, *m.MountPoint)
6576
// NOTE: nonempty is not supported for libfuse3: https://github.com/canonical/multipass/issues/1381
6677
rsf.SSHFSAdditionalArgs = []string{"-o", "nonempty"}
6778
if err := rsf.Start(); err != nil {
68-
return nil, fmt.Errorf("failed to mount reverse sshfs for %q on %q: %w", m.Location, *m.MountPoint, err)
79+
return nil, fmt.Errorf("failed to mount reverse sshfs for %q on %q: %w", resolvedLocation, *m.MountPoint, err)
6980
}
7081
}
7182

7283
res := &mount{
7384
close: func() error {
74-
logrus.Infof("Unmounting %q", m.Location)
85+
logrus.Infof("Unmounting %q", resolvedLocation)
7586
if err := rsf.Close(); err != nil {
76-
return fmt.Errorf("failed to unmount reverse sshfs for %q on %q: %w", m.Location, *m.MountPoint, err)
87+
return fmt.Errorf("failed to unmount reverse sshfs for %q on %q: %w", resolvedLocation, *m.MountPoint, err)
7788
}
7889
return nil
7990
},

pkg/limayaml/defaults.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/coreos/go-semver/semver"
2424
"github.com/docker/go-units"
2525
"github.com/goccy/go-yaml"
26+
"github.com/lima-vm/lima/pkg/ioutilx"
2627
"github.com/lima-vm/lima/pkg/version"
2728
"github.com/pbnjay/memory"
2829
"github.com/sirupsen/logrus"
@@ -830,7 +831,15 @@ func FillDefault(y, d, o *LimaYAML, filePath string, warn bool) {
830831
logrus.WithError(err).Warnf("Couldn't expand location %q", mount.Location)
831832
}
832833
if mount.MountPoint == nil {
833-
mounts[i].MountPoint = ptr.Of(mounts[i].Location)
834+
mountLocation := mounts[i].Location
835+
if runtime.GOOS == "windows" {
836+
var err error
837+
mountLocation, err = ioutilx.WindowsSubsystemPath(mountLocation)
838+
if err != nil {
839+
logrus.WithError(err).Warnf("Couldn't convert location %q into mount target", mounts[i].Location)
840+
}
841+
}
842+
mounts[i].MountPoint = ptr.Of(mountLocation)
834843
}
835844
}
836845

pkg/limayaml/defaults_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/google/go-cmp/cmp"
1717
"github.com/google/go-cmp/cmp/cmpopts"
18+
"github.com/lima-vm/lima/pkg/ioutilx"
1819
"github.com/lima-vm/lima/pkg/osutil"
1920
"github.com/lima-vm/lima/pkg/ptr"
2021
"github.com/lima-vm/lima/pkg/store/dirnames"
@@ -225,6 +226,12 @@ func TestFillDefault(t *testing.T) {
225226

226227
expect.Mounts = slices.Clone(y.Mounts)
227228
expect.Mounts[0].MountPoint = ptr.Of(expect.Mounts[0].Location)
229+
if runtime.GOOS == "windows" {
230+
mountLocation, err := ioutilx.WindowsSubsystemPath(expect.Mounts[0].Location)
231+
if err == nil {
232+
expect.Mounts[0].MountPoint = ptr.Of(mountLocation)
233+
}
234+
}
228235
expect.Mounts[0].Writable = ptr.Of(false)
229236
expect.Mounts[0].SSHFS.Cache = ptr.Of(true)
230237
expect.Mounts[0].SSHFS.FollowSymlinks = ptr.Of(false)
@@ -464,6 +471,12 @@ func TestFillDefault(t *testing.T) {
464471
expect.Containerd.Archives[0].Arch = *d.Arch
465472
expect.Mounts = slices.Clone(d.Mounts)
466473
expect.Mounts[0].MountPoint = ptr.Of(expect.Mounts[0].Location)
474+
if runtime.GOOS == "windows" {
475+
mountLocation, err := ioutilx.WindowsSubsystemPath(expect.Mounts[0].Location)
476+
if err == nil {
477+
expect.Mounts[0].MountPoint = ptr.Of(mountLocation)
478+
}
479+
}
467480
expect.Mounts[0].SSHFS.Cache = ptr.Of(true)
468481
expect.Mounts[0].SSHFS.FollowSymlinks = ptr.Of(false)
469482
expect.Mounts[0].SSHFS.SFTPDriver = ptr.Of("")

0 commit comments

Comments
 (0)