diff --git a/README.md b/README.md index b84facc..b4b6b23 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ with WinRM. Please, refer to Ansible documentation: - [Setting up a Windows Host](https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html) - [Windows Remote Management](https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html) +The account used to connect to the Windows virtual machine must have permissions to run the script with administrator permissions. + Role Variables -------------- @@ -44,4 +46,20 @@ configured and will try to install the agent. ansible_winrm_server_cert_validation: ignore roles: - role: fdupont_redhat.ims_premigration_windows -``` \ No newline at end of file +``` + +Pre-Migration Script and virt-v2v First Boot +------------------------------ + +This section provides a primer on how the script is used as part of the migration process. + +The pre-migration script `pre-migrate.ps1` captures +information about the VMs network and disk configuration and generates a powershell script to reapply the configuration after migration to OCP. The generated script is written to following path on the VM's system drive `\Program Files\Guestfs\Firstboot\scripts\`. + +After the VM has been migrated to OCP virt-v2v runs a series of tasks as part of a process called `first boot`. Part of process is executing scripts stored in above mentioned scripts directory. + +Post migration hooks are not required for scripts run as part of the first boot process. + +**NOTE:** The first boot directory path is hard coded in virt-v2v, do not change the script paths in the PowerShell file. + +**NOTE:** The pre-migration script makes no configuration changes to the VM, only the generated script makes configuration changes. diff --git a/files/pre-migrate.ps1 b/files/pre-migrate.ps1 index 2e225ae..a6a548f 100644 --- a/files/pre-migrate.ps1 +++ b/files/pre-migrate.ps1 @@ -36,12 +36,12 @@ script runs it. This avoids creating another scheduled task. .NOTES - Version: 1.0 + Version: 1.1 Author: Fabien Dupont Purpose/Change: Extract system configuration during premigration #> -$scriptDir = "C:\Program Files\Guestfs\Firstboot\scripts" +$scriptDir = $env:SystemDrive + "\Program Files\Guestfs\Firstboot\scripts" $restoreScriptFile = $scriptDir + "\9999-restore_config.ps1" $firstbootScriptFile = $scriptDir + "\9999-restore_config.bat" @@ -88,6 +88,7 @@ Get-NetAdapter | ForEach-Object { Write-Output ("Write-Output (' - Assign the IP addresses and netmask to the Red Hat network adapter') >> `$logFile") >> $restoreScriptFile Get-NetIPAddress -InterfaceIndex $_.InterfaceIndex | Where-Object { $_.PrefixOrigin -like "Manual" -or $_.SuffixOrigin -like "Manual"} | ForEach-Object { Write-Output ("Write-Output (' - IP address: " + $_.IPaddress + " - PrefixLength: " + $_.PrefixLength + "') >> `$logFile") >> $restoreScriptFile + Write-Output( "Remove-NetIPAddress " + $_.IPAddress + " -confirm:`$false") >> $restoreScriptFile Write-Output( "New-NetIPAddress -InterfaceIndex `$ifi -IPAddress '" + $_.IPAddress + "' -Prefixlength " + $_.PrefixLength) >> $restoreScriptFile } Write-Output ("") >> $restoreScriptFile @@ -124,9 +125,10 @@ Write-Output ("") >> $restoreScriptFile # Generate the script section to remove the access path on all partitions # but SystemDrive Write-Output ("# Remove the access path on all partitions but SystemDrive") >> $restoreScriptFile +$a = (Get-Item env:SystemDrive).Value.substring(0,1) Write-Output ("`$a = (Get-Item env:SystemDrive).Value.substring(0,1)") >> $restoreScriptFile Write-Output ("Write-Output('Remove the partition access path on all partitions but SystemDrive (" + $a +")') >> `$logFile") >> $restoreScriptFile -Write-Output ("Get-Partition | Where { `$_.DriveLetter -notlike `$a -and `$_.DriveLetter.length -gt 0 } | % {") >> $restoreScriptFile +Write-Output ("Get-Partition | Where { `$_.DriveLetter -notlike `$a -and `$_.DriveLetter -ne [char] `"``0`" } | % {") >> $restoreScriptFile Write-Output (" if ([string]::IsNullOrWhiteSpace(`$_.DriveLetter)) {") >> $restoreScriptFile Write-Output (" Write-Output (' - DiskNumber: ' + `$_.DiskNumber + ' - PartitionNumber: ' + `$_.PartitionNumber + ' - No AccessPath. Skipping') >> `$logFile") >> $restoreScriptFile Write-Output (" }") >> $restoreScriptFile @@ -139,18 +141,23 @@ Write-Output ("Write-Output ('') >> `$logFile") >> $restoreScriptFile Write-Output ("") >> $restoreScriptFile # Gnerate the script section to restore the drive letters +# DriveType 3 = Local disk +# Information about over drive types can be found here https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa394515(v=vs.85) Write-Output ("# Restore drive letters") >> $restoreScriptFile Write-Output ("Write-Output ('Restore drive letters') >> `$logFile") >> $restoreScriptFile -Get-Volume | ForEach-Object { - if ($_.FileSystemLabel -ne "System Reserved") { +Get-WmiObject -Class Win32_Volume | ForEach-Object { + if ($_.Label -ne "System Reserved" -and $_.DriveType -eq 3) { if ([string]::IsNullOrWhiteSpace($_.DriveLetter)) { - Write-Output ("Write-Output (' - DeviceId: " + $_.ObjectId + " - No DriveLetter. Skipping.') >> `$logFile") >> $restoreScriptFile + Write-Output ("Write-Output (' - DeviceId: " + $_.deviceID + " - No DriveLetter. Skipping.') >> `$logFile") >> $restoreScriptFile + } + elseif ($_.DriveLetter -eq $env:SystemDrive){ + Write-Output ("Write-Output (' - DeviceId: " + $_.deviceID + " - Is system drive. Skipping.') >> `$logFile") >> $restoreScriptFile } else { - Write-Output ("Write-Output (' - DeviceId: " + $_.ObjectId + " - DriveLetter: " + $_.DriveLetter + ":') >> `$logFile") >> $restoreScriptFile - $escObjectId = $_.ObjectId -replace "\\", "\\" - Write-Output ("`$wmiObject = Get-WmiObject -Class Win32_Volume -Filter `"DeviceId='" + $escObjectId + "'`"") >> $restoreScriptFile - Write-Output ("`$wmiObject.DriveLetter = '" + $_.DriveLetter + ":'") >> $restoreScriptFile + Write-Output ("Write-Output (' - DeviceId: " + $_.deviceID + " - DriveLetter: " + $_.DriveLetter + "') >> `$logFile") >> $restoreScriptFile + $escDeviceID = $_.deviceID -replace "\\", "\\" + Write-Output ("`$wmiObject = Get-WmiObject -Class Win32_Volume -Filter `"DeviceId='" + $escDeviceID + "'`"") >> $restoreScriptFile + Write-Output ("`$wmiObject.DriveLetter = '" + $_.DriveLetter + "'") >> $restoreScriptFile Write-Output ("`$wmiObject.Put()") >> $restoreScriptFile Write-Output ("") >> $restoreScriptFile }