Launch Ubuntu bionic VM with Hyper-V
|
$VMName = "Ubuntu Test" |
|
|
|
# Delete the VM if it is around |
|
If ((Get-VM -Name $VMName).Count -gt 0) {stop-vm $VMName -TurnOff -Confirm:$false -Passthru | Remove-VM -Force} |
|
# This script is based on |
|
# https://github.com/BenjaminArmstrong/Hyper-V-PowerShell/blob/8d166e1c2ba71c2b5120bc5490ea9be01891ec74/Ubuntu-VM-Build/BaseUbuntuBuild.ps1 |
|
# and modified for Ubuntu bionic. |
|
|
|
# Download from http://cloud-images.ubuntu.com/releases/bionic/release/ |
|
$imgPath = "${Env:USERPROFILE}\Downloads\ubuntu-18.04-server-cloudimg-amd64.img" |
|
|
|
$VMName = "Ubuntu Test" |
|
$virtualSwitchName = "Default Switch" |
|
$vmPath = "${Env:Public}\Documents\Hyper-V\$VMName" |
|
|
|
$vhdx = "$($vmPath)\test.vhdx" |
|
$cloudInitIso = "$($vmPath)\metadata.iso" |
|
|
|
& .\cloudinit.exe -iso $cloudInitIso |
|
|
|
# Download qemu-img from http://www.cloudbase.it/qemu-img-windows/ |
|
# and extract it to C:\qemu-img |
|
& C:\qemu-img\qemu-img convert -f qcow2 $imgPath -O vhdx -o subformat=dynamic $vhdx |
|
Resize-VHD -Path $vhdx -SizeBytes 100GB |
|
|
|
# Create new virtual machine and start it |
|
new-vm $VMName -MemoryStartupBytes 2048mb -VHDPath $vhdx -Generation 1 ` |
|
-SwitchName $virtualSwitchName -Path $vmPath | Out-Null |
|
set-vm -Name $VMName -ProcessorCount 2 |
|
Set-VMDvdDrive -VMName $VMName -Path $cloudInitIso |
|
Start-VM $VMName |
|
|
|
# Open up VMConnect |
|
Invoke-Expression "vmconnect.exe localhost `"$VMName`"" |
|
package main |
|
|
|
import ( |
|
"flag" |
|
"io" |
|
"log" |
|
"os" |
|
|
|
"github.com/diskfs/go-diskfs/filesystem/iso9660" |
|
) |
|
|
|
func main() { |
|
isoFilename := flag.String("iso", "", "output ISO filename") |
|
flag.Parse() |
|
|
|
err := run(*isoFilename) |
|
if err != nil { |
|
log.Fatal(err) |
|
} |
|
} |
|
|
|
func run(isoFilename string) error { |
|
file, err := os.Create(isoFilename) |
|
if err != nil { |
|
return err |
|
} |
|
defer file.Close() |
|
|
|
fs, err := iso9660.Create(file, 0, 0, 2048) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
if err = fs.Mkdir("/"); err != nil { |
|
return err |
|
} |
|
|
|
if err := addFile(fs, "meta-data"); err != nil { |
|
return err |
|
} |
|
|
|
if err := addFile(fs, "user-data"); err != nil { |
|
return err |
|
} |
|
|
|
if err := fs.Finalize(iso9660.FinalizeOptions{ |
|
RockRidge: true, |
|
VolumeIdentifier: "cidata", |
|
}); err != nil { |
|
return err |
|
} |
|
|
|
return nil |
|
} |
|
|
|
func addFile(fs *iso9660.FileSystem, filename string) error { |
|
src, err := os.Open(filename) |
|
if err != nil { |
|
return err |
|
} |
|
defer src.Close() |
|
|
|
dest, err := fs.OpenFile("/"+filename, os.O_CREATE|os.O_WRONLY) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
_, err = io.Copy(dest, src) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
return nil |
|
} |
|
#cloud-config |
|
password: ubuntu |
|
chpasswd: |
|
expire: false |
This comment has been minimized.
hnakamur commentedOct 30, 2019
The empty
meta-data
is OK, but it needs to be exist.Build
cloudinit.exe
with the following command.