Files
morningstar-sudo d4ad73166b feat(boot): support legacy BIOS clients
BIOS PXE ROMs (DHCP arch 0) got no boot file; Broom was UEFI-only.

- DHCP/TFTP: serve undionly.kpxe (iPXE + embedded bios.ipxe:
  chains autoexec.ipxe, so router-DHCP mode does not loop)
- iPXE patch 0003: menu rules as CP437 0xc4 on the BIOS text
  console, which does not decode UTF-8 (screen was garbled)
- SSD on BIOS machines: MBR (Windows boots BIOS only from MBR),
  disk id "BROM" ("BROL" Linux-only), partition role from its
  place. Relaid only when the table is surely of the other kind
- Windows stage: no BootNext under BIOS. Writes the boot
  partition + MBR code from bios.tar.gz, then POST
  /api/boot/local; next /boot.ipxe sanboots that drive once
  (300 s, only from the IP of the machine's menu choice). Drive
  from EDD MBR ids in /sys/kernel/boot_params, no edd module
- prep-win: build broom\bios (fixed MBR VHD, bcdboot /f BIOS);
  any failure there leaves a UEFI-only golden, never stops prep.
  Publish packs it as bios.tar.gz when present
- strict reset on BIOS: bootorder task deletes \bootmgr and drops
  BROOMWIN/BROOMEFI drive letters
- shim line skipped on pcbios; deploy warns on firmware mismatch
- docs: legacy BIOS, tested on Windows 11 Pro and Enterprise

Windows goldens prepped before this boot UEFI clients only: run the
prep command again, upload, Publish. Stage bundle needs a rebuild.
2026-10-30 16:27:32 +07:00

840 lines
50 KiB
Bash

#!/bin/sh
PREREQ=""
prereqs(){ echo "$PREREQ"; }
case $1 in prereqs) prereqs; exit 0;; esac
. /scripts/functions
PATH=/broom/bin:$PATH
# Log to screen + /run (copied to BROOMWIN\broom\stage.log at the end → readable from Windows), the file with the
# seconds since power-on: shows which step a slow boot spent its time in.
log(){ echo "broom: $*"; echo "[$(cut -d' ' -f1 /proc/uptime)] $*" >> /run/broom-stage.log; }
# panic = shell (initramfs); fix by hand then `exit` → the script CONTINUES from the failed step (on-site debug).
die(){ panic "broom stage ERROR: $*"; }
# Errors: 2 s to read the reason on screen. The normal end ("-> Windows") reboots at once.
restart(){ log "$*"; case "$1" in "-> Windows"*) ;; *) sleep 2;; esac; reboot -f 2>/dev/null || echo b > /proc/sysrq-trigger; sleep 30; }
# GNU wget copied by the hook, called by path: the initramfs busybox wget has no --post-file / -T (the driver list
# needs them) and must never be picked instead. Its defaults wait 15 min per read and retry 20 times — a stalled server
# must not hang the stage that long: 30 s, 3 tries (an -T given by the caller still wins).
if [ -x /broom/bin/wget ]; then wget(){ /broom/bin/wget -T 30 -t 3 "$@"; }
else log "WARNING: no GNU wget in the stage (publish again) -> drivers skipped"; fi
# GNU tee, as a function for the same reason: the golden is hashed while downloading through tee, and the busybox
# applet (which ash runs before anything in PATH) copies one byte per write() → ~300 KB/s instead of the network's.
tee(){ /broom/bin/tee "$@"; }
# GNU sha256sum (SHA-NI) for the same reason: the busybox applet hashes a golden several times slower.
sha256sum(){ /broom/bin/sha256sum "$@"; }
# One timeline for stage.log: the stage's lines ($1, "[secs] text") and the kernel's (GNU dmesg, "[ secs] text",
# marked "kernel:"), sorted by the seconds since power-on both carry.
kmsg(){ /broom/bin/dmesg 2>/dev/null; }
timeline(){
{ sed 's/^\[\([0-9.]*\)\] /\1 /' "$1" 2>/dev/null; kmsg | sed 's/^\[ *\([0-9.]*\)\] /\1 kernel: /'; } \
| sort -s -n -k1,1 | awk '{ t = $1; sub(/^[^ ]+ /, ""); printf "[%.2f] %s\n", t, $0 }'
}
# Whole-file download showing ONLY a progress bar (file name, %, bytes, speed, ETA) — no URL / connecting / headers /
# "saved" text. GNU wget: -q hides everything, --show-progress brings the bar back; bar:force because the initramfs
# console is not always seen as a tty; noscroll keeps the name still. Busybox wget: already just its own bar.
getfile(){
if [ -x /broom/bin/wget ]; then /broom/bin/wget -T 30 -t 3 -q --show-progress --progress=bar:force:noscroll "$@"
else wget "$@"; fi
}
NAME=""; HASH=""; SRV=""; HOST=""; LIC=""; MAC=""; REG=""; BASE=""; STRICT=""; LX=""; WB=""; CAPTURE=""
for a in $(cat /proc/cmdline); do
case "$a" in broom.name=*) NAME=${a#*=};; broom.hash=*) HASH=${a#*=};; broom.srv=*) SRV=${a#*=};; broom.capture=*) CAPTURE=${a#*=};;
broom.host=*) HOST=${a#*=};; broom.lic=*) LIC=${a#*=};; BOOTIF=01-*) MAC=$(echo "${a#BOOTIF=01-}" | tr - :);;
broom.reg=*) REG=${a#*=};; broom.base=*) BASE=${a#*=};; broom.strict=*) STRICT=${a#*=};; broom.lxgb=*) LX=${a#*=};; broom.wbgb=*) WB=${a#*=};; esac
done
# Legacy BIOS (CSM) instead of UEFI: MBR Broom SSD, no ESP / NVRAM boot entries (see layout and step 3).
BIOS=""; [ -d /sys/firmware/efi ] || BIOS=1
# Capture / deploy (boot menu [C]): this PC's disk -> an image on the server, or an image -> this PC's disk. Nothing
# below runs: only the network, then the server's own binary, which asks on screen and does the disk work. Into
# /broom (the initramfs root, RAM): /run is mounted noexec. After a Deploy (/run/broom-deploy = "<disk> <ESP number>")
# a firmware entry for the new Windows, booted next (BootNext; the order keeps PXE first). Legacy BIOS has no entries:
# the deployed disk's own MBR boot code boots it from the firmware's disk order.
if [ "$CAPTURE" = 1 ]; then
[ -n "$SRV" ] || die "missing broom.srv on cmdline"
udevadm settle 2>/dev/null
configure_networking
if getfile -O /broom/bootrom-mgmt "http://$SRV/broom-capture" && chmod +x /broom/bootrom-mgmt; then
echo 1 > /proc/sys/kernel/printk # kernel messages off the console: they would cut into the questions
/broom/bootrom-mgmt capture "$SRV" < /dev/console > /dev/console 2>&1
else
echo "broom: could not download the capture tool from $SRV" > /dev/console
fi
if [ -z "$BIOS" ] && read -r dd dp < /run/broom-deploy 2>/dev/null; then
modprobe efivarfs 2>/dev/null; mount -t efivarfs efivarfs /sys/firmware/efi/efivars 2>/dev/null
for s in $(efibootmgr | grep "Windows (deployed)" | sed -n 's/^Boot\([0-9A-Fa-f]\{4\}\).*/\1/p'); do efibootmgr -q -b $s -B; done
old=$(efibootmgr | sed -n 's/^BootOrder: //p')
if efibootmgr -q -c -d /dev/$dd -p $dp -L "Windows (deployed)" -l '\EFI\Microsoft\Boot\bootmgfw.efi'; then
n=$(efibootmgr | grep "Windows (deployed)" | sed -n 's/^Boot\([0-9A-Fa-f]\{4\}\).*/\1/p' | head -n 1)
[ -n "$old" ] && efibootmgr -q -o "$old,$n"
efibootmgr -q -n $n && echo "broom: next boot: Windows on /dev/$dd" > /dev/console
else
echo "broom: could not add a boot entry for /dev/$dd - pick its disk in the firmware boot menu" > /dev/console
fi
fi
printf 'broom: press Enter to restart ' > /dev/console; read -r ans < /dev/console
restart "capture finished"
fi
[ -n "$NAME" ] && [ -n "$HASH" ] && [ -n "$SRV" ] || die "missing broom.name/hash/srv on cmdline"
for m in ntfs3 vfat nls_cp437 nls_iso8859_1 nls_utf8 efivarfs; do modprobe $m 2>/dev/null; done
udevadm settle 2>/dev/null
log "stage of $NAME started (devices ready)"
# Local disk: internal only — not removable, not on USB (an external USB HDD/SSD often reports removable=0), size > 0.
part(){ case "$1" in *[0-9]) echo "/dev/${1}p$2";; *) echo "/dev/$1$2";; esac; }
lbl(){ blkid -s LABEL -o value "$1" 2>/dev/null; }
SYSB=/sys/block; CON=/dev/console
scan(){
disks=""
for d in $SYSB/*; do
n=${d##*/}
case "$n" in loop*|ram*|dm-*|sr*|nbd*|md*|fd*|zram*) continue;; esac
[ "$(cat $d/removable 2>/dev/null)" = 1 ] && continue
case "$(readlink -f $d)" in */usb*) continue;; esac
[ "$(cat $d/size 2>/dev/null || echo 0)" -gt 0 ] || continue
disks="$disks $n"
done
}
# The Broom SSD — ONE disk for Windows and Linux images alike (other disks stay normal disks), known by its GPT
# partition names (read from sysfs: no tool needed, the Linux side does the same):
# p1 BROOMEFI 512M | p2 BROOMWIN (Windows) | p3 broomwb (WB GB, broom.wbgb) + p4 broomcache (Linux, only when LX > 0)
# LX = GB kept for the Linux side, sized by the server from its Linux goldens (broom.lxgb; 0 = no Linux image).
# Legacy BIOS: the same partitions on MBR (Windows boots BIOS only from MBR), which has no names — the disk id
# 0x4d4f5242 ("BROM" at byte 440) marks it and each partition's place gives its name; "BROL" = a Linux-only layout
# (laid out by a Linux image: p1 broomwb, p2 broomcache).
case "$LX" in ''|*[!0-9]*) LX=0;; esac
case "$WB" in ""|*[!0-9]*) WB=30;; esac # writeback GB (broom.wbgb)
# Name of partition $2 (its sysfs dir) of disk $1.
pname(){
_pn=$(sed -n 's/^PARTNAME=//p' $2/uevent 2>/dev/null)
[ -n "$_pn" ] && { echo "$_pn"; return; }
case "$(dd if=/dev/$1 bs=1 skip=440 count=4 2>/dev/null):$(cat $2/partition 2>/dev/null)" in
BROM:1) echo BROOMEFI;; BROM:2) echo BROOMWIN;; BROM:3|BROL:1) echo broomwb;; BROM:4|BROL:2) echo broomcache;;
esac
}
pn(){ p=$(part $1 $2); pname $1 $SYSB/$1/${p##*/}; }
broomdisk(){
for n in $disks; do
for p in $SYSB/$n/$n*; do
case "$(pname $n $p)" in BROOMEFI|BROOMWIN|broomwb|broomcache) echo $n; return;; esac
done
done
}
# The disk's partition table is the one this firmware boots Windows from: GPT under UEFI, MBR under BIOS. Unknown (blkid
# failed or unsure) counts as right: only a table surely of the other kind wipes the disk.
fwtable(){ case "$(blkid -p -s PTTYPE -o value /dev/$1 2>/dev/null)" in gpt) [ -z "$BIOS" ];; dos) [ -n "$BIOS" ];; *) true;; esac; }
# sfdisk script of that layout for disk $1: Windows gets what LX leaves; under 32 GB left → no Linux part.
layout(){
gb=$(( $(cat $SYSB/$1/size) / 2097152 )); lx=$LX
[ -n "$BIOS" ] && [ $gb -gt 2047 ] && gb=2047 # MBR: 2 TiB at most
[ $((gb - 1 - lx)) -ge 32 ] || lx=0
if [ -n "$BIOS" ]; then
# Every size given (a partition left open would run past 2 TiB on a bigger disk), the last one 1 GiB short.
printf 'label: dos\nlabel-id: 0x4d4f5242\nsize=512MiB, type=c, bootable\n'
if [ $lx -gt 0 ]; then
printf 'size=%sGiB, type=7\nsize=%sGiB, type=83\nsize=%sGiB, type=83\n' $((gb - 1 - lx)) $WB $((lx - WB - 1))
else
printf 'size=%sGiB, type=7\n' $((gb - 2))
fi
return
fi
printf 'label: gpt\nsize=512MiB, type=U, name=BROOMEFI\n'
if [ $lx -gt 0 ]; then
printf 'size=%sGiB, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, name=BROOMWIN, attrs="GUID:63"\nsize=%sGiB, name=broomwb\nname=broomcache\n' $((gb - 1 - lx)) $WB
else
printf 'type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, name=BROOMWIN, attrs="GUID:63"\n'
fi
}
scan
# Never guess which disk to wipe: an unknown machine (not on the Machines page — anyone can PXE-boot) or one with
# several disks asks on its screen. Prints the disk typed, nothing for Enter / anything else.
ask_disk(){
echo "broom: no Broom disk on this machine yet - one disk must be ERASED for Windows." > $CON
if [ "$REG" != 1 ]; then
# Its IP + MAC, to find it on the server's Machines page. The network was brought up by the caller: NOT here —
# this runs in $(...), and configure_networking remembers it is done only in a shell variable (IP=done) that a
# subshell loses → the next call would run DHCP again on the configured NIC (seen: downloads at ~200 KB/s).
ip=$(sed -n "s/^IPV4ADDR=['\"]*\([0-9.]*\).*/\1/p" /run/net-*.conf 2>/dev/null | head -n 1)
echo "broom: this machine is NOT registered on the server (Machines page): IP ${ip:-?}, MAC ${MAC:-?}" > $CON
fi
for n in "$@"; do
printf 'broom: %-10s %6s GB %s\n' "$n" "$(( $(cat $SYSB/$n/size) / 2097152 ))" "$(cat $SYSB/$n/device/model 2>/dev/null)" > $CON
done
echo "broom: (or pick it on the server: Devices -> this machine -> Disks, then press Enter)" > $CON
printf 'broom: type the disk to ERASE (e.g. %s), or press Enter to reboot without touching any disk: ' "$1" > $CON
read -r ans < $CON
for n in "$@"; do [ "$ans" = "$n" ] && { echo "$n"; return; }; done
}
disk=$(broomdisk); NEWDISK=""; FORMAT=""; one=""
set -- $disks
if [ -z "$disk" ] && [ "$REG" = 1 ] && [ $# -eq 1 ]; then
# The single disk would be wiped without asking — but a disk that shows up late (slow controller, async probe)
# could be the Broom SSD, making this one the wrong choice. Look again after a pause: still that one disk → go.
one=$1; sleep 5; udevadm settle 2>/dev/null; scan; disk=$(broomdisk)
fi
# Every disk reported to the server (Devices page), which answers with the Broom disk chosen there (if any) and
# whether the other disks are hidden from the guest. A disk is known by a stable id — WWN, else serial, else
# name + size — since sda / nvme0n1 can swap between boots. The network comes up here, in this shell (see ask_disk).
did(){
p=$(udevadm info -q property -n /dev/$1 2>/dev/null)
i=$(echo "$p" | sed -n 's/^ID_WWN=//p' | head -n 1)
[ -n "$i" ] || i=$(echo "$p" | sed -n 's/^ID_SERIAL=//p' | head -n 1)
[ -n "$i" ] || i="$1-$(cat $SYSB/$1/size)"
echo "$i" | tr -c 'A-Za-z0-9._:\n-' '_'
}
report_disks(){
: > /run/broom-disks.txt
for n in $disks; do
b=0; [ "$n" = "$disk" ] && b=1
echo "$(did $n) $(( $(cat $SYSB/$n/size) * 512 )) $b $n $(cat $SYSB/$n/device/model 2>/dev/null)" >> /run/broom-disks.txt
done
[ -n "$MAC" ] && wget -q -T 10 -O /run/broom-plan --post-file=/run/broom-disks.txt "http://$SRV/api/disks/report?mac=$MAC" 2>/dev/null
}
configure_networking
report_disks || : > /run/broom-plan
WANT=$(sed -n 's/^broom=//p' /run/broom-plan); HIDE=$(sed -n 's/^hide=//p' /run/broom-plan)
if [ -n "$WANT" ]; then
want=""; for n in $disks; do [ "$(did $n)" = "$WANT" ] && want=$n; done
if [ -z "$want" ]; then
log "the Broom disk chosen on the server ($WANT) is not in this machine -> keeping /dev/${disk:-none}"
elif [ "$want" != "$disk" ] && [ "$REG" = 1 ]; then
# Chosen on the web (confirmed there). The old Broom disk only holds broom's own data: its partitions go, so
# the machine never has two.
if [ -n "$disk" ]; then
log "Broom disk moves from /dev/$disk to /dev/$want (chosen on the server): /dev/$disk loses its Broom partitions"
sfdisk -q --delete /dev/$disk >/dev/null 2>&1 || log "could not clear /dev/$disk"
udevadm settle 2>/dev/null
fi
disk=$want; NEWDISK=1
fi
fi
if [ -n "$disk" ]; then
# p1/p2 not Windows' = a Linux-only layout (no room for Windows then) → laid out again (only broom data on it).
# Laid out by a Linux boot → Windows' partitions are there, just not formatted yet.
# The firmware switched between UEFI and legacy BIOS → laid out again for it too (GPT ↔ MBR).
if ! fwtable $disk; then log "/dev/$disk is laid out for the other firmware mode (UEFI/BIOS) -> laid out again"; NEWDISK=1
elif [ "$(pn $disk 1)" != BROOMEFI ] || [ "$(pn $disk 2)" != BROOMWIN ]; then NEWDISK=1
elif [ "$(lbl $(part $disk 2))" != BROOMWIN ]; then FORMAT=1; fi
fi
if [ -z "$disk" ]; then
set -- $disks
[ $# -gt 0 ] || die "no local disk found"
if [ "$REG" = 1 ] && [ $# -eq 1 ] && [ "$1" = "$one" ]; then
disk=$1
else
disk=$(ask_disk "$@")
[ -n "$disk" ] || restart "no disk chosen -> nothing was touched"
fi
NEWDISK=1
fi
# end disk choice
if [ -n "$NEWDISK" ]; then
log "partitioning /dev/$disk as the Broom SSD (WIPES the disk; Linux part: ${LX} GB)"
layout $disk | sfdisk -q --wipe always --wipe-partitions always /dev/$disk >/dev/null || die "sfdisk /dev/$disk"
udevadm settle 2>/dev/null
i=0; while [ ! -b "$(part $disk 2)" ] && [ $i -lt 10 ]; do sleep 1; i=$((i+1)); done
FORMAT=1
report_disks >/dev/null 2>&1 # the Devices page shows the new Broom disk at once
fi
# Windows' partitions only — the Linux side formats its own.
if [ -n "$FORMAT" ]; then
mkfs.fat -F32 -n BROOMEFI "$(part $disk 1)" >/dev/null || die "mkfs.fat"
mkntfs -Q -F -L BROOMWIN "$(part $disk 2)" >/dev/null || die "mkntfs"
fi
W=/run/broomwin; E=/run/broomefi; B=$W/broom
mkdir -p $W $E
# BROOMWIN has no drive letter in Windows (GPT bit 63): users can't see/delete broom files;
# vhdmp still opens the VHDX normally. Flag missing → set again. (MBR has no such bit: broom-bootorder.ps1 takes the
# letters off BROOMWIN and the BIOS boot partition instead.)
[ -n "$BIOS" ] || [ "$(sfdisk --part-attrs /dev/$disk 2 2>/dev/null)" = GUID:63 ] \
|| { sfdisk -q --part-attrs /dev/$disk 2 GUID:63 >/dev/null 2>&1; udevadm settle 2>/dev/null; }
# Hard power-off while Windows runs (happens often) → NTFS host "dirty" → ntfs3 refuses to mount.
# ntfsfix -d clears the dirty flag + resets the journal (only broom files live here, child resets every boot)
# → Windows also skips autochk at boot. No ntfsfix → mount with force.
ntfsfix -d "$(part $disk 2)" >/dev/null 2>&1
# discard: every cluster freed here (old child.vhdx on reset, old base/golden) is TRIMmed on the SSD at once →
# the last session's data is not left readable on the disk. Device without TRIM → plain mount.
mnt(){ mount -t ntfs3 -o "$1" "$(part $disk 2)" $W 2>/dev/null; }
if mnt discard || mnt force,discard; then log "BROOMWIN: discard mount (freed space is TRIMmed)"
elif mnt rw || mnt force; then log "BROOMWIN: plain mount (the disk takes no TRIM)"
else die "mount ntfs3 $(part $disk 2)"; fi
mkdir -p $B
# The other disks hidden from the guest (Devices / Groups page): broom-watch takes them offline in Windows.
if [ "$HIDE" = 1 ]; then echo 1 > $B/hide-disks; else rm -f $B/hide-disks; fi
# The Remote agent's key (agentauth.rs): the server hands it over once — on the first boot, again after Re-issue on
# the Devices page — and answers empty (204) otherwise; kept here on BROOMWIN across resets, broom-watch gives it to
# the agent. The agent proves it to the server and checks the server's proof back: no other LAN host can drive it.
if wget -q -T 10 -O /run/broom-agent.key "http://$SRV/api/agent-key" 2>/dev/null && [ -s /run/broom-agent.key ]; then
cp /run/broom-agent.key $B/agent.key && log "Remote agent key received"
fi
rm -f /run/broom-agent.key
# 0. Every Windows image this machine boots keeps its files on BROOMWIN: the one booting now at the top of broom\ —
# where the boot loader (\broom\child.vhdx) and broom-done look — the others parked in broom\img.<image>\ (image
# names have no dot). Parking is a rename on the same volume (instant) and the VHDX parent links are relative, so a
# set works in either place. active.txt = the image at the top; every step can simply run again after a power cut.
# img.<image>/.parked = when it was parked = its last use (the oldest goes first when space runs out).
IMGSET="golden.vhdx golden.sha256 base.vhdx base.open drvs.vhdx drvs.sig base.host base.lic base-template.vhdx child-template.vhdx child-template.off efi.tar.gz bios.tar.gz"
cd $B
act=$(cat active.txt 2>/dev/null)
case "$act" in ''|*[!A-Za-z0-9_-]*) act=$NAME;; esac # a disk from before image folders: the top set is this image's
if [ "$act" != "$NAME" ]; then
log "image $act parked, $NAME to the top"
# The last session / an unfinished base build of the parked image go; a base open in base mode is parked as the base.
if [ -f base.open ] && [ -f child.vhdx ]; then mv child.vhdx base.vhdx; else rm -f child.vhdx; fi
rm -f first.pending base.ok drvs.pending drvs.ok
mkdir -p "img.$act"
for f in $IMGSET dl-*; do [ -e "$f" ] && mv "$f" "img.$act/"; done
date +%s > "img.$act/.parked"
echo "$NAME" > active.txt; sync
fi
if [ -d "img.$NAME" ]; then
for f in "img.$NAME"/*; do [ -e "$f" ] && mv "$f" .; done
rm -f "img.$NAME/.parked"; rmdir "img.$NAME" 2>/dev/null
# A parked set is trusted like the active one: its golden.sha256 was written once the download checked out, and step
# 1 compares it with the server's hash (republished meanwhile → downloaded again). No re-read of the whole golden on
# every switch of images.
fi
echo "$NAME" > active.txt
# Parked images the server no longer lets this machine keep (deleted / republished: "name hash" lines) → removed.
# No answer → kept.
configure_networking
log "network up"
if wget -q -O /run/broom-cache-list "http://$SRV/api/cache-list" 2>/dev/null; then
for p in img.*; do
[ -d "$p" ] || continue
grep -qxF "${p#img.} $(cat $p/golden.sha256 2>/dev/null)" /run/broom-cache-list \
|| { rm -rf "$p"; log "image ${p#img.} removed from this disk (no longer cached here / other version)"; }
done
fi
# The parked image unused the longest (smallest .parked; busybox ls can't sort by time) → $old, "" when none.
oldest(){
old=""; t=""
for p in $B/img.*; do
[ -d "$p" ] || continue
v=$(cat $p/.parked 2>/dev/null); case "$v" in ''|*[!0-9]*) v=0;; esac
if [ -z "$t" ] || [ "$v" -lt "$t" ]; then t=$v; old=$p; fi
done
}
# Room for this session (the child grows with what the guest writes; a base build needs a few GB): parked images go,
# the oldest first, while BROOMWIN has less than ROOM GB free (Settings → SSD room; 20 when the server doesn't say).
# (A new golden makes its own room in step 1.) Plus PF GB for Windows' pagefile (same settings, 0 = none); the last
# session's pagefile goes at the reset below, so its size counts as free here.
CFG=$(wget -q -O - "http://$SRV/api/client-config" 2>/dev/null | head -n 1)
ROOM=$(echo "$CFG" | awk '$1 ~ /^[0-9]+$/ { print $1 }'); [ -n "$ROOM" ] || ROOM=20
PF=$(echo "$CFG" | awk '$4 ~ /^[0-9]+$/ { print $4 }'); [ -n "$PF" ] || PF=0
OLDPF=$(ls -ln $B/pagefile.sys 2>/dev/null | awk '{ print int($5 / 1024) }'); [ -n "$OLDPF" ] || OLDPF=0
while df -Pk $W | tail -1 | awk -v r="$((ROOM + PF))" -v o="$OLDPF" '{ exit !($4 + o < r * 1048576) }'; do
oldest; [ -n "$old" ] || break
rm -rf "$old"; log "image ${old##*/img.} removed from this disk (unused the longest) to keep room for the session"
done
cd /
gb(){ echo "$1" | awk '{ printf "%.1f GB", $1 / 1073741824 }'; }
# Golden download queue (server: goldenram.rs): a few machines at a time, at full speed. "go" or "wait <place> <of>";
# nothing (a server without the queue) = go. slot_once: one ask, 0 = go. wait_slot: asks every 10 s until go.
slot_once(){
r=$(wget -q -T 10 -O - "http://$SRV/api/golden-slot" 2>/dev/null)
case "$r" in wait*) return 1;; *) return 0;; esac
}
wait_slot(){
while ! slot_once; do
log "download queue: $(echo "$r" | awk '{ print "place " $2 " of " $3 }') -> waiting for a free place"
sleep 10
done
}
fsize(){ ls -ln "$1" 2>/dev/null | awk '{ print $5 }'; }
# 1. Golden hash mismatch → download the whole golden again. No delta: one sequential write into free space (nothing
# to compare, the file stays unfragmented). The old golden + base/child go first: they belong to the old version
# and their space is needed.
if [ "$(cat $B/golden.sha256 2>/dev/null)" != "$HASH" ]; then
log "new golden ($HASH) -> downloading from $SRV"
configure_networking
# Server's golden.sha256: gone while a publish/rollback rebuilds the files → wait; another hash → this boot's
# hash is stale (image changed after iPXE) → reboot for the new boot script. Never mix files of two versions.
srv_hash(){ wget -q -O - http://$SRV/tftp/broom-win/$NAME/golden.sha256 2>/dev/null; }
i=0
while :; do
h=$(srv_hash)
[ "$h" = "$HASH" ] && break
[ -n "$h" ] && restart "image $NAME changed on the server -> reboot to get the new version"
i=$((i+1)); [ $i -gt 40 ] && die "server has no golden.sha256 for $NAME (publish failed?) -> Publish again, then reboot"
log "image $NAME is being published on the server -> waiting ($i/40)"; sleep 15
done
D=$B/dl-$HASH
for x in $B/dl-*; do [ "$x" = "$D" ] || rm -rf "$x"; done
rm -f $B/golden.sha256 $B/golden.vhdx $B/base.vhdx $B/base.ok $B/first.pending \
$B/child.vhdx $B/child-local.vhdx $B/base.open $B/drvs.vhdx $B/drvs.sig $B/drvs.pending
mkdir -p $D
# Golden size from the server. NO $((...)) on external data (an ash arithmetic error exits the whole script) → awk.
# Free space: the golden (minus the part a cut download already holds) + 1 GB for base/child to start with.
size=$(wget -q -O - http://$SRV/tftp/broom-win/$NAME/golden.size 2>/dev/null)
case "$size" in ''|*[!0-9]*) die "server has no golden.size for $NAME (publish failed?) -> Publish again, then reboot";; esac
room(){
have=$(fsize $D/golden.vhdx); avail=$(df -Pk $W | tail -1 | awk '{ print $4 }')
awk -v a="$avail" -v h="${have:-0}" -v s="$size" 'BEGIN { exit !(a * 1024 + h >= s + 1073741824) }'
}
# Short of space → parked images go, the one unused the longest first.
until room; do
oldest
[ -n "$old" ] || die "not enough space on BROOMWIN: the golden needs $(gb $size) + 1 GB -> a bigger disk or a smaller image"
rm -rf "$old"; log "image ${old##*/img.} removed from this disk (unused the longest) to make room"
done
# $f.ok = file fully downloaded (power loss midway → next boot skips finished files, resumes the partial one).
# Only -c -O: works with both busybox and GNU wget. -c failing (e.g. 416 when the file is complete but not yet .ok)
# → download again from scratch.
n=0
for f in golden.vhdx base-template.vhdx child-template.vhdx child-template.off efi.tar.gz; do
n=$((n + 1))
[ -f $D/$f.ok ] && continue
sz=""; [ "$f" = golden.vhdx ] && sz=", $(gb $size)"
log "downloading $f ($n/5$sz)"
U=http://$SRV/tftp/broom-win/$NAME/$f
# Fresh golden: sha256 WHILE downloading (tee) → no re-read of the whole file afterwards; counted only with the
# right length too (tee goes on after a write error, e.g. disk full). Cut midway → the file stays; -c below
# resumes it and the whole-file check runs at the end.
[ "$f" = golden.vhdx ] && wait_slot
if [ "$f" = golden.vhdx ] && [ ! -s $D/$f ]; then
h=$( (cd $D && getfile -O - $U) | tee $D/$f | sha256sum | cut -c1-64 )
[ "$h" = "$HASH" ] && [ "$(fsize $D/$f)" = "$size" ] && touch $D/golden.hashed
fi
# cd + relative -O: the bar shows the file name ("golden.vhdx"), not the long dl-<hash> path cut short.
if [ "$f" != golden.vhdx ] || [ ! -f $D/golden.hashed ]; then
[ "$f" = golden.vhdx ] && wait_slot
( cd $D && getfile -c -O $f $U ) || { rm -f $D/$f; [ "$f" = golden.vhdx ] && wait_slot; ( cd $D && getfile -O $f $U ); } || die "download $f"
fi
if [ "$f" = golden.vhdx ] && [ "$(fsize $D/$f)" != "$size" ]; then
rm -f $D/$f $D/golden.hashed; die "golden.vhdx is not complete (disk full?) -> fix, then exit to retry"
fi
touch $D/$f.ok
done
[ "$(srv_hash)" = "$HASH" ] || { rm -rf $D; restart "image $NAME changed on the server during the download -> reboot to get the new version"; }
# Hashed while downloading → no second full read. Otherwise (resumed download) → whole-file sha256.
if [ ! -f $D/golden.hashed ]; then
log "checking golden sha256 - rereads the whole file, may take a few minutes, DO NOT power off..."
[ "$(sha256sum $D/golden.vhdx | cut -d' ' -f1)" = "$HASH" ] || { rm -rf $D; die "golden sha256 mismatch"; }
fi
rm -f $D/*.ok $D/golden.hashed
mv $D/* $B/ && rmdir $D && sync && echo "$HASH" > $B/golden.sha256 && sync
fi
# 1b. The small boot files (EFI bundle, VHDX templates: the image's; the broom-done / boot-order / games scripts the
# golden's stubs run: the server's, same for every image) are checked against the server's sha256 on EVERY boot: the
# guest is a local admin and could swap them on BROOMWIN (e.g. a child template with its own data) to outlive the
# reset. Wrong or missing → downloaded again.
# check_list <list> <url dir> <names...>: the listed files among <names> made equal to the server's.
check_list(){
l=$1; u=$2; shift 2
while read -r h f; do
case " $* " in *" $f "*) ;; *) continue;; esac
[ "$(sha256sum $B/$f 2>/dev/null | cut -c1-64)" = "$h" ] && continue
log "$f differs from the server's -> downloading it again"
if wget -q -O $B/$f.tmp $u/$f && [ "$(sha256sum $B/$f.tmp | cut -c1-64)" = "$h" ]; then
mv $B/$f.tmp $B/$f
else
rm -f $B/$f.tmp; restart "could not get a good $f (publish or server update running?) -> retrying"
fi
done < $l
}
configure_networking
if wget -q -O /run/broom-files.sha256 http://$SRV/tftp/broom-win/$NAME/files.sha256 2>/dev/null && [ -s /run/broom-files.sha256 ]; then
# These files belong to the golden named on the list's "golden" line: another one (published meanwhile) → reboot
# for the new boot script instead of putting new templates next to this golden.
g=$(sed -n 's/^\([0-9a-f]*\) golden$/\1/p' /run/broom-files.sha256)
[ -z "$g" ] || [ "$g" = "$HASH" ] || restart "image $NAME changed on the server -> reboot to get the new version"
# bios.tar.gz (legacy BIOS only, golden prepped by a version that makes it): fetched here the first time too.
check_list /run/broom-files.sha256 http://$SRV/tftp/broom-win/$NAME efi.tar.gz ${BIOS:+bios.tar.gz} child-template.vhdx child-template.off base-template.vhdx
else
log "no files.sha256 from the server for $NAME -> boot files not checked (Publish again if this repeats)"
fi
if wget -q -O /run/broom-scripts.sha256 http://$SRV/tftp/broom-scripts/scripts.sha256 2>/dev/null && [ -s /run/broom-scripts.sha256 ]; then
check_list /run/broom-scripts.sha256 http://$SRV/tftp/broom-scripts broom-done.ps1 broom-bootorder.ps1 broom-games.ps1 broom-watch.ps1 broom-fw.cs broom-fw.dll broom-vnc.cs broom-vnc.exe
# broom-fw.dll / broom-vnc.exe (SYSTEM loads / runs them) only as the server ships them: none listed (a local build)
# -> none kept.
for x in broom-fw.dll broom-vnc.exe; do grep -q " $x\$" /run/broom-scripts.sha256 || rm -f $B/$x; done
else
log "no scripts.sha256 on the server -> Windows scripts not checked this boot"; rm -f $B/broom-fw.dll $B/broom-vnc.exe
fi
# 1c. Preload: the other Windows images of this machine set to preload (cache-list lines "preload name hash windows")
# are fetched now into broom\img.<name>\ as parked sets, so choosing one later needs no download (its base is
# still built on its first boot). Only into free space beyond the session's room (ROOM GB): a preload never
# evicts anything. Downloaded into pre.<name>.<hash>\ first, resumed after a cut; the golden is checked against the
# server's hash when the set is taken out (step 0), like any parked set.
pre_room(){ df -Pk $W | tail -1 | awk -v s="$1" -v h="${2:-0}" -v r="${ROOM:-20}" '{ exit !($4 * 1024 + h >= s + r * 1073741824) }'; }
if [ -s /run/broom-cache-list ]; then
pl=$(sed -n 's/^preload \([A-Za-z0-9_-]*\) \([0-9a-f]*\) windows$/\1 \2/p' /run/broom-cache-list)
for d in $B/pre.*; do
[ -d "$d" ] || continue
printf '%s\n' "$pl" | grep -qxF "$(echo "${d#$B/pre.}" | sed 's/\./ /')" || rm -rf "$d"
done
printf '%s\n' "$pl" | while read -r n h; do
[ -n "$n" ] && [ "$n" != "$NAME" ] || continue
[ "$(cat $B/img.$n/golden.sha256 2>/dev/null)" = "$h" ] && continue
U=http://$SRV/tftp/broom-win/$n
[ "$(wget -q -O - $U/golden.sha256 2>/dev/null)" = "$h" ] || continue # being published: next boot
size=$(wget -q -O - $U/golden.size 2>/dev/null); case "$size" in ''|*[!0-9]*) continue;; esac
P=$B/pre.$n.$h; mkdir -p $P
pre_room $size "$(fsize $P/golden.vhdx)" || { rm -rf $P; log "preload $n ($(gb $size)): not enough room on BROOMWIN -> skipped"; continue; }
log "preloading image $n ($(gb $size)) for later..."
ok=1
for f in golden.vhdx base-template.vhdx child-template.vhdx child-template.off efi.tar.gz; do
[ -f $P/$f.ok ] && continue
# A preload never waits in the download queue: server busy -> next boot.
if [ "$f" = golden.vhdx ] && ! slot_once; then log "preload $n: server download queue full -> next boot"; ok=""; break; fi
( cd $P && getfile -c -O $f $U/$f ) || { rm -f $P/$f; ( cd $P && getfile -O $f $U/$f ); } || { ok=""; break; }
touch $P/$f.ok
done
if [ -z "$ok" ] || [ "$(fsize $P/golden.vhdx)" != "$size" ]; then log "preload $n: cut -> resumed next boot"; continue; fi
rm -f $P/*.ok; rm -rf $B/img.$n
mv $P $B/img.$n && echo "$h" > $B/img.$n/golden.sha256 && date +%s > $B/img.$n/.parked && sync && log "image $n preloaded"
done
fi
# DataWriteGuid of the current header (highest seq) as {..}; empty if the log was not replayed.
vhdx_guid(){
f=$1; best=0; g=""
for o in 65536 131072; do
[ "$(dd if=$f bs=1 skip=$o count=4 2>/dev/null)" = head ] || continue
# SequenceNumber u64 LE → number (od -tx1 only: busybox od may lack -tu8).
set -- $(od -An -tx1 -j $((o+8)) -N8 $f)
[ $# -eq 8 ] || continue
s=$((0x$8$7$6$5$4$3$2$1))
[ "$s" -gt "$best" ] || continue
best=$s; g=""
[ "$(od -An -tx1 -j $((o+48)) -N16 $f | tr -d ' \n')" = 00000000000000000000000000000000 ] || continue
set -- $(od -An -tx1 -j $((o+32)) -N16 $f)
g="{$4$3$2$1-$6$5-$8$7-$9${10}-${11}${12}${13}${14}${15}${16}}"
done
echo "$g"
}
# Write ASCII string $2 as UTF-16LE into file $1 at offset $3.
patch16(){
s=$2; fmt=""
while [ -n "$s" ]; do
c=${s%"${s#?}"}; s=${s#?}
case "$c" in '\') c='\\';; '%') c='%%';; esac # printf format: a backslash / percent as itself
fmt="$fmt$c\\000"
done
printf "$fmt" | dd of=$1 bs=1 seek=$3 conv=notrunc 2>/dev/null
}
# 2. base/child state machine.
cd $B
# first.pending + base.ok while a base already exists = not written by a base build (the guest is a local admin and
# can write BROOMWIN): committing would make the reset child (parent .\base.vhdx) the base → base points at itself
# and Windows never boots again. Ignore them.
if [ -f first.pending ] && { [ -f base.vhdx ] || [ -f base.open ]; }; then
log "first.pending found next to an existing base -> ignored (not a base build)"; rm -f first.pending base.ok
fi
if [ -f first.pending ]; then
if [ -f base.ok ]; then
g=$(vhdx_guid child.vhdx)
if [ -n "$g" ]; then
mv child.vhdx base.vhdx
# Name that broom-done set inside base = host.txt of the boot that created base.
cp host.txt base.host 2>/dev/null
cp lic.txt base.lic 2>/dev/null
rm -f first.pending base.ok
log "base done (specialized once on this machine) -> reset mode"
else
log "base was not shut down cleanly -> redo first boot"; rm -f base.ok
fi
else
log "first boot not finished -> redo (repeating every boot = Windows cannot write base.ok: old golden/broom-done error -> Publish again)"
fi
fi
# Base mode (Machines page: this machine + this image, broom.base=1): Windows boots the base itself — renamed to
# child.vhdx, the file the boot loader opens (its parent is still .\golden.vhdx) — so everything done is kept, over as
# many restarts as it takes (Windows Update, an app's own restarts). base.open marks it. Turned off → renamed back:
# that is the new base, every later boot resets to it. Left dirty (power cut) → open until Windows has shut it down
# cleanly once (Windows replays the VHDX log when it boots it). While open, the rebuild triggers below (name, license,
# drivers) wait: they look for base.vhdx.
[ -f base.open ] && [ -f base.vhdx ] && [ ! -f child.vhdx ] && mv base.vhdx child.vhdx # back from being parked
[ -f base.open ] && [ ! -f child.vhdx ] && rm -f base.open
if [ -f base.open ] && [ "$BASE" != 1 ]; then
if [ -n "$(vhdx_guid child.vhdx)" ]; then
mv child.vhdx base.vhdx && rm -f base.open && log "base mode off -> the base is saved with its changes"
else
log "base mode off, but Windows did not shut the base down cleanly -> kept open: start Windows, shut it down once"
fi
elif [ "$BASE" = 1 ] && [ ! -f base.open ] && [ -f base.vhdx ]; then
mv base.vhdx child.vhdx && touch base.open && log "base mode on -> Windows boots the base itself, every change is kept"
fi
# Driver layer (drvs.vhdx, built below): the driver session just finished (Windows wrote drvs.ok) → its child is the
# layer; drvs.pending holds what it is for ("<packages> <base GUID>"). Next to an existing layer it was not written by
# a driver session (the guest can write BROOMWIN) → ignored.
if [ -f drvs.pending ] && [ -f drvs.vhdx ]; then rm -f drvs.pending drvs.ok
elif [ -f drvs.pending ]; then
if [ -f drvs.ok ] && [ -f child.vhdx ] && [ -n "$(vhdx_guid child.vhdx)" ]; then
mv child.vhdx drvs.vhdx && mv drvs.pending drvs.sig && log "drivers installed -> kept in the driver layer"
else
rm -f drvs.pending; log "driver install did not finish -> again"
fi
rm -f drvs.ok
fi
# Machine name (Machines table on the web, by MAC) → host.txt; broom-done sets it when creating base. Every boot
# resets to base → renaming = rebuild base (specialize once more). Unregistered machine: keep the Windows name.
if [ -n "$HOST" ]; then
echo "$HOST" > host.txt
if [ -f base.vhdx ] && [ "$(cat base.host 2>/dev/null)" != "$HOST" ]; then
log "machine name -> $HOST: rebuilding base"
rm -f base.vhdx child-local.vhdx base.host
fi
else
rm -f host.txt
fi
# License key (Machines page): LIC = generation from the server (a counter, never the key). Set / re-armed →
# rebuild base so broom-done fetches the key (once) while base is built. srv.txt = where broom-done asks.
echo "$SRV" > srv.txt
# Base mode (Machines page): a base being built waits for a technician instead of being committed right away.
if [ "$BASE" = 1 ]; then echo 1 > basemode.txt; else rm -f basemode.txt; fi
if [ -n "$LIC" ]; then
echo "$LIC" > lic.txt
if [ -f base.vhdx ] && [ "$(cat base.lic 2>/dev/null)" != "$LIC" ]; then
log "license key set/re-armed: rebuilding base"
rm -f base.vhdx child-local.vhdx base.host base.lic
fi
else
rm -f lic.txt
fi
# Drivers (Drivers page): send this machine's PCI/USB IDs, the server answers "name sha256" for its packages
# (hardware match / group / all machines) → kept in broom\drivers\<name>\; Windows installs them once into a driver
# layer over the base (drvs.vhdx, below): a new or updated package needs no new base. No answer → keep what is here.
# printf, NOT echo: dash/ash echo mangles backslashes (PCI\VEN_...).
hwids(){
for d in /sys/bus/pci/devices/*; do
[ -f $d/vendor ] && printf 'PCI\\VEN_%s&DEV_%s\n' "$(cut -c3- $d/vendor)" "$(cut -c3- $d/device)"
done
for d in /sys/bus/usb/devices/*; do
[ -f $d/idVendor ] && printf 'USB\\VID_%s&PID_%s\n' "$(cat $d/idVendor)" "$(cat $d/idProduct)"
done
}
configure_networking
hwids | tr a-f A-F | sort -u > /run/broom-hw.txt
if [ -n "$MAC" ] && wget -q -T 10 -O /run/broom-drv.txt --post-file=/run/broom-hw.txt "http://$SRV/api/drivers/for?mac=$MAC"; then
mkdir -p drivers; : > /run/broom-drv-have.txt
while read -r n h; do
[ -n "$n" ] || continue
if [ "$(cat drivers/$n.sha256 2>/dev/null)" != "$h" ] || [ ! -f drivers/$n.tar.gz ]; then
log "driver $n: downloading"
rm -rf drivers/$n drivers/$n.sha256 drivers/$n.tmp drivers/$n.tar.gz; mkdir -p drivers/$n.tmp
# Checked against the server's sha256 BEFORE extracting: Windows installs these as SYSTEM.
if wget -q -O drivers/$n.tar.gz "http://$SRV/tftp/broom-drivers/$n.tar.gz" \
&& [ "$(sha256sum drivers/$n.tar.gz | cut -c1-64)" = "$h" ] && tar -xzf drivers/$n.tar.gz -C drivers/$n.tmp; then
mv drivers/$n.tmp drivers/$n && echo "$h" > drivers/$n.sha256
else
rm -rf drivers/$n.tmp drivers/$n.tar.gz; log "driver $n: download failed or sha256 mismatch (retried next boot)"
fi
fi
[ -f drivers/$n.sha256 ] && echo "$n $h" >> /run/broom-drv-have.txt
done < /run/broom-drv.txt
for f in drivers/*.sha256; do
[ -f "$f" ] || continue; n=${f##*/}; n=${n%.sha256}
grep -q "^$n " /run/broom-drv.txt || { rm -rf drivers/$n "$f" drivers/$n.tar.gz; log "driver $n: removed"; }
done
log "drivers: $(grep -c . /run/broom-drv.txt) package(s) for this machine, $(grep -c . /run/broom-drv-have.txt) ready"
# Signature of the packages here (the driver layer is made for one); none → empty.
DRV=""; [ -s /run/broom-drv-have.txt ] && DRV=$(sha256sum /run/broom-drv-have.txt | cut -c1-16)
else
log "drivers: server did not answer -> keeping the current ones"; DRV=keep
fi
# The extracted folders Windows installs from are on BROOMWIN, writable by the guest → extracted again from the checked
# archives (server's list when it answered, else the stored sha256) each time Windows is to install them.
extract_drivers(){
for x in drivers/*; do [ -d "$x" ] && rm -rf "$x"; done
for t in drivers/*.tar.gz; do
[ -f "$t" ] || continue; n=${t##*/}; n=${n%.tar.gz}
h=$(sed -n "s/^$n //p" /run/broom-drv.txt 2>/dev/null); [ -n "$h" ] || h=$(cat drivers/$n.sha256 2>/dev/null)
mkdir -p drivers/$n
if [ -n "$h" ] && [ "$(sha256sum $t | cut -c1-64)" = "$h" ] && tar -xzf $t -C drivers/$n; then :
else rm -rf drivers/$n drivers/$n.sha256 $t; log "driver $n: archive does not match -> left out"; fi
done
}
# Benchmark page: the admin asked to measure this machine → 1 GB of the golden read straight from the SSD (iflag=direct:
# the disk, not the page cache) + zeros downloaded from the server, both timed; the results go back to the server.
# GNU dd by path (busybox runs its own applet first). NO $((...)) on these numbers → awk. A function with local
# variables: this script's globals ($disk = the Broom SSD!) must not be touched.
bench(){
local t0 t1 bytes rd dl
[ -n "$MAC" ] && [ -f golden.vhdx ] || return 0
[ "$(wget -q -T 10 -O - "http://$SRV/api/bench/pending?mac=$MAC" 2>/dev/null | cut -d' ' -f1)" = run=1 ] || return 0
log "benchmark: measuring disk + network"
t0=$(cut -d' ' -f1 /proc/uptime)
bytes=$(/broom/bin/dd if=golden.vhdx of=/dev/null bs=4M count=256 iflag=direct 2>&1 | awk '/ bytes / { print $1; exit }')
t1=$(cut -d' ' -f1 /proc/uptime)
rd=$(awk -v b="$bytes" -v a="$t0" -v z="$t1" 'BEGIN { if (b > 0 && z > a) printf "%.1f", b / (z - a) / 1e6 }')
t0=$(cut -d' ' -f1 /proc/uptime)
bytes=$(wget -q -t 1 -O - "http://$SRV/api/bench/blob?mac=$MAC" | wc -c)
t1=$(cut -d' ' -f1 /proc/uptime)
dl=$(awk -v b="$bytes" -v a="$t0" -v z="$t1" 'BEGIN { if (b > 0 && z > a) printf "%.1f", b / (z - a) / 1e6 }')
log "benchmark: disk ${rd:-?} MB/s, network ${dl:-?} MB/s"
wget -q -T 10 -O /dev/null --post-data= "http://$SRV/api/bench/report?mac=$MAC&disk=$rd&net=$dl&src=ssd" || log "benchmark: report not sent"
}
bench
# Last session's writes: delete first → freed (and TRIMmed, discard mount) before the fresh child is written. Same for
# its writes on the games disk (broom-games.ps1 makes that child again once Windows is up).
# The child is rebuilt from the (checked) template every boot, with base's DataWriteGuid as its parent link — base is
# only ever opened read-only, so that GUID is still the one it had when committed. (child-local.vhdx: older layout.)
# The pagefile goes with them (what was paged out of the last session's RAM); broom-bootorder.ps1 makes a fresh one of
# PF GB once Windows is up (pagefile.txt) and says how that went (pagefile.result).
[ -f pagefile.result ] && log "pagefile of the last session: $(cat pagefile.result)"
[ -f base.open ] || rm -f child.vhdx # (in base mode child.vhdx IS the base, open: kept)
rm -f child-local.vhdx games-*-child.vhdx pagefile.sys pagefile.result
if [ "$PF" -gt 0 ]; then echo "$PF" > pagefile.txt; else rm -f pagefile.txt; fi
g=""; [ -f base.vhdx ] && g=$(vhdx_guid base.vhdx)
if [ -f base.vhdx ] && [ -z "$g" ]; then
log "base.vhdx header unreadable -> rebuilding base"; rm -f base.vhdx base.host base.lic
fi
# Drivers (drvinstall.txt, read by broom-bootorder.ps1): installed into the base being built or the open base ("keep");
# a driver session installs them into a fresh layer over the base, then restarts ("once"); else they are in the layer.
rm -f drvinstall.txt
# child.vhdx over a parent whose DataWriteGuid is $1, from the template (parent .\base.vhdx); $2 = another parent file
# name of the same length (.\drvs.vhdx): the relative path sits right after the GUID and its key (+102 bytes).
child_on(){
off=$(cat child-template.off)
cp child-template.vhdx child.vhdx && patch16 child.vhdx "$1" "$off" \
&& { [ -z "$2" ] || patch16 child.vhdx "$2" $((off + 102)); } || die "build child.vhdx"
}
if [ -f base.open ]; then
extract_drivers; echo keep > drvinstall.txt
MODE="base mode (every change is kept)"
elif [ -n "$g" ]; then
# The driver layer: base ← drvs.vhdx ← child, made once for this set of packages over this base (a base changed
# since has another GUID → made again). No packages → no layer. Server silent → the layer of this base as it is.
ds=$(cat drvs.sig 2>/dev/null); dg=""
case "$DRV" in keep) [ "${ds#* }" = "$g" ] || ds="";; *) [ "$ds" = "$DRV $g" ] || ds="";; esac
[ -n "$ds" ] && [ -f drvs.vhdx ] && dg=$(vhdx_guid drvs.vhdx)
if [ -n "$dg" ]; then
child_on "$dg" '.\drvs.vhdx'; MODE=reset
elif [ -z "$DRV" ] || [ "$DRV" = keep ]; then
rm -f drvs.vhdx drvs.sig; child_on "$g"; MODE=reset
else
rm -f drvs.vhdx drvs.sig; child_on "$g"
echo "$DRV $g" > drvs.pending; extract_drivers; echo once > drvinstall.txt
MODE="drivers (installed once into the driver layer, then a restart)"
fi
else
cp base-template.vhdx child.vhdx || die "build child.vhdx"
extract_drivers; echo keep > drvinstall.txt
touch first.pending; MODE="first boot (specialize, a few minutes)"
fi
cd /
# 3-4 under legacy BIOS: no ESP, no NVRAM boot entries. p1 = the image's BIOS boot partition (bios.tar.gz, made by the
# Windows prep: FAT32 with bootmgr + Boot\BCD → vhd=[locate]\broom\child.vhdx, its boot sector loading bootmgr),
# written whole every boot like the ESP's fresh filesystem; MBR boot code in front, p1 active. No BootNext either:
# the server answers this machine's next PXE boot once with "boot the local disk" (/api/boot/local), on the BIOS
# drive number the firmware gave this SSD (EDD: the drive whose MBR carries the disk id "BROM").
# bios_drive <boot_params file>: that drive in hex (81), nothing if unknown. The kernel's setup code, running under the
# BIOS at boot, read the MBR disk id of each BIOS disk into boot_params — edd_mbr_sig_buffer (u32[16] at 0x290, entry
# i = drive 0x80+i), edd_mbr_sig_buf_entries (u8 at 0x1ea) — which every x86 kernel shows in /sys/kernel/boot_params:
# no edd module needed. Ids are read at boot: a disk laid out this boot is not known yet.
bios_drive(){
c=$(od -An -tu1 -j490 -N1 "$1" 2>/dev/null | tr -d ' ')
i=0
while [ $i -lt "${c:-0}" ] && [ $i -lt 16 ]; do
[ "$(od -An -tx4 -j$((656 + 4 * i)) -N4 "$1" 2>/dev/null | tr -d ' ')" = 4d4f5242 ] && { printf '%x\n' $((128 + i)); return; }
i=$((i + 1))
done
}
if [ -n "$BIOS" ]; then
[ -s $B/bios.tar.gz ] || die "image $NAME has no legacy BIOS boot files - run the Windows prep command again in its VM, upload, Publish"
# Unpacked first (~100 MB in RAM): a broken archive stops here, not as a half-written p1 ("BOOTMGR is missing").
tar -xzf $B/bios.tar.gz -C /run esp.img mbr.bin || die "unpack bios.tar.gz"
dd if=/run/esp.img of="$(part $disk 1)" bs=1M conv=fsync 2>/dev/null || die "write the BIOS boot partition"
dd if=/run/mbr.bin of=/dev/$disk bs=440 count=1 conv=notrunc,fsync 2>/dev/null || die "write the MBR boot code"
rm -f /run/esp.img /run/mbr.bin
sfdisk -q --activate /dev/$disk 1 >/dev/null 2>&1 || die "sfdisk --activate"
# Strict reset: the BroomBootOrder task deletes \bootmgr from p1 once Windows is up → the SSD boots only through here.
if [ "$STRICT" = 1 ]; then echo bios > $B/strict.txt; else rm -f $B/strict.txt; fi
rm -f $B/bootorder.txt
drv=$(bios_drive /sys/kernel/boot_params/data)
[ -n "$drv" ] || { drv=80; log "BIOS drive of /dev/$disk unknown (no EDD data, or laid out this boot) -> 0x80"; }
{ echo "=== $(date '+%F %T') $MODE"; timeline /run/broom-stage.log; } >> $B/stage.log
tail -n 5000 $B/stage.log > $B/stage.log.t && mv $B/stage.log.t $B/stage.log
sync; umount $W
[ -n "$MAC" ] || die "no BOOTIF on the cmdline: the server cannot hand this machine to its disk"
wget -q -T 10 -O /dev/null --post-data= "http://$SRV/api/boot/local?mac=$MAC&drive=0x$drv" || die "the server did not take the local boot (/api/boot/local)"
log "boot order: next PXE boot -> local disk 0x$drv (/dev/$disk)"
restart "-> Windows ($MODE)"
fi
# 3. ESP: bootmgr + BCD (vhd=[locate]\broom\child.vhdx) — a fresh filesystem every boot, so nothing written to the
# ESP during a session (another loader, a changed BCD) survives it; the partition (PARTUUID) stays.
mkfs.fat -F32 -n BROOMEFI "$(part $disk 1)" >/dev/null || die "mkfs.fat ESP"
mount -t vfat "$(part $disk 1)" $E || die "mount ESP"
tar -xzf $B/efi.tar.gz -C $E || die "extract efi.tar.gz"
# Drop the fallback loader \EFI\Boot\bootx64.efi (copied by bcdboot): with it the firmware boots the SSD directly
# (default disk entry) → skips the stage → NO reset. The only way in is the stage's BootNext.
rm -rf $E/EFI/Boot
sync; umount $E
# 4. "Broom Windows" entry (SSD) comes after PXE in BootOrder: PXE (first) always runs the stage to check for a new
# image + reset; the SSD is only for booting Windows — the stage enters it via BootNext, and if the server/PXE
# does not answer the firmware falls through to the SSD (boots, but that session is NOT reset/updated) — unless
# strict reset is on (Network page): then no Windows entry stays in BootOrder at all (see below).
mount -t efivarfs efivarfs /sys/firmware/efi/efivars 2>/dev/null
pu=$(blkid -s PARTUUID -o value "$(part $disk 1)")
bootnum(){ efibootmgr -v | grep "$1" | grep -i "$pu" | sed -n 's/^Boot\([0-9A-Fa-f]\{4\}\).*/\1/p' | head -1; }
num(){ bootnum "Broom Windows"; }
n=$(num)
if [ -z "$n" ]; then
for s in $(efibootmgr | grep "Broom Windows" | sed -n 's/^Boot\([0-9A-Fa-f]\{4\}\).*/\1/p'); do efibootmgr -q -b $s -B; done
efibootmgr -q -C -d /dev/$disk -p 1 -L "Broom Windows" -l '\EFI\Microsoft\Boot\bootmgfw.efi' || die "efibootmgr create entry"
n=$(num)
fi
[ -n "$n" ] || die "Broom Windows entry not found"
# FORCE the order: PXE → Broom Windows → Windows Boot Manager (+ other bootmgfw entries) → other network → rest.
# Windows pulls "Windows Boot Manager" to the top on every boot → fixed here + by the BroomBootOrder task
# inside Windows, which restores broom\bootorder.txt by number. Without strict reset, Broom Windows stays in the order
# (server down still boots, that session is not reset).
all=$(efibootmgr -v)
# printf, NOT echo: dash/ash echo interprets "\b" in "\Boot\bootmgfw.efi" → no match.
nums(){ printf '%s\n' "$all" | grep -Ei "$1" | sed -n 's/^Boot\([0-9A-Fa-f]\{4\}\).*/\1/p' | tr '\n' ' '; }
line(){ printf '%s\n' "$all" | grep -i "^Boot$1"; }
wins=" $(nums 'bootmgfw\.efi') "
net=" $(nums 'MAC\(|IPv4\(|IPv6\(|PXE|Network') "
# The PXE entry = BootCurrent: the firmware booted THIS session from it (PXE → iPXE → stage), whatever its name
# ("IBA GE Slot 0100", "Realtek PXE B03", "UEFI: PXE IPv4 ..."). Unknown → every network entry goes first.
pxe=$(efibootmgr | sed -n 's/^BootCurrent: //p')
case "$wins $n " in *" $pxe "*) pxe="";; esac
[ -n "$pxe" ] && [ -n "$(line $pxe)" ] || pxe=""
order=$(efibootmgr | sed -n 's/^BootOrder: //p')
b=""; c=""; d=""; e=""; old=$IFS; IFS=,
# Every entry that boots from this SSD (its ESP's PARTUUID), whatever its name or loader file.
mine=""; [ -n "$pu" ] && mine=" $(nums "$pu") "
for x in $order; do
case ",$n,$pxe," in *",$x,"*) continue;; esac
case "$wins" in *" $x "*) c="${c:+$c,}$x"; continue;; esac
# Strict reset: no way into this SSD but the stage's BootNext — any other entry on it stays out of the order too.
[ "$STRICT" = 1 ] && case "$mine" in *" $x "*) continue;; esac
# Other NICs / IPv6 PXE after Windows: server down → the firmware reaches Broom Windows without their timeouts.
case "$net" in *" $x "*) if [ -n "$pxe" ]; then d="${d:+$d,}$x"; else b="${b:+$b,}$x"; fi; continue;; esac
e="${e:+$e,}$x"
done
IFS=$old
if [ "$STRICT" = 1 ]; then
# Strict reset (Network page): no Windows entry of this SSD in BootOrder at all — Windows is reached only through
# this stage's BootNext. strict.txt tells the BroomBootOrder task in Windows to keep them out (+ drop the loader).
want=$(echo "$pxe,$b,$d,$e" | sed 's/,,*/,/g; s/^,//; s/,$//')
echo "$n,$c" | sed 's/,,*/,/g; s/,$//' > $B/strict.txt
else
want=$(echo "$pxe,$b,$n,$c,$d,$e" | sed 's/,,*/,/g; s/^,//; s/,$//')
rm -f $B/strict.txt
fi
if [ "$want" != "$order" ]; then
efibootmgr -q -o "$want" && log "BootOrder forced [$want]"
fi
if [ -n "$pxe" ]; then p="Boot$pxe ($(line $pxe | cut -f1 | sed 's/^Boot[0-9A-Fa-f]*\** *//'))"
else p="unknown (no BootCurrent) -> every network entry first"; fi
log "boot order: PXE $p -> Broom Windows Boot$n"
# The order for the BroomBootOrder task in Windows (numbers, no name matching there).
echo "$want" > $B/bootorder.txt
# Stage log on BROOMWIN: the stage's lines + the kernel's (drivers, network, disks) of each boot, the last 5000 lines
# (a few boots) — readable from Windows: mountvol + type broom\stage.log.
{ echo "=== $(date '+%F %T') $MODE"; timeline /run/broom-stage.log; } >> $B/stage.log
tail -n 5000 $B/stage.log > $B/stage.log.t && mv $B/stage.log.t $B/stage.log
sync; umount $W
efibootmgr -q -n $n || die "efibootmgr BootNext"
restart "-> Windows ($MODE)"