| 1 | param([Parameter(Mandatory)][string]$Implementation) |
| 2 | $ErrorActionPreference='Stop' |
| 3 | $global:cageStorageVMID=[guid]'2a6f8208-3906-4e16-8233-2b9d28ecf7be' |
| 4 | $old=[pscustomobject]@{configuration="C:\Cage original\config";snapshots="C:\Cage original\snapshots";paging="C:\Cage original\paging"} |
| 5 | $next=[pscustomobject]@{configuration="C:\ProgramData\Cage\测试's\config";snapshots="C:\ProgramData\Cage\测试's\snapshots";paging="C:\ProgramData\Cage\测试's\paging"} |
| 6 | $source="C:\Cage original\system.vhdx" |
| 7 | $destination="D:\Cage 新位置's\system.vhdx" |
| 8 | function Reset-VM { |
| 9 | $global:cageStorageVM=[pscustomobject]@{Id=$global:cageStorageVMID;State='Off';Generation=2;ConfigurationLocation=$old.configuration;SnapshotFileLocation=$old.snapshots;SmartPagingFilePath=$old.paging} |
| 10 | $global:cageStorageDisk=[pscustomobject]@{Path=$source} |
| 11 | $global:cageStorageMoves=0 |
| 12 | $global:cageStorageAttachments=0 |
| 13 | $global:cageStorageFailMove=$false |
| 14 | } |
| 15 | function Get-VM { param([guid]$Id) if($Id -ne $global:cageStorageVMID){throw 'wrong_vm'};return $global:cageStorageVM } |
| 16 | function Get-VMNetworkAdapter { param($VM) if($VM.Id -ne $global:cageStorageVMID){throw 'wrong_vm'} } |
| 17 | function Get-VMFirmware { param($VM) [pscustomobject]@{SecureBoot='On'} } |
| 18 | function Get-VMSecurity { param($VM) [pscustomobject]@{TpmEnabled=$true} } |
| 19 | function Get-VMHardDiskDrive { param($VM) if($VM.Id -ne $global:cageStorageVMID){throw 'wrong_vm'};return $global:cageStorageDisk } |
| 20 | function Move-VMStorage { |
| 21 | param($VM,[string]$VirtualMachinePath,[string]$SnapshotFilePath,[string]$SmartPagingFilePath,[switch]$Confirm) |
| 22 | if($VM.Id -ne $global:cageStorageVMID -or $VM.State -ne 'Off'){throw 'unsafe_move'} |
| 23 | $global:cageStorageMoves++ |
| 24 | if($VirtualMachinePath){$global:cageStorageVM.ConfigurationLocation=$VirtualMachinePath} |
| 25 | if($global:cageStorageFailMove){throw 'injected_metadata_failure'} |
| 26 | if($SnapshotFilePath){$global:cageStorageVM.SnapshotFileLocation=$SnapshotFilePath} |
| 27 | if($SmartPagingFilePath){$global:cageStorageVM.SmartPagingFilePath=$SmartPagingFilePath} |
| 28 | } |
| 29 | function Set-VMHardDiskDrive { |
| 30 | param($VMHardDiskDrive,[string]$Path) |
| 31 | if($global:cageStorageVM.State -notin @('Off','OffCritical') -or $VMHardDiskDrive -ne $global:cageStorageDisk){throw 'unsafe_attachment'} |
| 32 | $global:cageStorageAttachments++ |
| 33 | $global:cageStorageDisk.Path=$Path |
| 34 | $global:cageStorageVM.State='Off' |
| 35 | } |
| 36 | function Invoke-Storage($Target,[string]$Disk) { |
| 37 | $inputData=[pscustomobject]@{vm_id=$global:cageStorageVMID.ToString('D');action='apply';old=$old;next=$next;target=$Target;old_disk=$source;source_disk=$source;new_disk=$destination;disk=$Disk} |
| 38 | & $Implementation | Out-Null |
| 39 | } |
| 40 | Reset-VM |
| 41 | Invoke-Storage $next $source |
| 42 | if($global:cageStorageMoves -ne 1 -or $global:cageStorageAttachments -ne 0){throw 'registration_not_prepared_before_copy'} |
| 43 | Invoke-Storage $next $destination |
| 44 | if($global:cageStorageVM.ConfigurationLocation -cne $next.configuration -or $global:cageStorageVM.SnapshotFileLocation -cne $next.snapshots -or $global:cageStorageVM.SmartPagingFilePath -cne $next.paging -or $global:cageStorageDisk.Path -cne $destination){throw 'incomplete_migration'} |
| 45 | if($global:cageStorageMoves -ne 1 -or $global:cageStorageAttachments -ne 1){throw 'unexpected_mutation_count'} |
| 46 | Invoke-Storage $next $destination |
| 47 | if($global:cageStorageMoves -ne 1 -or $global:cageStorageAttachments -ne 1){throw 'retry_repeated_mutations'} |
| 48 | $global:cageStorageVM.State='OffCritical' |
| 49 | Invoke-Storage $next $source |
| 50 | if($global:cageStorageVM.ConfigurationLocation -cne $next.configuration -or $global:cageStorageVM.SnapshotFileLocation -cne $next.snapshots -or $global:cageStorageDisk.Path -cne $source){throw 'offline_target_rollback_failed'} |
| 51 | |
| 52 | Reset-VM |
| 53 | $global:cageStorageFailMove=$true |
| 54 | $failed=$false |
| 55 | try{Invoke-Storage $next $destination}catch{$failed=$true} |
| 56 | if(!$failed -or $global:cageStorageVM.ConfigurationLocation -cne $next.configuration -or $global:cageStorageAttachments -ne 0){throw 'partial_failure_fixture_invalid'} |
| 57 | $global:cageStorageFailMove=$false |
| 58 | Invoke-Storage $next $source |
| 59 | if($global:cageStorageVM.ConfigurationLocation -cne $next.configuration -or $global:cageStorageDisk.Path -cne $source){throw 'partial_metadata_rollback_failed'} |
| 60 | |
| 61 | foreach($failure in @('running','foreign_disk','foreign_configuration')){ |
| 62 | Reset-VM |
| 63 | if($failure -eq 'running'){$global:cageStorageVM.State='Running'} |
| 64 | if($failure -eq 'foreign_disk'){$global:cageStorageDisk.Path='C:\Another VM\private.vhdx'} |
| 65 | if($failure -eq 'foreign_configuration'){$global:cageStorageVM.ConfigurationLocation='C:\Another VM\config'} |
| 66 | $failed=$false |
| 67 | try{Invoke-Storage $next $destination}catch{$failed=$true} |
| 68 | if(!$failed -or $global:cageStorageMoves -ne 0 -or $global:cageStorageAttachments -ne 0){throw ('unsafe_state_accepted_'+$failure)} |
| 69 | } |
| 70 | Write-Output 'PASS fixed system registration, disk move, idempotence, offline target rollback, partial metadata recovery, running and unrelated VM state rejection' |