forked from cloudbase/windows-imaging-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageChecks.ps1
186 lines (171 loc) · 6.11 KB
/
ImageChecks.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
param (
[parameter(Mandatory=$true)]
[String]$Image,
[parameter(Mandatory=$true)]
[String]$ConfigFile
)
$ErrorActionPreference = "Stop"
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$resourcesDir = "$scriptPath\UnattendResources"
function Get-ImageType {
<#
.SYNOPSIS
Gets the image type from the config file
#>
param($ConfigFile)
Import-Module "$resourcesDir\ini.psm1"
$type = Get-IniFileValue -Path $ConfigFile -Section "DEFAULT" -Key "image_type"
return $type
}
function Extract-File {
<#
.SYNOPSIS
Extracts a tar or tgz file and returns the path to the extracted file
#>
param($File)
$imageFilePath = Split-Path -Path $File
$imageFilePath = Join-Path -Path $imageFilePath -ChildPath "extracted"
Write-Host "Image is being extracted to $imageFilePath"
& 7z.exe e $File "-o$imageFilePath" | Out-Null
if ($LastExitCode -ne 0) {
Write-Host "Something went wrong while extracting"
exit
}
$tarPath = Join-Path -Path $imageFilePath -ChildPath "*.tar"
$img = Get-ChildItem $tarPath
& 7z.exe x -aoa -ttar $img "-o$imageFilePath" | Out-Null
if ($LastExitCode -eq 0) {
Write-Host "Done extracting"
} else {
Write-Host "Something went wrong while extracting"
exit
}
Remove-Item $tarPath -ErrorAction $ErrorActionPreference
$imageFilePath = Join-Path -Path $imageFilePath -ChildPath "*.*"
$imgExtracted = Get-ChildItem $imageFilePath
return $imgExtracted
}
function Get-VHDXImage {
<#
.SYNOPSIS
Converts and mounts the given Image
.DESCRIPTION
The given image is converted with qemu to a vhdx format
that will be mounted later. After that it returns the vhdx image
and it also returns the mounting point for futher checks
.PARAMETER Image
The image that must be converted
.PARAMETER Extension
The format of the image from which it will be converted to a vhdx
#>
param($Image, $Extension)
[String]$qemuPath = "$scriptPath\bin\qemu-img.exe"
$converted = $Image.split(".")[0]
$converted = -join ($converted, ".vhdx")
Write-Host "Converting $Image to $converted..."
& $qemuPath convert -f $Extension -O vhdx $Image $converted
if ($LastExitCode -ne 0) {
Write-Host "Error while converting"
exit
}
Write-Host "Mounting $converted..."
$volume = (Mount-VHD -Path $converted -Passthru | Get-Disk | Get-Partition | Get-Volume).DriveLetter
$volume = -join ($volume, ":\")
if (Test-Path $volume) {
Write-Host "Mounting point: $volume"
} else {
Write-Host "Something wrong with the path of the mounting point"
exit
}
return $converted, $volume
}
function Check-ImageFormat {
<#
.SYNOPSIS
Checks the format of an image for the given type
.DESCRIPTION
This function checks the format of an image to see if it corresponds to the given type,
by looking at its extension. After this, based on the type it proceeds
with further checks, for MAAS checking that the image has curtin,
for KVM looking for VirtIO drivers
.PARAMETER Type
The image type, it will be taken from the config file
.PARAMETER Image
The image that will be checked
#>
param($Type, $Image)
$extension = [IO.Path]::GetExtension($Image)
$extension = $extension.split(".")[1]
if ($Type -eq "Hyper-V") {
if ($extension -eq "VHDX" -Or $extension -eq "VHD") {
Write-Host "OK"
} else {
throw "Wrong format for Hyper-V"
}
} elseif ($Type -eq "MAAS") {
if ($extension -eq "raw") {
$convertedMounted = Get-VHDXImage $Image $extension
Write-Host "Checking for curtin..."
$curtin = Get-ChildItem $convertedMounted[1] | Select-String -Quiet "curtin"
if ($curtin) {
Write-Host "OK"
} else {
Write-Host "$Image doesn't have curtin"
}
Dismount-VHD $convertedMounted[0]
Remove-Item $convertedMounted[0] -ErrorAction $ErrorActionPreference
} else {
throw "Wrong format for MAAS"
}
} elseif ($Type -eq "KVM") {
if ($extension -eq "raw" -Or $extension -eq "qcow2") {
$convertedMounted = Get-VHDXImage $Image $extension
Write-Host "Checking for VirtIO..."
$virtio = Get-ChildItem -Recurse -Force $convertedMounted[1] -ErrorAction SilentlyContinue `
| Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "vio*") }
if ($virtio) {
Write-Host "OK"
} else {
Write-Host "$Image doesn't have VirtIO"
}
Dismount-VHD $convertedMounted[0]
Remove-Item $convertedMounted[0] -ErrorAction $ErrorActionPreference
} else {
throw "Wrong format for KVM"
}
} else {
throw "Wrong type"
}
}
function Validate-Image {
<#
.SYNOPSIS
Checks what type of file was given and after that checks its format
.DESCRIPTION
At first checks to see if the given file must be extracted or not,
after that it proceeds with checking its format and
in the end it deletes the extracted file if it's the case
#>
param (
[String]$Image,
[String]$ConfigFile
)
$extension=[IO.Path]::GetExtension($Image)
$extension=$extension.split(".")[1]
if ($extension -eq "tgz" -or $extension -eq "gz" -or $extension -eq "tar") {
$Image = Extract-File $Image
$imgExtracted = $Image
}
$type = Get-ImageType $ConfigFile
try {
Check-ImageFormat $type $Image
} catch {
Write-Host $_
}
if ($imgExtracted) {
Write-Host "Deleting $imgExtracted..."
$imageFilePath = Split-Path -Path $imgExtracted
Remove-Item -Recurse $imageFilePath -ErrorAction $ErrorActionPreference
}
}
Validate-Image $Image $ConfigFile