PortValhalla/profiles/DerGeret/Windows/Setup.ps1

142 lines
5.2 KiB
PowerShell
Raw Normal View History

2023-07-12 20:37:31 +00:00
#!/bin/pwsh
2023-06-21 20:11:38 +00:00
$env:WIN_COMPUTER_NAME = "DerGeret";
$env:SETUP_SCRIPT_NAME = "$PSScriptRoot/Restore.ps1";
2024-03-23 12:45:15 +00:00
function Initialize-SetupConfig() {
param(
[xml] $config,
[System.Xml.XmlNamespaceManager] $namespace
);
2024-03-24 13:43:52 +00:00
$null = New-Module {
2024-03-23 13:00:42 +00:00
<#
.SYNOPSIS
Gets the XML element describing the installation partition ID.
#>
2024-03-24 13:43:17 +00:00
function Get-InstallationPartition {
2024-03-23 13:00:42 +00:00
$setupComponent.SelectSingleNode("./ua:ImageInstall/ua:OSImage/ua:InstallTo/ua:PartitionID", $namespace)
}
2024-03-23 12:45:15 +00:00
<#
.SYNOPSIS
Increases the ID of all partitions in the specified range by 1.
#>
function Move-PartitionRange {
param (
[int]$From = 0,
[System.Nullable[int]]$To = $null,
[int]$By = 1
)
2024-03-23 13:00:42 +00:00
# Update installation partition ID if necessary
$installationPartition = Get-InstallationPartition;
2024-03-23 12:45:15 +00:00
$installPartitionID = [int]$installationPartition.InnerText;
2024-03-23 13:01:25 +00:00
2024-03-23 13:06:30 +00:00
if (($installPartitionID -ge $From) -and (($null -eq $To) -or ($installPartitionID -le $To))) {
2024-03-23 12:45:15 +00:00
$installationPartition.InnerText = "$($installPartitionID + $By)";
}
2024-03-23 13:01:25 +00:00
2024-03-23 13:00:42 +00:00
# Update IDs of all partition creations
2024-03-23 12:45:15 +00:00
foreach ($partition in $partitionCreations) {
$orderNode = $partition.SelectSingleNode("./ua:Order", $namespace);
$order = [int]$orderNode.InnerText;
$newOrder = $order;
2024-03-23 13:01:25 +00:00
2024-03-23 13:06:30 +00:00
if (($newOrder -ge $From) -and (($null -eq $To) -or ($newOrder -le $To))) {
2024-03-23 12:45:15 +00:00
$newOrder += $By;
}
2024-03-23 13:01:25 +00:00
2024-03-23 12:45:15 +00:00
if ($order -ne $newOrder) {
$orderNode.InnerText = "$newOrder";
}
}
2024-03-23 13:01:25 +00:00
2024-03-23 13:00:42 +00:00
# Update IDs of all partition modifications
2024-03-23 12:45:15 +00:00
foreach ($partition in $partitionModifications) {
$partitionNode = $partition.SelectSingleNode("./ua:PartitionID", $namespace);
$partitionID = [int]$partitionNode.InnerText;
$newID = $partitionID;
2024-03-23 13:01:25 +00:00
2024-03-23 13:06:30 +00:00
if (($newID -ge $From) -and (($null -eq $To) -or ($newID -le $To))) {
2024-03-23 12:45:15 +00:00
$newID += $By;
}
2024-03-23 13:01:25 +00:00
2024-03-23 12:45:15 +00:00
if ($partitionID -ne $newID) {
$partitionNode.InnerText = "$newID";
$partition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$newID";
}
}
}
2024-03-23 13:01:25 +00:00
2024-03-23 12:45:15 +00:00
function Add-Partition {
param (
[int]$Index,
[int]$Size,
[string]$Type = "Primary"
)
2024-03-23 13:01:25 +00:00
2024-03-23 12:45:15 +00:00
Move-PartitionRange -From $Index -By 1;
2024-03-23 13:01:25 +00:00
2024-03-23 12:45:15 +00:00
$newPartition = $partitionCreations[0].CloneNode($true);
$newPartition.SelectSingleNode("./ua:Order", $namespace).InnerText = "$Index";
$newPartition.SelectSingleNode("./ua:Type", $namespace).InnerText = "$Type";
$newPartition.SelectSingleNode("./ua:Size", $namespace).InnerText = "$Size";
$null = $partitionCreationContainer.AppendChild($newPartition);
2024-03-23 13:01:25 +00:00
2024-03-23 12:45:15 +00:00
$newModification = $partitionModifications[2].CloneNode($true);
$newModification.SelectSingleNode("./ua:Order", $namespace).InnerText = "$Index";
$newModification.SelectSingleNode("./ua:PartitionID", $namespace).InnerText = "$Index";
$null = $partitionModificationContainer.AppendChild($newModification);
}
2024-03-23 13:01:25 +00:00
2024-03-23 12:45:15 +00:00
<#
.SYNOPSIS
Relocates the partition with the specified `$From` ID to the specified `$To` ID.
#>
function Invoke-PartitionRelocation {
param (
[int]$From,
[int]$To
)
2024-03-23 13:01:25 +00:00
2024-03-23 13:06:30 +00:00
Move-PartitionRange $From $From (-1 * ($From + 1))
2024-03-23 13:01:25 +00:00
2024-03-23 12:45:15 +00:00
if ($From -gt $To) {
2024-03-23 13:06:30 +00:00
Move-PartitionRange $To ($From - 1);
2024-03-23 12:45:15 +00:00
}
elseif ($From -lt $To) {
2024-03-23 13:06:30 +00:00
Move-PartitionRange ($From + 1) $To -1;
2024-03-23 12:45:15 +00:00
}
2024-03-23 13:01:25 +00:00
2024-03-23 13:06:30 +00:00
Move-PartitionRange -1 -1 ($To + 1)
2024-03-23 12:45:15 +00:00
}
}
2023-07-03 10:40:28 +00:00
$setupComponent = $config.SelectSingleNode(
"/ua:unattend/ua:settings[@pass='windowsPE']/ua:component[@name='Microsoft-Windows-Setup']",
$namespace);
2023-07-03 10:40:28 +00:00
$diskConfig = $setupComponent.SelectSingleNode("./ua:DiskConfiguration/ua:Disk", $namespace);
$partitionCreationContainer = $diskConfig.SelectSingleNode("./ua:CreatePartitions", $namespace);
$partitionCreations = $partitionCreationContainer.SelectNodes("./ua:CreatePartition", $namespace);
$partitionModificationContainer = $diskConfig.SelectSingleNode("./ua:ModifyPartitions", $namespace);
$partitionModifications = $partitionModificationContainer.SelectNodes("./ua:ModifyPartition", $namespace);
2024-03-23 12:57:18 +00:00
# Resize EFI partition to 1GB
2023-07-11 21:37:27 +00:00
$partitionCreations[1].SelectSingleNode("./ua:Size", $namespace).InnerText = "$(1024)";
2024-03-23 12:57:18 +00:00
# Swap Windows RE partition (partition #1) and boot partition (partition #2)
Invoke-PartitionRelocation 2 1;
# Add space before Windows installation... wha-!? For Linux, ofc! I use Arch Linux, btw.
2023-07-09 21:34:22 +00:00
$swapSpacer = 100;
Add-Partition 2 $swapSpacer;
2024-03-23 12:57:18 +00:00
# Add a 1.2 TB partition for Linux
2023-07-29 19:14:21 +00:00
Add-Partition 3 ((1.2 * 1024 * 1024) - 1024 - $swapSpacer);
}
2023-07-03 10:47:54 +00:00
. "$PSScriptRoot/../../../scripts/Windows/OS/Setup.ps1";