From 44d484e2e09bcb4ea6d266c78f09fd5ee0e74bf6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Oct 2015 11:58:47 -0400 Subject: [PATCH] providers/virtualbox: ability to customize linked clone snapshot --- .../providers/virtualbox/action/create_clone.rb | 13 +++++++++++-- .../providers/virtualbox/action/import_master.rb | 14 ++++++++++---- plugins/providers/virtualbox/config.rb | 10 ++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/plugins/providers/virtualbox/action/create_clone.rb b/plugins/providers/virtualbox/action/create_clone.rb index 9cc8f7ed5..a76e23794 100644 --- a/plugins/providers/virtualbox/action/create_clone.rb +++ b/plugins/providers/virtualbox/action/create_clone.rb @@ -12,9 +12,18 @@ module VagrantPlugins def call(env) @logger.info("Creating linked clone from master '#{env[:master_id]}'") - env[:ui].info I18n.t("vagrant.actions.vm.clone.creating", name: env[:machine].box.name) + # Get the snapshot to base the linked clone on. This defaults + # to "base" which is automatically setup with linked clones. + snapshot = "base" + if env[:machine].provider_config.linked_clone_snapshot + snapshot = env[:machine].provider_config.linked_clone_snapshot + end + + # Do the actual clone + env[:ui].info I18n.t( + "vagrant.actions.vm.clone.creating", name: env[:machine].box.name) env[:machine].id = env[:machine].provider.driver.clonevm( - env[:master_id], env[:machine].box.name, "base") do |progress| + env[:master_id], env[:machine].box.name, snapshot) do |progress| env[:ui].clear_line env[:ui].report_progress(progress, 100, false) end diff --git a/plugins/providers/virtualbox/action/import_master.rb b/plugins/providers/virtualbox/action/import_master.rb index 5b9ae7790..985a346ba 100644 --- a/plugins/providers/virtualbox/action/import_master.rb +++ b/plugins/providers/virtualbox/action/import_master.rb @@ -59,10 +59,16 @@ module VagrantPlugins "Imported box #{env[:machine].box.name} as master vm " + "with id #{env[:master_id]}") - @logger.info("Creating base snapshot for master VM.") - env[:machine].provider.driver.create_snapshot(env[:master_id], "base") do |progress| - env[:ui].clear_line - env[:ui].report_progress(progress, 100, false) + if !env[:machine].provider_config.linked_clone_snapshot + snapshots = env[:machine].provider.driver.list_snapshots(env[:master_id]) + if !snapshots.include?("base") + @logger.info("Creating base snapshot for master VM.") + env[:machine].provider.driver.create_snapshot( + env[:master_id], "base") do |progress| + env[:ui].clear_line + env[:ui].report_progress(progress, 100, false) + end + end end @logger.debug("Writing id of master VM '#{env[:master_id]}' to #{master_id_file}") diff --git a/plugins/providers/virtualbox/config.rb b/plugins/providers/virtualbox/config.rb index 12b882b01..d549606ec 100644 --- a/plugins/providers/virtualbox/config.rb +++ b/plugins/providers/virtualbox/config.rb @@ -38,6 +38,14 @@ module VagrantPlugins # @return [Boolean] attr_accessor :linked_clone + # The snapshot to base the linked clone from. If this isn't set + # a snapshot will be made with the name of "base" which will be used. + # + # If this is set, then the snapshot must already exist. + # + # @return [String] + attr_accessor :linked_clone_snapshot + # This should be set to the name of the machine in the VirtualBox # GUI. # @@ -66,6 +74,7 @@ module VagrantPlugins @network_adapters = {} @gui = UNSET_VALUE @linked_clone = UNSET_VALUE + @linked_clone_snapshot = UNSET_VALUE # We require that network adapter 1 is a NAT device. network_adapter(1, :nat) @@ -145,6 +154,7 @@ module VagrantPlugins # Do not create linked clone by default @linked_clone = false if @linked_clone == UNSET_VALUE + @linked_clone_snapshot = nil if @linked_clone_snapshot == UNSET_VALUE # The default name is just nothing, and we default it @name = nil if @name == UNSET_VALUE