-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
2848 lines (2587 loc) · 129 KB
/
Copy pathgenerate.py
File metadata and controls
2848 lines (2587 loc) · 129 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""
BAUER GROUP XPD-RPIImage - variant config renderer.
Reads a JSON variant config, resolves ${ENV} references, validates against
schema.json, and renders all artifacts into the CustomPiOS module tree at
src/modules/<module>/filesystem/root/opt/bgrpiimage/<module>/ plus the variant shell config at
src/variants/<name>/config.
Usage:
python scripts/generate.py config/variants/canbus-plattform.json
"""
from __future__ import annotations
import argparse
import json
import os
import re
import shlex
import shutil
import stat
import sys
import textwrap
from pathlib import Path
from typing import Any
try:
import jsonschema
from rich import box
from rich.console import Console
from rich.json import JSON
from rich.panel import Panel
from rich.table import Table
except ImportError:
print("error: missing dependencies. run: pip install -r scripts/requirements.txt", file=sys.stderr)
sys.exit(2)
# On Windows the default stdout encoding is cp1252 which can't render the
# Unicode glyphs rich uses for borders / status. Reconfigure to UTF-8 and
# disable rich's legacy Win32 renderer so ANSI escapes are used instead.
for _stream in (sys.stdout, sys.stderr):
try:
_stream.reconfigure(encoding="utf-8")
except (AttributeError, Exception):
pass
console = Console(highlight=False, legacy_windows=False)
# Diagnostics go to STDERR, never stdout. `--json` writes the resolved config to
# stdout for CI to pipe into jq, and _semantic_validate() runs before that
# branch - so a note printed on the shared console lands INSIDE the JSON and
# makes it unparseable. The build step that reads it (`generate.py --json >
# /tmp/resolved.json`, then `jq .variant.version`) fails with no obvious link
# back to the note that caused it.
err_console = Console(highlight=False, legacy_windows=False, stderr=True)
def _error_panel(title: str, body: str, hint: str | None = None) -> None:
text = body
if hint:
text += f"\n\n[dim]hint:[/] {hint}"
console.print(Panel(text, title=f"[red]{title}[/]", border_style="red", box=box.ROUNDED))
ROOT = Path(__file__).resolve().parent.parent
CONFIG_DIR = ROOT / "config"
SRC_DIR = ROOT / "src"
MODULES_DIR = SRC_DIR / "modules"
VARIANTS_DIR = SRC_DIR / "variants"
SCHEMA_PATH = CONFIG_DIR / "schema.json"
# systemd leaves tx_queue_len to the driver when unset, and the CAN core
# (can_setup() in drivers/net/can/dev/dev.c) picks 10 - far too small to
# absorb a burst. 1024 is the house value across every CAN interface.
# Sizing note: a classic 8-byte frame is ~111 bits on the wire, so at
# 500 kbit/s a full queue is ~227 ms of backlog. The previous 65535 was
# ~14.5 s - latency no control bus can use, and it never applied anyway.
CAN_TXQUEUELEN_DEFAULT = 1024
# A bus-off controller does not come back on its own. can_bus_off() in
# drivers/net/can/dev/dev.c only queues the recovery work "if
# (priv->restart_ms)", and both the kernel and systemd default that to 0 -
# so the shipped image had no auto-recovery at all and `ip -details link
# show` read "can state ERROR-ACTIVE restart-ms 0". On the MCP2515 the miss
# is worse than a slow recovery: mcp251x.c takes the restart_ms == 0 branch
# to set force_quit and call mcp251x_hw_sleep(), i.e. it puts the chip to
# SLEEP and kills its own ISR loop, defeating the controller's own hardware
# bus-off recovery. Nothing short of an ip link down/up revives it - which
# is exactly why the field report needed an on-site engineer.
# 100 ms is the kernel documentation's own example value. On mcp251x the
# number is effectively a boolean (any non-zero value just declines the
# sleep path and lets the chip self-recover in ~2.8 ms of bus time at
# 500 kbit/s); it only becomes load bearing if the fleet moves to a
# controller that uses the generic restart timer.
CAN_RESTART_MS_DEFAULT = 100
# systemd's RuntimeWatchdogSec: the hardware timeout PID 1 programmes into
# /dev/watchdog, kicked once per main-loop iteration and at least every
# timeout/2.
#
# The schema caps it at 15 and that cap is load-bearing, but NOT because larger
# values are clamped - they are not. bcm2835_wdt_start() arms the hardware with
# SECS_TO_WDOG_TICKS(wdog->timeout) & PM_WDOG_TIME_SET
# i.e. (t << 16) & 0xfffff, so only the low four bits of the seconds survive:
# the effective timeout is t MOD 16. 20 becomes 4 s, 30 becomes 14 s, and 16
# becomes ZERO - an immediate reset loop. Since the 6.8 fix backported into
# rpi-6.1.y and later (f33f5b1fd1be, "Fix WDIOC_SETTIMEOUT handling") the ioctl
# no longer even returns EINVAL for an out-of-range value, so nothing is logged
# at all. Trixie ships exactly those kernels.
#
# The default is the hardware MAXIMUM rather than something comfortably below
# it, which is the opposite of the usual instinct. Nothing else pings: below
# 16 s the kernel starts no keepalive worker (watchdog_need_worker() needs a
# timeout above max_hw_heartbeat_ms = 15999), so PID 1 is the only kicker and
# there is no safety net behind it. systemd issue 7932 measured 4.3 s of PID 1
# blocked in a single SIGCHLD dispatch under a fork storm - which is a near
# miss against the 5 s budget a timeout of 10 gives, and comfortably absorbed
# by the 7.5 s that 15 gives. The extra margin costs nothing.
WATCHDOG_RUNTIME_SEC_DEFAULT = 15
# RebootWatchdogSec does NOT bound the orderly shutdown, which is the natural
# reading and the wrong one. It arms the watchdog only for the SECOND phase of
# a reboot - after PID 1 has been replaced by systemd-shutdown. Stopping Docker
# and its containers happens in phase one, still governed by RuntimeWatchdogSec
# and the units' own TimeoutStopSec.
#
# Phase two then runs sync_with_progress() (which resets its own attempt
# counter while dirty pages shrink, so it is effectively unbounded), then
# SIGTERM to everything with a 90 s timeout, then SIGKILL with another 90 s -
# and only THEN issues its first watchdog_ping(). Worst case before that first
# ping is 210 s and open-ended, which is why upstream defaults to 10 minutes.
# A shorter value hard-resets the board mid-unmount_all(), i.e. it corrupts the
# filesystem it was installed to protect. We take upstream's default.
WATCHDOG_REBOOT_SEC_DEFAULT = 600
# Maps audio.default_output to the ALSA slave PCM. Kept beside the other module
# defaults so the apply script never has to know the mapping.
_ALSA_PCM_BY_OUTPUT = {
"hdmi0": "sysdefault:CARD=vc4hdmi0",
"hdmi1": "sysdefault:CARD=vc4hdmi1",
"headphones": "sysdefault:CARD=Headphones",
"dac": "sysdefault:CARD=DAC",
}
# Version of the on-device identity contract written to /etc/bgrpiimage-release.
# An in-place updater reads this FIRST: a device whose release file has no
# BGRPIIMAGE_APPLY_CONTRACT predates the contract, carries none of the identity
# an update needs to be safe, and must be told to reflash rather than be
# updated on a best-effort basis. Bump this only when the meaning or the
# required set of keys changes, never for a new optional key.
APPLY_CONTRACT_VERSION = 1
# -----------------------------------------------------------------------------
# Env var resolution
# -----------------------------------------------------------------------------
# Names that look like ${...} but must pass through to downstream tools that
# do their own substitution (notably unattended-upgrades' APT origin patterns).
_PASSTHROUGH_NAMES: set[str] = {"distro_id", "distro_codename"}
_ENV_VAR_RE = re.compile(
r"\$\{(?P<name>[A-Za-z_][A-Za-z0-9_]*)(?:(?P<op>:-)(?P<default>[^}]*))?\}"
)
def resolve_env_vars(value: str, env: dict[str, str]) -> str:
"""Resolve ${VAR} / ${VAR:-default} references in a string.
Behaviour:
- ${VAR} -> env[VAR]; raises KeyError if unset.
- ${VAR:-default} -> env[VAR] if set & non-empty, else default.
- Names in _PASSTHROUGH_NAMES are left untouched (downstream resolves).
- Resolution is single-pass; defaults are not re-parsed.
Rationale:
BAUER GROUP security standard - fail fast on missing secrets; never
silently default a secret to empty. Defaults exist exactly for the
values that are explicitly non-sensitive.
"""
def replace(m: re.Match[str]) -> str:
name = m.group("name")
if name in _PASSTHROUGH_NAMES:
return m.group(0)
op = m.group("op")
default = m.group("default")
val = env.get(name)
if op == ":-":
return val if val else (default or "")
if val is None:
raise KeyError(
f"environment variable '{name}' is required by config but not set "
f"(use ${{{name}:-default}} to provide a fallback)"
)
return val
return _ENV_VAR_RE.sub(replace, value)
def resolve_tree(node: Any, env: dict[str, str]) -> Any:
"""Recursively resolve ${...} in every string leaf of a JSON-like tree."""
if isinstance(node, str):
return resolve_env_vars(node, env)
if isinstance(node, list):
return [resolve_tree(x, env) for x in node]
if isinstance(node, dict):
return {k: resolve_tree(v, env) for k, v in node.items()}
return node
# -----------------------------------------------------------------------------
# Variant composition via `extends`
# -----------------------------------------------------------------------------
def load_variant(path: Path, _seen: set[Path] | None = None) -> dict[str, Any]:
"""Load a variant JSON, recursively applying any `extends` reference.
`extends` is a relative path (from the current file) to a parent JSON.
The parent is loaded first (recursively), then the child is deep-merged
onto it. This is BEFORE env-var resolution - so a child can override an
`${ADMIN_PASSWORD:-...}` default by setting a literal.
"""
_seen = _seen or set()
resolved = path.resolve()
if resolved in _seen:
chain = " -> ".join(str(p) for p in _seen) + f" -> {resolved}"
raise ValueError(f"circular extends chain: {chain}")
_seen.add(resolved)
with resolved.open("r", encoding="utf-8") as f:
data = json.load(f)
data.pop("$schema", None)
parent_ref = data.pop("extends", None)
if parent_ref:
parent_path = (resolved.parent / parent_ref).resolve()
parent = load_variant(parent_path, _seen=_seen)
data = deep_merge(parent, data)
return data
def deep_merge(parent: Any, child: Any) -> Any:
"""Merge child onto parent.
- dicts : recursive merge; child keys win on conflict
- scalar lists : concat(parent + child) with stable-order dedupe
- named records : lists of dicts where every item has a `name` field
are merged by name (same name -> deep-merge entries)
- other lists : concat(parent + child)
- scalars : child overrides parent
"""
if isinstance(parent, dict) and isinstance(child, dict):
out: dict[str, Any] = {**parent}
for k, v in child.items():
out[k] = deep_merge(out[k], v) if k in out else v
return out
if isinstance(parent, list) and isinstance(child, list):
combined = list(parent) + list(child)
if not combined:
return combined
if all(isinstance(x, (str, int, float, bool)) for x in combined):
seen: set[Any] = set()
deduped: list[Any] = []
for x in combined:
if x not in seen:
seen.add(x)
deduped.append(x)
return deduped
if all(isinstance(x, dict) and "name" in x for x in combined):
# `name` is the merge key only when it is actually unique. It is not
# for dtoverlays: mcp2515 ships one overlay PER CHANNEL
# (mcp2515-can0 / mcp2515-can1), but mcp251xfd ships ONE overlay for
# every channel and selects the chip with a boolean spi<n>-<m>
# parameter - so a two-channel CAN FD HAT needs two entries that are
# both named "mcp251xfd". Merging those by name collapsed them into a
# single overlay carrying both spi selectors, with `interrupt`
# resolved last-wins: one CAN interface instead of two, pointed at
# the other channel's INT GPIO. Nothing failed - `make validate`
# passed, the image built, and only the hardware disagreed.
# `id` is the opt-in disambiguator. It is a merge key ONLY: it is
# never rendered, so `{"id": "can1", "name": "mcp251xfd"}` still
# emits `dtoverlay=mcp251xfd,...`. Absent, the key is `name`, so
# every existing variant merges exactly as before.
by_name: dict[str, dict[str, Any]] = {}
order: list[str] = []
for item in combined:
n = item.get("id") or item["name"]
if n in by_name:
by_name[n] = deep_merge(by_name[n], item)
else:
by_name[n] = item
order.append(n)
return [by_name[n] for n in order]
return combined
return child
# -----------------------------------------------------------------------------
# File writing helpers
# -----------------------------------------------------------------------------
def write(path: Path, content: str, *, executable: bool = False) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
# LF line endings regardless of host OS - these files run on Linux.
path.write_bytes(content.encode("utf-8"))
if executable:
path.chmod(path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
def shell_var(name: str, value: str | int | bool) -> str:
if isinstance(value, bool):
value = "yes" if value else "no"
return f"{name}={shlex.quote(str(value))}\n"
def shell_array(name: str, values: list[str]) -> str:
quoted = " ".join(shlex.quote(v) for v in values)
return f'{name}="{quoted}"\n'
def clean_generated(module_name: str) -> Path:
# Write to module/filesystem/root/opt/bgrpiimage/<module>/ so the files
# end up INSIDE the chroot: CustomPiOS copies `module/filesystem/` into
# the chroot root; our start_chroot_script calls `unpack /filesystem/
# root / root` which moves the contents to `/`. Result in the chroot:
# /opt/bgrpiimage/<module>/<generated files>.
gen = MODULES_DIR / module_name / "filesystem" / "root" / "opt" / "bgrpiimage" / module_name
if gen.exists():
shutil.rmtree(gen)
gen.mkdir(parents=True)
return gen
def stage_apply(module_name: str, gen: Path) -> None:
"""Copy a module's hand-written apply.sh into its generated payload.
The apply logic stays hand-written and git-tracked at
src/modules/<m>/apply.sh - those scripts run ~60% comment density and
every comment is a scar (rfkill dpkg-conffile semantics, mcp2515 probe
order, agetty's inotify redraw, the ip6tables -C that left a unit
permanently failed). Generating shell would bury all of it inside Python
string literals, where shellcheck cannot see it and `git blame` points at
the generator instead of the decision.
But the payload directory must stay 100% derived, or clean_generated()'s
invariant breaks and a stale apply.sh could outlive a rename. So the file
is COPIED here on every render: one owner for the logic, one owner for the
tree it ships in.
"""
src = MODULES_DIR / module_name / "apply.sh"
if not src.exists():
return
# Normalise to LF: the repo is developed on Windows and scripts/build.sh
# bind-mounts the working tree straight into the build container, where a
# CRLF shebang execs as "bad interpreter: No such file or directory" and
# reads to a user as "the command does not exist".
body = src.read_text(encoding="utf-8").replace("\r\n", "\n")
write(gen / "apply.sh", body, executable=True)
def render_common(cfg: dict[str, Any]) -> None:
"""Ship the shared apply library.
Carries no configuration of its own. It exists so that every module's
apply.sh has a library to source, and it is first in ACTIVE_MODULES so
that library is on disk before anything sources it.
"""
gen = clean_generated("bgrpiimage-common")
lib = MODULES_DIR / "bgrpiimage-common" / "apply-lib.sh"
body = lib.read_text(encoding="utf-8").replace("\r\n", "\n")
write(gen / "apply-lib.sh", body)
# -----------------------------------------------------------------------------
# Renderers - one per feature area
# -----------------------------------------------------------------------------
def render_base(cfg: dict[str, Any]) -> None:
gen = clean_generated("bgrpiimage-base")
write(gen / "hostname", cfg["hostname"] + "\n")
locale = cfg.get("locale", {})
lines = [
shell_var("BGRPIIMAGE_TIMEZONE", locale.get("timezone", "UTC")),
shell_var("BGRPIIMAGE_LOCALE", locale.get("locale", "en_US.UTF-8")),
shell_var("BGRPIIMAGE_KEYBOARD", locale.get("keyboard", "us")),
]
write(gen / "locale.env", "".join(lines))
packages = list(cfg.get("packages", []))
bluetooth = cfg.get("bluetooth") or {}
if bluetooth.get("enabled", True) and "bluez" not in packages:
# Raspberry Pi OS Lite already ships bluez (pi-gen
# stage2/01-sys-tweaks/00-packages), so this is a no-op install - but it
# turns an inherited dependency into a declared one, and the image no
# longer silently loses Bluetooth if the base image drops it.
packages.append("bluez")
write(gen / "packages.list", "\n".join(packages) + ("\n" if packages else ""))
# /etc/bgrpiimage-release - sourced by the MOTD banner and any ops tooling.
#
# The last three keys are for in-place updates, not for display.
#
# BASE_IMAGE_SHA256 is the line between a config update and an OS update.
# A release that rebases onto a new Raspberry Pi OS changes it, and an
# updater whose bundle records a different value must refuse rather than
# apply configuration built against a base the device is not running.
# The schema requires only url and arch, so this can legitimately be
# empty - which means "unknown", never "matches".
#
# BASE_IMAGE_URL is kept beside it because the hash alone identifies
# nothing to a human reading the file during a support call.
#
# APPLY_CONTRACT is the marker an updater tests before touching anything.
# Absent means the image predates the contract, so the honest answer is
# "reflash", not a best-effort apply. It is written here rather than
# derived from BGRPIIMAGE_VERSION because a version number says when an
# image was built, not what guarantees it makes.
variant = cfg["variant"]
base_image = cfg.get("base_image") or {}
release_lines = [
'BGRPIIMAGE_DIST="bgrpiimage"\n',
f'BGRPIIMAGE_VARIANT={shlex.quote(variant["name"])}\n',
f'BGRPIIMAGE_VERSION={shlex.quote(variant.get("version", "0.0.0"))}\n',
f'BGRPIIMAGE_DESCRIPTION={shlex.quote(variant.get("description", ""))}\n',
f'BGRPIIMAGE_BASE_IMAGE_URL={shlex.quote(base_image.get("url", ""))}\n',
f'BGRPIIMAGE_BASE_IMAGE_SHA256={shlex.quote(base_image.get("sha256", ""))}\n',
f'BGRPIIMAGE_APPLY_CONTRACT={APPLY_CONTRACT_VERSION}\n',
]
write(gen / "release.env", "".join(release_lines))
ssh = cfg.get("ssh") or {}
write(gen / "ssh.env", shell_var("BGRPIIMAGE_SSH_ENABLED", bool(ssh.get("enabled", True))))
write(
gen / "bluetooth.env",
shell_var("BGRPIIMAGE_BLUETOOTH_ENABLED", bool(bluetooth.get("enabled", True))),
)
banner = cfg.get("banner") or {}
if banner.get("enabled", True):
_render_banner(gen, cfg, banner)
# Width of the rule in /etc/issue and /etc/issue.net, and therefore the width
# everything in those files wraps to. 68 leaves a little room inside an 80
# column terminal for the SSH client's own "| " banner prefix, which is two
# more columns nobody accounts for until the line wraps.
_BANNER_RULE_WIDTH = 68
def _wrap_banner_text(text: str, indent: str = " ") -> str:
"""Wrap one paragraph to the banner rule, on word boundaries.
Returns "" for empty input rather than a blank line, so an absent
description does not leave a hole in the block.
"""
if not text.strip():
return ""
lines = textwrap.wrap(
text.strip(),
width=_BANNER_RULE_WIDTH - len(indent),
break_long_words=False,
break_on_hyphens=False,
)
return "".join(f"{indent}{line}\n" for line in lines)
def _render_banner(gen: Path, cfg: dict[str, Any], banner: dict[str, Any]) -> None:
"""Emit /etc/issue, /etc/issue.net, the MOTD script and the sshd banner
drop-in. The MOTD script is static - all dynamic info (hostname, IPs, CAN
state, docker, uptime, pending reboots) is resolved at login time from the
running system."""
variant = cfg["variant"]
note = banner.get("pre_login_note", "")
# The description is wrapped to the rule, not printed as one long line.
# /etc/issue.net has no idea how wide the client's terminal is, so an
# unwrapped line is broken by the SSH client wherever it happens to run
# out - mid-word, and with its own "> " continuation marker, which reads
# as a corrupted banner rather than a long one:
# | BAUER GROUP ... dual isolated C
# > AN HAT (MCP2515 on SPI)
# Wrapping at the rule width also keeps the block rectangular, which is
# the whole point of having a rule.
rule = "-" * _BANNER_RULE_WIDTH + "\n"
header = f"bgRPIImage {variant['name']} v{variant.get('version', '0.0.0')}\n"
header += _wrap_banner_text(variant.get("description", ""))
# /etc/issue is deliberately STATIC and short.
#
# agetty redraws the whole issue file whenever anything calls
# `agetty --reload` (it watches /run/agetty.reload via inotify) - which
# happens on network events and on every cloud-init boot stage. A ten
# line issue with per-interface escapes therefore repaints half the
# console several times during boot, which reads as a bug.
#
# Dynamic state belongs in the MOTD, which renders once per login and
# can run real commands. The hostname is not lost: agetty already
# prefixes the prompt with it ("bg-canbus login:").
issue = f"{header}{rule}"
if note:
issue += _wrap_banner_text(note, indent="")
write(gen / "issue", issue)
# /etc/issue.net: sshd reads raw (no escapes), so keep it static.
issue_net = f"{header}{rule}"
if note:
issue_net += _wrap_banner_text(note, indent="")
issue_net += rule
write(gen / "issue.net", issue_net)
# sshd drop-in to surface the pre-login banner.
write(
gen / "sshd_banner.conf",
"# bgRPIImage pre-login banner\n"
"Banner /etc/issue.net\n",
)
# Dynamic MOTD - runs on login (pam_motd) and also from console.
write(gen / "motd-banner.sh", _MOTD_SCRIPT, executable=True)
_MOTD_SCRIPT = r"""#!/bin/bash
# bgRPIImage dynamic MOTD - shown after login.
# Keep this script minimal and tolerant: it must never block a login.
set +e
[ -r /etc/bgrpiimage-release ] && . /etc/bgrpiimage-release
# Written only by bgrpiimage-update. Absent on a device that has never
# been updated, which is why every read below is defaulted.
[ -r /etc/bgrpiimage-applied ] && . /etc/bgrpiimage-applied
if [ -t 1 ]; then
CY=$'\033[1;36m'; GR=$'\033[1;32m'; DIM=$'\033[2m'
YE=$'\033[1;33m'; RD=$'\033[1;31m'; NC=$'\033[0m'
else
CY=''; GR=''; DIM=''; YE=''; RD=''; NC=''
fi
cols=$(tput cols 2>/dev/null || echo 72); [ "$cols" -lt 60 ] && cols=72
sep=$(printf '=%.0s' $(seq 1 "$cols"))
active_color() { [ "$1" = "active" ] && echo "$GR" || echo "$YE"; }
echo "${CY}${sep}${NC}"
# The image version and the configuration version legitimately differ once
# an update has been applied, and both matter: the first says which image was
# flashed - the anchor for deciding whether the next release is a config
# change or a reflash - and the second says what the device is actually
# running. Showing only one of them would make a support call guesswork, so
# the second is appended when, and only when, it differs.
_ver="v${BGRPIIMAGE_VERSION:-0.0.0}"
if [ -n "${BGRPIIMAGE_CONFIG_VERSION:-}" ] \
&& [ "${BGRPIIMAGE_CONFIG_VERSION}" != "${BGRPIIMAGE_VERSION:-}" ]; then
_ver="${_ver} ${DIM}(config v${BGRPIIMAGE_CONFIG_VERSION})${NC}"
fi
printf " ${GR}%s${NC} %s ${DIM}%s${NC}\n" \
"${BGRPIIMAGE_DIST:-bgRPIImage}" \
"${BGRPIIMAGE_VARIANT:-unknown}" \
"$_ver"
if [ "${BGRPIIMAGE_CONFIG_RESULT:-ok}" = "verify-failed" ]; then
printf " ${RD}the last configuration update did not verify${NC} ${DIM}(sudo bgrpiimage-update status)${NC}\n"
fi
# Wrapped to the rule rather than printed as one line. $cols already follows
# the real terminal, so the separator above is exactly as wide as the window -
# but an unwrapped description is broken by the TERMINAL instead, mid-word and
# without the two-space indent, which leaves a ragged line hanging under a
# neat block. `fold -s` breaks on spaces; the sed strips the trailing space it
# leaves behind on each break.
if [ -n "${BGRPIIMAGE_DESCRIPTION:-}" ]; then
printf '%s\n' "$BGRPIIMAGE_DESCRIPTION" \
| fold -s -w "$((cols - 2))" \
| sed 's/[[:space:]]*$//' \
| while IFS= read -r _dline; do
printf " ${DIM}%s${NC}\n" "$_dline"
done
fi
model=""
if [ -r /sys/firmware/devicetree/base/model ]; then
model=$(tr -d '\0' < /sys/firmware/devicetree/base/model)
fi
printf " ${DIM}host:${NC} %-20s ${DIM}kernel:${NC} %s\n" "$(hostname)" "$(uname -r)"
[ -n "$model" ] && printf " ${DIM}model:${NC} %s\n" "$model"
up=$(uptime -p 2>/dev/null)
[ -n "$up" ] && printf " ${DIM}uptime:${NC} %s\n" "$up"
echo "${CY}${sep}${NC}"
# Physical + virtual interfaces we care about.
iface_count=0
for iface in $(ip -o link show 2>/dev/null | \
awk -F': ' '$2 !~ /^(lo|docker|podman|veth|br-|bond|vlan)/ {print $2}' | \
cut -d'@' -f1); do
iface_count=$((iface_count+1))
state=$(ip -br link show "$iface" 2>/dev/null | awk '{print $2}')
case "$iface" in
can*)
# The netdev flag is the wrong health signal for a CAN link. A
# bus-off controller keeps it at UP while passing nothing at all,
# so this line used to read "can1 UP 500 kbit/s" for a bus that had
# been dead for weeks - the exact failure the banner exists to
# surface. Read the controller state as well.
# Match "can .*state", not "can state": iproute2 prints the
# ctrlmode list first, so "can <BERR-REPORTING> state ..." is a
# normal reading once listen-only or berr-reporting is enabled.
# Anchoring on ^[[:space:]]*can keeps it off the header line
# ("4: can0: ... state UP") and off "link/can promiscuity ...".
det=$(ip -details link show "$iface" 2>/dev/null)
bitrate=$(echo "$det" | grep -oE 'bitrate [0-9]+' | awk '{print $2}')
[ -n "$bitrate" ] && rate_str="$((bitrate/1000)) kbit/s" || rate_str="(no bitrate)"
can_state=$(echo "$det" | \
sed -n 's/^[[:space:]]*can .*state \([A-Z-]\{1,\}\).*/\1/p' | head -1)
case "$can_state" in
""|ERROR-ACTIVE) cstr="" ;;
BUS-OFF) cstr=" ${RD}BUS-OFF${NC}" ;;
*) cstr=" ${YE}${can_state}${NC}" ;;
esac
sc=$([ "$state" = "UP" ] && echo "$GR" || echo "$DIM")
printf " ${DIM}%-7s${NC} ${sc}%-6s${NC} %s%s\n" \
"$iface" "$state" "$rate_str" "$cstr"
;;
*)
v4=$(ip -4 -br addr show "$iface" 2>/dev/null | awk '{$1=$2=""; print $0}' | xargs)
v6=$(ip -6 -br addr show "$iface" 2>/dev/null | \
awk '{for(i=3;i<=NF;i++) print $i}' | \
grep -v '^fe80' | head -2 | tr '\n' ' ')
sc=$([ "$state" = "UP" ] && echo "$GR" || echo "$DIM")
printf " ${DIM}%-7s${NC} ${sc}%-6s${NC} v4: %s\n" "$iface" "$state" "${v4:-(none)}"
[ -n "$v6" ] && printf " %-7s %-6s v6: %s\n" "" "" "$v6"
;;
esac
done
[ "$iface_count" -eq 0 ] && printf " ${DIM}(no external network interfaces detected)${NC}\n"
echo "${CY}${sep}${NC}"
ssh_s=$(systemctl is-active ssh 2>/dev/null || echo "?")
# Which runtime is installed? podman-docker ships /usr/bin/docker as a
# shim, so probing for `docker` is true under both - `podman` is the only
# honest discriminator. Podman is daemonless, so the unit that means
# "the API is reachable" is the socket, not a service.
if command -v podman >/dev/null 2>&1; then
rt_name="podman"; rt_unit="podman.socket"
else
rt_name="docker"; rt_unit="docker"
fi
dk_s=$(systemctl is-active "$rt_unit" 2>/dev/null || echo "?")
uu_s=$(systemctl is-active unattended-upgrades 2>/dev/null || echo "?")
bt_s=$(systemctl is-active bluetooth 2>/dev/null || echo "?")
printf " ${DIM}ssh:${NC} $(active_color "$ssh_s")%s${NC}" "$ssh_s"
printf " ${DIM}%s:${NC} $(active_color "$dk_s")%s${NC}" "$rt_name" "$dk_s"
if [ "$dk_s" = "active" ]; then
n=$("$rt_name" ps -q 2>/dev/null | wc -l)
printf " ${DIM}(%d running)${NC}" "$n"
fi
printf " ${DIM}bt:${NC} $(active_color "$bt_s")%s${NC}" "$bt_s"
printf " ${DIM}unattended-upgrades:${NC} $(active_color "$uu_s")%s${NC}\n" "$uu_s"
if [ -f /var/run/reboot-required ]; then
pkgs=""
[ -s /var/run/reboot-required.pkgs ] && pkgs=$(tr '\n' ' ' < /var/run/reboot-required.pkgs)
printf " ${YE}reboot pending${NC} %s\n" "${pkgs:+(triggered by: ${pkgs})}"
fi
# Warn while a shipped demo password is still in place.
#
# The marker records user:<first 12 chars of the crypt hash> at build time;
# a still-matching hash means the credential was never rotated. The previous
# test compared sp_lstchg against 0, which can never be true here: PAM's
# account phase forces the expired password to be changed before the session
# phase ever runs pam_motd, so the field is always rewritten first.
#
# Bare-username lines (markers written by images <= 0.5.0) fall back to the
# old test so already-flashed devices keep working.
if [ -f /etc/bgrpiimage-default-password-active ]; then
_stale=""
while IFS=: read -r _u _h; do
[ -n "$_u" ] || continue
if [ -n "$_h" ]; then
_cur=$(awk -F: -v u="$_u" '$1==u{print substr($2,1,12)}' /etc/shadow 2>/dev/null)
[ -n "$_cur" ] && [ "$_cur" = "$_h" ] && _stale="${_stale} ${_u}"
else
[ "$(awk -F: -v u="$_u" '$1==u{print $3}' /etc/shadow 2>/dev/null)" = "0" ] \
&& _stale="${_stale} ${_u}"
fi
done < /etc/bgrpiimage-default-password-active
if [ -n "$_stale" ]; then
printf " ${RD}SECURITY:${NC} default password still unchanged for:%s\n" "$_stale"
printf " rotate it now: ${YE}sudo bgrpiimage-setup password${NC}\n"
fi
fi
echo "${CY}${sep}${NC}"
"""
# Known-weak credentials that must never reach a device unannounced.
# The CI placeholders matter as much as the config default: build.yml falls
# back to them when secrets.ADMIN_PASSWORD / secrets.WIFI_PSK are unset, so
# without them listed here a published release asset would carry a password
# that is written in plain text in a public repository - and would get
# neither the build warning nor the on-device MOTD nag.
_KNOWN_DEMO_PASSWORDS = {"12345678", "ci-placeholder-pw", "ci-placeholder-psk"}
def render_users(cfg: dict[str, Any]) -> None:
gen = clean_generated("bgrpiimage-users")
users = cfg.get("users", [])
remove_users = cfg.get("remove_users", [])
root = cfg.get("root", {})
script = ["#!/bin/bash", "# Auto-generated by scripts/generate.py", "set -euo pipefail", ""]
# Remove the stock accounts FIRST. `useradd` hands out the lowest free
# UID, so creating admin while `pi` still holds 1000 pushed admin to 1001
# and left the image with no UID-1000 user at all - which breaks
# raspi-config's UID-1000 fallback and gives the raspios first-boot
# wizard a reason to ask which account to rename.
for victim in remove_users:
script.append(f"if id -u {shlex.quote(victim)} >/dev/null 2>&1; then")
script.append(f" deluser --remove-home {shlex.quote(victim)} || true")
script.append(f" delgroup {shlex.quote(victim)} 2>/dev/null || true")
script.append("fi")
if remove_users:
script.append("")
# Build side: shout when a ship-default password survived env resolution.
# Without this the only signal is on the device itself, so an image can be
# built, published and flashed before anyone learns it carries a known
# credential (e.g. when ADMIN_PASSWORD is dropped by sudo's env_reset in CI).
weak = [u["name"] for u in users if u.get("password") in _KNOWN_DEMO_PASSWORDS]
if weak:
console.print(
f"[bold red]SECURITY:[/] user(s) {', '.join(weak)} carry a known "
f"default password - set ADMIN_PASSWORD to build a hardened image"
)
for user in users:
name = user["name"]
pw = user["password"]
shell = user.get("shell", "/bin/bash")
groups = ",".join(user.get("groups", []))
script.append(f"# === user: {name} ===")
script.append(f"if ! id -u {shlex.quote(name)} >/dev/null 2>&1; then")
script.append(f" useradd -m -s {shlex.quote(shell)} {shlex.quote(name)}")
script.append("fi")
if groups:
# `usermod -aG` fails hard on any non-existent group. Pre-create
# each one with `groupadd -f` so user-add works even when module
# order puts users before the packages (docker, i2c-tools) that
# would otherwise install the group.
for g in user.get("groups", []):
script.append(
f"getent group {shlex.quote(g)} >/dev/null || groupadd {shlex.quote(g)}"
)
script.append(f"usermod -aG {shlex.quote(groups)} {shlex.quote(name)}")
# chpasswd via stdin keeps the password out of argv / process lists.
script.append(f"echo {shlex.quote(f'{name}:{pw}')} | chpasswd")
if pw in _KNOWN_DEMO_PASSWORDS:
# These are PUBLIC images, so the default credential stays
# documented and discoverable - that is what keeps onboarding
# fast. Expiring it only means the operator is prompted to set a
# new password at the first login they were going to do anyway;
# login(1) on the console and sshd (UsePAM yes) both enforce it.
script.append(f"chage -d 0 {shlex.quote(name)}")
if user.get("sudo_nopasswd"):
sudoers_line = f"{name} ALL=(ALL) NOPASSWD:ALL"
script.append(
f"echo {shlex.quote(sudoers_line)} > /etc/sudoers.d/010-bgrpiimage-{name}"
)
script.append(f"chmod 440 /etc/sudoers.d/010-bgrpiimage-{name}")
keys = user.get("ssh_authorized_keys") or []
if keys:
script.append(f"install -d -m 700 -o {shlex.quote(name)} -g {shlex.quote(name)} /home/{name}/.ssh")
authfile = f"/home/{name}/.ssh/authorized_keys"
script.append(f"cat > {authfile} <<'__BGRPIIMAGE_EOF__'")
script.extend(keys)
script.append("__BGRPIIMAGE_EOF__")
script.append(f"chown {shlex.quote(name)}:{shlex.quote(name)} {authfile}")
script.append(f"chmod 600 {authfile}")
script.append("")
if weak:
# Runtime side: record user:<first 12 chars of the crypt hash>, and do
# it AFTER the accounts exist so the hash is real. The MOTD compares the
# live hash against this instead of testing sp_lstchg==0, which can
# never be true by the time pam_motd runs - PAM's account phase has
# already forced the expired password to be changed.
script.append("# --- default-password marker (read by the MOTD) ---")
script.append(": > /etc/bgrpiimage-default-password-active")
for u in weak:
q = shlex.quote(u)
awk = (
"awk -F: -v u=" + q + " '$1==u{print substr($2,1,12)}' /etc/shadow"
)
script.append(
"printf '%s:%s\\n' " + q + ' "$(' + awk + ')"'
" >> /etc/bgrpiimage-default-password-active"
)
script.append("chmod 644 /etc/bgrpiimage-default-password-active")
script.append("")
# su without password for listed users -> pam_wheel.so group trust.
su_users = root.get("su_nopasswd_users") or []
if su_users:
script.append("# su without password for trusted users -> group 'wheel'")
script.append("getent group wheel >/dev/null || groupadd wheel")
for u in su_users:
script.append(f"usermod -aG wheel {shlex.quote(u)}")
script.append("install -m 644 /tmp/_bgrpiimage_su_pam /etc/pam.d/su")
ssh_pw = root.get("ssh_password_auth", True)
ssh_root = root.get("ssh_permit_root_login", False)
script.append("")
script.append("# sshd hardening")
script.append("mkdir -p /etc/ssh/sshd_config.d")
sshd = []
sshd.append(f"PasswordAuthentication {'yes' if ssh_pw else 'no'}")
sshd.append(f"PermitRootLogin {'yes' if ssh_root else 'no'}")
sshd.append("ChallengeResponseAuthentication no")
sshd.append("UsePAM yes")
script.append("cat > /etc/ssh/sshd_config.d/10-bgrpiimage.conf <<'__BGRPIIMAGE_EOF__'")
script.extend(sshd)
script.append("__BGRPIIMAGE_EOF__")
script.append("chmod 644 /etc/ssh/sshd_config.d/10-bgrpiimage.conf")
write(gen / "create-users.sh", "\n".join(script) + "\n", executable=True)
# /etc/pam.d/su drop-in enabling pam_wheel trust.
pam_su = (
"# /etc/pam.d/su - generated by bgRPIImage\n"
"auth sufficient pam_rootok.so\n"
"auth [success=ignore default=1] pam_succeed_if.so user = root\n"
"auth sufficient pam_wheel.so trust use_uid\n"
"auth required pam_wheel.so use_uid\n"
"auth required pam_unix.so\n"
"account required pam_unix.so\n"
"session required pam_unix.so\n"
"session optional pam_xauth.so\n"
)
write(gen / "pam_su", pam_su)
def render_network(cfg: dict[str, Any]) -> None:
gen = clean_generated("bgrpiimage-network")
net = cfg.get("network", {})
nwd = gen / "systemd-networkd"
nwd.mkdir(parents=True, exist_ok=True)
def iface_network_file(idx: int, iface: dict[str, Any], match: str) -> str:
mode = iface.get("mode", "dhcp")
ipv6 = iface.get("ipv6", True)
lines = [f"[Match]", f"Name={match}", "", "[Network]"]
if mode == "dhcp":
lines.append("DHCP=ipv4")
if ipv6:
lines.append("IPv6AcceptRA=yes")
lines.append("LinkLocalAddressing=ipv6")
else:
lines.append("LinkLocalAddressing=no")
elif mode == "static":
addr = iface.get("address")
prefix = iface.get("prefix", 24)
gw = iface.get("gateway")
if addr:
lines.append(f"Address={addr}/{prefix}")
if gw:
lines.append(f"Gateway={gw}")
for dns in iface.get("dns", []):
lines.append(f"DNS={dns}")
if ipv6:
addr6 = iface.get("address_v6")
prefix6 = iface.get("prefix_v6", 64)
gw6 = iface.get("gateway_v6")
if addr6:
lines.append(f"Address={addr6}/{prefix6}")
if gw6:
lines.append(f"Gateway={gw6}")
# RequiredForOnline gates systemd-networkd-wait-online, which
# `systemctl enable systemd-networkd` pulls in through the Also= in its
# [Install] section. A managed link defaults to "yes", so a wlan0 that
# never associates (no AP, or rfkill-blocked) holds up
# network-online.target - and docker.service Wants=/After= that target,
# so Docker and the Portainer first-boot install queue behind it.
# Wireless is therefore never required. Ethernet keeps systemd's own
# default of "degraded"; "routable" would be STRICTER than today and
# would stall for 120 s on a LAN with no DHCP.
required = "no" if match.startswith("wlan") else "degraded"
lines += ["", "[Link]", f"RequiredForOnline={required}"]
return "\n".join(lines) + "\n"
eth = net.get("ethernet")
if eth and eth.get("mode") != "disabled":
write(nwd / "10-eth.network", iface_network_file(10, eth, eth.get("interface", "eth0")))
wifi = net.get("wifi")
if wifi and wifi.get("mode") != "disabled":
write(nwd / "20-wlan.network", iface_network_file(20, wifi, wifi.get("interface", "wlan0")))
wpa_dir = gen / "wpa_supplicant"
wpa_dir.mkdir(parents=True, exist_ok=True)
country = wifi.get("country", "DE")
wpa = [
"ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev",
"update_config=1",
f"country={country}",
"",
]
for net_entry in wifi.get("networks", []):
wpa.append("network={")
wpa.append(f' ssid="{net_entry["ssid"]}"')
wpa.append(f' psk="{net_entry["psk"]}"')
if "priority" in net_entry:
wpa.append(f' priority={net_entry["priority"]}')
if net_entry.get("hidden"):
wpa.append(" scan_ssid=1")
wpa.append(" key_mgmt=WPA-PSK")
wpa.append("}")
wpa.append("")
iface_name = wifi.get("interface", "wlan0")
write(wpa_dir / f"wpa_supplicant-{iface_name}.conf", "\n".join(wpa))
# raspberrypi-sys-mods ships /etc/modprobe.d/rfkill_default.conf containing
# `options rfkill default_state=0`. Because CONFIG_RFKILL=m on Pi that takes
# effect at module init, where net/rfkill/core.c calls
# rfkill_update_global_state(RFKILL_TYPE_ALL, ...): EVERY radio type is
# soft-blocked before any switch registers. It exists to stop a device
# radiating before a WLAN regulatory domain is known, and it is what prints
# "Wi-Fi is currently blocked by rfkill" on every login. It blocks Bluetooth
# too - that only works today because pi-gen whitelists a handful of known
# BT device ids under /var/lib/systemd/rfkill, and only if one of them
# happens to match the board.
#
# We pin the domain ourselves, so restore the kernel default. Emitted
# regardless of wifi.mode: an image shipped with WiFi off still needs a
# working Bluetooth radio, and would otherwise keep showing a nag pointing
# at raspi-config, which this image does not use.
#
# modprobe concatenates /etc/modprobe.d/*.conf in lexicographic order and
# the kernel's parse_args takes the LAST occurrence of an option - hence the
# zz- prefix. The vendor file is a dpkg conffile of raspberrypi-sys-mods and
# is deliberately left untouched so upgrades never hit a conffile prompt.
regdom = (net.get("wifi") or {}).get("country", "DE")
modprobe_dir = gen / "modprobe.d"
modprobe_dir.mkdir(parents=True, exist_ok=True)
write(
modprobe_dir / "zz-bgrpiimage-rfkill.conf",
"# bgRPIImage - overrides raspberrypi-sys-mods' rfkill_default.conf.\n"
"# That file is a dpkg conffile; editing it turns every upgrade into a\n"
"# conffile conflict, so we win on load order instead.\n"
"options rfkill default_state=1\n"
f"options cfg80211 ieee80211_regdom={regdom}\n",
)
def _overlay_line(name: str, params: dict[str, Any] | None = None) -> str:
"""Render a `dtoverlay=...` line with optional comma-joined params.
A JSON `true` renders as a BARE token, not `k=true`. The firmware treats a
boolean overlay parameter as present-is-true / absent-is-false (dtoverlay.c,
DTOVERRIDE_BOOLEAN: "The target is a boolean parameter (present->true,
absent->false)"), and upstream's own example is the bare form:
`dtoverlay=mcp251xfd,spi0-0,interrupt=25`. The old renderer interpolated
every value with f"{k}={v}", so a JSON `true` arrived as Python's repr and
emitted `spi0-0=True` - not a value the parser accepts, so the chip-select
selector was dropped and the overlay fell back to its default target.
`false` is therefore rendered by OMITTING the key: writing `k=false` would
still make the parameter present.
"""
params = params or {}
if not params:
return f"dtoverlay={name}"
parts = [name]
for k, v in params.items():
if isinstance(v, bool):
if v:
parts.append(k)
continue
parts.append(f"{k}={v}")
return "dtoverlay=" + ",".join(parts)
_MCP2515_OVERLAY_RE = re.compile(r"^mcp2515-can(\d+)$")
def _dtoverlay_lines(overlays: list[dict[str, Any]]) -> list[str]:
"""Render `dtoverlay=` lines, forcing a canonical order for MCP2515 CAN.
The netdev name is NOT chosen by the overlay name: mcp251x calls
alloc_candev(..., "can%d") and the index is handed out by dev_alloc_name()
at register_netdevice() time, i.e. in probe order. Probe order follows the
device-tree child order of &spi0, and the firmware merges each dtoverlay
with libfdt's fdt_add_subnode(), which inserts the new node *before* the
target's existing children. Net effect: the overlay applied LAST probes
FIRST and takes "can0".
So to make can0 the CS0 chip - which is what every label, doc and .network
file assumes - the highest chip-select must be emitted first. Waveshare's
own config.txt for the 2-CH CAN HAT does exactly this (mcp2515-can1 before
mcp2515-can0); shipping them in the "natural" order silently swaps the two
physical connectors.
Sorting here rather than in the variant JSON keeps the JSON readable and
means a later tidy-up of that array cannot re-introduce the swap.
"""
mcp: list[tuple[int, dict[str, Any]]] = []
rest: list[dict[str, Any]] = []
for ovl in overlays:
m = _MCP2515_OVERLAY_RE.match(ovl["name"])
if m:
mcp.append((int(m.group(1)), ovl))