Wsl2DebianEnv/scripts/system/install-debian.ps1
Mars 6ddba3e722 Initial commit
feat: 完成 Zsh 插件配置脚本的优化和测试 - 修复 autojump 配置冲突 - 清理重复的插件安装 - 优化脚本结构和注释

feat: 优化shell脚本 - 1. 增强错误处理和恢复机制 2. 添加网络连接优化和镜像源支持 3. 改进进度显示和用户交互 4. 优化配置文件管理和备份 5. 改进插件管理机制 6. 增强依赖检查和安装 7. 添加完整的日志记录功能 8. 修复字体安装相关问题

docs: 完善shell、system和utils部分的README文档 - 1. 添加详细的脚本说明和使用方法 2. 补充依赖要求和注意事项 3. 添加常见问题解答 4. 更新版本日志

feat(editor): 优化 neovim 和 nvchad 安装脚本,添加性能优化配置和详细文档

feat: 优化Python开发环境安装脚本,分离基础包和机器学习包,修复virtualenvwrapper配置

feat: 优化开发工具安装脚本,统一使用common.sh中的函数,改进错误处理

fix: 修复DNMP安装脚本,跳过自动配置PHP开发环境

fix: 提交删除的 init.sh 和 mysql.sh 文件
2025-03-25 16:14:03 +08:00

213 lines
6.4 KiB
PowerShell

# WSL2 Environment Upgrade Script
# This script upgrades Debian system packages and configures WSL2 settings
# Set error action
$ErrorActionPreference = 'Stop'
# Color definitions
$Red = [System.ConsoleColor]::Red
$Green = [System.ConsoleColor]::Green
$Yellow = [System.ConsoleColor]::Yellow
# Logging functions
function Write-Info {
param([string]$Message)
Write-Host '[INFO] ' -NoNewline -ForegroundColor $Green
Write-Host $Message
}
function Write-Warn {
param([string]$Message)
Write-Host '[WARN] ' -NoNewline -ForegroundColor $Yellow
Write-Host $Message
}
function Write-Error {
param([string]$Message)
Write-Host '[ERROR] ' -NoNewline -ForegroundColor $Red
Write-Host $Message
}
function Wait-KeyPress {
Write-Host "`nPress any key to exit..." -NoNewline
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
Write-Host ''
}
function Get-UserConfirmation {
param([string]$Message)
$response = Read-Host -Prompt "$Message [y/N]"
return $response -eq 'y' -or $response -eq 'Y'
}
function Get-CustomPath {
param([string]$DefaultPath)
Write-Host "`nCurrent WSL installation path: $DefaultPath"
$response = Read-Host -Prompt "Enter new installation path or press Enter to keep current"
if ([string]::IsNullOrWhiteSpace($response)) {
return $DefaultPath
}
return $response
}
# Check administrator privileges
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Warn 'Administrator privileges required'
Write-Info 'Please run this script as Administrator'
Wait-KeyPress
exit 1
}
Write-Info "`nStarting WSL2 environment configuration...`n"
Write-Info 'Note: Please make sure you have manually updated WSL2 kernel before proceeding.'
Write-Info 'You can update WSL2 kernel by running "wsl --update" in an administrator PowerShell.'
Write-Host ''
# Check if Debian is installed
$debianInstalled = $false
try {
wsl -d Debian echo "Check Debian" 2>$null
$debianInstalled = $true
Write-Info "Found existing Debian installation"
} catch {
Write-Info "No existing Debian installation found"
}
# Handle existing Debian installation
if ($debianInstalled) {
if (Get-UserConfirmation "Do you want to remove the existing Debian installation?") {
Write-Info "Removing existing Debian installation..."
try {
wsl --unregister Debian
Write-Info "Existing Debian installation removed"
$debianInstalled = $false
} catch {
Write-Error "Failed to remove Debian: $($_.Exception.Message)"
Wait-KeyPress
exit 1
}
}
}
# Get custom installation path if Debian needs to be installed
if (-not $debianInstalled) {
$defaultPath = "$env:USERPROFILE\WSL\Debian"
$customPath = Get-CustomPath $defaultPath
if ($customPath -ne $defaultPath) {
Write-Info "Setting up custom installation path: $customPath"
# Create directory if it doesn't exist
if (-not (Test-Path $customPath)) {
try {
New-Item -ItemType Directory -Path $customPath -Force | Out-Null
Write-Info "Created directory: $customPath"
} catch {
Write-Error "Failed to create directory: $($_.Exception.Message)"
Wait-KeyPress
exit 1
}
}
# Set WSL default installation path
Write-Info "Setting WSL installation path..."
try {
[System.Environment]::SetEnvironmentVariable('WSLENV', "WSLPATH/up:$customPath", [System.EnvironmentVariableTarget]::User)
Write-Info "WSL installation path set to: $customPath"
} catch {
Write-Error "Failed to set WSL installation path: $($_.Exception.Message)"
Wait-KeyPress
exit 1
}
}
# Install Debian
Write-Info "Installing Debian..."
try {
wsl --install -d Debian
Write-Info "Debian installation completed"
# Wait for Debian to be ready
Write-Info "Waiting for Debian to be ready..."
Start-Sleep -Seconds 10
} catch {
Write-Error "Failed to install Debian: $($_.Exception.Message)"
Wait-KeyPress
exit 1
}
}
# Shutdown WSL to ensure clean state
Write-Info 'Shutting down WSL to ensure clean state...'
try {
wsl --shutdown
Start-Sleep -Seconds 2
Write-Info 'WSL shutdown completed'
} catch {
Write-Error "Failed to shutdown WSL: $($_.Exception.Message)"
Wait-KeyPress
exit 1
}
# Update Debian system
Write-Info 'Updating Debian system packages...'
try {
# Update package lists
Write-Info 'Updating package lists...'
wsl -d Debian -u root apt update
# Upgrade packages
Write-Info 'Upgrading packages...'
wsl -d Debian -u root apt upgrade -y
# Clean up
Write-Info 'Cleaning up...'
wsl -d Debian -u root apt autoremove -y
wsl -d Debian -u root apt clean
Write-Info 'Debian system update completed'
} catch {
Write-Error "Failed to update Debian system: $($_.Exception.Message)"
Wait-KeyPress
exit 1
}
# Configure WSL memory limit
Write-Info 'Configuring WSL memory limit...'
try {
$wslConfigPath = "$env:USERPROFILE\.wslconfig"
$wslConfig = @"
[wsl2]
memory=8GB
processors=4
swap=2GB
"@
if (Test-Path $wslConfigPath) {
Write-Info 'Backing up existing .wslconfig...'
Copy-Item $wslConfigPath "$wslConfigPath.bak"
}
$wslConfig | Out-File $wslConfigPath -Encoding UTF8
Write-Info 'WSL memory limit configured'
} catch {
Write-Error "Failed to configure WSL memory limit: $($_.Exception.Message)"
Wait-KeyPress
exit 1
}
Write-Host "`n============================================"
Write-Host " Configuration Complete! "
Write-Host "============================================"
Write-Host "`nYour WSL2 environment has been configured:`n"
Write-Info '1. Debian system packages updated'
Write-Info '2. WSL memory limit configured (8GB RAM, 4 cores, 2GB swap)'
if (-not [string]::IsNullOrWhiteSpace($customPath)) {
Write-Info "3. WSL installation path set to: $customPath"
}
Write-Info 'Please restart your computer to apply all changes.'
Write-Host "============================================"
# Wait for user input before exit
Wait-KeyPress