Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for running from inside a wsl2 vm #279

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions lib/vagrant-hostmanager/hosts_file/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,34 @@ def update_host
# copy and modify hosts file on host with Vagrant-managed entries
file = @global_env.tmp_path.join('hosts.local')

if WindowsSupport.windows?
if WindowsSupport.windows? || WindowsSupport.wsl?
# lazily include windows Module
class << self
include WindowsSupport unless include? WindowsSupport
end
hosts_location = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
copy_proc = Proc.new { windows_copy_file(file, hosts_location) }
windir = ENV['WINDIR']
if WindowsSupport.wsl?
Dir.chdir('/mnt/c'){
windir = `cmd.exe /c echo %WINDIR%`.strip
}
# convert wsl path to windows path
if file.to_s =~ /\/mnt\/[a-z]\//
win_file = file.to_s.sub(/^\/mnt\/([a-z])\//, '\1:\\').gsub('/', '\\')
else
win_file = "\\\\wsl\$\\#{ENV['WSL_DISTRO_NAME']}" + file.to_s.gsub('/', '\\')
end
win_hosts_location = "#{windir}\\System32\\drivers\\etc\\hosts"
hosts_location = "/mnt/" + windir[0].downcase + "/" + windir[3..-1] + "/System32/drivers/etc/hosts"

# add to both, windows host and wsl machine
copy_proc = Proc.new {
wsl_copy_file(file, hosts_location, win_file, win_hosts_location)
`[ -w "/etc/hosts" ] && cat "#{file}" > "/etc/hosts" || sudo cp "#{file}" "/etc/hosts"`
}
else
hosts_location = "#{windir}\\System32\\drivers\\etc\\hosts"
copy_proc = Proc.new { windows_copy_file(file, hosts_location) }
end
line_endings = "crlf"
else
hosts_location = '/etc/hosts'
Expand All @@ -70,6 +91,9 @@ class << self
end

FileUtils.cp(hosts_location, file)
if WindowsSupport.wsl?
FileUtils.chmod("+w", file)
end

if update_file(file, nil, true, line_endings)
copy_proc.call
Expand Down Expand Up @@ -183,6 +207,10 @@ def self.windows?
RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
end

def self.wsl?
ENV.include?('WSLENV')
end

require 'win32ole' if windows?

def windows_copy_file(source, dest)
Expand All @@ -195,6 +223,16 @@ def windows_copy_file(source, dest)
end
end

def wsl_copy_file(source, dest, win_source, win_dest)
begin
# First, try Ruby copy
FileUtils.cp(source, dest)
rescue Errno::EACCES
# Access denied, try with elevated privileges
system('powershell.exe', 'Start-Process -Verb Runas -FilePath cmd.exe -Argumentlist "/C","copy","' + win_source + '","' + win_dest + '"')
end
end

private

def windows_copy_file_elevated(source, dest)
Expand Down