Last active
September 5, 2021 23:21
-
-
Save hnakamur/49d3a4d726c41fbc3100744403663dcc to your computer and use it in GitHub Desktop.
Launch Ubuntu bionic VM with Hyper-V
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" | |
& .\cloudinitiso.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 4096mb -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`"" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cloud-config | |
password: ubuntu | |
chpasswd: | |
expire: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The empty
meta-data
is OK, but it needs to be exist.Build
cloudinitiso.exe
with the following command.