From e8e38a400785b38b459b332196e99606f957587d Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 8 May 2017 15:21:39 -0700 Subject: [PATCH] Halt Vagrant if within WSL and attempting to operate out of rootfs --- lib/vagrant/errors.rb | 4 ++++ lib/vagrant/util/platform.rb | 40 +++++++++++++++++++++--------------- templates/locales/en.yml | 8 ++++++++ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index b822644ea..d8d4ba44b 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -895,5 +895,9 @@ module Vagrant class WSLVagrantVersionMismatch < VagrantError error_key(:wsl_vagrant_version_mismatch) end + + class WSLVagrantAccessError < VagrantError + error_key(:wsl_vagrant_access_error) + end end end diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index 4b744c3e7..3e8d41057 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -290,22 +290,29 @@ module Vagrant # simple setup to allow sharing of the user's VAGRANT_HOME directory # within the subsystem # + # @param [Environment] env # @param [Logger] logger Optional logger to display information - def wsl_init(logger=nil) - if wsl? && ENV["VAGRANT_WSL_ACCESS_WINDOWS_USER"] - wsl_validate_matching_vagrant_versions! - shared_user = ENV["VAGRANT_WSL_ACCESS_WINDOWS_USER"] - if logger - 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") + def wsl_init(env, logger=nil) + if wsl? + if ENV["VAGRANT_WSL_ACCESS_WINDOWS_USER"] + wsl_validate_matching_vagrant_versions! + shared_user = ENV["VAGRANT_WSL_ACCESS_WINDOWS_USER"] 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 @@ -319,13 +326,14 @@ module Vagrant if result.exit_code == 0 windows_version = result.stdout.match(/Installed Version: (?.+$)/) if windows_version - valid = windows_version[:version] == Vagrant::VERSION + windows_version = windows_version[:version].strip + valid = windows_version == Vagrant::VERSION end end if !valid raise Vagrant::Errors::WSLVagrantVersionMismatch, wsl_version: Vagrant::VERSION, - windows_version: windows_version ? windows_version[:version] : "unknown" + windows_version: windows_version || "unknown" end end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 8c08135f9..be8e38c75 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1530,6 +1530,14 @@ en: Windows Vagrant version: %{windows_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