From 1330244fef0593858cb0c7bd28fbcb16afbec1d8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 6 Jul 2015 18:04:16 -0600 Subject: [PATCH 1/3] core: save the UID that created a machine --- lib/vagrant/machine.rb | 27 +++++++++++++++++++++++++++ test/unit/vagrant/machine_test.rb | 15 +++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index cebd1eea1..b01087be1 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -279,6 +279,13 @@ module Vagrant end end + if uid_file + # Write the user id that created this machine + uid_file.open("w+") do |f| + f.write(Process.uid.to_s) + end + end + # If we don't have a UUID, then create one if index_uuid.nil? # Create the index entry and save it @@ -311,6 +318,7 @@ module Vagrant else # Delete the file, since the machine is now destroyed id_file.delete if id_file && id_file.file? + uid_file.delete if uid_file && uid_file.file? # If we have a UUID associated with the index, remove it uuid = index_uuid @@ -495,6 +503,17 @@ module Vagrant result end + # Returns the user ID that created this machine. This is specific to + # the host machine that this was created on. + # + # @return [String] + def uid + path = uid_file + return nil if !path + return nil if !path.file? + return uid_file.read.chomp + end + # Temporarily changes the machine UI. This is useful if you want # to execute an {#action} with a different UI. def with_ui(ui) @@ -508,5 +527,13 @@ module Vagrant end end end + + protected + + # Returns the path to the file that stores the UID. + def uid_file + return nil if !@data_dir + @data_dir.join("creator_uid") + end end end diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index d146d932e..4064fa1d5 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -424,6 +424,21 @@ describe Vagrant::Machine do third = new_instance expect(third.id).to be_nil end + + it "should set the UID that created the machine" do + instance.id = "foo" + + second = new_instance + expect(second.uid).to eq(Process.uid.to_s) + end + + it "should delete the UID when the id is nil" do + instance.id = "foo" + instance.id = nil + + second = new_instance + expect(second.uid).to be_nil + end end describe "#index_uuid" do From 71940c60ba541e8c5dc69d0380dfca3febd2d389 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 6 Jul 2015 18:13:59 -0600 Subject: [PATCH 2/3] providers/virtualbox: error if uid mismatch --- lib/vagrant/errors.rb | 4 ++++ plugins/providers/virtualbox/provider.rb | 7 +++++++ templates/locales/en.yml | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 02804b5b0..b705b362e 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -764,6 +764,10 @@ module Vagrant error_key(:virtualbox_name_exists) end + class VirtualBoxUserMismatch < VagrantError + error_key(:virtualbox_user_mismatch) + end + class VirtualBoxVersionEmpty < VagrantError error_key(:virtualbox_version_empty) end diff --git a/plugins/providers/virtualbox/provider.rb b/plugins/providers/virtualbox/provider.rb index 9428fe6d7..270286341 100644 --- a/plugins/providers/virtualbox/provider.rb +++ b/plugins/providers/virtualbox/provider.rb @@ -73,6 +73,13 @@ module VagrantPlugins # # @return [Symbol] def state + # We have to check if the UID matches to avoid issues with + # VirtualBox. + uid = @machine.uid + if uid && uid.to_s == Process.uid.to_s + raise Vagrant::Errors::VirtualBoxUserMismatch, uid: uid.to_s + end + # Determine the ID of the state here. state_id = nil state_id = :not_created if !@driver.uuid diff --git a/templates/locales/en.yml b/templates/locales/en.yml index a6abe138c..285960f39 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1317,6 +1317,13 @@ en: Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires this to be available on the PATH. If VirtualBox is installed, please find the `VBoxManage` binary and add it to the PATH environmental variable. + virtualbox_user_mismatch: |- + The VirtualBox VM was created with a user that doesn't match the + current user running Vagrant. VirtualBox requires that the same user + be used to manage the VM that was created. Please re-run Vagrant with + that user. + + The UID used to create the VM was: %{uid} virtualbox_version_empty: |- Vagrant detected that VirtualBox appears installed on your system, but calls to detect the version are returning empty. This is often From cd18eddb1c1bce47317f99e8f98c9cb574488967 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 6 Jul 2015 18:18:13 -0600 Subject: [PATCH 3/3] providers/virtualbox: fix bug, make message clearer --- plugins/providers/virtualbox/provider.rb | 6 ++++-- templates/locales/en.yml | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/providers/virtualbox/provider.rb b/plugins/providers/virtualbox/provider.rb index 270286341..969fb4718 100644 --- a/plugins/providers/virtualbox/provider.rb +++ b/plugins/providers/virtualbox/provider.rb @@ -76,8 +76,10 @@ module VagrantPlugins # We have to check if the UID matches to avoid issues with # VirtualBox. uid = @machine.uid - if uid && uid.to_s == Process.uid.to_s - raise Vagrant::Errors::VirtualBoxUserMismatch, uid: uid.to_s + if uid && uid.to_s != Process.uid.to_s + raise Vagrant::Errors::VirtualBoxUserMismatch, + original_uid: uid.to_s, + uid: Process.uid.to_s end # Determine the ID of the state here. diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 285960f39..fd01e17e9 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1321,9 +1321,10 @@ en: The VirtualBox VM was created with a user that doesn't match the current user running Vagrant. VirtualBox requires that the same user be used to manage the VM that was created. Please re-run Vagrant with - that user. + that user. This is not a Vagrant issue. - The UID used to create the VM was: %{uid} + The UID used to create the VM was: %{original_uid} + Your UID is: %{uid} virtualbox_version_empty: |- Vagrant detected that VirtualBox appears installed on your system, but calls to detect the version are returning empty. This is often