使用云镜像和cloud‑init在Windows Hyper‑V上创建Ubuntu 24.04虚拟机,自动解决稀疏VHDX修复、hv_netvsc网络配置、权限等常见问题。
Create Ubuntu 24.04 VMs on Windows Hyper-V from cloud images with cloud-init. Returns a Docker-ready VM with SSH access.
This is a base skill. It creates the VM. Other skills (soc-deploy-thehive, soc-deploy-misp) deploy applications onto it.
ParameterDefaultRequiredVM name-YesHyper-V hosthyperv-host (YOUR_HYPERV_IP)NoCPU cores2NoRAM4GBNoDisk40GBNoVM user password(generated)NoExtra cloud-init packages-NoNetwork switchDNS-NIC-SwitchNo
bash
# SSH to Hyper-V host
ssh hyperv-host "echo OK" 2>/dev/null || echo "FAIL: Cannot SSH to Hyper-V host"
# qemu-img on Windows
ssh hyperv-host 'where "C:\Program Files\qemu\qemu-img.exe"' 2>/dev/null || echo "FAIL: qemu-img not installed (choco install qemu -y)"
# genisoimage on Linux (for building cloud-init ISO)
which genisoimage || echo "FAIL: genisoimage not installed (apt install genisoimage)"
bash
# Password via env var (recommended, avoids shell history/process list exposure)
VM_PASSWORD="<password>" bash scripts/build-cidata-iso.sh <vm-name> [ssh-public-key]
# Or via stdin
echo "<password>" | bash scripts/build-cidata-iso.sh <vm-name> [ssh-public-key]
# Creates /tmp/<vm-name>-cidata.iso
The ISO contains three files:
user-data: deploy user, Docker, Compose v2, SSH password authmeta-data: instance-id and hostnamenetwork-config: hv_netvsc DHCP match (CRITICAL for Hyper-V networking)bash
# Cloud image (if not already cached)
wget -q https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img -O /tmp/ubuntu-24.04-cloud.img
scp /tmp/ubuntu-24.04-cloud.img hyperv-host:C:/Users/youruser/Downloads/
# Cloud-init ISO
scp /tmp/<vm-name>-cidata.iso hyperv-host:C:/Users/youruser/Downloads/
bash
# Copy script to host
scp scripts/create-vm.ps1 hyperv-host:C:/Users/youruser/Downloads/
# Execute (needs elevation)
ssh hyperv-host "powershell -ExecutionPolicy Bypass -File C:\\Users\\youruser\\Downloads\\create-vm.ps1 \
-VMName <vm-name> \
-CloudInitISO C:\\Users\\youruser\\Downloads\\<vm-name>-cidata.iso \
-DiskSizeGB <disk> -MemoryGB <ram> -CPUCount <cores>"
bash
sleep 90 # Cloud-init needs ~90 seconds
# Hyper-V VMs have MACs starting with 00-15-5d
arp -a | grep "00-15-5d"
# Get VM MAC to match
ssh hyperv-host "powershell (Get-VMNetworkAdapter -VMName '<vm-name>').MacAddress"
# PowerShell shows: 00155D38010A
# ARP shows: 00-15-5d-38-01-0a
bash
ssh deploy@<ip> "docker --version && docker compose version && echo 'VM READY'"
Report to caller:
text
VM Created: <vm-name>
IP: <ip>
SSH: deploy@<ip> (password: <password>)
Docker: installed
Docker Compose v2: installed
To destroy a VM completely:
bash
ssh hyperv-host "powershell -Command \"Stop-VM -Name '<vm-name>' -Force -TurnOff; Remove-VM -Name '<vm-name>' -Force; Remove-Item 'C:\\ProgramData\\Microsoft\\Windows\\Virtual Hard Disks\\<vm-name>.vhdx' -Force\""
Or use scripts/destroy-vm.ps1:
bash
scp scripts/destroy-vm.ps1 hyperv-host:C:/Users/youruser/Downloads/
ssh hyperv-host "powershell -ExecutionPolicy Bypass -File C:\\Users\\youruser\\Downloads\\destroy-vm.ps1 -VMName <vm-name>"
See references/gotchas.md for full details. Top blockers:
fsutil sparse setflag <path> 0 BEFORE Resize-VHD or error 0xC03A001Amatch: driver: hv_netvsc or VM gets no IPicacls /grant "NT VIRTUAL MACHINE\Virtual Machines:(F)" or Start-VM fails