-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathCreate-VM.ps1
268 lines (235 loc) · 9.8 KB
/
Create-VM.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
param (
# path to the iso file (path to local file or URL)
# - URL like https://osrelease.download.prss.microsoft.com/pr/download/Mariner-2.0-x86_64.iso
# - C:\MyFolder\Mariner-2.0-x86_64.iso
[string]
$isoFile = "https://osrelease.download.prss.microsoft.com/pr/download/Mariner-2.0-x86_64.iso",
# ISO file checksum, could be either:
# - URL like file:https://osrelease.download.prss.microsoft.com/pr/download/Mariner-2.0-x86_64.iso.sha256
# - text [checksum type]:[value], e.g.: sha256:246F000F1C493E5A8F78D13D4503DDB4BE5A77A5418A42809CC3CA5B8111931A
[string]
$isoChecksum = "file:https://osrelease.download.prss.microsoft.com/pr/download/Mariner-2.0-x86_64.iso.sha256",
# Name of the CBL-Mariner configuration in the ISO
# Note that default config name for CBL-Mariner ISO is "CBL-Mariner Full"
[string]
$marinerConfigName = 'CBL-Mariner Full',
# username used to login
[Parameter(mandatory=$true)]
[ValidateNotNullorEmpty()]
[string]
$userName,
# password use to login
[Parameter(mandatory=$true)]
[ValidateNotNullorEmpty()]
[string]
$password,
# path to provisionner folder
# Note that everything under that path will be copied to the VM
[string]
$srcProvisionerFolder = "$PSScriptRoot\provisioners",
# name of the 'main' provisioner script
# this script must be in the provisionner folder
[string]
$provisionerScript = 'customizeMariner.sh',
# Name of the VM
[string]
$vmName = 'TestVM',
# folder where VHDX will be copied
[string]
$outDir = "$PSScriptRoot\outdir",
# Size of the VM/VHDX hard drive
[string]
$diskSize = '10240',
# Number of CPU of the VM/VHDX
[string]
$cpu = '2',
# Amount of RAM for VM/VHDX
[string]
$memory = '2048',
# name of the Hyper-V virtual switch to use
#
# note that this virtual network switch should allow
# packer to communicate with its target using the http server packer
# will set up ('http://{{ .HTTPIP }}:{{ .HTTPPort }}' in packer configuration file)
#
# see packer documentation to alternate way of passing file from packer to target (e.g.: mounting a 2nd iso file)
#
[Parameter(mandatory=$true)]
[ValidateNotNullorEmpty()]
[string]
$hyperVSwitchName
)
function Replace-InFile {
param (
$tagToReplace,
$tagValue,
$fileName
)
$tempName = (Get-ChildItem $fileName).Name
Write-Host "$tempName -> replace $tagToReplace with $tagValue"
(Get-Content $fileName) | %{$_ -replace $tagToReplace,$tagValue} | Set-Content $fileName
}
if (! (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) )
{
Write-Host "Error: needs admin rights to run"
exit 1
}
$tempFolder = Join-Path $Env:Temp $(New-Guid)
$packerHttpFolder = "$tempFolder\packer_http"
$tempOutDir=".\\outdir"
$tempProvisionerFolderName="provisioners"
$marinerUnattendedConfigFile = "mariner_config.json"
$marinerPostInstallScript = "postinstall.sh"
$packerConfigFile = "packer_config.json"
try
{
# create a brand new output directory
if (Test-Path $outDir) {
Write-Host "-- Remove $outDir"
Remove-Item $outDir -Recurse -Force -Confirm:$false
}
New-Item -Path $outDir -ItemType directory
# creat working directories
New-Item -Path $packerHttpFolder -ItemType directory
if ($isoFile.Contains("https://")) {
Write-Host "get iso from $isoFile"
Invoke-WebRequest -Uri $isoFile -OutFile $tempFolder/MarinerImage.iso
$isoFileName = MarinerImage.iso
}
else {
# copy iso file to build dir (temp folder)
Write-Host "Copy iso file to working directory"
Copy-Item $isoFile -Destination $tempFolder -Force
$isoFileName = (Get-ChildItem $isoFile).Name
}
# Get package list from atttended config file in the iso
Write-Host "Get package lists from iso"
Mount-DiskImage -ImagePath $tempFolder/$isoFileName
$driveLetter=(Get-DiskImage -ImagePath $tempFolder/$isoFileName | Get-Volume).DriveLetter
$attendedConfig=(Get-Childitem -Path "${driveLetter}:\*" -Include "ATTENDED_CONFIG.json" -Recurse).FullName
if (! $attendedConfig) {
Write-Host "Error: The iso contains an unattended config file and cannot be customized using packer"
exit 1
}
$packageListFound = $false
$packageLists = ""
Write-Host "Get package list from $attendedConfig"
$configJson = Get-Content $attendedConfig -Raw | ConvertFrom-Json
ForEach ($systemConfig in $configJson.SystemConfigs) {
if ($systemConfig.Name -eq $marinerConfigName) {
Write-Host "Extract package list from '$marinerConfigName' config"
$packageListFound = $true
ForEach ($package in $systemConfig.PackageLists) {
if ($packageLists.Length -ne 0) {
$packageLists += ", "
}
$packageLists = $packageLists + '"' + $package + '"'
}
break
}
}
if (!$packageListFound) {
Write-Host "Error: config '$marinerConfigName' cannot be found in the iso"
exit 1
}
# populate working dir
Write-Output "Populate working folder ($tempFolder)"
Copy-Item $PSScriptRoot\$packerConfigFile -Destination $tempFolder -Force
Copy-Item $PSScriptRoot\$marinerUnattendedConfigFile -Destination $packerHttpFolder -Force
Copy-Item $PSScriptRoot\$marinerPostInstallScript -Destination $packerHttpFolder -Force
New-Item -Path $tempFolder\$tempProvisionerFolderName -ItemType directory
Copy-Item $srcProvisionerFolder\* -Destination $tempFolder\$tempProvisionerFolderName -Force -Recurse
# customized config files (packer and mariner)
Replace-InFile -tagToReplace "@VMNAME@" `
-tagValue "$vmName" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@NETWORKSWITCH@" `
-tagValue "$hyperVSwitchName" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@USERNAME@" `
-tagValue "$userName" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@PASSWORD@" `
-tagValue "$password" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@ISOFILE@" `
-tagValue "$isoFileName" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@ISOCHECKSUM@" `
-tagValue "$isoChecksum" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@DISKSIZE@" `
-tagValue "$diskSize" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@CPU@" `
-tagValue "$cpu" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@MEMORY@" `
-tagValue "$memory" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@OUTDIR@" `
-tagValue "$tempOutDir" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@MARINERCONFIGFILE@" `
-tagValue "$marinerUnattendedConfigFile" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@POSTINSTALLSCRIPT@" `
-tagValue "$marinerPostInstallScript" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@PROVISIONERSCRIPT@" `
-tagValue "$provisionerScript" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@PROVISIONERSRCFOLDER@" `
-tagValue "$tempProvisionerFolderName" `
-fileName $tempFolder\$packerConfigFile
Replace-InFile -tagToReplace "@VMNAME@" `
-tagValue "$vmName" `
-fileName $packerHttpFolder\$marinerUnattendedConfigFile
Replace-InFile -tagToReplace "@CONFIGNAME@" `
-tagValue "$marinerConfigName" `
-fileName $packerHttpFolder\$marinerUnattendedConfigFile
Replace-InFile -tagToReplace '"@PACAKGELIST@"' `
-tagValue "$packageLists" `
-fileName $packerHttpFolder\$marinerUnattendedConfigFile
Replace-InFile -tagToReplace "@USERNAME@" `
-tagValue "$userName" `
-fileName $packerHttpFolder\$marinerUnattendedConfigFile
Replace-InFile -tagToReplace "@PASSWORD@" `
-tagValue "$password" `
-fileName $packerHttpFolder\$marinerUnattendedConfigFile
Replace-InFile -tagToReplace "@POSTINSTALLSCRIPT@" `
-tagValue "$marinerPostInstallScript" `
-fileName $packerHttpFolder\$marinerUnattendedConfigFile
# launch packer
#
# notes:
# - packer executable must be in system PATH
# - packer must be launched from location of its config file
# because config file uses relative path
# - last <wait> in the 'boot_command' is used to leave time
# to Mariner to setup, reboot and start sshd service
# before packer starts to poke ssh connection and overload it
# - launch with '-debug' option to debug
Push-Location $tempFolder
Write-Output 'Launch packer'
packer build .\$packerConfigFile
if (Test-Path $tempOutDir)
{
Copy-Item -Path $tempOutDir\* -Destination $outDir -Recurse
}
Pop-Location
}
finally
{
Write-Host "`n=========================="
Write-Host "== Cleanup test machine =="
Write-Host "=========================="
Write-Host "-- dismount iso ($tempFolder/$isoFileName)"
Dismount-DiskImage -ImagePath $tempFolder/$isoFileName
Pop-Location
if (Test-Path $tempFolder)
{
Write-Host "-- Remove $tempFolder"-
Remove-Item $tempFolder -Recurse -Force -Confirm:$false
}
}