$ErrorActionPreference = 'Continue' $ProgressPreference = 'SilentlyContinue' Start-Transcript -Path 'C:\linux-install\setup-grub.log' -Force function Step($m){ Write-Host ("[{0}] {1}" -f (Get-Date -Format HH:mm:ss), $m) } try { New-Item -ItemType Directory -Force -Path 'C:\linux-install','C:\grub2\userfiles' | Out-Null $id = [Security.Principal.WindowsIdentity]::GetCurrent() $p = New-Object Security.Principal.WindowsPrincipal($id) Step ("User={0} Admin={1}" -f $id.Name, $p.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) # ISO $src = @( 'C:\Users\felipe.almeida\Downloads\linuxmint-CONEXLOG-rustdesk-live.iso', 'C:\Users\nubiway\Downloads\linuxmint-CONEXLOG-rustdesk-live.iso' ) | Where-Object { Test-Path $_ } | Select-Object -First 1 if (-not $src) { throw 'ISO nao encontrada' } if (-not (Test-Path 'C:\conexlog-mint.iso') -or ((Get-Item 'C:\conexlog-mint.iso').Length -lt 2GB)) { Step "Copiando ISO de $src" Copy-Item -LiteralPath $src -Destination 'C:\conexlog-mint.iso' -Force } Step ("ISO OK {0:N2}GB" -f ((Get-Item 'C:\conexlog-mint.iso').Length/1GB)) # GRUB2Win download if (-not (Test-Path 'C:\grub2\grub2win.exe')) { Step 'Baixando GRUB2Win zip' $zip = 'C:\linux-install\grub2win.zip' $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)' $urls = @( 'https://sourceforge.net/projects/grub2win/files/latest/download', 'https://master.dl.sourceforge.net/project/grub2win/Grub2Win.zip' ) $got = $false foreach($u in $urls){ try { Step "GET $u" Invoke-WebRequest -Uri $u -OutFile $zip -UserAgent $ua -MaximumRedirection 10 if ((Get-Item $zip).Length -gt 500KB) { $got=$true; break } } catch { Step "fail $u : $_" } } if (-not $got) { throw 'Download GRUB2Win falhou' } Step ("zip {0:N1}MB" -f ((Get-Item $zip).Length/1MB)) $ex = 'C:\linux-install\grub2win-ex' if (Test-Path $ex) { Remove-Item $ex -Recurse -Force } Expand-Archive -Path $zip -DestinationPath $ex -Force $exe = Get-ChildItem $ex -Recurse -Filter 'grub2win.exe' | Select-Object -First 1 if (-not $exe) { Get-ChildItem $ex -Recurse | Select-Object -First 40 FullName | ForEach-Object { Step $_.FullName } throw 'grub2win.exe nao veio no zip' } $tree = $exe.Directory.FullName Step "Copy tree $tree -> C:\grub2" robocopy $tree 'C:\grub2' /E /NFL /NDL /NJH /NJS /nc /ns /np | Out-Null if (-not (Test-Path 'C:\grub2\grub2win.exe')) { throw 'Copia GRUB falhou' } } Step 'GRUB2Win presente' # Menu Mint auto $mint = @' menuentry 'Linux Mint CONEXLOG Live (RustDesk) Hotkey=m' --hotkey=m --class ubuntu --class icon-linux { clear set reviewpause=1 set isopath="/conexlog-mint.iso" set kernelpath="/casper/vmlinuz" set initrdpath="/casper/initrd.lz" set bootparms="boot=casper iso-scan/filename=$isopath noprompt noeject quiet splash ---" g2wisoboot } '@ Set-Content 'C:\grub2\userfiles\usersection.cfg' -Value $mint -Encoding ASCII $cfgPath = 'C:\grub2\grub.cfg' if (-not (Test-Path $cfgPath)) { $skel = @" set pager=1 set timeout=2 set default=0 set reviewpause=1 insmod part_gpt insmod part_msdos insmod ntfs insmod fat insmod loopback insmod iso9660 # start-grub2win-user-section $mint # end-grub2win-user-section "@ [IO.File]::WriteAllText($cfgPath, $skel) Step 'grub.cfg criado' } else { Copy-Item $cfgPath 'C:\grub2\grub.cfg.bak-conexlog' -Force $cfg = Get-Content $cfgPath -Raw if ($cfg -match 'set default=') { $cfg = [regex]::Replace($cfg,'set default=[^\r\n]+','set default=0') } else { $cfg = "set default=0`r`n$cfg" } if ($cfg -match 'set timeout=') { $cfg = [regex]::Replace($cfg,'set timeout=\d+','set timeout=2') } else { $cfg = "set timeout=2`r`n$cfg" } $pat = '(?s)# start-grub2win-user-section.*?# end-grub2win-user-section[^\r\n]*' $rep = "# start-grub2win-user-section`r`n$mint`r`n# end-grub2win-user-section" if ($cfg -match '# start-grub2win-user-section') { $cfg = [regex]::Replace($cfg,$pat,$rep) } else { $cfg += "`r`n$rep" } [IO.File]::WriteAllText($cfgPath, $cfg) Step 'grub.cfg atualizado default=0 timeout=2' } # Instalar bootloader via grub2win (precisa GUI as vezes) — tenta BCD direto $g2ldrCandidates = @( 'C:\grub2\g2bootmanager\g2ldr', 'C:\grub2\g2bootmgr\g2ldr', 'C:\grub2\windata\sysfiles\g2ldr', 'C:\g2ldr' ) Get-ChildItem 'C:\grub2' -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.Name -match 'g2ldr|grub2win.efi' } | Select-Object -First 30 FullName | ForEach-Object { Step ("found "+$_.FullName) } # Roda setup do GRUB2Win em modo instalacao se houver Install.bat $setupScripts = Get-ChildItem 'C:\grub2' -Recurse -Include '*.bat','*.exe' -ErrorAction SilentlyContinue | Where-Object { $_.Name -match '^(setup|install|grub2win)' } | Select-Object -First 10 $setupScripts | ForEach-Object { Step ("bin "+$_.FullName) } # Chama grub2win.exe com args comuns de install (best-effort) if (Test-Path 'C:\grub2\grub2win.exe') { Step 'Executando grub2win.exe (install/update boot)' # Documentacao informal: sem args abre GUI. Com /updateefi tenta atualizar. $p = Start-Process -FilePath 'C:\grub2\grub2win.exe' -WorkingDirectory 'C:\grub2' -PassThru -WindowStyle Hidden Start-Sleep 8 if (-not $p.HasExited) { Step 'grub2win ainda aberto — matando GUI e seguindo BCD'; Stop-Process -Id $p.Id -Force -ErrorAction SilentlyContinue } } # BCD order: se g2ldr existir apos setup $g2ldr = $g2ldrCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1 if (-not $g2ldr) { $g2ldr = (Get-ChildItem 'C:\grub2' -Recurse -Filter 'g2ldr' -ErrorAction SilentlyContinue | Select-Object -First 1).FullName } if ($g2ldr) { Step "BCD g2ldr=$g2ldr" $enum = (bcdedit /enum all) | Out-String if ($enum -notmatch 'grub2win|GRUB2Win|g2ldr') { $create = bcdedit /create /d 'GRUB2Win CONEXLOG' /application bootsector Step $create $guid = ([regex]::Match($create,'\{[0-9a-fA-F\-]+\}')).Value if ($guid) { $rel = $g2ldr.Substring(2) # drop C: bcdedit /set $guid device partition=C: bcdedit /set $guid path $rel bcdedit /displayorder $guid /addfirst bcdedit /default $guid bcdedit /timeout 2 Step "BCD default=$guid" } } else { Step 'BCD ja tem GRUB — forcando timeout 2' bcdedit /timeout 2 # tenta achar id $lines = bcdedit /enum all $cur=$null foreach($line in $lines){ if ($line -match '^identifier\s+(.*)$'){ $cur=$Matches[1].Trim() } if ($cur -and $line -match 'grub2win|g2ldr|GRUB2Win'){ bcdedit /default $cur bcdedit /displayorder $cur /addfirst Step "default forced $cur" break } } } } else { Step 'AVISO: g2ldr nao encontrado — abra grub2win.exe uma vez na GUI pra instalar bootloader' } try { $sb = Confirm-SecureBootUEFI Step ("SecureBoot=$sb") if ($sb) { Step 'ALERTA: Secure Boot ON — desligar na BIOS senao nao sobe GRUB' } } catch { Step 'SecureBoot N/A' } Step '=== VERIFY ===' Select-String -Path $cfgPath -Pattern 'set default=|set timeout=|conexlog-mint|CONEXLOG' | ForEach-Object { Step $_.Line.Trim() } bcdedit /enum {bootmgr} Step 'DONE_SETUP' } catch { Step ("ERROR: " + $_.Exception.Message) Step $_.ScriptStackTrace exit 1 } finally { Stop-Transcript | Out-Null }