Skip to content

Commit

Permalink
Merge 0397dc1 into 2f61ae2
Browse files Browse the repository at this point in the history
  • Loading branch information
ader1990 authored Sep 11, 2019
2 parents 2f61ae2 + 0397dc1 commit 437732b
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 188 deletions.
186 changes: 0 additions & 186 deletions ImageChecks.ps1

This file was deleted.

162 changes: 161 additions & 1 deletion WinImageBuilder.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,166 @@ function New-WindowsFromGoldenImage {
}
}


function Decompress-File {
Param(
[parameter(Mandatory=$true)]
[string]$FilePath,
[parameter(Mandatory=$true)]
[string]$CompressionFormat
)

return $FilePath
}


function Test-OfflineWindowsImage {
<#
.SYNOPSIS
This function verifies if a Windows image has been properly generated according to
the configuration file. The verification is performed offline, without instantiating
the image.
.DESCRIPTION
This function first tests if the config.image_path exists, then uses the extension and the
config.compression_format to detect the compression and qemu-img binary to detect
the image format.
If any compression is detected, a decompression is performed for each compression.
If the image format is other than vhdx, "qemu-img convert -O vhdx" is performed.
Finally, the full chain of decompressed/converted files is removed.
Fix me: add specific checks for every existent testable functionality.
#>
[CmdletBinding()]
Param(
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$ConfigFilePath
)

Write-Log "Offline Windows image validation started."
$windowsImageConfig = Get-WindowsImageConfig -ConfigFilePath $ConfigFilePath
Is-Administrator

if (!(Test-Path $windowsImageConfig.image_path)) {
throw "Image validation failed: $($windowsImageConfig.image_path) does not exist."
}

$imageChain = @()
$imagePath = $windowsImageConfig.image_path

try {
if ($windowsImageConfig.compression_format) {
$compressionFormats = $windowsImageConfig.compression_format.split(".")
$invalidCompressionFormat = $compressionFormats | Where-Object `
{$AvailableCompressionFormats -notcontains $_}
if ($invalidCompressionFormat) {
throw "Compresion format $invalidCompressionFormat not available."
} else {
Write-Log "Compression format ${invalidCompressionFormat} is available."
}
foreach($compressionFormat in $compressionFormats) {
$imageChain += $imagePath
$imagePath = Decompress-File $imagePath $compressionFormat
Write-Log "Image $($imageChain[$imageChain.Count - 1]) decompressed to ${imagePath}"
}
}

$imageFileExtension = [System.IO.Path]::GetExtension($imagePath)
$fileExtension = '*'
$diskFormat = '*'
if ($windowsImageConfig.image_type -eq "HYPER-V") {
$fileExtension = 'vhdx'
$diskFormat = 'vhdx'
if ($imageFileExtension -eq '.vhd') {
$fileExtension = 'vhd'
$diskFormat = 'vpc'
}
}
if ($windowsImageConfig.image_type -eq "KVM") {
$fileExtension = 'qcow2'
$diskFormat = 'qcow2'
}
if ($windowsImageConfig.image_type -eq "MAAS") {
$fileExtension = '*'
$diskFormat = 'raw'
}

if (!([System.IO.Path]::GetExtension($imagePath) -like ".${fileExtension}")) {
throw "${imagePath} does not have ${fileExtension} extension."
} else {
Write-Log "${imagePath} has the correct ${fileExtension} extension."
}

$qemuInfoOutput = & "$scriptPath\bin\qemu-img.exe" info --output=json $imagePath
$qemuInfoJson = ConvertFrom-Json ($qemuInfoOutput -join "")
$qemuImgFormat = $qemuInfoJson | Select-Object "Format"
if ($qemuImgFormat.Format -ne $diskFormat) {
throw "${imagePath} does not have ${diskFormat} format."
} else {
Write-Log "${imagePath} has the correct ${diskFormat} format."
}

if (!(@("vhd", "vhdx").Contains($fileExtension))) {
$barePath = Get-PathWithoutExtension $imagePath
$tempImagePath = $barePath + ".vhdx"
Convert-VirtualDisk -vhdPath $imagePath -outPath $tempImagePath `
-format "vhdx"
$imagePath = $tempImagePath
$imageChain += $imagePath
}

$childImagePath = $imagePath -ireplace ".vhd", "_bak.vhd"
New-VHD -ParentPath $imagePath -Path $childImagePath | Out-Null
$imagePath = $childImagePath
$imageChain += $imagePath
Mount-VHD -Path $imagePath -Passthru | Out-Null

try {
Get-PSDrive | Out-Null
$driveNumber = (Get-DiskImage -ImagePath $imagePath | Get-Disk).Number
$mountPoint = (Get-Partition -DiskNumber $driveNumber | `
Where-Object {@("Basic", "IFS") -contains $_.Type}).DriveLetter + ":"

# Test if Cloudbase-Init is installed
$cloudbaseInitPath = "Program Files\Cloudbase Solutions\Cloudbase-Init"
$cloudbaseInitPathX86 = "${cloudbaseInitPath} (x86)"
if ((Test-Path (Join-Path $mountPoint $cloudbaseInitPath)) -or `
(Test-Path (Join-Path $mountPoint $cloudbaseInitPathX86))) {
Write-Log "Cloudbase-Init is installed."
} else {
throw "Cloudbase-Init is not installed on the image."
}

# Test if curtin modules are installed
if ($windowsImageConfig.install_maas_hooks -or $windowsImageConfig.image_type -eq "MAAS") {
if (Test-Path (Join-Path $mountPoint "curtin")) {
Write-Log "Curtin hooks are installed."
} else {
throw "Curtin hooks are not installed on the image."
}
}

# Test if extra drivers are installed
if ($windowsImageConfig.virtio_iso_path -or $windowsImageConfig.virtio_base_path `
-or $windowsImageConfig.drivers_path -or $windowsImageConfig.image_type -eq "KVM") {
$extraDriversNr = (Get-Item "$mountPoint\Windows\INF\OEM*.inf" | Measure-Object).Count
if ($extraDriversNr) {
Write-Log "Found ${extraDriversNr} extra drivers installed."
} else {
throw "No extra drivers installed on the image."
}
}
} finally {
Dismount-VHD $imagePath
}
} finally {
foreach ($chainItem in $imageChain) {
Write-Log "Removing chain file item ${chainItem}"
Remove-Item -Force $chainItem -ErrorAction SilentlyContinue
}
Write-Log "Offline Windows image validation finished."
}
}


Export-ModuleMember New-WindowsCloudImage, Get-WimFileImagesInfo, New-MaaSImage, Resize-VHDImage,
New-WindowsOnlineImage, Add-VirtIODriversFromISO, New-WindowsFromGoldenImage, Get-WindowsImageConfig,
New-WindowsImageConfig
New-WindowsImageConfig, Test-OfflineWindowsImage
2 changes: 1 addition & 1 deletion WindowsImageBuilder.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ModuleList = @("Config.psm1", "UnattendResources\ini.psm1")

# Functions to export from this module
FunctionsToExport = "New-WindowsCloudImage", "New-WindowsOnlineImage", "New-WindowsFromGoldenImage",
"Get-WindowsImageConfig", "New-WindowsImageConfig"
"Get-WindowsImageConfig", "New-WindowsImageConfig", "Test-OfflineWindowsImage"

AliasesToExport = ""
}

0 comments on commit 437732b

Please sign in to comment.