Skip to content

Instantly share code, notes, and snippets.

@so0k
Last active September 11, 2018 04:14
Show Gist options
  • Save so0k/cb9cdabcbf88560d817a to your computer and use it in GitHub Desktop.
Save so0k/cb9cdabcbf88560d817a to your computer and use it in GitHub Desktop.
#This gist expects msys git installed in path ${env:ProgramFiles(x86)}\Git\
$title = "Create CoreOS Docker host for Hyper-V"
$message = "Are you sure you want to start the setup for CoreOS, please ensure msys git has been installed first?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Confirm msys git is installed and it is safe to proceed with setup."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Cancel setup."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {echo "proceeding with installation"}
1 {return}
}
#should validate if git exists with (get-command git)
#Enable Windows Hyper-V Feature
Enable-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online -All -NoRestart
Import-Module Hyper-V
#get first physical adapter which is up
$NetAdapter = (Get-NetAdapter -Physical | ? Status -eq "Up")[0]
#create external switch for OS + Host-only switch for VM
$external = New-VMSwitch -Name "External Switch" -NetAdapterName $NetAdapter.Name -AllowManagementOS $true -Notes 'Parent OS, VMs, LAN'
$internal = New-VMSwitch -Name "Host-Only Switch" -SwitchType Internal -Notes 'Parent OS, and internal VMs'
#get network adapter created for VMSwitch & try to enable internet connection sharing
#$externalVMSwitchAdapter = Get-NetAdapter | ? (Get-VMNetworkAdapter -ManagementOS -SwitchName $external.Name).DeviceId
#$internalVMSwitchAdapter = Get-NetAdapter | ? (Get-VMNetworkAdapter -ManagementOS -SwitchName $internal.Name).DeviceId
Write-Host -ForeGround Red "Manual step required:"
Write-Host -ForeGround Red "Open 'control netconnections' and enable Internet Connection sharing from $($externalVMSwitchAdapter.Name) with $($internalVMSwitchAdapter)"
pause
#specify location for putty / plink & pageant
$BIN = 'D:\Bin\'
$vm_hostname = 'coreos'
$vm_ip = '192.168.137.5/24'
$vm_dns1 = '8.8.8.8'
$vm_dns2 = '8.8.4.4'
#use Internet Connection Sharing on the Virtual switch connected to your network bridge
$ics_gateway = '192.168.137.1' 
#this is coreos insecure_key, replace with your own public key
$rsa_key = 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ=='
$coreos_url = "http://alpha.release.core-os.net/amd64-usr/current/coreos_production_hyperv_image.vhd.bz2"
$coreos = "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\coreos_production_hyperv_image.vhd"
$config2_url = "https://github.com/paulshir/coreos-hyperv/raw/master/files/config2_base.vhdx.bz2"
$config2 = "C:\Users\Public\Documents\Hyper-V\Virtual hard disks\config2_base.vhdx"
#download Putty & pageant
$PUTTY = Join-Path $BIN 'putty.exe'
$PLINK = Join-Path $BIN 'plink.exe'
$PAGEANT = Join-Path $BIN 'pageant.exe'
$PUTTYGEN = Join-Path $BIN 'puttygen.exe'
If (!(Test-Path $PUTTY)) { Invoke-RestMethod "http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe" -OutFile $PUTTY }
If (!(Test-Path $PLINK)) { Invoke-RestMethod "http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe" -OutFile $PLINK }
If (!(Test-Path $PAGEANT)) { Invoke-RestMethod "http://the.earth.li/~sgtatham/putty/latest/x86/pageant.exe" -OutFile $PAGEANT }
If (!(Test-Path $PUTTYGEN)) { Invoke-RestMethod "http://the.earth.li/~sgtatham/putty/latest/x86/puttygen.exe" -OutFile $PUTTYGEN }
$res = (!(Test-Path $PUTTY)) -or (!(Test-Path $PLINK)) -or (!(Test-Path $PAGEANT))
If ($res) {
Write-Output "Commands 'putty', 'pageant' and 'plink' are required but not installed."
} else {
#add putty to PATH environment variable:
[System.Environment]::SetEnvironmentVariable("PATH",($env:Path + ";$BIN"))
#now where.exe shld find plink too!
[System.Environment]::SetEnvironmentVariable("GIT_SSH",(where.exe plink),"User")
#set home variable (required by powershell script to load ssh keys)
[Environment]::SetEnvironmentVariable("HOME", $env:USERPROFILE, "User")
}
$msysgit = Split-Path (get-command git).Definition -Parent | Split-Path -Parent
$bzip = Join-Path $msysgit "bin\bzip2.exe"
If (!(Test-Path "$coreos.bz2"))
{
Invoke-RestMethod -Uri $coreos_url -OutFile "$coreos.bz2"
}
If (!(Test-Path $coreos_vhdbz2))
{
& cmd /C "`"`"$Bzip`" -c -d -k `"$coreos.bz2`" > `"$coreos`"`"" | Out-Null
}
If (!(Test-Path "$config2.bz2"))
{
Invoke-RestMethod -Uri "$config2_url" -OutFile "$config2.bz2"
}
If(!(Test-Path $config2))
{
& cmd /C "`"`"$Bzip`" -c -d -k `"$config2.bz2`" > `"$config2`"`"" | Out-Null
}
$vhd = Mount-VHD -Path $config2 -ErrorAction:Stop -Passthru | Get-Disk | Get-Partition | Get-Volume
Start-Sleep -s 1
if(!(Test-Path "$($vhd.DriveLetter):\openstack\latest"))
{
mkdir "$($vhd.DriveLetter):\openstack\latest" | Out-Null
}
#create or replace user_data
# https://coreos.com/docs/cluster-management/setup/cloudinit-config-drive/
@"
#cloud-config
hostname: $vm_hostname
ssh_authorized_keys:
- $rsa_key
coreos:
units:
- name: 00-eth0.network
runtime: true
content: |
[Match]
Name=eth0
[Network]
Address=$vm_ip
Gateway=$ics_gateway
DNS=$vm_dns1
DNS=$vm_dns2
"@ | Set-Content "$($vhd.DriveLetter):\openstack\latest\user_data"
Dismount-VHD $config2 | Out-Null
New-VM -Name $vm_hostname -MemoryStartupBytes 1GB -VHDPath $coreos -BootDevice VHD -SwitchName $internal.Name -Verbose
Add-VMHardDiskDrive -VMName $vm_hostname -ControllerType IDE -ControllerNumber 1 -ControllerLocation 0 -Path $config2
#finally start the machine
Start-VM -Name $vm_hostname
Write-Host -ForeGround Green "Hyper-V VM: $vm_host created"
Write-Host -ForeGround Green "start pageant, load coreos insecure_key & run: 'putty core@$vm_ip'"
@so0k
Copy link
Author

so0k commented Jul 10, 2015

Todo:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment