Skip to content

Commit

Permalink
Merge c42bd07 into 5652eeb
Browse files Browse the repository at this point in the history
  • Loading branch information
ader1990 authored Sep 10, 2019
2 parents 5652eeb + c42bd07 commit 0b90311
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 188 deletions.
186 changes: 0 additions & 186 deletions ImageChecks.ps1

This file was deleted.

107 changes: 106 additions & 1 deletion WinImageBuilder.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1750,6 +1750,111 @@ 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."
}
foreach($compressionFormat in $compressionFormats) {
$imageChain += $imagePath
$imagePath = Decompress-File $imagePath $compressionFormat
}
}

$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"
}

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

if (!(@("vhd", "vhdx").Contains($fileExtension))) {
$barePath = Get-PathWithoutExtension $imagePath
$tempImagePath = $barePath + ".${diskFormat}"
$imageChain += $imagePath
Convert-VirtualDisk -vhdPath $imagePath -outPath $tempImagePath -format $diskFormat
$imagePath = $tempImagePath
}
} finally {
foreach ($chainItem in $imageChain) {
Write-Host "Removing chain file item ${chainItem}"
}
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 0b90311

Please sign in to comment.