diff --git a/plugins/providers/hyperv/action.rb b/plugins/providers/hyperv/action.rb index 05d058b35..c7e1fb939 100644 --- a/plugins/providers/hyperv/action.rb +++ b/plugins/providers/hyperv/action.rb @@ -139,17 +139,23 @@ module VagrantPlugins next end - b2.use Provision - b2.use Configure - b2.use SetName - b2.use NetSetVLan - b2.use NetSetMac - b2.use StartInstance - b2.use WaitForIPAddress - b2.use WaitForCommunicator, [:running] - b2.use SyncedFolderCleanup - b2.use SyncedFolders - b2.use SetHostname + b2.use Call, IsState, :saved do |env3, b3| + # When state is `:saved` it is a snapshot being restored + if !env3[:result] + b3.use Provision + b3.use Configure + b3.use SetName + b3.use NetSetVLan + b3.use NetSetMac + end + + b3.use StartInstance + b3.use WaitForIPAddress + b3.use WaitForCommunicator, [:running] + b3.use SyncedFolderCleanup + b3.use SyncedFolders + b3.use SetHostname + end end end end diff --git a/plugins/providers/hyperv/action/configure.rb b/plugins/providers/hyperv/action/configure.rb index 30831ac12..f25737f8a 100644 --- a/plugins/providers/hyperv/action/configure.rb +++ b/plugins/providers/hyperv/action/configure.rb @@ -70,6 +70,7 @@ module VagrantPlugins "AutoStartAction" => env[:machine].provider_config.auto_start_action, "AutoStopAction" => env[:machine].provider_config.auto_stop_action, "EnableCheckpoints" => env[:machine].provider_config.enable_checkpoints, + "EnableAutomaticCheckpoints" => env[:machine].provider_config.enable_automatic_checkpoints, "VirtualizationExtensions" => !!env[:machine].provider_config.enable_virtualization_extensions, } options.delete_if{|_,v| v.nil? } diff --git a/plugins/providers/hyperv/config.rb b/plugins/providers/hyperv/config.rb index 3403ef682..96014258c 100644 --- a/plugins/providers/hyperv/config.rb +++ b/plugins/providers/hyperv/config.rb @@ -40,8 +40,10 @@ module VagrantPlugins attr_accessor :auto_start_action # @return [String] Automatic action on stop of host. Default: ShutDown (ShutDown, TurnOff, Save) attr_accessor :auto_stop_action - # @return [Boolean] Enable automatic checkpoints. Default: false + # @return [Boolean] Enable checkpoints. Default: true attr_accessor :enable_checkpoints + # @return [Boolean] Enable automatic checkpoints. Default: false + attr_accessor :enable_automatic_checkpoints # @return [Boolean] Enable virtualization extensions attr_accessor :enable_virtualization_extensions # @return [Hash] Options for VMServiceIntegration @@ -60,6 +62,7 @@ module VagrantPlugins @auto_start_action = UNSET_VALUE @auto_stop_action = UNSET_VALUE @enable_virtualization_extensions = UNSET_VALUE + @enable_automatic_checkpoints = UNSET_VALUE @enable_checkpoints = UNSET_VALUE @vm_integration_services = {} end @@ -85,11 +88,20 @@ module VagrantPlugins @auto_start_action = "Nothing" if @auto_start_action == UNSET_VALUE @auto_stop_action = "ShutDown" if @auto_stop_action == UNSET_VALUE @enable_virtualization_extensions = false if @enable_virtualization_extensions == UNSET_VALUE + + if @enable_automatic_checkpoints == UNSET_VALUE + @enable_automatic_checkpoints = false + else + @enable_automatic_checkpoints = !!@enable_automatic_checkpoints + end if @enable_checkpoints == UNSET_VALUE - @enable_checkpoints = false + @enable_checkpoints = true else @enable_checkpoints = !!@enable_checkpoints end + + # If automatic checkpoints are enabled, checkpoints will automatically be enabled + @enable_checkpoints ||= @enable_automatic_checkpoints end def validate(machine) diff --git a/plugins/providers/hyperv/scripts/configure_vm.ps1 b/plugins/providers/hyperv/scripts/configure_vm.ps1 index ff2b8ca3b..54f878f25 100644 --- a/plugins/providers/hyperv/scripts/configure_vm.ps1 +++ b/plugins/providers/hyperv/scripts/configure_vm.ps1 @@ -18,7 +18,9 @@ param( [parameter (Mandatory=$false)] [switch] $VirtualizationExtensions, [parameter (Mandatory=$false)] - [switch] $EnableCheckpoints + [switch] $EnableCheckpoints, + [parameter (Mandatory=$false)] + [switch] $EnableAutomaticCheckpoints ) $ErrorActionPreference = "Stop" @@ -76,6 +78,7 @@ if($SwitchID) { Set-VagrantVMSwitch -VM $VM -SwitchName $SwitchName } catch { Write-ErrorMessage "Failed to configure network adapter: ${PSItem}" + exit 1 } } @@ -93,3 +96,18 @@ try { Write-ErrorMessage "Failed to ${CheckpointAction} checkpoints on VM: ${PSItem}" exit 1 } + +if($EnableAutomaticCheckpoints) { + $autochecks = 1 + $AutoAction = "enabled" +} else { + $autochecks = 0 + $AutoAction = "disable" +} + +try { + Hyper-V\Set-VM -VM $VM -AutomaticCheckpointsEnabled $autochecks +} catch { + Write-ErrorMessage "Failed to ${AutoAction} automatic checkpoints on VM: ${PSItem}" + exit 1 +} diff --git a/plugins/providers/hyperv/scripts/create_snapshot.ps1 b/plugins/providers/hyperv/scripts/create_snapshot.ps1 index 4a61acf68..8b98f1c6e 100644 --- a/plugins/providers/hyperv/scripts/create_snapshot.ps1 +++ b/plugins/providers/hyperv/scripts/create_snapshot.ps1 @@ -10,7 +10,14 @@ $ErrorActionPreference = "Stop" try { $VM = Hyper-V\Get-VM -Id $VmId + $ChkPnt = $VM.CheckpointType + if($ChkPnt -eq "Disabled") { + Hyper-V\Set-VM -VM $VM -CheckpointType "Standard" + } Hyper-V\Checkpoint-VM $VM -SnapshotName $SnapName + if($ChkPnt -eq "Disabled") { + Hyper-V\Set-VM -VM $VM -CheckpointType "Disabled" + } } catch { Write-ErrorMessage "Failed to create snapshot: ${PSItem}" exit 1 diff --git a/test/unit/plugins/providers/hyperv/action/configure_test.rb b/test/unit/plugins/providers/hyperv/action/configure_test.rb index 9b2d8222b..504e416ef 100644 --- a/test/unit/plugins/providers/hyperv/action/configure_test.rb +++ b/test/unit/plugins/providers/hyperv/action/configure_test.rb @@ -26,6 +26,7 @@ describe VagrantPlugins::HyperV::Action::Configure do auto_start_action: "Nothing", auto_stop_action: "Save", enable_checkpoints: false, + enable_automatic_checkpoints: true, enable_virtualization_extensions: false, vm_integration_services: vm_integration_services ) diff --git a/test/unit/plugins/providers/hyperv/config_test.rb b/test/unit/plugins/providers/hyperv/config_test.rb index 23328cf3b..4b57a508a 100644 --- a/test/unit/plugins/providers/hyperv/config_test.rb +++ b/test/unit/plugins/providers/hyperv/config_test.rb @@ -163,11 +163,31 @@ describe VagrantPlugins::HyperV::Config do end describe "#enable_checkpoints" do - it "is false by default" do + it "is true by default" do + subject.finalize! + expect(subject.enable_checkpoints).to eq(true) + end + + it "can be set" do + subject.enable_checkpoints = false subject.finalize! expect(subject.enable_checkpoints).to eq(false) end + it "is enabled automatically when enable_automatic_checkpoints is enabled" do + subject.enable_checkpoints = false + subject.enable_automatic_checkpoints = true + subject.finalize! + expect(subject.enable_checkpoints).to eq(true) + end + end + + describe "#enable_automatic_checkpoints" do + it "is false by default" do + subject.finalize! + expect(subject.enable_automatic_checkpoints).to eq(false) + end + it "can be set" do subject.enable_checkpoints = true subject.finalize!