diff --git a/.assets/provision/autoexec.sh b/.assets/provision/autoexec.sh new file mode 100755 index 00000000..7986a51f --- /dev/null +++ b/.assets/provision/autoexec.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env sh +# :script executed on WSL startup via [boot] command defined in wsl.conf diff --git a/wsl/wsl_network_fix.ps1 b/wsl/wsl_network_fix.ps1 index 9497ad6b..e12ddca5 100644 --- a/wsl/wsl_network_fix.ps1 +++ b/wsl/wsl_network_fix.ps1 @@ -70,7 +70,7 @@ begin { } # instantiate string builder - $builder = [System.Text.StringBuilder]::new("# Generated by wsl_network_fix.ps1 on $((Get-Date).ToString('s'))") + $builder = [System.Text.StringBuilder]::new("# Generated by wsl_network_fix.ps1 on $((Get-Date).ToString('s'))`n") } process { @@ -113,20 +113,20 @@ process { $idx = -1 $selection = Read-Host -Prompt "Please select the interface for propagating DNS Servers:`n$($list | Format-Table | Out-String)" [bool]$returnedInt = [int]::TryParse($selection, [ref]$idx) - } until ($returnedInt -and $idx -ge 0 -and $idx -lt $netAdapters.Count) + } until ($returnedInt -and $idx -ge 0 -and $idx -lt $ipConfig.Count) $dnsServers = $ipConfig[$idx].DNSServer $dnsServers.ForEach({ $builder.AppendLine("nameserver $_") | Out-Null }) } - # get DNS suffix search list - $searchSuffix = (Get-DnsClientGlobalSetting).SuffixSearchList -join ',' - if ($searchSuffix) { - $builder.AppendLine("search $searchSuffix") | Out-Null - } # get distro default gateway $def_gtw = (wsl.exe -d $Distro -u root --exec sh -c 'ip route show default' | Select-String '(?<=via )[\d\.]+(?= dev)').Matches.Value if ($def_gtw) { $builder.AppendLine("nameserver $def_gtw") | Out-Null } + # get DNS suffix search list + $searchSuffix = (Get-DnsClientGlobalSetting).SuffixSearchList -join ',' + if ($searchSuffix) { + $builder.AppendLine("search $searchSuffix") | Out-Null + } $builder.AppendLine('options timeout:1 retries:1') | Out-Null $resolvConf = $builder.ToString().Replace("`r`n", "`n") # save resolv.conf file diff --git a/wsl/wsl_setup.ps1 b/wsl/wsl_setup.ps1 index c0454f99..03fe1726 100644 --- a/wsl/wsl_setup.ps1 +++ b/wsl/wsl_setup.ps1 @@ -225,14 +225,15 @@ process { '[ -d $HOME/miniconda3 ] && python="true" || python="false";', '[ -f $HOME/.ssh/id_ed25519 ] && ssh_key="true" || ssh_key="false";', '[ -d /mnt/wslg ] && wslg="true" || wslg="false";', + '[ -x /etc/autoexec.sh ] && wsl_boot="true" || wsl_boot="false";', 'git_user_name="$(git config --global --get user.name 2>/dev/null)";', '[ -n "$git_user_name" ] && git_user="true" || git_user="false";', 'git_user_email="$(git config --global --get user.email 2>/dev/null)";', '[ -n "$git_user_email" ] && git_email="true" || git_email="false";', 'grep -qw "systemd.*true" /etc/wsl.conf 2>/dev/null && systemd="true" || systemd="false";', 'grep -Fqw "dark" /etc/profile.d/gtk_theme.sh 2>/dev/null && gtkd="true" || gtkd="false";', - 'printf "{\"user\":\"$(id -un)\",\"shell\":$shell,\"k8s_base\":$k8s_base,\"k8s_ext\":$k8s_ext,', - '\"omp\":$omp,\"az\":$az,\"wslg\":$wslg,\"python\":$python,\"systemd\":$systemd,\"gtkd\":$gtkd,', + 'printf "{\"user\":\"$(id -un)\",\"shell\":$shell,\"k8s_base\":$k8s_base,\"k8s_ext\":$k8s_ext,\"omp\":$omp,', + '\"az\":$az,\"wslg\":$wslg,\"wsl_boot\":$wsl_boot,\"python\":$python,\"systemd\":$systemd,\"gtkd\":$gtkd,', '\"pwsh\":$pwsh,\"zsh\":$zsh,\"git_user\":$git_user,\"git_email\":$git_email,\"ssh_key\":$ssh_key}"' ) # check existing distro setup @@ -287,6 +288,11 @@ process { Write-Warning 'SSL certificate problem: self-signed certificate in certificate chain. Script execution halted.' exit } + # *boot setup + if (-not $chk.wsl_boot) { + wsl.exe --distribution $Distro --user root install -m 0755 .assets/provision/autoexec.sh /etc + Set-WslConf -Distro $Distro -ConfDict ([ordered]@{ boot = @{ command = '"[ -x /etc/autoexec.sh ] && /etc/autoexec.sh || true"' } }) + } switch ($scopes) { distrobox { Write-Host 'installing distrobox...' -ForegroundColor Cyan diff --git a/wsl/wsl_wslg.ps1 b/wsl/wsl_wslg.ps1 index 4e42173c..15e3589f 100644 --- a/wsl/wsl_wslg.ps1 +++ b/wsl/wsl_wslg.ps1 @@ -7,14 +7,14 @@ Enable/disable WSLg. Specify the value to true or false to enable/disable WSLg. .EXAMPLE -wsl/wsl_wslg.ps1 -WSLg 'true' -wsl/wsl_wslg.ps1 -WSLg 'false' +wsl/wsl_wslg.ps1 -GuiApplications 'true' +wsl/wsl_wslg.ps1 -GuiApplications 'false' #> [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0)] [ValidateSet('true', 'false')] - [string]$WSLg + [string]$GuiApplications ) begin { @@ -36,14 +36,14 @@ process { if (Test-Path "$HOME/.wslconfig") { $wslConfig = Get-Content -Path "$HOME/.wslconfig" | ConvertFrom-Cfg if ($wslConfig.wsl2) { - $wslConfig.wsl2.guiApplications = $WSLg + $wslConfig.wsl2.guiApplications = $GuiApplications } else { - $wslConfig.wsl2 = @{ guiApplications = $WSLg } + $wslConfig.wsl2 = @{ guiApplications = $GuiApplications } } } else { $wslConfig = [ordered]@{ wsl2 = @{ - guiApplications = $WSLg + guiApplications = $GuiApplications } } } @@ -54,5 +54,5 @@ process { } end { - Write-Host "WSLg $($WSLg -eq 'true' ? 'enabled': 'disabled')." + Write-Host "WSLg $($GuiApplications -eq 'true' ? 'enabled': 'disabled')." }