Skip to content

Commit

Permalink
Reset BCD store for images created from gold
Browse files Browse the repository at this point in the history
If multiple images are created at the same time from a gold VHD,
there is a chance that some of them do not boot because HyperV
boot environment does not recognize the boot device anymore
with error code 0xc00000e.

Resetting the BCD store to locate the os device and resetting the
VHD GUID should solve the issue.
  • Loading branch information
ader1990 committed Oct 14, 2019
1 parent 5810a52 commit 57ca463
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions WinImageBuilder.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,35 @@ function Apply-Image {
if ($LASTEXITCODE) { throw "Dism apply-image failed" }
}

function Reset-BCDSearchOrder {
Param(
[parameter(Mandatory=$true)]
[string]$systemDrive,
[parameter(Mandatory=$true)]
[string]$windowsDrive,
[parameter(Mandatory=$true)]
[string]$diskLayout
)

if ($diskLayout -eq "BIOS") {
Write-Log "Resetting BCD boot border"
$bcdeditPath = "${windowsDrive}\windows\system32\bcdedit.exe"
if (!(Test-Path $bcdeditPath)) {
Write-Warning ('"{0}" not found, using online version' -f $bcdeditPath)
$bcdeditPath = "bcdedit.exe"
}

& $bcdeditPath /store ${systemDrive}\boot\BCD /set `{bootmgr`} device locate
if ($LASTEXITCODE) { Write-Warning "BCDEdit failed: bootmgr device locate" }

& $bcdeditPath /store ${systemDrive}\boot\BCD /set `{default`} device locate
if ($LASTEXITCODE) { Write-Warning "BCDEdit failed: default device locate" }

& $bcdeditPath /store ${systemDrive}\boot\BCD /set `{default`} osdevice locate
if ($LASTEXITCODE) { Write-Warning "BCDEdit failed: default osdevice locate" }
}
}

function Create-BCDBootConfig {
Param(
[parameter(Mandatory=$true)]
Expand Down Expand Up @@ -251,22 +280,9 @@ function Create-BCDBootConfig {
throw "BCDBoot failed with error: $bcdbootOutput"
}

if ($diskLayout -eq "BIOS") {
$bcdeditPath = "${windowsDrive}\windows\system32\bcdedit.exe"
if (!(Test-Path $bcdeditPath)) {
Write-Warning ('"{0}" not found, using online version' -f $bcdeditPath)
$bcdeditPath = "bcdedit.exe"
}

& $bcdeditPath /store ${systemDrive}\boot\BCD /set `{bootmgr`} device locate
if ($LASTEXITCODE) { Write-Warning "BCDEdit failed: bootmgr device locate" }

& $bcdeditPath /store ${systemDrive}\boot\BCD /set `{default`} device locate
if ($LASTEXITCODE) { Write-Warning "BCDEdit failed: default device locate" }
Reset-BCDSearchOrder -systemDrive $systemDrive -windowsDrive $windowsDrive `
-diskLayout $diskLayout

& $bcdeditPath /store ${systemDrive}\boot\BCD /set `{default`} osdevice locate
if ($LASTEXITCODE) { Write-Warning "BCDEdit failed: default osdevice locate" }
}
$ErrorActionPreference = "Stop"
Write-Log "BCDBoot config has been created."
}
Expand Down Expand Up @@ -1774,6 +1790,7 @@ function New-WindowsFromGoldenImage {
try {
Execute-Retry {
Resize-VHD -Path $windowsImageConfig.gold_image_path -SizeBytes $windowsImageConfig.disk_size
Set-VHD -Path $windowsImageConfig.gold_image_path -ResetDiskIdentifier -Force
} | Out-Null

Mount-VHD -Path $windowsImageConfig.gold_image_path -Passthru | Out-Null
Expand Down Expand Up @@ -1815,6 +1832,8 @@ function New-WindowsFromGoldenImage {
}

$resourcesDir = Join-Path -Path $driveLetterGold -ChildPath "UnattendResources"
Reset-BCDSearchOrder -systemDrive $driveLetterGold -windowsDrive $driveLetterGold `
-diskLayout $windowsImageConfig.disk_layout
Copy-UnattendResources -resourcesDir $resourcesDir -imageInstallationType $windowsImageConfig.image_name `
-InstallMaaSHooks $windowsImageConfig.install_maas_hooks `
-VMwareToolsPath $windowsImageConfig.vmware_tools_path
Expand Down

0 comments on commit 57ca463

Please sign in to comment.