-
Notifications
You must be signed in to change notification settings - Fork 72
/
New-VMFromDebianImage.ps1
355 lines (294 loc) · 10 KB
/
New-VMFromDebianImage.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
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
#Requires -RunAsAdministrator
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$SourcePath,
[ValidateScript({
$existingVm = Get-VM -Name $_ -ErrorAction SilentlyContinue
if (-not $existingVm) {
return $True
}
throw "There is already a VM named '$VMName' in this server."
})]
[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',
[ValidateScript({
if ($_ -match '^([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})$') {
return $True
}
throw "-MacAddress must be in format 'xx:xx:xx:xx:xx:xx'."
})]
[string]$MacAddress,
[ValidateScript({
$sIp, $suffix = $_.Split('/')
if ($ip = $sIp -as [ipaddress]) {
$maxSuffix = if ($ip.AddressFamily -eq 'InterNetworkV6') { 128 } else { 32 }
if ($suffix -in 1..$maxSuffix) {
return $True
}
throw "Invalid -IPAddress suffix ($suffix)."
}
throw "Invalid -IPAddress ($sIp)."
})]
[string]$IPAddress,
[string]$Gateway,
[string[]]$DnsAddresses = @('1.1.1.1','1.0.0.1'),
[string]$InterfaceName = 'eth0',
[string]$VlanId,
[Parameter(Mandatory=$false, ParameterSetName='RootPassword')]
[Parameter(Mandatory=$false, ParameterSetName='RootPublicKey')]
[string]$SecondarySwitchName,
[ValidateScript({
if ($_ -match '^([0-9A-Fa-f]{2}:){5}([0-9A-Fa-f]{2})$') {
return $True
}
throw "-SecondaryMacAddress must be in format 'xx:xx:xx:xx:xx:xx'."
})]
[string]$SecondaryMacAddress,
[ValidateScript({
$sIp, $suffix = $_.Split('/')
if ($ip = $sIp -as [ipaddress]) {
$maxSuffix = if ($ip.AddressFamily -eq 'InterNetworkV6') { 128 } else { 32 }
if ($suffix -in 1..$maxSuffix) {
return $True
}
throw "Invalid -SecondaryIPAddress suffix ($suffix)."
}
throw "Invalid -SecondaryIPAddress ($sIp)."
})]
[string]$SecondaryIPAddress,
[string]$SecondaryInterfaceName,
[string]$SecondaryVlanId,
[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 = Get-WmiObject -namespace root\virtualization\v2 Msvm_VirtualSystemManagementService
$vmmsSettings = Get-WmiObject -namespace root\virtualization\v2 Msvm_VirtualSystemManagementServiceSettingData
$vhdxPath = Join-Path $vmmsSettings.DefaultVirtualHardDiskPath "$VMName.vhdx"
# 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 -VHDPath $vhdxPath -SwitchName $SwitchName
$vm | Set-VMProcessor -Count $ProcessorCount
$vm | Get-VMIntegrationService -Name "Guest Service Interface" | Enable-VMIntegrationService
$vm | Set-VMMemory -DynamicMemoryEnabled:$EnableDynamicMemory.IsPresent
# Sets Secure Boot Template.
# Set-VMFirmware -SecureBootTemplate 'MicrosoftUEFICertificateAuthority' doesn't work anymore (!?).
$vm | Set-VMFirmware -SecureBootTemplateId ([guid]'272e7447-90a4-4563-a4b9-8e4ab00526ce')
# Cloud-init 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 ($VlanId) {
$eth0 | Set-VMNetworkAdapterVlan -Access -VlanId $VlanId
}
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(':', '')
if ($SecondaryVlanId) {
$eth1 | Set-VMNetworkAdapterVlan -Access -VlanId $SecondaryVlanId
}
}
}
# 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: https://cloudinit.readthedocs.io/en/latest/reference/datasources/nocloud.html
$instanceId = [Guid]::NewGuid().ToString()
$metadata = @"
instance-id: $instanceId
local-hostname: $VMName
"@
$displayInterface = " $($InterfaceName): \4{$InterfaceName} \6{$InterfaceName}"
$displaySecondaryInterface = ''
if ($SecondarySwitchName) {
$displaySecondaryInterface = " $($SecondaryInterfaceName): \4{$SecondaryInterfaceName} \6{$SecondaryInterfaceName}`n"
}
$sectionWriteFiles = @"
write_files:
- content: |
\S{PRETTY_NAME} \n \l
$displayInterface
$displaySecondaryInterface
path: /etc/issue
owner: root:root
permissions: '0644'
"@
$sectionRunCmd = @'
runcmd:
- 'apt-get update'
- 'grep -o "^[^#]*" /etc/netplan/50-cloud-init.yaml > /etc/netplan/80-static.yaml' # https://unix.stackexchange.com/a/157607
- '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 update -y'
- 'apt install -y ca-certificates curl gnupg lsb-release'
- 'mkdir -p /etc/apt/keyrings'
- 'curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg'
- 'echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null'
- 'apt update -y'
- 'apt install -y docker-ce docker-ce-cli containerd.io docker-compose'
'@
}
$userdata = @"
#cloud-config
hostname: $FQDN
fqdn: $FQDN
disable_root: false
$sectionPasswd
$sectionWriteFiles
$sectionRunCmd
power_state:
mode: reboot
timeout: 300
"@
# Uses netplan to setup network.
if ($IPAddress) {
$NetworkConfig = @"
version: 2
ethernets:
$($InterfaceName):
match:
macaddress: $MacAddress
set-name: $($InterfaceName)
addresses: [$IPAddress]
nameservers:
addresses: [$($DnsAddresses -join ', ')]
routes:
- to: 0.0.0.0/0
via: $Gateway
on-link: true
"@
} else {
$NetworkConfig = @"
version: 2
ethernets:
$($InterfaceName):
match:
macaddress: $MacAddress
set-name: $($InterfaceName)
dhcp4: true
dhcp-identifier: mac
"@
}
if ($SecondarySwitchName) {
if ($SecondaryIPAddress) {
$NetworkConfig += @"
$($SecondaryInterfaceName):
match:
macaddress: $SecondaryMacAddress
set-name: $($SecondaryInterfaceName)
addresses: [$SecondaryIPAddress]
"@
} else {
$NetworkConfig += @"
$($SecondaryInterfaceName):
match:
macaddress: $SecondaryMacAddress
set-name: $($SecondaryInterfaceName)
dhcp4: true
dhcp-identifier: mac
"@
}
}
# Adds DVD with metadata.iso
. .\tools\Metadata-Functions.ps1
$metadataIso = New-MetadataIso -VMName $VMName $metadata -UserData $userdata -NetworkConfig $NetworkConfig
$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