SchOS/tools/windows/makeiso.ps1
Nexoniarz 686d5cf006 init survives stray signals; commands are 4-34x faster
Two bugs reported from actual use, plus a test harness that had stopped
testing.

init could be killed by Ctrl-C, taking the machine with it. SIGINT goes
to the foreground process group, init is pgid 1, and any window where
tty_fg_pgrp is 1 -- before a shell claims the terminal, after one exits,
or between the two while the key is held -- lands it on init. init has
no handler, so killed was set, exit() ran, and exit() panics outright on
initproc. Spamming Ctrl-C at a frozen program is exactly the input that
finds that window. POSIX and Linux both discard a default-action signal
for pid 1; init_ignores() now does the same, SIGKILL included, because
init is not killable from userspace on any Unix.

Everything was slow because BSIZE was 1024 while a SchFS block is 4096,
so every block cost four IDE requests, each with its own interrupt and
sleep/wakeup. exec() of the 800 KB toybox binary meant ~800 round trips.
The giveaway: `seq 1 200`, which touches no file and prints nothing,
cost the same 2.0s as `cat /bin/toybox`, and `ls | wc -l` cost exactly
double -- both dominated by loading the same binary twice.

BSIZE is 4096 now, one request per SchFS block, and NBUF is 256 blocks
(1 MB) rather than 90 KB -- smaller than the binary every command execs,
so nothing was ever still resident. ideinit() issues SET MULTIPLE MODE,
which is what makes a 4096-byte transfer legal: READ/WRITE MULTIPLE move
the drive's multiple count per interrupt and nothing had ever set it,
which is the real reason idestart() carried a sector_per_block > 7 panic.

  seq 1 200          2.02s -> 0.06s
  ls /bin            2.76s -> 0.68s
  ls /bin | wc -l    4.68s -> 0.73s

And the harness. bootcheck called Find-Qemu from a param() default,
which is bound before any function in the body exists -- so it died on
its first line and printed an error containing neither "FAIL" nor
"Exception", the two things being grepped for. It had not run since it
was made host-portable. Worse, an earlier index-based edit had deleted
the Expect blocks for -Tty, -Usb, -Sock, -Ps and -Evdev, so five of the
seven modes asserted nothing at all. Restored, and the USB run gets its
own 16 MB build/usbstick.img: it had been pointed at build/fs.img, which
nothing builds since SchFS became root, so QEMU refused to start.

242 checks across seven modes, all passing, and make schfs-check green.
2026-08-09 15:54:05 +02:00

179 lines
7.5 KiB
PowerShell

