okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221tools/windows/enable_large_pages.ps1: grant "Lock pages in memory" to the current account (#2185)2f335a5ea

main
4.4 KiB89 linesraw
1<#
2.SYNOPSIS
3    Enables the 'Lock pages in memory' privilege (SeLockMemoryPrivilege) on Windows.
4.DESCRIPTION
5    Strata keeps the experts the GPU does not hold in a host memory arena. With SeLockMemoryPrivilege
6    granted, Windows lets it allocate that arena in 2 MB large pages (MEM_LARGE_PAGES); without it the
7    engine says at start that the large pages were refused and uses 4 KB pages (docs/DETAILS.md, #1412).
8    The same can be done by hand with secpol.msc (User Rights Assignment, "Lock pages in memory").
9
10    NOTE: Windows applies security token privilege changes ONLY after sign-out/sign-in or reboot.
11#>
12
13[CmdletBinding()]
14param (
15    [string]$AccountName = $env:USERNAME
16)
17
18function Test-IsAdmin {
19    $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
20    return $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
21}
22
23if (-not (Test-IsAdmin)) {
24    Write-Host "[!] This script must be run as Administrator." -ForegroundColor Yellow
25    Write-Host "    Right-click PowerShell -> 'Run as Administrator', then run:"
26    Write-Host "    .\tools\windows\enable_large_pages.ps1"
27    Write-Host ""
28    Write-Host "    GUI Alternative:"
29    Write-Host "    1. Press Win + R, type 'secpol.msc' and press Enter."
30    Write-Host "    2. Navigate to: Security Settings -> Local Policies -> User Rights Assignment"
31    Write-Host "    3. Double-click 'Lock pages in memory'."
32    Write-Host "    4. Click 'Add User or Group...', add '$AccountName', and click OK."
33    Write-Host "    5. Sign out of Windows and sign back in."
34    exit 1
35}
36
37Write-Host "=== Configuring 'Lock pages in memory' (SeLockMemoryPrivilege) for $AccountName ===" -ForegroundColor Cyan
38
39$tempInf = [System.IO.Path]::GetTempFileName()
40$tempSdb = [System.IO.Path]::ChangeExtension($tempInf, ".sdb")
41
42try {
43    Write-Host "1. Exporting current security policy..."
44    secedit /export /cfg $tempInf | Out-Null
45
46    $content = Get-Content $tempInf -Raw
47    $userSid = (New-Object System.Security.Principal.NTAccount($AccountName)).Translate([System.Security.Principal.SecurityIdentifier]).Value
48
49    if ($content -match "SeLockMemoryPrivilege\s*=\s*(.*)") {
50        $currentList = $matches[1].Trim()
51        if ($currentList -match [regex]::Escape($userSid) -or $currentList -match [regex]::Escape($AccountName)) {
52            Write-Host "[OK] $AccountName already possesses SeLockMemoryPrivilege." -ForegroundColor Green
53            Write-Host "     If 'whoami /priv' still shows it as missing, please SIGN OUT and sign back in to refresh your token."
54            return
55        }
56        $newList = "$currentList,*$userSid"
57        $content = $content -replace "SeLockMemoryPrivilege\s*=.*", "SeLockMemoryPrivilege = $newList"
58    } else {
59        # Section [Privilege Rights]
60        if ($content -match "\[Privilege Rights\]") {
61            $content = $content -replace "\[Privilege Rights\]", "[Privilege Rights]`r`nSeLockMemoryPrivilege = *$userSid"
62        } else {
63            $content += "`r`n[Privilege Rights]`r`nSeLockMemoryPrivilege = *$userSid`r`n"
64        }
65    }
66
67    Set-Content -Path $tempInf -Value $content -Encoding ASCII
68
69    Write-Host "2. Importing updated policy database..."
70    secedit /import /db $tempSdb /cfg $tempInf | Out-Null
71
72    Write-Host "3. Applying updated policy to Windows system..."
73    secedit /configure /db $tempSdb /cfg $tempInf /areas USER_RIGHTS | Out-Null   # only the user rights, nothing else of the exported policy
74
75    Write-Host ""
76    Write-Host "[SUCCESS] SeLockMemoryPrivilege has been granted to $AccountName ($userSid)!" -ForegroundColor Green
77    Write-Host "==========================================================================" -ForegroundColor Cyan
78    Write-Host "IMPORTANT: You MUST sign out of Windows and sign back in (or restart)" -ForegroundColor Yellow
79    Write-Host "           for the new privilege to take effect on your user session." -ForegroundColor Yellow
80    Write-Host "==========================================================================" -ForegroundColor Cyan
81}
82catch {
83    Write-Host "[ERROR] Failed to configure privilege automatically: $_" -ForegroundColor Red
84    Write-Host "Please use the GUI steps via secpol.msc outlined above."
85}
86finally {
87    if (Test-Path $tempInf) { Remove-Item $tempInf -Force -ErrorAction SilentlyContinue }
88    if (Test-Path $tempSdb) { Remove-Item $tempSdb -Force -ErrorAction SilentlyContinue }
89}