diff --git a/lib/vagrant-persistent-storage/action/create_storage.rb b/lib/vagrant-persistent-storage/action/create_storage.rb index 7c55716..7f4b223 100644 --- a/lib/vagrant-persistent-storage/action/create_storage.rb +++ b/lib/vagrant-persistent-storage/action/create_storage.rb @@ -24,15 +24,21 @@ def call(env) return @app.call(env) unless env[:machine].config.persistent_storage.enabled? # check config to see if the disk should be created - return @app.call(env) unless @machine.config.persistent_storage.create? - @logger.info '** Creating Persistent Storage **' - - env[:ui].info I18n.t("vagrant_persistent_storage.action.create_storage") - location = env[:machine].config.persistent_storage.location - size = env[:machine].config.persistent_storage.size - env[:machine].provider.driver.create_storage(location, size) - - @app.call(env) + return @app.call(env) unless env[:machine].config.persistent_storage.create? + + if File.exists?(env[:machine].config.persistent_storage.location) + @logger.info '** Persistent Storage Volume exists, not creating **' + env[:ui].info I18n.t("vagrant_persistent_storage.action.not_creating") + @app.call(env) + + else + @logger.info '** Creating Persistent Storage **' + env[:ui].info I18n.t("vagrant_persistent_storage.action.create_storage") + location = env[:machine].config.persistent_storage.location + size = env[:machine].config.persistent_storage.size + env[:machine].provider.driver.create_storage(location, size) + @app.call(env) + end end diff --git a/lib/vagrant-persistent-storage/action/detach_storage.rb b/lib/vagrant-persistent-storage/action/detach_storage.rb index 82637d3..2c0e650 100644 --- a/lib/vagrant-persistent-storage/action/detach_storage.rb +++ b/lib/vagrant-persistent-storage/action/detach_storage.rb @@ -14,8 +14,8 @@ def initialize(app, env) end def call(env) - # skip if machine is not running and the action is destroy, halt or suspend - return @app.call(env) if @machine.state.id != :running && [:destroy, :halt, :suspend].include?(env[:machine_action]) + # skip if machine is not running and the action is halt or suspend + return @app.call(env) if @machine.state.id != :running && [:halt, :suspend].include?(env[:machine_action]) # skip if machine is not saved and the action is resume return @app.call(env) if @machine.state.id != :saved && env[:machine_action] == :resume # skip if machine is not running and the action is suspend diff --git a/lib/vagrant-persistent-storage/manage_storage.rb b/lib/vagrant-persistent-storage/manage_storage.rb index aba3d3c..635f85b 100644 --- a/lib/vagrant-persistent-storage/manage_storage.rb +++ b/lib/vagrant-persistent-storage/manage_storage.rb @@ -51,7 +51,7 @@ def populate_template(m) <% if format == true %> # Create the filesytem if it doesn't already exist -[[ `blkid | grep #{device}` ]] || mkfs.#{fs_type} #{device} +[[ `blkid | grep #{mnt_name} | grep #{fs_type}` ]] || mkfs.#{fs_type} #{device} echo "#{fs_type} creation return: $?" >> disk_operation_log.txt <% if mount == true %> # Create mountpoint #{mnt_point} diff --git a/lib/vagrant-persistent-storage/plugin.rb b/lib/vagrant-persistent-storage/plugin.rb index 0dd31de..bca47f7 100644 --- a/lib/vagrant-persistent-storage/plugin.rb +++ b/lib/vagrant-persistent-storage/plugin.rb @@ -40,7 +40,10 @@ class Plugin < Vagrant.plugin('2') end action_hook(:persistent_storage, :machine_action_destroy) do |hook| - hook.prepend(VagrantPlugins::PersistentStorage::Action.detach_storage) + hook.after VagrantPlugins::ProviderVirtualBox::Action::action_halt, + VagrantPlugins::PersistentStorage::Action.detach_storage + hook.before VagrantPlugins::ProviderVirtualBox::Action::Destroy, + VagrantPlugins::PersistentStorage::Action.detach_storage end end diff --git a/lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb b/lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb index c730922..5ad9764 100644 --- a/lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb +++ b/lib/vagrant-persistent-storage/providers/virtualbox/driver/base.rb @@ -8,15 +8,11 @@ def create_adapter end def create_storage(location, size) - if ! File.exists?(location) - execute("createhd", "--filename", location, "--size", size) - end + execute("createhd", "--filename", location, "--size", size) end def attach_storage(location) -# if location != 0 and read_persistent_storage(location) == location execute("storageattach", @uuid, "--storagectl", "SATA Controller", "--port", "1", "--device", "0", "--type", "hdd", "--medium", "#{location}") -# end end def detach_storage(location) diff --git a/locales/en.yml b/locales/en.yml index ca4a449..b64bd7f 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -3,6 +3,7 @@ en: action: create_adapter: "** Creating adapter for persistent storage **" create_storage: "** Creating persistent storage **" + not_creating: "** Persistent Storage Volume exists, not creating **" attach_storage: "** Attaching persistent storage **" detach_storage: "** Detaching persistent storage **" manage_storage: "** Managing persistent storage **"