Skip to content

Instantly share code, notes, and snippets.

@juliusl
Created January 13, 2023 19:12
Show Gist options
  • Save juliusl/4e7d39a665b23a7147966f482c1a51db to your computer and use it in GitHub Desktop.
Save juliusl/4e7d39a665b23a7147966f482c1a51db to your computer and use it in GitHub Desktop.
Cloud Init Hyper-V
$tempPath = [System.IO.Path]::GetTempPath() + [System.Guid]::NewGuid().ToString()
# ADK Download - https://www.microsoft.com/en-us/download/confirmation.aspx?id=39982
# You only need to install the deployment tools
$oscdimgPath = "C:\Program Files (x86)\Windows Kits\8.1\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe"
# Download qemu-img from here: http://www.cloudbase.it/qemu-img-windows/
$qemuImgPath = "E:\backbonev2\lib\bin\qemu-img-win-x64-2_3_0\qemu-img.exe"
# Update this to the release of Ubuntu that you want
$ubuntuPath = "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64"
$VMName = "UbuntuTest2"
$virtualSwitchName = "Default Switch"
$imageCachePath = "E:\dev\backbonev2\imagecache"
$vmPath = "E:\dev\backbonev2\VM2"
$vhdx = "$($vmPath)\$VMName.vhdx"
$metaDataIso = "$($vmPath)\metadata.iso"
# Get the timestamp of the latest build on the Ubuntu cloud-images site
$stamp = (Invoke-WebRequest "$($ubuntuPath).manifest").BaseResponse.LastModified.ToFileTimeUtc()
$metadata = @"
instance-id: guid-$([GUID]::NewGuid())
local-hostname: $($VMName)
"@
$networkconfig=@"
version: 2
ethernets:
eth0:
dhcp4: true
"@
# Check Paths
if (!(test-path $imageCachePath)) {mkdir $imageCachePath}
# Helper function for no error file cleanup
Function cleanupFile ([string]$file) {if (test-path $file) {Remove-Item $file}}
# Delete the VM if it is around
If ((Get-VM | ? name -eq $VMName).Count -gt 0)
{stop-vm $VMName -TurnOff -Confirm:$false -Passthru | Remove-VM -Force}
cleanupFile $vhdx
cleanupFile $metaDataIso
# Make temp location
if (!(test-path "$($imageCachePath)\ubuntu-$($stamp).img")) {
# If we do not have a matching image - delete the old ones and download the new one
Remove-Item "$($imageCachePath)\ubuntu-*.img"
Invoke-WebRequest "$($ubuntuPath).img" -UseBasicParsing -OutFile "$($imageCachePath)\ubuntu-$($stamp).img"
}
# Convert cloud image to VHDX
& $qemuImgPath convert -f qcow2 "$($imageCachePath)\ubuntu-$($stamp).img" -O vhdx -o subformat=dynamic $vhdx
Resize-VHD -Path $vhdx -SizeBytes 512GB
# Output meta and user data to files
Set-Content "$($vmPath)\NoCloud\meta-data" ([byte[]][char[]] "$metadata") -Encoding Byte
Set-Content "$($vmPath)\NoCloud\network-config" ([byte[]][char[]] "$networkconfig") -Encoding Byte
# Create meta data ISO image
& oscdimg.exe "$($vmPath)\NoCloud" $metaDataIso -j2 -lcidata
# Create new virtual machine and start it
New-VM $VMName -MemoryStartupBytes 32000mb -BootDevice VHD -VHDPath $vhdx -Generation 2 `
-SwitchName $virtualSwitchName -Path $vmPath | Out-Null
Set-VM -Name $VMName -ProcessorCount 2 -AutomaticStopAction ShutDown -AutomaticStartAction StartIfRunning -AutomaticStartDelay (Get-Random -Minimum 100 -Maximum 800)
Set-VMFirmware -VMName $VMName -EnableSecureBoot Off -FirstBootDevice (Get-VMHardDiskDrive -VMName $VMName)
Get-VM -VMname $VMName | Enable-VMIntegrationService -Name *
Add-VMDvdDrive -VMName $VMName
Set-VMDvdDrive -VMName $VMName -Path $metaDataIso
Start-VM $VMName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment