Skip to content

Commit

Permalink
Add support for WSL [automount] configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dorssel committed Dec 15, 2023
1 parent fd6eacc commit 1f4dc1a
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions Usbipd/Wsl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static partial class Wsl
const string ListDistributionsUrl = "https://learn.microsoft.com/windows/wsl/basic-commands#install";
const string InstallDistributionUrl = "https://learn.microsoft.com/windows/wsl/basic-commands#install";
const string SetWslVersionUrl = "https://learn.microsoft.com/windows/wsl/basic-commands#set-wsl-version-to-1-or-2";
const string AutomountWslUrl = "https://learn.microsoft.com/windows/wsl/wsl-config#automount-settings";

static readonly string WslPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "wsl.exe");

Expand Down Expand Up @@ -269,14 +270,44 @@ public static async Task<ExitCode> Attach(BusId busId, bool autoAttach, string?

// Check: our distribution-independent usbip client must be runnable.
var wslWindowsPath = Path.Combine(Path.GetDirectoryName(Environment.ProcessPath)!, "wsl");
if (!LocalDriveRegex().IsMatch(Path.GetPathRoot(wslWindowsPath) ?? string.Empty))
var wslWindowsPathRoot = Path.GetPathRoot(wslWindowsPath);

Check warning on line 273 in Usbipd/Wsl.cs

View check run for this annotation

Codecov / codecov/patch

Usbipd/Wsl.cs#L273

Added line #L273 was not covered by tests
if ((wslWindowsPathRoot is null) || (!LocalDriveRegex().IsMatch(wslWindowsPathRoot)))
{
// We need the wsl utility directory to be accessible from within WSL.
console.ReportError($"Option '--wsl' requires that this software is installed on a local drive.");
return ExitCode.Failure;
}
var driveLetter = wslWindowsPath[0..1].ToLowerInvariant();
var wslLinuxPath = Path.Combine(@"\mnt", driveLetter, Path.GetRelativePath(Path.GetPathRoot(wslWindowsPath)!, wslWindowsPath)).Replace('\\', '/');
string wslLinuxMountPoint;
{
var wslResult = await RunWslAsync(distribution, null, cancellationToken, "cat", "/proc/self/mountinfo");

Check warning on line 282 in Usbipd/Wsl.cs

View check run for this annotation

Codecov / codecov/patch

Usbipd/Wsl.cs#L282

Added line #L282 was not covered by tests
// Example output:
//
// 46 51 0:26 / /mnt/wsl rw,relatime shared:1 - tmpfs none rw
// 47 51 0:28 / /usr/lib/wsl/drivers ro,nosuid,nodev,noatime - 9p none ro,dirsync,aname=drivers;fmask=222;dmask=222,mmap,access=client,msize=65536,trans=fd,rfd=7,wfd=7
// 51 38 8:32 / / rw,relatime - ext4 /dev/sdc rw,discard,errors=remount-ro,data=ordered
// ...
// 82 68 0:56 / /sys/fs/cgroup/rdma rw,nosuid,nodev,noexec,relatime shared:25 - cgroup cgroup rw,rdma
// 83 68 0:57 / /sys/fs/cgroup/misc rw,nosuid,nodev,noexec,relatime shared:26 - cgroup cgroup rw,misc
// 84 51 0:58 / /mnt/c rw,noatime - 9p drvfs rw,dirsync,aname=drvfs;path=C:\;uid=1000;gid=1000;symlinkroot=/mnt/,mmap,access=client,msize=262144,trans=virtio
//
// NOTE: The final backslash (\) is optional, and the drive letter is not case sensitive.
if (wslResult.ExitCode != 0)
{
console.ReportError($"Unable to parse the WSL mount points. Please report this at https://github.com/dorssel/usbipd-win/issues.");
return ExitCode.Failure;

Check warning on line 297 in Usbipd/Wsl.cs

View check run for this annotation

Codecov / codecov/patch

Usbipd/Wsl.cs#L296-L297

Added lines #L296 - L297 were not covered by tests
}
if ((wslResult.StandardOutput.Split('\n')
.FirstOrDefault(line => line.Split(" - ").Skip(1).FirstOrDefault()?.Split(' ').Skip(2).FirstOrDefault()?.Split(';').Any(
o => o.ToLowerInvariant().TrimEnd('\\') == $"path={wslWindowsPathRoot.ToLowerInvariant().TrimEnd('\\')}") ?? false) is not string mountLine)
|| (mountLine.Split(' ').Skip(4).FirstOrDefault() is not string mountPoint))

Check warning on line 302 in Usbipd/Wsl.cs

View check run for this annotation

Codecov / codecov/patch

Usbipd/Wsl.cs#L301-L302

Added lines #L301 - L302 were not covered by tests
{
console.ReportError($"Option '--wsl' requires that drive {wslWindowsPathRoot} is mounted in WSL; see {AutomountWslUrl}.");
return ExitCode.Failure;

Check warning on line 305 in Usbipd/Wsl.cs

View check run for this annotation

Codecov / codecov/patch

Usbipd/Wsl.cs#L304-L305

Added lines #L304 - L305 were not covered by tests
}
wslLinuxMountPoint = mountPoint;

Check warning on line 307 in Usbipd/Wsl.cs

View check run for this annotation

Codecov / codecov/patch

Usbipd/Wsl.cs#L307

Added line #L307 was not covered by tests
}
var wslLinuxPath = Path.Combine(wslLinuxMountPoint, Path.GetRelativePath(wslWindowsPathRoot, wslWindowsPath)).Replace('\\', '/');
console.ReportInfo($"Using client tools located at {wslLinuxPath}");

Check warning on line 310 in Usbipd/Wsl.cs

View check run for this annotation

Codecov / codecov/patch

Usbipd/Wsl.cs#L309-L310

Added lines #L309 - L310 were not covered by tests

{
var wslResult = await RunWslAsync(distribution, null, cancellationToken, wslLinuxPath + "/usbip", "version");
Expand Down

0 comments on commit 1f4dc1a

Please sign in to comment.