diff --git a/lib/vagrant/action/vm/check_guest_additions.rb b/lib/vagrant/action/vm/check_guest_additions.rb index 045d91a39..fb96a5e65 100644 --- a/lib/vagrant/action/vm/check_guest_additions.rb +++ b/lib/vagrant/action/vm/check_guest_additions.rb @@ -12,7 +12,7 @@ module Vagrant def call(env) # Use the raw interface for now, while the virtualbox gem # doesn't support guest properties (due to cross platform issues) - version = env[:vm].driver.guest_additions_version(env[:vm].uuid) + version = env[:vm].driver.read_guest_additions_version if !version env[:ui].warn I18n.t("vagrant.actions.vm.check_guest_additions.not_detected") else diff --git a/lib/vagrant/action/vm/destroy.rb b/lib/vagrant/action/vm/destroy.rb index 271e1451d..2af9b0a18 100644 --- a/lib/vagrant/action/vm/destroy.rb +++ b/lib/vagrant/action/vm/destroy.rb @@ -8,7 +8,7 @@ module Vagrant def call(env) env[:ui].info I18n.t("vagrant.actions.vm.destroy.destroying") - env[:vm].driver.delete(env[:vm].uuid) + env[:vm].driver.delete env[:vm].uuid = nil @app.call(env) diff --git a/lib/vagrant/action/vm/match_mac_address.rb b/lib/vagrant/action/vm/match_mac_address.rb index ad316620a..70da6b2a4 100644 --- a/lib/vagrant/action/vm/match_mac_address.rb +++ b/lib/vagrant/action/vm/match_mac_address.rb @@ -11,8 +11,7 @@ module Vagrant # Create the proc which we want to use to modify the virtual machine env[:ui].info I18n.t("vagrant.actions.vm.match_mac.matching") - env[:vm].driver.set_mac_address(env[:vm].uuid, - env[:vm].config.vm.base_mac) + env[:vm].driver.set_mac_address(env[:vm].config.vm.base_mac) @app.call(env) end diff --git a/lib/vagrant/driver/virtualbox.rb b/lib/vagrant/driver/virtualbox.rb index 71e618676..7313197e2 100644 --- a/lib/vagrant/driver/virtualbox.rb +++ b/lib/vagrant/driver/virtualbox.rb @@ -10,7 +10,9 @@ module Vagrant # The version of virtualbox that is running. attr_reader :version - def initialize + def initialize(uuid) + @uuid = uuid + # Read and assign the version of VirtualBox we know which # specific driver to instantiate. begin @@ -35,21 +37,21 @@ module Vagrant end # This deletes the VM with the given name. - def delete(uuid) - execute("unregistervm", uuid, "--delete") + def delete + execute("unregistervm", @uuid, "--delete") end # This reads the guest additions version for a VM. - def guest_additions_version(uuid) - output = execute("guestproperty", "get", uuid, "/VirtualBox/GuestAdd/Version") + def read_guest_additions_version + output = execute("guestproperty", "get", @uuid, "/VirtualBox/GuestAdd/Version") return $1.to_s if output =~ /^Value: (.+?)$/ return nil end # This reads the state for the given UUID. The state of the VM # will be returned as a symbol. - def read_state(uuid) - output = execute("showvminfo", uuid, "--machinereadable") + def read_state + output = execute("showvminfo", @uuid, "--machinereadable") if output =~ /^name=""$/ return :inaccessible elsif output =~ /^VMState="(.+?)"$/ @@ -60,8 +62,8 @@ module Vagrant end # This sets the MAC address for a network adapter. - def set_mac_address(uuid, mac) - execute("modifyvm", uuid, "--macaddress1", mac) + def set_mac_address(mac) + execute("modifyvm", @uuid, "--macaddress1", mac) end protected diff --git a/lib/vagrant/vm.rb b/lib/vagrant/vm.rb index f6c1595c9..cf17d889d 100644 --- a/lib/vagrant/vm.rb +++ b/lib/vagrant/vm.rb @@ -20,12 +20,13 @@ module Vagrant @env = env @config = config @box = env.boxes.find(config.vm.box) - @driver = Driver::VirtualBox.new - # Look for the VM if it exists + # Load the UUID if its saved. active = env.local_data[:active] || {} @uuid = active[@name.to_s] - # @vm = VirtualBox::VM.find(@uuid) if @uuid + + # Reload ourselves to get the state + reload! # Load the associated guest. load_guest! @@ -78,7 +79,7 @@ module Vagrant # @return [Symbol] def state return :not_created if !@uuid - state = @driver.read_state(@uuid) + state = @driver.read_state return :not_created if !state return state end @@ -96,8 +97,6 @@ module Vagrant # to persist the VM. Otherwise, it will remove itself from the # local data (if it exists). def uuid=(value) - @uuid = value - env.local_data[:active] ||= {} if value env.local_data[:active][name.to_s] = value @@ -108,10 +107,14 @@ module Vagrant # Commit the local data so that the next time vagrant is initialized, # it realizes the VM exists env.local_data.commit + + # Store the uuid and reload the instance + @uuid = value + reload! end def reload! - @vm = VirtualBox::VM.find(@vm.uuid) + @driver = Driver::VirtualBox.new(@uuid) end def package(options=nil)