Skip to content

Commit fdd7f63

Browse files
Brett Johnsonfabiendupont
andcommitted
Update for server 2016
Co-authored-by: Fabien Dupont <[email protected]>
1 parent 753b428 commit fdd7f63

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ with WinRM. Please, refer to Ansible documentation:
1717
- [Setting up a Windows Host](https://docs.ansible.com/ansible/latest/user_guide/windows_setup.html)
1818
- [Windows Remote Management](https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html)
1919

20+
The account used to connect to the Windows virtual machine must have permissions to run the script with administrator permissions.
21+
2022
Role Variables
2123
--------------
2224

@@ -44,4 +46,20 @@ configured and will try to install the agent.
4446
ansible_winrm_server_cert_validation: ignore
4547
roles:
4648
- role: fdupont_redhat.ims_premigration_windows
47-
```
49+
```
50+
51+
Pre-Migration Script and virt-v2v First Boot
52+
------------------------------
53+
54+
This section provides a primer on how the script is used as part of the migration process.
55+
56+
The pre-migration script `pre-migrate.ps1` captures
57+
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\`.
58+
59+
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.
60+
61+
Post migration hooks are not required for scripts run as part of the first boot process.
62+
63+
**NOTE:** The first boot directory path is hard coded in virt-v2v, do not change the script paths in the PowerShell file.
64+
65+
**NOTE:** The pre-migration script makes no configuration changes to the VM, only the generated script makes configuration changes.

files/pre-migrate.ps1

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
script runs it. This avoids creating another scheduled task.
3737
3838
.NOTES
39-
Version: 1.0
39+
Version: 1.1
4040
Author: Fabien Dupont <[email protected]>
4141
Purpose/Change: Extract system configuration during premigration
4242
#>
4343

44-
$scriptDir = "C:\Program Files\Guestfs\Firstboot\scripts"
44+
$scriptDir = $env:SystemDrive + "\Program Files\Guestfs\Firstboot\scripts"
4545
$restoreScriptFile = $scriptDir + "\9999-restore_config.ps1"
4646
$firstbootScriptFile = $scriptDir + "\9999-restore_config.bat"
4747

@@ -88,6 +88,7 @@ Get-NetAdapter | ForEach-Object {
8888
Write-Output ("Write-Output (' - Assign the IP addresses and netmask to the Red Hat network adapter') >> `$logFile") >> $restoreScriptFile
8989
Get-NetIPAddress -InterfaceIndex $_.InterfaceIndex | Where-Object { $_.PrefixOrigin -like "Manual" -or $_.SuffixOrigin -like "Manual"} | ForEach-Object {
9090
Write-Output ("Write-Output (' - IP address: " + $_.IPaddress + " - PrefixLength: " + $_.PrefixLength + "') >> `$logFile") >> $restoreScriptFile
91+
Write-Output( "Remove-NetIPAddress " + $_.IPAddress + " -confirm:`$false") >> $restoreScriptFile
9192
Write-Output( "New-NetIPAddress -InterfaceIndex `$ifi -IPAddress '" + $_.IPAddress + "' -Prefixlength " + $_.PrefixLength) >> $restoreScriptFile
9293
}
9394
Write-Output ("") >> $restoreScriptFile
@@ -124,9 +125,10 @@ Write-Output ("") >> $restoreScriptFile
124125
# Generate the script section to remove the access path on all partitions
125126
# but SystemDrive
126127
Write-Output ("# Remove the access path on all partitions but SystemDrive") >> $restoreScriptFile
128+
$a = (Get-Item env:SystemDrive).Value.substring(0,1)
127129
Write-Output ("`$a = (Get-Item env:SystemDrive).Value.substring(0,1)") >> $restoreScriptFile
128130
Write-Output ("Write-Output('Remove the partition access path on all partitions but SystemDrive (" + $a +")') >> `$logFile") >> $restoreScriptFile
129-
Write-Output ("Get-Partition | Where { `$_.DriveLetter -notlike `$a -and `$_.DriveLetter.length -gt 0 } | % {") >> $restoreScriptFile
131+
Write-Output ("Get-Partition | Where { `$_.DriveLetter -notlike `$a -and `$_.DriveLetter -ne [char] `"``0`" } | % {") >> $restoreScriptFile
130132
Write-Output (" if ([string]::IsNullOrWhiteSpace(`$_.DriveLetter)) {") >> $restoreScriptFile
131133
Write-Output (" Write-Output (' - DiskNumber: ' + `$_.DiskNumber + ' - PartitionNumber: ' + `$_.PartitionNumber + ' - No AccessPath. Skipping') >> `$logFile") >> $restoreScriptFile
132134
Write-Output (" }") >> $restoreScriptFile
@@ -139,18 +141,23 @@ Write-Output ("Write-Output ('') >> `$logFile") >> $restoreScriptFile
139141
Write-Output ("") >> $restoreScriptFile
140142

141143
# Gnerate the script section to restore the drive letters
144+
# DriveType 3 = Local disk
145+
# Information about over drive types can be found here https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/aa394515(v=vs.85)
142146
Write-Output ("# Restore drive letters") >> $restoreScriptFile
143147
Write-Output ("Write-Output ('Restore drive letters') >> `$logFile") >> $restoreScriptFile
144-
Get-Volume | ForEach-Object {
145-
if ($_.FileSystemLabel -ne "System Reserved") {
148+
Get-WmiObject -Class Win32_Volume | ForEach-Object {
149+
if ($_.Label -ne "System Reserved" -and $_.DriveType -eq 3) {
146150
if ([string]::IsNullOrWhiteSpace($_.DriveLetter)) {
147-
Write-Output ("Write-Output (' - DeviceId: " + $_.ObjectId + " - No DriveLetter. Skipping.') >> `$logFile") >> $restoreScriptFile
151+
Write-Output ("Write-Output (' - DeviceId: " + $_.deviceID + " - No DriveLetter. Skipping.') >> `$logFile") >> $restoreScriptFile
152+
}
153+
elseif ($_.DriveLetter -eq $env:SystemDrive){
154+
Write-Output ("Write-Output (' - DeviceId: " + $_.deviceID + " - Is system drive. Skipping.') >> `$logFile") >> $restoreScriptFile
148155
}
149156
else {
150-
Write-Output ("Write-Output (' - DeviceId: " + $_.ObjectId + " - DriveLetter: " + $_.DriveLetter + ":') >> `$logFile") >> $restoreScriptFile
151-
$escObjectId = $_.ObjectId -replace "\\", "\\"
152-
Write-Output ("`$wmiObject = Get-WmiObject -Class Win32_Volume -Filter `"DeviceId='" + $escObjectId + "'`"") >> $restoreScriptFile
153-
Write-Output ("`$wmiObject.DriveLetter = '" + $_.DriveLetter + ":'") >> $restoreScriptFile
157+
Write-Output ("Write-Output (' - DeviceId: " + $_.deviceID + " - DriveLetter: " + $_.DriveLetter + "') >> `$logFile") >> $restoreScriptFile
158+
$escDeviceID = $_.deviceID -replace "\\", "\\"
159+
Write-Output ("`$wmiObject = Get-WmiObject -Class Win32_Volume -Filter `"DeviceId='" + $escDeviceID + "'`"") >> $restoreScriptFile
160+
Write-Output ("`$wmiObject.DriveLetter = '" + $_.DriveLetter + "'") >> $restoreScriptFile
154161
Write-Output ("`$wmiObject.Put()") >> $restoreScriptFile
155162
Write-Output ("") >> $restoreScriptFile
156163
}

0 commit comments

Comments
 (0)