Files
mini-pc-setup/scripts/install.bat
T
2026-11-15 00:00:49 +07:00

272 lines
16 KiB
Batchfile

@echo off
setlocal EnableDelayedExpansion
:: ============================================================================
:: CONFIGURATION (RESOLVE PARENT ROOT DIRECTORY FROM /scripts)
:: ============================================================================
set "SCRIPT_DIR=%~dp0"
for %%I in ("%SCRIPT_DIR%..") do set "ROOT_DIR=%%~fI\"
set "WSL_DISTRO=Ubuntu-22.04"
set "PROJECT_DIR=/opt/walmart"
set "REGISTRY_URL=gitea.digitoolsolutions.synology.me"
set "DEFAULT_KIOSK_USER=Torque Vault"
set "KIOSK_APP_DIR=C:\KioskApp"
set "KIOSK_DATA_DIR=C:\KioskBrowserData"
set "EDGE_EXE=C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
if not exist "%EDGE_EXE%" set "EDGE_EXE=C:\Program Files\Microsoft\Edge\Application\msedge.exe"
:: ============================================================================
:: 0. AUTO-ELEVATE TO ADMINISTRATOR
:: ============================================================================
net session >nul 2>&1
if %errorLevel% neq 0 (
echo Requesting Administrator privileges...
powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
exit /b
)
echo ============================================================================
echo DTS TORQUE VAULT - MASTER MINI-PC DEPLOYMENT (24/7 PRODUCTION EDITION)
echo ============================================================================
echo.
:: ============================================================================
:: PRE-FLIGHT CHECK: REQUIRED FILES, EDGE, INTERNET & BIOS VIRTUALIZATION
:: ============================================================================
echo [PRE-CHECK] Verifying required files, Edge, Internet and CPU Virtualization...
if exist "%EDGE_EXE%" goto :EDGE_OK
echo [ERROR] Microsoft Edge not found at "%EDGE_EXE%".
pause
exit /b 1
:EDGE_OK
if exist "%ROOT_DIR%.env" goto :ENV_OK
if exist "%ROOT_DIR%.env.template" (
copy /Y "%ROOT_DIR%.env.template" "%ROOT_DIR%.env" >nul
echo [ACTION REQUIRED] Created .env from .env.template. Configure it in Notepad, save, and close...
start /wait notepad.exe "%ROOT_DIR%.env"
)
:ENV_OK
if not exist "%ROOT_DIR%templates\startup.html" (
echo [ERROR] Missing required file: "%ROOT_DIR%templates\startup.html"
pause
exit /b 1
)
if not exist "%ROOT_DIR%compose.yml" (
echo [ERROR] Missing required file: "%ROOT_DIR%compose.yml"
pause
exit /b 1
)
if not exist "%ROOT_DIR%.env" (
echo [ERROR] Missing required file: "%ROOT_DIR%.env"
pause
exit /b 1
)
ping -n 1 8.8.8.8 >nul 2>&1
if %errorLevel% neq 0 (
echo [ERROR] No Internet connection detected! Connect LAN/Wi-Fi and run again.
pause
exit /b 1
)
powershell -NoProfile -Command "$cs = Get-CimInstance Win32_ComputerSystem; $cpu = Get-CimInstance Win32_Processor | Select-Object -First 1; if ($cs.HypervisorPresent -or $cpu.VirtualizationFirmwareEnabled) { exit 0 } else { exit 1 }"
if %errorLevel% neq 0 (
echo [ERROR] Hardware Virtualization is DISABLED in BIOS!
echo Please reboot into BIOS, enable Virtualization, and run again.
pause
exit /b 1
)
echo - Pre-flight checks passed!
echo.
:: ============================================================================
:: PHASE 1: CHECK & INSTALL WSL2 + UBUNTU-22.04 (ENFORCES REQUIRED REBOOT)
:: ============================================================================
wsl -d %WSL_DISTRO% -u root true >nul 2>&1
if %errorLevel% neq 0 (
wsl -l -q 2>nul | findstr /i "%WSL_DISTRO%" >nul
if !errorLevel! neq 0 (
echo [PHASE 1] WSL2 / %WSL_DISTRO% not found. Installing now...
wsl --install -d %WSL_DISTRO% --no-launch
if !errorLevel! neq 0 (
echo [ERROR] WSL installation failed. Ensure Virtualization is enabled in BIOS.
pause
exit /b 1
)
) else (
echo [PHASE 1] %WSL_DISTRO% is staged, but Windows has NOT rebooted yet!
)
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v "DTS_Master_Setup" /t REG_SZ /d "\"%~f0\"" /f >nul
echo.
echo ============================================================================
echo [REBOOT REQUIRED] Windows must restart to activate WSL2 kernel.
echo This installer will resume automatically after you log back in as Admin.
echo ============================================================================
echo.
pause
shutdown /r /t 3
exit /b
)
:: ============================================================================
:: PHASE 2: INITIALIZE UBUNTU USER & ENABLE SYSTEMD IN WSL2
:: ============================================================================
echo [1/7] Initializing Ubuntu-22.04 and enabling systemd...
wsl -d %WSL_DISTRO% -u root bash -c "id -u dtsadmin >/dev/null 2>&1 || (useradd -m -s /bin/bash -G sudo dtsadmin && echo 'dtsadmin ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/dtsadmin)"
wsl -d %WSL_DISTRO% -u root bash -c "echo [user] > /etc/wsl.conf && echo default=dtsadmin >> /etc/wsl.conf && echo [boot] >> /etc/wsl.conf && echo systemd=true >> /etc/wsl.conf"
wsl --terminate %WSL_DISTRO% >nul 2>&1
timeout /t 2 /nobreak >nul
:: ============================================================================
:: PHASE 3: INSTALL & PIN DOCKER ENGINE 28.3.2 + COMPOSE V2 (IDEMPOTENT)
:: ============================================================================
echo [2/7] Installing Docker Engine 28.3.2 + Compose V2 (if not installed)...
wsl -d %WSL_DISTRO% -u root bash -c "echo c2V0IC1lCmlmICEgY29tbWFuZCAtdiBkb2NrZXIgPi9kZXYvbnVsbCAyPiYxOyB0aGVuCiAgYXB0LWdldCB1cGRhdGUKICBhcHQtZ2V0IGluc3RhbGwgLXkgY2EtY2VydGlmaWNhdGVzIGN1cmwgZ251cGcgbHNiLXJlbGVhc2UKICBpbnN0YWxsIC1tIDA3NTUgLWQgL2V0Yy9hcHQva2V5cmluZ3MKICBjdXJsIC1mc1NMIGh0dHBzOi8vZG93bmxvYWQuZG9ja2VyLmNvbS9saW51eC91YnVudHUvZ3BnIHwgZ3BnIC0tZGVhcm1vciAtLXllcyAtbyAvZXRjL2FwdC9rZXlyaW5ncy9kb2NrZXIuZ3BnCiAgZWNobyAiZGViIFthcmNoPSQoZHBrZyAtLXByaW50LWFyY2hpdGVjdHVyZSkgc2lnbmVkLWJ5PS9ldGMvYXB0L2tleXJpbmdzL2RvY2tlci5ncGddIGh0dHBzOi8vZG93bmxvYWQuZG9ja2VyLmNvbS9saW51eC91YnVudHUgJChsc2JfcmVsZWFzZSAtY3MpIHN0YWJsZSIgPiAvZXRjL2FwdC9zb3VyY2VzLmxpc3QuZC9kb2NrZXIubGlzdAogIGFwdC1nZXQgdXBkYXRlCiAgYXB0LWdldCBpbnN0YWxsIC15IGRvY2tlci1jZT01OjI4LjMuMi0xfnVidW50dS4yMi4wNH5qYW1teSBkb2NrZXItY2UtY2xpPTU6MjguMy4yLTF+dWJ1bnR1LjIyLjA0fmphbW15IGNvbnRhaW5lcmQuaW8gZG9ja2VyLWNvbXBvc2UtcGx1Z2luCiAgYXB0LW1hcmsgaG9sZCBkb2NrZXItY2UgZG9ja2VyLWNlLWNsaSBjb250YWluZXJkLmlvCmZpCnN5c3RlbWN0bCBlbmFibGUgZG9ja2VyCnN5c3RlbWN0bCBzdGFydCBkb2NrZXIKdXNlcm1vZCAtYUcgZG9ja2VyIGR0c2FkbWluCg== | base64 -d | bash"
if %errorLevel% neq 0 (
echo [ERROR] Docker installation or startup failed inside WSL.
pause
exit /b 1
)
:: ============================================================================
:: PHASE 4: DEPLOY DOCKER CONTAINERS & REGISTER WSL BOOT TASK
:: ============================================================================
echo [3/7] Deploying Walmart containers to %PROJECT_DIR%...
echo Please log in to Docker registry (%REGISTRY_URL%) if prompted:
wsl -d %WSL_DISTRO% -u root docker login %REGISTRY_URL%
if %errorLevel% neq 0 (
echo [ERROR] Docker registry login failed.
pause
exit /b 1
)
set "CLEAN_ROOT=%ROOT_DIR:~0,-1%"
for /f "delims=" %%i in ('wsl -d %WSL_DISTRO% -u root wslpath -a "%CLEAN_ROOT%"') do set "WSL_ROOT_DIR=%%i"
wsl -d %WSL_DISTRO% -u root bash -c "mkdir -p %PROJECT_DIR% && cp '%WSL_ROOT_DIR%/compose.yml' %PROJECT_DIR%/ && cp '%WSL_ROOT_DIR%/.env' %PROJECT_DIR%/ && sed -i 's/\r$//' %PROJECT_DIR%/.env && cd %PROJECT_DIR% && docker compose pull && docker compose up -d"
if %errorLevel% neq 0 (
echo [ERROR] Failed to start Docker containers.
pause
exit /b 1
)
echo [4/7] Configuring WSL + Docker to auto-start on Windows Boot...
powershell -NoProfile -ExecutionPolicy Bypass -Command "$action = New-ScheduledTaskAction -Execute 'wsl.exe' -Argument '-d %WSL_DISTRO% -u root -- service docker start'; $trigger = New-ScheduledTaskTrigger -AtStartup; $principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest; $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 0; Register-ScheduledTask -TaskName 'DTS_WSL_Docker_AutoStart' -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Force | Out-Null"
:: ============================================================================
:: PHASE 5: INTERACTIVE KIOSK USER CREATION & AUTO-LOGON SETUP
:: ============================================================================
echo.
echo [5/7] Configuring Kiosk Mode Account...
echo Existing local user accounts:
powershell -NoProfile -Command "Get-LocalUser | Where-Object { $_.Enabled -eq $true } | Select-Object -ExpandProperty Name"
echo.
set "PREV_KIOSK_USER="
for /f "tokens=2*" %%A in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "DefaultUserName" 2^>nul') do set "PREV_KIOSK_USER=%%B"
set "KIOSK_USER="
set /p KIOSK_USER="Enter Kiosk username to create/configure [Default: %DEFAULT_KIOSK_USER%]: "
if "%KIOSK_USER%"=="" set "KIOSK_USER=%DEFAULT_KIOSK_USER%"
if /i "%KIOSK_USER%"=="%USERNAME%" (
echo [ERROR] Cannot use current Administrator account "%KIOSK_USER%" for Kiosk!
pause
exit /b 1
)
set "KIOSK_PASS="
set /p KIOSK_PASS="Enter password for '%KIOSK_USER%' (leave blank for no password): "
if "%PREV_KIOSK_USER%"=="" goto :SKIP_REMOVE_OLD
if /i "%PREV_KIOSK_USER%"=="%KIOSK_USER%" goto :SKIP_REMOVE_OLD
if /i "%PREV_KIOSK_USER%"=="%USERNAME%" goto :SKIP_REMOVE_OLD
if /i "%PREV_KIOSK_USER%"=="Administrator" goto :SKIP_REMOVE_OLD
echo - Removing previous Kiosk user "%PREV_KIOSK_USER%" and its profile...
for /f "tokens=3" %%S in ('query user "%PREV_KIOSK_USER%" 2^>nul ^| findstr /i "%PREV_KIOSK_USER%"') do logoff %%S >nul 2>&1
timeout /t 2 /nobreak >nul
powershell -NoProfile -ExecutionPolicy Bypass -Command "$u='%PREV_KIOSK_USER%'; $acc=Get-LocalUser -Name $u -ErrorAction SilentlyContinue; if ($acc) { $sid=$acc.SID.Value; Get-CimInstance Win32_UserProfile -Filter \"SID='$sid'\" -ErrorAction SilentlyContinue | Remove-CimInstance -ErrorAction SilentlyContinue; Remove-LocalUser -Name $u -ErrorAction SilentlyContinue }"
:SKIP_REMOVE_OLD
net accounts /minpwlen:0 >nul 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "$u = '%KIOSK_USER%'; $p = '%KIOSK_PASS%'; $existing = Get-LocalUser -Name $u -ErrorAction SilentlyContinue; if (-not $existing) { if ([string]::IsNullOrEmpty($p)) { New-LocalUser -Name $u -NoPassword | Out-Null } else { New-LocalUser -Name $u -Password (ConvertTo-SecureString $p -AsPlainText -Force) -PasswordNeverExpires | Out-Null } Add-LocalGroupMember -Group 'Users' -Member $u -ErrorAction SilentlyContinue } else { Enable-LocalUser -Name $u | Out-Null; if ([string]::IsNullOrEmpty($p)) { Set-LocalUser -Name $u -Password ([securestring]::new()) } else { Set-LocalUser -Name $u -Password (ConvertTo-SecureString $p -AsPlainText -Force) -PasswordNeverExpires $true } }"
wmic useraccount where name="%KIOSK_USER%" set PasswordExpires=False >nul 2>&1
net user "%KIOSK_USER%" /logonpasswordchg:no >nul 2>&1
if %errorLevel% neq 0 (
echo [ERROR] Failed to create or update user "%KIOSK_USER%".
pause
exit /b 1
)
echo [6/7] Applying Kiosk Shell, Auto-Logon and Persistent Browser Profile...
if not exist "%KIOSK_DATA_DIR%" mkdir "%KIOSK_DATA_DIR%"
if not exist "%KIOSK_APP_DIR%" mkdir "%KIOSK_APP_DIR%"
copy /Y "%ROOT_DIR%templates\startup.html" "%KIOSK_APP_DIR%\startup.html" >nul
icacls "%KIOSK_DATA_DIR%" /grant "%KIOSK_USER%:(OI)(CI)F" /T >nul
icacls "%KIOSK_APP_DIR%" /grant "Users:(OI)(CI)RX" /T >nul
set "WINLOGON_KEY=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
reg add "%WINLOGON_KEY%" /v "AutoAdminLogon" /t REG_SZ /d "1" /f >nul
reg add "%WINLOGON_KEY%" /v "DefaultDomainName" /t REG_SZ /d "%COMPUTERNAME%" /f >nul
reg add "%WINLOGON_KEY%" /v "DefaultUserName" /t REG_SZ /d "%KIOSK_USER%" /f >nul
reg add "%WINLOGON_KEY%" /v "DefaultPassword" /t REG_SZ /d "%KIOSK_PASS%" /f >nul
reg delete "%WINLOGON_KEY%" /v "AutoLogonCount" /f >nul 2>&1
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "HideFastUserSwitching" /t REG_DWORD /d 0 /f >nul
reg delete "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "Shell" /f >nul 2>&1
set "EDGE_POL=HKLM\SOFTWARE\Policies\Microsoft\Edge"
reg add "%EDGE_POL%" /v "InPrivateModeAvailability" /t REG_DWORD /d 1 /f >nul
reg add "%EDGE_POL%" /v "ClearBrowsingDataOnExit" /t REG_DWORD /d 0 /f >nul
reg add "%EDGE_POL%" /v "RestoreOnStartup" /t REG_DWORD /d 4 /f >nul
powershell -NoProfile -ExecutionPolicy Bypass -Command "$user = '%KIOSK_USER%'; $edge = '%EDGE_EXE%'; $sid = (New-Object System.Security.Principal.NTAccount($user)).Translate([System.Security.Principal.SecurityIdentifier]).Value; $profReg = \"HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$sid\"; if (-not (Test-Path $profReg)) { Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; using System.Text; public class Prof { [DllImport(\"userenv.dll\", CharSet=CharSet.Unicode, SetLastError=true)] public static extern bool CreateProfile(string pszUserSid, string pszUserName, StringBuilder pszProfilePath, uint cchProfilePath); }'; $sb = New-Object System.Text.StringBuilder 260; [Prof]::CreateProfile($sid, $user, $sb, $sb.Capacity) | Out-Null } $profilePath = (Get-ItemProperty $profReg -ErrorAction SilentlyContinue).ProfileImagePath; if ($profilePath) { $hivePath = Join-Path $profilePath 'NTUSER.DAT'; $edgeCmd = '\"' + $edge + '\" --kiosk \"file:///C:/KioskApp/startup.html\" --edge-kiosk-type=fullscreen --user-data-dir=\"C:\KioskBrowserData\" --no-first-run --disable-features=TranslateUI --allow-file-access-from-files'; $loaded = $false; if (-not (Test-Path \"Registry::HKEY_USERS\$sid\")) { reg load \"HKU\$sid\" \"$hivePath\" >$null 2>&1; if ($LASTEXITCODE -eq 0) { $loaded = $true } } if (Test-Path \"Registry::HKEY_USERS\$sid\") { [Microsoft.Win32.Registry]::SetValue(\"HKEY_USERS\$sid\Software\Microsoft\Windows\CurrentVersion\Policies\System\", 'Shell', $edgeCmd, [Microsoft.Win32.RegistryValueKind]::String); $stuckPath = \"Registry::HKEY_USERS\$sid\Software\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3\"; if (Test-Path $stuckPath) { $v = (Get-ItemProperty -Path $stuckPath -Name Settings).Settings; $v[8] = 3; Set-ItemProperty -Path $stuckPath -Name Settings -Value $v } } if ($loaded) { [gc]::Collect(); Start-Sleep -Milliseconds 300; reg unload \"HKU\$sid\" >$null 2>&1 } }"
schtasks /delete /tn "DTS_Torque_Vault_Kiosk" /f >nul 2>&1
:: ============================================================================
:: PHASE 6: CONFIGURE 24/7 ALWAYS-ON POWER PLAN & BLOCK AUTO-REBOOT UPDATES
:: ============================================================================
echo [7/7] Configuring 24/7 Always-On Power Plan and blocking Windows Update auto-reboots...
powercfg -setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c >nul 2>&1
powercfg -h off >nul 2>&1
powercfg -change -monitor-timeout-ac 0 >nul
powercfg -change -monitor-timeout-dc 0 >nul
powercfg -change -standby-timeout-ac 0 >nul
powercfg -change -standby-timeout-dc 0 >nul
powercfg -change -hibernate-timeout-ac 0 >nul
powercfg -change -hibernate-timeout-dc 0 >nul
powercfg -change -disk-timeout-ac 0 >nul
powercfg -change -disk-timeout-dc 0 >nul
powercfg -setacvalueindex SCHEME_CURRENT 2a737441-1930-4402-8d77-b2bebba308a3 48e6b7a6-50f5-4782-a5d4-53bb8f07e226 0 >nul 2>&1
powercfg -setdcvalueindex SCHEME_CURRENT 2a737441-1930-4402-8d77-b2bebba308a3 48e6b7a6-50f5-4782-a5d4-53bb8f07e226 0 >nul 2>&1
powercfg -setactive SCHEME_CURRENT >nul 2>&1
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" /v "NoAutoRebootWithLoggedOnUsers" /t REG_DWORD /d 1 /f >nul
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" /v "AUOptions" /t REG_DWORD /d 3 /f >nul
echo.
echo ============================================================================
echo ALL-IN-ONE 24/7 KIOSK DEPLOYMENT COMPLETED!
echo - WSL2 + Docker 28.3.2 : Installed and Auto-Starting at Boot
echo - Walmart Containers : Running in %PROJECT_DIR%
echo - Kiosk User : %KIOSK_USER% (Auto-Logon + Persistent Data)
echo - Power and Updates : 24/7 Always-On (No Sleep / No Auto-Reboot)
echo ============================================================================
echo.
set /p REBOOT_FINAL="Reboot now to enter full Kiosk Production mode? (Y/N): "
if /i "%REBOOT_FINAL%"=="Y" shutdown /r /t 3
pause