# Build a bootable SchOS ISO, ready for VMware or a real machine.
#
# Wraps `make iso`, which builds the ramdisk-root variant and wraps it in an
# El Torito image. The ramdisk root is the point: the ISO carries a complete
# userland inside the kernel, so it boots with no hard disk attached at all.
#
# tools/makeiso.ps1 build it
# tools/makeiso.ps1 -Run build it, then boot it under QEMU to check
# tools/makeiso.ps1 -Clean force a full rebuild first
#
# Afterwards the ISO is schos.iso in the repository root; point a VM's optical
# drive at it and boot from CD.
param(
[switch]$Run,
[switch]$Clean,
[string]$Qemu
)
# QEMU, on whichever host this is.
#
# PowerShell 7 runs on Linux, macOS and the BSDs as well as Windows, so these
# scripts are usable from all of them -- but the old default was a hardcoded
# scoop path under $env:USERPROFILE, which exists on exactly one of those. PATH
# first, because a package-managed qemu is on it everywhere; the scoop location
# is only a fallback for the Windows install that does not add itself to PATH.
function Find-Qemu {
$exe = if ($IsWindows -or $env:OS -eq 'Windows_NT') { 'qemu-system-x86_64.exe' }
else { 'qemu-system-x86_64' }
$onPath = Get-Command $exe -ErrorAction SilentlyContinue
if ($onPath) { return $onPath.Source }
if ($env:USERPROFILE) {
$scoop = Join-Path (Join-Path $env:USERPROFILE 'scoop/apps/qemu/current') $exe
if (Test-Path $scoop) { return $scoop }
}
foreach ($p in @('/usr/bin', '/usr/local/bin', '/opt/homebrew/bin')) {
$c = Join-Path $p $exe
if (Test-Path $c) { return $c }
}
throw "qemu-system-x86_64 not found on PATH. Install QEMU, or pass -Qemu <path>."
}
if (-not $Qemu) { $Qemu = Find-Qemu }
$ErrorActionPreference = 'Stop'
# Repo root is two levels up: this script lives in tools/windows/.
$root = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
Push-Location $root
function Fail($msg) {
Write-Host "FAIL: $msg" -ForegroundColor Red
Pop-Location
exit 1
}
# Git Bash, found by full path and never as bare `bash`.
#
# From PowerShell, `bash` resolves to C:\...\WindowsApps\bash.exe -- the *WSL*
# launcher -- long before anything on PATH. On this machine WSL2 cannot start at
# all, because it needs the Hyper-V hypervisor underneath the whole system and
# that is deliberately off. So calling `bash` here does not run a different
# shell, it fails with a wall of virtualisation advice and an exit code that
# looks like the build broke.
$BashCandidates = @(
"$env:ProgramFiles\Git\bin\bash.exe"
"${env:ProgramFiles(x86)}\Git\bin\bash.exe"
"$env:USERPROFILE\scoop\apps\git\current\bin\bash.exe"
"$env:LOCALAPPDATA\Programs\Git\bin\bash.exe"
)
$Bash = $BashCandidates | Where-Object { Test-Path $_ } | Select-Object -First 1
if (-not $Bash) { Fail "Git Bash not found; looked in:`n $($BashCandidates -join "`n ")" }
function Invoke-Make([string]$cmd) {
# -c, not -lc: a login shell sources the profile for no benefit here.
# stderr folded in inside bash, because $ErrorActionPreference = 'Stop'
# turns a native program's stderr into a terminating error, and mkisofs
# writes a harmless warning there on every single run.
& $Bash -c "cd '$($root -replace '\\','/')' && $cmd 2>&1"
}
# An ISO needs one of these to assemble the filesystem. There is no xorriso
# package for Windows in scoop; cdrtools provides mkisofs, and tools/mkiso.sh
# accepts either -- their El Torito arguments are compatible.
$haveIso = (Get-Command xorriso -ErrorAction SilentlyContinue) -or
(Get-Command mkisofs -ErrorAction SilentlyContinue)
if (-not $haveIso) {
Write-Host "Need xorriso or mkisofs on PATH." -ForegroundColor Yellow
Write-Host " Windows: scoop install cdrtools"
Write-Host " Debian/Ubuntu: apt install xorriso"
Fail "no ISO builder"
}
if ($Clean) {
Write-Host "==> cleaning" -ForegroundColor Cyan
# Not `make clean`: that would drop the ports too, and those take an
# afternoon to rebuild on a host where they are not rebuilt at all.
Invoke-Make "rm -rf build/kernel build/lib build/cmd" | Out-Null
}
Write-Host "==> building (this makes the ramdisk-root kernel, which is larger)" -ForegroundColor Cyan
# Output captured whole rather than piped through a filter. Piping a native
# command into ForEach-Object leaves $LASTEXITCODE reflecting the pipeline
# rather than the command, which reported a perfectly successful `make iso` as
# a failure. Redirect stderr inside bash too: with $ErrorActionPreference set to
# Stop, PowerShell turns a native program's stderr into terminating errors, and
# mkisofs writes a harmless warning there every single run.
$out = Invoke-Make "make iso"
$code = $LASTEXITCODE
$out | Where-Object { $_ -match '^==>|mkiso:|nmeta|rror' } |
ForEach-Object { Write-Host " $_" -ForegroundColor DarkGray }
if ($code -ne 0) {
$out | Select-Object -Last 20 | ForEach-Object { Write-Host $_ }
Fail "make iso returned $code"
}
$iso = Join-Path $root 'schos.iso'
if (-not (Test-Path $iso)) { Fail "schos.iso was not produced" }
# `make iso` leaves the tree holding objects compiled with -DRAMDISK_ROOT. A
# following plain `make` would then fail to link, on a missing ramdisk symbol,
# for reasons that look nothing like their cause. Put it back.
Write-Host "==> restoring the normal (disk-root) build" -ForegroundColor Cyan
Invoke-Make "rm -rf build/kernel build/lib build/cmd && make >/dev/null" | Out-Null
$mb = [Math]::Round((Get-Item $iso).Length / 1MB, 1)
Write-Host ""
Write-Host "==> $iso ($mb MB)" -ForegroundColor Green
Write-Host ""
Write-Host "VMware:" -ForegroundColor Cyan
Write-Host " New VM -> I will install the OS later -> Other / Other 64-bit"
Write-Host " CD/DVD -> Use ISO image file -> $iso, and tick Connect at power on"
Write-Host " Boot order: CD-ROM first (F2 at power on for the firmware setup)"
Write-Host ""
Write-Host " Set the VM to BIOS, not UEFI. SchOS's loader is 16-bit real"
Write-Host " mode and there is no EFI stub yet, so a UEFI-only VM will not boot."
Write-Host ""
Write-Host " Processors: as many as you like. SMP works as of 2026-08-08;"
Write-Host " check with 'cat /proc/cpuinfo', which lists the online ones."
Write-Host ""
Write-Host " RAM: 64 MB is plenty. The old 224 MB floor is gone."
if ($Run) {
Write-Host ""
Write-Host "==> booting the ISO under QEMU, no disk attached" -ForegroundColor Cyan
$log = Join-Path $root 'build/isoboot.log'
if (Test-Path $log) { Clear-Content $log }
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $Qemu
$psi.Arguments = @(
"-cdrom `"$iso`" -boot d"
'-m 256M -smp 1 -cpu qemu64,+rdtscp -machine pc-i440fx-9.2'
'-display none'
"-chardev stdio,id=c0,logfile=`"$log`""
'-serial chardev:c0'
'-no-reboot'
) -join ' '
$psi.RedirectStandardInput = $true
$psi.UseShellExecute = $false
$p = [System.Diagnostics.Process]::Start($psi)
Start-Sleep -Seconds 25
if (-not $p.HasExited) { $p.Kill() }
Start-Sleep -Milliseconds 600
$text = if (Test-Path $log) { Get-Content $log -Raw } else { "" }
if ($text -match 'init: ready') {
Write-Host "==> the ISO boots to a shell" -ForegroundColor Green
($text -split "`n" | Select-Object -First 12) | ForEach-Object { Write-Host " $_" -ForegroundColor DarkGray }
} else {
Write-Host "==> the ISO did NOT reach a shell" -ForegroundColor Red
Write-Host $text
Pop-Location
exit 1
}
}
Pop-Location