forked from fdcastel/Hyper-V-Automation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-VMFromUbuntuImage.ps1
More file actions
466 lines (380 loc) · 12.2 KB
/
New-VMFromUbuntuImage.ps1
File metadata and controls
466 lines (380 loc) · 12.2 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#Requires -RunAsAdministrator
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$SourcePath,
[Parameter(Mandatory=$true)]
[string]$VMName,
[string]$FQDN = $VMName,
[Parameter(Mandatory=$true, ParameterSetName='RootPassword')]
[string]$RootPassword,
[Parameter(Mandatory=$true, ParameterSetName='RootPublicKey')]
[string]$RootPublicKey,
[uint64]$VHDXSizeBytes,
[int64]$MemoryStartupBytes = 1GB,
[switch]$EnableDynamicMemory,
[int64]$ProcessorCount = 2,
[string]$SwitchName = 'SWITCH',
[string]$MacAddress,
[string]$IPAddress,
[string]$Gateway,
[string[]]$DnsAddresses = @('1.1.1.1','1.0.0.1'),
[string]$InterfaceName = 'eth0',
[Parameter(Mandatory=$false, ParameterSetName='RootPassword')]
[Parameter(Mandatory=$false, ParameterSetName='RootPublicKey')]
[Parameter(Mandatory=$true, ParameterSetName='EnableRouting')]
[switch]$EnableRouting,
[Parameter(Mandatory=$false, ParameterSetName='RootPassword')]
[Parameter(Mandatory=$false, ParameterSetName='RootPublicKey')]
[Parameter(Mandatory=$true, ParameterSetName='EnableRouting')]
[string]$SecondarySwitchName,
[string]$SecondaryMacAddress,
[string]$SecondaryIPAddress,
[string]$SecondaryInterfaceName,
[string]$LoopbackIPAddress,
[switch]$InstallDocker
)
$ErrorActionPreference = 'Stop'
function Normalize-MacAddress ([string]$value) {
$value.`
Replace('-', '').`
Replace(':', '').`
Insert(2,':').Insert(5,':').Insert(8,':').Insert(11,':').Insert(14,':').`
ToLowerInvariant()
}
# Get default VHD path (requires administrative privileges)
#$vmms = gwmi -namespace root\virtualization\v2 Msvm_VirtualSystemManagementService
#$vmmsSettings = gwmi -namespace root\virtualization\v2 Msvm_VirtualSystemManagementServiceSettingData
#$vhdxPath = Join-Path $vmmsSettings.DefaultVirtualHardDiskPath "$VMName.vhdx"
#$metadataIso = Join-Path $vmmsSettings.DefaultVirtualHardDiskPath "$VMName-metadata.iso"
$vmms = Get-CimInstance -namespace root\virtualization\v2 Msvm_VirtualSystemManagementService
$vmmsSettings = Get-CimInstance -namespace root\virtualization\v2 Msvm_VirtualSystemManagementServiceSettingData
$vhdRootPath = [IO.Path]::Combine($vmmsSettings.DefaultExternalDataRoot,"$VMName","Virtual Hard Disks")
$vhdxPath = [IO.Path]::Combine($vhdRootPath,"$VMName.vhdx")
$metadataIso = [IO.Path]::Combine($vhdRootPath, "$VMName-metadata.iso")
$vmPath = [IO.Path]::Combine($vmmsSettings.DefaultExternalDataRoot,$VMName,"Virtual Machines")
$vmSnapshotPath = [IO.Path]::Combine($vmmsSettings.DefaultExternalDataRoot,$VMName,"Snapshots")
#Create the vhdx Root path if not exist
if(-Not (Test-Path -Path $vhdRootPath))
{
New-Item -ItemType Directory -Force -Path $vhdRootPath
}
# Convert cloud image to VHDX
Write-Verbose 'Creating VHDX from cloud image...'
$ErrorActionPreference = 'Continue'
& {
& qemu-img.exe convert -f qcow2 $SourcePath -O vhdx -o subformat=dynamic $vhdxPath
if ($LASTEXITCODE -ne 0) {
throw "qemu-img returned $LASTEXITCODE. Aborting."
}
}
$ErrorActionPreference = 'Stop'
if ($VHDXSizeBytes) {
Resize-VHD -Path $vhdxPath -SizeBytes $VHDXSizeBytes
}
# Create VM
Write-Verbose 'Creating VM...'
$vm = New-VM -Name $VMName -Generation 2 -MemoryStartupBytes $MemoryStartupBytes -Path $vmPath -VHDPath $vhdxPath -SwitchName $SwitchName
$vm | Set-VMProcessor -Count $ProcessorCount
$vm | Get-VMIntegrationService -Name "Guest Service Interface" | Enable-VMIntegrationService
if ($EnableDynamicMemory) {
$vm | Set-VMMemory -DynamicMemoryEnabled $true
}
# Sets Snapshot Path
$vm | Set-VM -SnapshotFileLocation $vmSnapshotPath
# Sets Secure Boot Template.
# Set-VMFirmware -SecureBootTemplate 'MicrosoftUEFICertificateAuthority' doesn't work anymore (!?).
$vm | Set-VMFirmware -SecureBootTemplateId ([guid]'272e7447-90a4-4563-a4b9-8e4ab00526ce')
# Ubuntu 16.04/18.04 startup hangs without a serial port (!?) -- https://bit.ly/2AhsihL
$vm | Set-VMComPort -Number 2 -Path "\\.\pipe\dbg1"
# Setup first network adapter
if ($MacAddress) {
$MacAddress = Normalize-MacAddress $MacAddress
$vm | Set-VMNetworkAdapter -StaticMacAddress $MacAddress.Replace(':', '')
}
$eth0 = Get-VMNetworkAdapter -VMName $VMName
$eth0 | Rename-VMNetworkAdapter -NewName $InterfaceName
if ($SecondarySwitchName) {
# Add secondary network adapter
$eth1 = Add-VMNetworkAdapter -VMName $VMName -Name $SecondaryInterfaceName -SwitchName $SecondarySwitchName -PassThru
if ($SecondaryMacAddress) {
$SecondaryMacAddress = Normalize-MacAddress $SecondaryMacAddress
$eth1 | Set-VMNetworkAdapter -StaticMacAddress $SecondaryMacAddress.Replace(':', '')
}
}
# Start VM just to create MAC Addresses
$vm | Start-VM
Start-Sleep -Seconds 1
$vm | Stop-VM -Force
# Wait for Mac Addresses
Write-Verbose "Waiting for MAC addresses..."
do {
$eth0 = Get-VMNetworkAdapter -VMName $VMName -Name $InterfaceName
$MacAddress = Normalize-MacAddress $eth0.MacAddress
Start-Sleep -Seconds 1
} while ($MacAddress -eq '00:00:00:00:00:00')
if ($SecondarySwitchName) {
do {
$eth1 = Get-VMNetworkAdapter -VMName $VMName -Name $SecondaryInterfaceName
$SecondaryMacAddress = Normalize-MacAddress $eth1.MacAddress
Start-Sleep -Seconds 1
} while ($SecondaryMacAddress -eq '00:00:00:00:00:00')
}
# Create metadata ISO image
# Creates a NoCloud data source for cloud-init.
# More info: http://cloudinit.readthedocs.io/en/latest/topics/datasources/nocloud.html
Write-Verbose 'Creating metadata ISO image...'
$instanceId = [Guid]::NewGuid().ToString()
$metadata = @"
instance-id: $instanceId
local-hostname: $VMName
"@
$RouterMark = if ($EnableRouting) { '<->' } else { ' ' }
$IpForward = if ($EnableRouting) { 'IPForward=yes' } else { '' }
$IpMasquerade = if ($EnableRouting) { 'IPMasquerade=yes' } else { '' }
if ($SecondarySwitchName) {
$DisplayInterfaces = " $($InterfaceName): \4{$InterfaceName} $RouterMark $($SecondaryInterfaceName): \4{$SecondaryInterfaceName}"
} else {
$DisplayInterfaces = " $($InterfaceName): \4{$InterfaceName}"
}
$sectionWriteFiles = @"
write_files:
- content: |
\S{PRETTY_NAME} \n \l
$DisplayInterfaces
path: /etc/issue
owner: root:root
permissions: '0644'
- content: |
[Match]
MACAddress=$MacAddress
[Link]
Name=$InterfaceName
path: /etc/systemd/network/20-$InterfaceName.link
owner: root:root
permissions: '0644'
- content: |
# Please see /etc/systemd/network/ for current configuration.
#
# systemd.network(5) was used directly to configure this system
# due to limitations of netplan(5).
path: /etc/netplan/README
owner: root:root
permissions: '0644'
"@
if ($IPAddress) {
# eth0 (Static)
# Fix for /32 addresses
if ($IPAddress.EndsWith('/32')) {
$RouteForSlash32 = @"
[Route]
Destination=0.0.0.0/0
Gateway=$Gateway
GatewayOnlink=true
"@
}
$sectionWriteFiles += @"
- content: |
[Match]
Name=$InterfaceName
[Network]
Address=$IPAddress
Gateway=$Gateway
DNS=$($DnsAddresses[0])
DNS=$($DnsAddresses[1])
$IpForward
$RouteForSlash32
path: /etc/systemd/network/20-$InterfaceName.network
owner: root:root
permissions: '0644'
"@
} else {
# eth0 (DHCP)
$sectionWriteFiles += @"
- content: |
[Match]
Name=$InterfaceName
[Network]
DHCP=true
$IpForward
[DHCP]
UseMTU=true
path: /etc/systemd/network/20-$InterfaceName.network
owner: root:root
permissions: '0644'
"@
}
if ($SecondarySwitchName) {
$sectionWriteFiles += @"
- content: |
[Match]
MACAddress=$SecondaryMacAddress
[Link]
Name=$SecondaryInterfaceName
path: /etc/systemd/network/20-$SecondaryInterfaceName.link
owner: root:root
permissions: '0644'
"@
if ($SecondaryIPAddress) {
# eth1 (Static)
$sectionWriteFiles += @"
- content: |
[Match]
Name=$SecondaryInterfaceName
[Network]
Address=$SecondaryIPAddress
$IpForward
$IpMasquerade
path: /etc/systemd/network/20-$SecondaryInterfaceName.network
owner: root:root
permissions: '0644'
"@
} else {
# eth1 (DHCP)
$sectionWriteFiles += @"
- content: |
[Match]
Name=$SecondaryInterfaceName
[Network]
DHCP=true
$IpForward
$IpMasquerade
[DHCP]
UseMTU=true
path: /etc/systemd/network/20-$SecondaryInterfaceName.network
owner: root:root
permissions: '0644'
"@
}
}
if ($LoopbackIPAddress) {
# lo
$sectionWriteFiles += @"
- content: |
[Match]
Name=lo
[Network]
Address=$LoopbackIPAddress
path: /etc/systemd/network/20-lo.network
owner: root:root
permissions: '0644'
"@
}
$sectionRunCmd = @'
runcmd:
- 'apt-get update'
- 'rm /etc/netplan/50-cloud-init.yaml'
- 'touch /etc/cloud/cloud-init.disabled'
- 'update-grub' # fix "error: no such device: root." -- https://bit.ly/2TBEdjl
'@
if ($RootPassword) {
$sectionPasswd = @"
password: $RootPassword
chpasswd: { expire: False }
ssh_pwauth: True
"@
} elseif ($RootPublicKey) {
$sectionPasswd = @"
ssh_authorized_keys:
- $RootPublicKey
"@
}
if ($InstallDocker) {
$sectionRunCmd += @'
- 'apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common'
- 'curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -'
- 'add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"'
- 'apt update -y'
- 'apt install -y docker-ce docker-ce-cli containerd.io docker-compose'
'@
}
$userdata = @"
#cloud-config
hostname: $FQDN
fqdn: $FQDN
$sectionPasswd
$sectionWriteFiles
$sectionRunCmd
power_state:
mode: reboot
timeout: 300
"@
# Uses netplan to setup first network interface on first boot (due to cloud-init).
# Then erase netplan and uses systemd-network for everything.
if ($IPAddress) {
# Fix for /32 addresses
if ($IPAddress.EndsWith('/32')) {
$RouteForSlash32 = @"
routes:
- to: 0.0.0.0/0
via: $Gateway
on-link: true
"@
}
$NetworkConfig = @"
version: 2
ethernets:
eth0:
addresses: [$IPAddress]
gateway4: $Gateway
nameservers:
addresses: [$($DnsAddresses -join ', ')]
$RouteForSlash32
"@
} else {
$NetworkConfig = @"
version: 2
ethernets:
eth0:
dhcp4: true
"@
}
# Save all files in temp folder and create metadata .iso from it
$tempPath = Join-Path ([System.IO.Path]::GetTempPath()) $instanceId
mkdir $tempPath | Out-Null
try {
$metadata | Out-File "$tempPath\meta-data" -Encoding ascii
$userdata | Out-File "$tempPath\user-data" -Encoding ascii
$NetworkConfig | Out-File "$tempPath\network-config" -Encoding ascii
$oscdimgPath = Join-Path $PSScriptRoot '.\tools\oscdimg.exe'
& {
$ErrorActionPreference = 'Continue'
& $oscdimgPath $tempPath $metadataIso -j2 -lcidata
if ($LASTEXITCODE -gt 0) {
throw "oscdimg.exe returned $LASTEXITCODE."
}
}
}
finally {
rmdir -Path $tempPath -Recurse -Force
$ErrorActionPreference = 'Stop'
}
# Adds DVD with metadata.iso
$dvd = $vm | Add-VMDvdDrive -Path $metadataIso -Passthru
# Disable Automatic Checkpoints. Check if command is available since it doesn't exist in Server 2016.
$command = Get-Command Set-VM
if ($command.Parameters.AutomaticCheckpointsEnabled) {
$vm | Set-VM -AutomaticCheckpointsEnabled $false
}
# Wait for VM
$vm | Start-VM
Write-Verbose 'Waiting for VM integration services (1)...'
Wait-VM -Name $VMName -For Heartbeat
# Cloud-init will reboot after initial machine setup. Wait for it...
Write-Verbose 'Waiting for VM initial setup...'
try {
Wait-VM -Name $VMName -For Reboot
} catch {
# Win 2016 RTM doesn't have "Reboot" in WaitForVMTypes type.
# Wait until heartbeat service stops responding.
$heartbeatService = ($vm | Get-VMIntegrationService -Name 'Heartbeat')
while ($heartbeatService.PrimaryStatusDescription -eq 'OK') { Start-Sleep 1 }
}
Write-Verbose 'Waiting for VM integration services (2)...'
Wait-VM -Name $VMName -For Heartbeat
# Removes DVD and metadata.iso
$dvd | Remove-VMDvdDrive
$metadataIso | Remove-Item -Force
# Return the VM created.
Write-Verbose 'All done!'
$vm