-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-setup-wsl.ps1
150 lines (120 loc) · 5.53 KB
/
1-setup-wsl.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
$ErrorActionPreference = "Stop"
function Get-RegistryItemValue {
param(
[string]$KeyPath,
[string]$ItemName
)
try {
$value = Get-ItemProperty -Path $KeyPath -Name $ItemName -ErrorAction Stop | Select-Object -ExpandProperty $ItemName
}
catch {
$value = $null
}
return $value
}
function Enable-WindowsFeature {
param(
[string]$FeatureName
)
$feature = Get-WindowsOptionalFeature -Online -FeatureName $FeatureName
if ($feature.State -ne 'Enabled') {
Enable-WindowsOptionalFeature -Online -FeatureName $FeatureName
}
}
$WslDistribution = "Ubuntu-22.04"
$WslDistributionInstallerName = "ubuntu2204"
$WslKernelVersion = '6.11.5'
$WslKernelPath = 'c:\data\wsl2'
$WslUsername = 'taliesins'
$NatNetwork = '10.152.0.0/16'
$NatGatewayIpAddress = '10.152.0.1'
$NatIpAddress = '10.152.0.5'
# #Enable WSL pre-requisities
# Enable-WindowsFeature -FeatureName Microsoft-Windows-Subsystem-Linux
# Enable-WindowsFeature -FeatureName VirtualMachinePlatform
# #Setup WSL instance
# $env:WSL_UTF8 = "1"
# wsl --shutdown
# wsl --update
# wsl --set-default-version 2
# $foundInstance = $(wsl -l | % { $_.Replace("`0", "") } | ? { $_ -match "^$($WslDistribution)" })
# if (!$foundInstance) {
# wsl --list --online
# wsl --install $WslDistribution --no-launch
# & "$($env:LOCALAPPDATA)\Microsoft\WindowsApps\$($WslDistributionInstallerName)" install --root
# }
# wsl --setdefault $WslDistribution
# wsl --shutdown
# #Download custom kernel
# $customKernelPath = "$($WslKernelPath)\bzImage-x86_64"
# if (!(Test-Path $customKernelPath)) {
# $url = "https://github.com/taliesins/WSL2-Linux-Kernel-Rolling/releases/download/linux-wsl-stable-$($WslKernelVersion)/bzImage-x86_64"
# New-Item -Path $WslKernelPath -ItemType Directory -Force
# $webClient = New-Object System.Net.WebClient
# $webClient.DownloadFile($url, $customKernelPath)
# $webClient.Dispose()
# }
# #Configure wsl network options
# $RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss'
# $CurrentNatNetwork = Get-RegistryItemValue -KeyPath $RegistryPath -ItemName 'NatNetwork'
# if (!$CurrentNatNetwork -or $CurrentNatNetwork -ne $NatNetwork) {
# New-ItemProperty -Path $RegistryPath -Name 'NatNetwork' -Value $NatNetwork -PropertyType String -Force
# }
# $CurrentNatGatewayIpAddress = Get-RegistryItemValue -KeyPath $RegistryPath -ItemName 'NatGatewayIpAddress'
# if (!$CurrentNatGatewayIpAddress -or $CurrentNatGatewayIpAddress -ne $NatGatewayIpAddress) {
# New-ItemProperty -Path $RegistryPath -Name 'NatGatewayIpAddress' -Value $NatGatewayIpAddress -PropertyType String -Force
# }
# $RegistryPath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss'
# $CurrentNatIpAddress = Get-RegistryItemValue -KeyPath $RegistryPath -ItemName 'NatIpAddress'
# if (!$CurrentNatIpAddress -or $CurrentNatIpAddress -ne $NatIpAddress) {
# New-ItemProperty -Path $RegistryPath -Name 'NatIpAddress' -Value $NatIpAddress -PropertyType String -Force
# }
# # Configure wsl options
# $memory = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).sum / 1gb
# if ($memory -gt 8) {
# $memory = $memory - 4
# }
# $processors = (Get-ComputerInfo).CsNumberOfLogicalProcessors
# $wslConfigPath = "$env:USERPROFILE\.wslconfig"
# $wslConfig = @"
# [wsl2]
# ipv6=true
# # by default WSL supports both but this causes issues for Kubernetes instances
# kernelCommandLine = cgroup_no_v1=all
# # Limits VM memory to use no more than $($memory)GB, this can be set as whole numbers using GB or MB
# memory=$($memory)GB
# # Sets the VM to use all the virtual processors
# processors=$processors
# # Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
# kernel=$($customKernelPath.Replace("\","\\"))
# "@
# Set-Content -Path $wslConfigPath -Value $wslConfig
# #Fix nvidia
# $nvidiaLibPath = "$env:windir\System32\lxss\lib"
# if (Test-Path "$nvidiaLibPath\libcuda.so.1.1") {
# if (!(Test-Path -Path "$nvidiaLibPath\libcuda.so")) {
# New-Item -Path "$nvidiaLibPath\libcuda.so" -ItemType SymbolicLink -Value "$nvidiaLibPath\libcuda.so.1.1"
# }
# if (!(Test-Path -Path "$nvidiaLibPath\libcuda.so.1")) {
# New-Item -Path "$nvidiaLibPath\libcuda.so.1" -ItemType SymbolicLink -Value "$nvidiaLibPath\libcuda.so.1.1"
# }
# }
# # if (!(Test-Path -Path "$env:windir\System32\DriverStore\FileRepository\nv_dispig.inf_amd64_2fe7c165c5dd3267\libdxcore.so")) {
# # New-Item -Path "$env:windir\System32\DriverStore\FileRepository\nv_dispig.inf_amd64_2fe7c165c5dd3267\libdxcore.so" -ItemType SymbolicLink -Value "$nvidiaLibPath\libdxcore.so"
# # }
# # Update the system
# #wsl -d $WslDistribution -u root bash -ic "whoami"
# # create your user and add it to sudoers
# wsl -d $WslDistribution -u root bash -ic "./scripts/1-create-user.sh '$WslUsername'"
# wsl --shutdown
# # Update the system
# wsl -d $WslDistribution -u $WslUsername bash -c "./scripts/2-install-ansible.sh"
# # $windowsSshPath = "$($env:USERPROFILE)\.ssh"
# # $gitRepoUri = $(git config --get remote.origin.url)
# # if (!$gitRepoUri) {
# # $gitRepoUri = 'https://github.com/taliesins/localdev-wsl.git'
# # }
wsl -d $WslDistribution -u $WslUsername bash -ic "./scripts/3-install-ansible-solution.sh '$windowsSshPath' '$gitRepoUri' '$NatNetwork' '$NatIpAddress' "
wsl -d $WslDistribution -u $WslUsername bash -c "./scripts/4-run-ansible-solution.sh "
# # ensure WSL Distro is restarted when first used with user account
# # wsl -t $WslDistribution