Halt Vagrant if within WSL and attempting to operate out of rootfs

This commit is contained in:
Chris Roberts 2017-05-08 15:21:39 -07:00
parent 9a06374d29
commit e8e38a4007
3 changed files with 36 additions and 16 deletions

View File

@ -895,5 +895,9 @@ module Vagrant
class WSLVagrantVersionMismatch < VagrantError class WSLVagrantVersionMismatch < VagrantError
error_key(:wsl_vagrant_version_mismatch) error_key(:wsl_vagrant_version_mismatch)
end end
class WSLVagrantAccessError < VagrantError
error_key(:wsl_vagrant_access_error)
end
end end
end end

View File

@ -290,22 +290,29 @@ module Vagrant
# simple setup to allow sharing of the user's VAGRANT_HOME directory # simple setup to allow sharing of the user's VAGRANT_HOME directory
# within the subsystem # within the subsystem
# #
# @param [Environment] env
# @param [Logger] logger Optional logger to display information # @param [Logger] logger Optional logger to display information
def wsl_init(logger=nil) def wsl_init(env, logger=nil)
if wsl? && ENV["VAGRANT_WSL_ACCESS_WINDOWS_USER"] if wsl?
wsl_validate_matching_vagrant_versions! if ENV["VAGRANT_WSL_ACCESS_WINDOWS_USER"]
shared_user = ENV["VAGRANT_WSL_ACCESS_WINDOWS_USER"] wsl_validate_matching_vagrant_versions!
if logger shared_user = ENV["VAGRANT_WSL_ACCESS_WINDOWS_USER"]
logger.warn("Windows Subsystem for Linux detected. Allowing access to user: #{shared_user}")
logger.warn("Vagrant will be allowed to control Vagrant managed machines within the user's home path.")
end
if ENV["VAGRANT_HOME"] || ENV["VAGRANT_WSL_DISABLE_VAGRANT_HOME"]
logger.warn("VAGRANT_HOME environment variable already set. Not overriding!") if logger
else
home_path = wsl_windows_accessible_path
ENV["VAGRANT_HOME"] = File.join(home_path, ".vagrant.d")
if logger if logger
logger.info("Overriding VAGRANT_HOME environment variable to configured windows user. (#{ENV["VAGRANT_HOME"]})") logger.warn("Windows Subsystem for Linux detected. Allowing access to user: #{shared_user}")
logger.warn("Vagrant will be allowed to control Vagrant managed machines within the user's home path.")
end
if ENV["VAGRANT_HOME"] || ENV["VAGRANT_WSL_DISABLE_VAGRANT_HOME"]
logger.warn("VAGRANT_HOME environment variable already set. Not overriding!") if logger
else
home_path = wsl_windows_accessible_path
ENV["VAGRANT_HOME"] = File.join(home_path, ".vagrant.d")
if logger
logger.info("Overriding VAGRANT_HOME environment variable to configured windows user. (#{ENV["VAGRANT_HOME"]})")
end
end
else
if env.local_data_path.to_s.start_with?("/mnt/")
raise Vagrant::Errors::WSLVagrantAccessError
end end
end end
end end
@ -319,13 +326,14 @@ module Vagrant
if result.exit_code == 0 if result.exit_code == 0
windows_version = result.stdout.match(/Installed Version: (?<version>.+$)/) windows_version = result.stdout.match(/Installed Version: (?<version>.+$)/)
if windows_version if windows_version
valid = windows_version[:version] == Vagrant::VERSION windows_version = windows_version[:version].strip
valid = windows_version == Vagrant::VERSION
end end
end end
if !valid if !valid
raise Vagrant::Errors::WSLVagrantVersionMismatch, raise Vagrant::Errors::WSLVagrantVersionMismatch,
wsl_version: Vagrant::VERSION, wsl_version: Vagrant::VERSION,
windows_version: windows_version ? windows_version[:version] : "unknown" windows_version: windows_version || "unknown"
end end
end end

View File

@ -1530,6 +1530,14 @@ en:
Windows Vagrant version: %{windows_version} Windows Vagrant version: %{windows_version}
Windows Subsystem for Linux Vagrant version: %{wsl_version} Windows Subsystem for Linux Vagrant version: %{wsl_version}
wsl_vagrant_access_error: |-
Vagrant will not operate outside the Windows Subsystem for Linux unless explicitly
instructed. Due to the inability to enforce expected Linux file ownership and
permissions on the Windows system, Vagrant will not make modifications to prevent
unexpected errors. To learn more about this, and the options that are available,
please refer to the Vagrant documentation:
https://www.vagrantup.com/docs/other/wsl
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Translations for config validation errors # Translations for config validation errors