Skip to content

Commit

Permalink
hashicorp#9298: Compute a correct path to the current WSL instance
Browse files Browse the repository at this point in the history
  • Loading branch information
BR0kEN- authored and chrisroberts committed Feb 28, 2018
1 parent caf43b7 commit 4769818
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion lib/vagrant/util/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,27 @@ def platform
return @_platform
end

def linux_distro
return @_linux_distro if defined?(@_linux_distro)

@_linux_distro = nil

if linux?
# A simplest way to get the Linux distribution name.
result = Subprocess.execute(
"python",
"-c",
"import platform;print(platform.linux_distribution()[0])"
)

if result.exit_code.zero?
@_linux_distro = result.stdout.chomp
end
end

@_linux_distro
end

# Determine if given path is within the WSL rootfs. Returns
# true if within the subsystem, or false if outside the subsystem.
#
Expand All @@ -291,6 +312,28 @@ def wsl_path?(path)
wsl? && !path.to_s.downcase.start_with?("/mnt/")
end

# Compute the path to rootfs of currently active WSL.
#
# @return [String] A path to rootfs of a current WSL instance.
def wsl_rootfs
return @_wsl_rootfs if defined?(@_wsl_rootfs)

@_wsl_rootfs = nil

if wsl?
# Handle WSL installation from Microsoft Store.
@_wsl_rootfs = PowerShell.execute_cmd('(Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss" -Recurse | ForEach-Object { Get-ItemProperty $_.pspath } | Where-Object { $_.PackageFamilyName -eq ($(get-appxpackage).PackageFamilyName | findstr ' + linux_distro + ') }).BasePath')
end

if @_wsl_rootfs.nil?
# Looks like WSL has been installed via "lxrun /install" which is deprecated.
@_wsl_rootfs = wsl_windows_appdata_local + '\\lxss'
else
# The path has been found in the registry, so append the directory with FS.
@_wsl_rootfs += '\\rootfs'
end
end

# Convert a WSL path to the local Windows path. This is useful
# for conversion when calling out to Windows executables from
# the WSL
Expand All @@ -302,7 +345,7 @@ def wsl_to_windows_path(path)
if wsl_path?(path)
parts = path.split("/")
parts.delete_if(&:empty?)
[wsl_windows_appdata_local, "lxss", *parts].join("\\")
[wsl_rootfs, *parts].join("\\")
else
path = path.to_s.sub("/mnt/", "")
parts = path.split("/")
Expand Down

0 comments on commit 4769818

Please sign in to comment.