Skip to content

Commit

Permalink
hashicorp#9298: Increase stability of determination of a current WSL …
Browse files Browse the repository at this point in the history
…instance
  • Loading branch information
BR0kEN- authored and chrisroberts committed Feb 28, 2018
1 parent 72664ea commit 62740e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
4 changes: 4 additions & 0 deletions lib/vagrant/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -947,5 +947,9 @@ class WSLVagrantAccessError < VagrantError
class WSLVirtualBoxWindowsAccessError < VagrantError
error_key(:wsl_virtualbox_windows_access)
end

class WSLRootFsNotFoundError < VagrantError
error_key(:wsl_rootfs_not_found_error)
end
end
end
55 changes: 25 additions & 30 deletions lib/vagrant/util/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,27 +282,6 @@ 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].split(' ')[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 @@ -321,17 +300,33 @@ def 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
PowerShell.execute_cmd('(Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | ForEach-Object {Get-ItemProperty $_.PSPath}).BasePath').split(" ").each do |path|
# Lowercase the drive letter, skip the next symbol (which is a
# colon from a Windows path) and convert path to UNIX style.
path = "/mnt/" + path[0, 1].downcase + path[2..-1].tr('\\', '/') + "/rootfs"

begin
fs = Dir.open(path)
rescue Errno::EACCES
# Current WSL instance doesn't have an access to its mount from
# within itself despite all others are available. That's the
# hacky way we're using to determine current instance.
@_wsl_rootfs = path
# You can create and simultaneously run multiple WSL instances,
# comment out the "break", run this script within each one and
# it'll return only single value.
break
else
fs.close
end
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'
if @_wsl_rootfs.nil?
raise Vagrant::Errors::WSLRootFsNotFoundError
end
end

@_wsl_rootfs
end

# Convert a WSL path to the local Windows path. This is useful
Expand Down
4 changes: 4 additions & 0 deletions templates/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,10 @@ en:
Linux, please refer to the Vagrant documentation:
https://www.vagrantup.com/docs/other/wsl.html
wsl_rootfs_not_found_error: |-
Vagrant is unable to determine WSL instance it is currently in. If you see this
error then your WSL installation is incorrect.
#-------------------------------------------------------------------------------
# Translations for config validation errors
#-------------------------------------------------------------------------------
Expand Down

0 comments on commit 62740e2

Please sign in to comment.