Merge pull request #5895 from mitchellh/b-uid-track

Track UID used to create machine, error in VB if mismatch
This commit is contained in:
Mitchell Hashimoto 2015-07-06 18:22:05 -06:00
commit c37df3dfb5
5 changed files with 63 additions and 0 deletions

View File

@ -764,6 +764,10 @@ module Vagrant
error_key(:virtualbox_name_exists) error_key(:virtualbox_name_exists)
end end
class VirtualBoxUserMismatch < VagrantError
error_key(:virtualbox_user_mismatch)
end
class VirtualBoxVersionEmpty < VagrantError class VirtualBoxVersionEmpty < VagrantError
error_key(:virtualbox_version_empty) error_key(:virtualbox_version_empty)
end end

View File

@ -279,6 +279,13 @@ module Vagrant
end end
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 we don't have a UUID, then create one
if index_uuid.nil? if index_uuid.nil?
# Create the index entry and save it # Create the index entry and save it
@ -311,6 +318,7 @@ module Vagrant
else else
# Delete the file, since the machine is now destroyed # Delete the file, since the machine is now destroyed
id_file.delete if id_file && id_file.file? 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 # If we have a UUID associated with the index, remove it
uuid = index_uuid uuid = index_uuid
@ -495,6 +503,17 @@ module Vagrant
result result
end 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 # Temporarily changes the machine UI. This is useful if you want
# to execute an {#action} with a different UI. # to execute an {#action} with a different UI.
def with_ui(ui) def with_ui(ui)
@ -508,5 +527,13 @@ module Vagrant
end end
end 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
end end

View File

@ -73,6 +73,15 @@ module VagrantPlugins
# #
# @return [Symbol] # @return [Symbol]
def state 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,
original_uid: uid.to_s,
uid: Process.uid.to_s
end
# Determine the ID of the state here. # Determine the ID of the state here.
state_id = nil state_id = nil
state_id = :not_created if !@driver.uuid state_id = :not_created if !@driver.uuid

View File

@ -1317,6 +1317,14 @@ en:
Vagrant uses the `VBoxManage` binary that ships with VirtualBox, and requires 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 this to be available on the PATH. If VirtualBox is installed, please find the
`VBoxManage` binary and add it to the PATH environmental variable. `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. This is not a Vagrant issue.
The UID used to create the VM was: %{original_uid}
Your UID is: %{uid}
virtualbox_version_empty: |- virtualbox_version_empty: |-
Vagrant detected that VirtualBox appears installed on your system, Vagrant detected that VirtualBox appears installed on your system,
but calls to detect the version are returning empty. This is often but calls to detect the version are returning empty. This is often

View File

@ -424,6 +424,21 @@ describe Vagrant::Machine do
third = new_instance third = new_instance
expect(third.id).to be_nil expect(third.id).to be_nil
end 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 end
describe "#index_uuid" do describe "#index_uuid" do