Skip to content

Commit 7ccd8b8

Browse files
committed
Update scripts to include logging, remove python requirement
Uses OCE-Build to bootstrap python environment
1 parent e50fb6d commit 7ccd8b8

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

docs/README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,6 @@ Refer to [HyperV-versions.md](https://github.com/acidanthera/MacHyperVSupport/bl
350350

351351
## ✨ Getting Started
352352

353-
> [!IMPORTANT]
354-
> This project requires Python 3 to be installed. You can download the latest version of Python from the [official website](https://www.python.org/downloads/) or from the [Microsoft Store](https://apps.microsoft.com/search/publisher?name=Python+Software+Foundation&hl=en-us&gl=US).
355-
>
356-
> After installing Python, you can check if it's installed correctly by running the below command in PowerShell:
357-
> ```ps
358-
> python --version
359-
> ```
360-
361353
If you opt to use one of the pre-built releases from this repository, you can skip to [2. Configure OpenCore for your hardware](#2-configure-opencore-for-your-hardware) to setup OpenCore for your specific CPU, and then proceed to [4. Setting up Hyper-V](#4-setting-up-hyper-v) to create a new virtual machine.
362354

363355
Those who wish to build this project from source can follow the below steps to clone this repository, build the EFI, and setup Hyper-V.

scripts/lib/convert-efi-disk.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@ param (
1111
[string]$path = "$($pwd)\EFI",
1212
[string]$dest = "$($pwd)\EFI.vhdx"
1313
)
14+
$ErrorActionPreference = 'Stop'
1415

1516
# Prompt for Administrator priviledges
1617
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
17-
Start-Process powershell.exe -Verb RunAs -ArgumentList ("-noprofile -file `"{0}`" -elevated -pwd $pwd -path $path -dest $dest" -f ($myinvocation.MyCommand.Definition));
18+
Start-Process powershell.exe -Verb RunAs -ArgumentList ("-NoExit -noprofile -file `"{0}`" -elevated -pwd $pwd -name $name -version $version -cpu $cpu -ram $ram -size $size -outdir $outdir" -f ($myinvocation.MyCommand.Definition));
1819
exit;
1920
}
2021

22+
# Log a warning if the Microsoft-Hyper-V feature is not enabled
23+
if (-not (Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -ErrorAction SilentlyContinue).State -eq 'Enabled') {
24+
Write-Warning "Microsoft-Hyper-V feature is not enabled. " +
25+
"Please enable it in Windows Features before creating a new VM."
26+
}
27+
2128

2229
# Create and mount a new EFI.vhdx disk
30+
Write-Host "Creating and mounting EFI VHDX disk at $dest..."
2331
$efiDisk = New-VHD -Path "$dest" -Dynamic -SizeBytes 5GB |
2432
Mount-VHD -Passthru |
2533
Initialize-Disk -PartitionStyle "GPT" -Confirm:$false -Passthru |
@@ -35,6 +43,11 @@ if (Test-Path -Path $scriptsDir) {
3543
# Only copy shell scripts (.sh) intended for post-install
3644
$postInstallScripts = Get-ChildItem -Path $scriptsDir -Filter "*.sh" -Recurse
3745
if ($postInstallScripts) {
46+
# Create Scripts directory on the VHDX disk
47+
New-Item -Path "$($efiDisk.DriveLetter):\Scripts" -ItemType Directory -Force | Out-Null
48+
49+
# Copy each script to the Scripts directory on the VHDX disk
50+
Write-Host "Copying post-install scripts to $($efiDisk.DriveLetter):\Scripts"
3851
foreach ($script in $postInstallScripts) {
3952
$destinationPath = "$($efiDisk.DriveLetter):\Scripts\$($script.Name)"
4053
Copy-Item -Path $script.FullName -Destination $destinationPath -Force
@@ -57,12 +70,16 @@ if (Test-Path -Path $toolsDir) {
5770
# Copy macOS recovery image if present
5871
$recoveryImage = "com.apple.recovery.boot"
5972
if (Test-Path -Path "$($pwd)\$recoveryImage") {
73+
Write-Host "Copying macOS recovery image to EFI disk..."
6074
Copy-Item -Path "$($pwd)\$recoveryImage" -Recurse -Destination "$($efiDisk.DriveLetter):\$recoveryImage"
6175
}
6276

6377
# Unmount VHDX disk
78+
Write-Host "Unmounting EFI VHDX disk..."
6479
Dismount-VHD -Path "$dest" | Out-Null
6580

81+
Write-Host "Optimizing EFI VHDX disk..."
82+
6683
# Optimize the VHDX disk to compress the image
6784
Mount-VHD -Path "$dest" -ReadOnly | Out-Null
6885
Optimize-VHD -Path "$dest" -Mode Full

scripts/lib/create-macos-recovery.ps1

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ param (
1212
[string]$version = "latest",
1313
[string]$outdir = "$($pwd)\com.apple.recovery.boot"
1414
)
15-
16-
# Check if python is installed, otherwise fail the script immediately.
17-
if (-not (Get-Command python.exe -ErrorAction SilentlyContinue)) {
18-
Write-Error "Python is not installed. Please install Python 3.8 or later."
19-
exit 1
20-
}
15+
$ErrorActionPreference = 'Stop'
2116

2217
# Create `outdir` if it doesn't exist
2318
New-Item -ItemType Directory "$outdir" -Force | Out-Null
@@ -42,4 +37,13 @@ switch ($version)
4237
}
4338

4439
# Run macrecovery.py script
45-
& python.exe "$macrecovery" -b "$b" -m "$m" -o "$outdir" download
40+
# & python.exe "$macrecovery" -b "$b" -m "$m" -o "$outdir" download
41+
icm `
42+
-ScriptBlock $([Scriptblock]::Create($(iwr 'https://raw.githubusercontent.com/Qonfused/OCE-Build/main/ci/bootstrap.ps1'))) `
43+
-ArgumentList (@("-e $macrecovery -b $b -m $m -o $outdir download"))
44+
45+
# Check if the outdir was created
46+
if (-not (Test-Path -Path $outdir)) {
47+
Write-Error "Failed to create recovery directory at $outdir. Please check the script and try again."
48+
exit 1
49+
}

scripts/lib/create-virtual-machine.ps1

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,38 @@ param (
1515
[int]$size = 50, #
1616
[string]$outdir = "$env:USERPROFILE\Documents\Hyper-V"
1717
)
18+
$ErrorActionPreference = 'Stop'
1819

1920
# Prompt for Administrator priviledges
2021
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
21-
Start-Process powershell.exe -Verb RunAs -ArgumentList ("-noprofile -file `"{0}`" -elevated -pwd $pwd -name $name -version $version -cpu $cpu -ram $ram -size $size -outdir $outdir" -f ($myinvocation.MyCommand.Definition));
22+
Start-Process powershell.exe -Verb RunAs -ArgumentList ("-NoExit -noprofile -file `"{0}`" -elevated -pwd $pwd -name $name -version $version -cpu $cpu -ram $ram -size $size -outdir $outdir" -f ($myinvocation.MyCommand.Definition));
2223
exit;
2324
}
2425

26+
# Log a warning if the Microsoft-Hyper-V feature is not enabled
27+
if (-not (Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -ErrorAction SilentlyContinue).State -eq 'Enabled') {
28+
Write-Warning "Microsoft-Hyper-V feature is not enabled. " +
29+
"Please enable it in Windows Features before creating a new VM."
30+
}
31+
32+
# Check if $outdir/$name already exists (and is non-empty)
33+
if ((Test-Path -Path "$outdir\$name") -and (Get-ChildItem -Path "$outdir\$name").Count -gt 0) {
34+
Write-Host "Virtual machine '$name' already exists in '$outdir'. Please choose a different name or delete the existing one."
35+
exit 1
36+
}
37+
2538

2639
# Create new virtual machine
40+
Write-Host "Creating new virtual machine '$name'..."
2741
New-VM -Generation 2 -Name "$name" -path "$outdir" -NoVHD | Out-Null
2842

2943
# Configure network adapter to use the default vswitch
44+
Write-Host "Configuring network adapter for virtual machine '$name'..."
3045
$networkAdapter = Get-VMNetworkAdapter -VMName "$name"
3146
Connect-VMNetworkAdapter -VMName "$name" -Name "$($networkAdapter.name)" -SwitchName "Default Switch"
3247

3348
# Create EFI disk
49+
Write-Host "Creating EFI disk for virtual machine '$name'..."
3450
$efiVHD = "$outdir\$name\EFI.vhdx"
3551
& powershell.exe "$PSScriptRoot\create-macos-recovery.ps1" -version "$version"
3652
& powershell.exe "$PSScriptRoot\convert-efi-disk.ps1" -dest "$efiVHD"
@@ -40,7 +56,8 @@ $efiDisk = Get-VMHardDiskDrive -VMName "$name"
4056
# Create post-install VHDX if tools folder is present
4157
$toolsDir = "$($pwd)\Tools"
4258
if (Test-Path -Path "$toolsDir") {
43-
$toolsVHD = "$outdir\$name\Tools.vhdx"
59+
$toolsVHD = "$outdir\$name\Tools.vhdx"
60+
Write-Host "Creating Tools VHDX disk for virtual machine '$name'..."
4461
# Create and mount a new tools.vhdx disk
4562
$toolsDisk = New-VHD -Path "$toolsVHD" -Dynamic -SizeBytes 512MB |
4663
Mount-VHD -Passthru |
@@ -57,6 +74,7 @@ if (Test-Path -Path "$toolsDir") {
5774

5875
# Create macOS disk
5976
$macOSVHD = "$outdir\$name\$name.vhdx"
77+
Write-Host "Creating macOS disk for virtual machine '$name'..."
6078
New-VHD -Path "$macOSVHD" -SizeBytes $($size * 1GB) |
6179
Mount-VHD -Passthru |
6280
Initialize-Disk -PartitionStyle "GPT" -Confirm:$false -Passthru |
@@ -65,9 +83,11 @@ New-VHD -Path "$macOSVHD" -SizeBytes $($size * 1GB) |
6583
Dismount-DiskImage -ImagePath "$macOSVHD" | Out-Null
6684

6785
# Add macOS disk to virtual machine
86+
Write-Host "Adding macOS disk to virtual machine '$name'..."
6887
Add-VMHardDiskDrive -VMName "$name" -Path "$macOSVHD" -ControllerType SCSI
6988

7089
# Configure virtual machine
90+
Write-Host "Configuring virtual machine '$name'..."
7191
Set-VM `
7292
-Name "$name" `
7393
-ProcessorCount $cpu `
@@ -76,3 +96,6 @@ Set-VM `
7696
Set-VMFirmware -VMName "$name" `
7797
-EnableSecureBoot Off `
7898
-FirstBootDevice $efiDisk
99+
100+
# Wait for user input before closing (to show any errors)
101+
Write-Host "`nVirtual machine '$name' created successfully at $outdir\$name.`n"

0 commit comments

Comments
 (0)