Repository navigation
Expand file tree
/
Copy pathrigforge.sh
More file actions
executable file
·6302 lines (5977 loc) · 355 KB
/
Copy pathrigforge.sh
File metadata and controls
executable file
·6302 lines (5977 loc) · 355 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 bash
#
# XMRig Worker Deployment Script
# Automates the provisioning of a high-performance Monero mining worker.
# Handles dependency installation, kernel tuning (HugePages/MSR), and service configuration.
#
# Sections, in order (search for "# --- "):
# Logging utilities · Global variables · Helpers (idempotent edits, build math, GRUB)
# Setup pipeline: prerequisites & config → workspace & deps → build → XMRig config → service/kernel
# Orchestration (main) · Lifecycle (upgrade / uninstall)
# Auto-tuning: memo & stats → power/thermal sensing → measurement → search → 'tune' command
# Backup / restore · Commands (service control, version, apply, bench) · Doctor · Usage & dispatch
#
set -Eeuo pipefail
# --- Logging Utilities ---
# Color only when stdout is a terminal and NO_COLOR (https://no-color.org) is unset/empty (#144) —
# pipes, captures, CI logs, and the journal entries timer units write all get plain text. The
# ${_tty:+...} shape (instead of an if/else around the assignments) keeps every line executing on
# every run, so the no-pty CI coverage gate sees the whole block.
_tty=""
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then _tty=y; fi
C_RESET="${_tty:+\033[0m}"
C_GREEN="${_tty:+\033[1;32m}"
C_YELLOW="${_tty:+\033[1;33m}"
C_RED="${_tty:+\033[1;31m}"
C_BLUE="${_tty:+\033[1;34m}"
readonly C_RESET C_GREEN C_YELLOW C_RED C_BLUE
unset _tty
log() { echo -e "${C_GREEN}[INFO]${C_RESET} $1"; }
warn() { echo -e "${C_YELLOW}[WARN]${C_RESET} $1" >&2; }
error() {
echo -e "${C_RED}[ERROR]${C_RESET} $1" >&2
exit 1
}
# Names the phase currently running so the ERR trap can report where an unexpected failure happened.
CURRENT_STEP="starting up"
on_err() {
local ec=$?
echo -e "${C_RED}[ERROR]${C_RESET} rigforge aborted while ${CURRENT_STEP} (exit $ec)." >&2
if [ -n "${BUILD_LOG:-}" ] && [ -f "${BUILD_LOG:-}" ]; then
echo " Build output is in: $BUILD_LOG — last lines:" >&2
tail -n 20 "$BUILD_LOG" >&2 2>/dev/null || true
fi
echo " Re-run with 'bash -x $0' to trace the exact command." >&2
}
# --- Global Variables ---
OS_TYPE="$(uname -s)" # Darwin: DEPRECATED 2026-09-14, unsupported/untested going forward (#493)
# Resolve symlinks to the real script directory, so an on-PATH link still finds config and assets.
# Portable (no `readlink -f`, which BSD/macOS lacks): follow links one hop at a time.
_script_dir() {
local src="${1:-${BASH_SOURCE[0]}}" dir
while [ -L "$src" ]; do
dir=$(cd -P "$(dirname "$src")" &>/dev/null && pwd)
src=$(readlink "$src")
case "$src" in /*) ;; *) src="$dir/$src" ;; esac
done
cd -P "$(dirname "$src")" &>/dev/null && pwd
}
# Base for bundled assets and runtime state. RIGFORGE_HOME lets tests sandbox this real script so
# black-box runs remain isolated while coverage still credits the production file (#68).
SCRIPT_DIR="${RIGFORGE_HOME:-$(_script_dir)}"
# Operator for root-written files; timer units bake it into RIGFORGE_OPERATOR when SUDO_USER is absent.
REAL_USER="${SUDO_USER:-${RIGFORGE_OPERATOR:-${USER:-$(id -un)}}}"
CONFIG_JSON="$SCRIPT_DIR/config.json"
APPLIANCE_MARKER="${RIGFORGE_APPLIANCE_MARKER:-$SCRIPT_DIR/.rigforge-appliance}"
LEGACY_APPLIANCE_UNIT="${RIGFORGE_LEGACY_APPLIANCE_UNIT:-/run/systemd/system/xmrig.service}"
CONFIG_META_FILE="${RIGFORGE_CONFIG_META:-$SCRIPT_DIR/.rigforge-config-meta.json}" # #254 provenance sidecar
REBOOT_REQUIRED=false
SERVICE_INSTALLED=false
# True when the caller owns service restart; install_service then only writes/reloads/enables (#413).
SERVICE_NO_START=false
# Pinned XMRig release for reproducible builds; environment-overridable.
XMRIG_VERSION="${XMRIG_VERSION:-v6.26.0}"
XMRIG_COMMIT="${XMRIG_COMMIT:-b2ca72480c58d197e18c885d9fc1a0c8d517e60a}"
# Set to false when the pinned XMRig commit is already built, so a re-run/upgrade skips the (slow)
# recompile and the service restart — making re-runs idempotent (#4).
XMRIG_REBUILD=true
# Persist appliance behavior; recognize pre-marker Pithead installs by their runtime-only miner unit.
RIGFORGE_APPLIANCE="${RIGFORGE_APPLIANCE:-$({ [ -d "$APPLIANCE_MARKER" ] || { [ -f "$LEGACY_APPLIANCE_UNIT" ] && [ ! -e /etc/systemd/system/xmrig.service ]; }; } && printf 1 || printf 0)}"
if [ "$RIGFORGE_APPLIANCE" = 1 ]; then
# Preset only — an explicit SYSTEMD_DIR in the environment (the test sandbox) still wins, and
# the non-appliance default below keeps this value because it is now set.
SYSTEMD_DIR="${SYSTEMD_DIR:-/run/systemd/system}"
fi
# Appliance enable/disable both need --runtime: its units and wants links live under /run, and a
# plain disable leaves a runtime link enabled (#353, verified on systemd 255).
ENABLE_RUNTIME=""
if [ "$RIGFORGE_APPLIANCE" = 1 ]; then ENABLE_RUNTIME="--runtime"; fi
# Writable system paths are overridable for the test sandbox; defaults remain the real locations.
LOGROTATE_DIR="${LOGROTATE_DIR:-/etc/logrotate.d}"
GRUB_DEFAULT="${GRUB_DEFAULT:-/etc/default/grub}"
FSTAB="${FSTAB:-/etc/fstab}"
LIMITS_CONF="${LIMITS_CONF:-/etc/security/limits.conf}"
MODULES_LOAD_DIR="${MODULES_LOAD_DIR:-/etc/modules-load.d}"
MODULES_FILE="${MODULES_FILE:-/etc/modules}"
SYSTEMD_DIR="${SYSTEMD_DIR:-/etc/systemd/system}"
HUGEPAGES_1G_DIR="${HUGEPAGES_1G_DIR:-/dev/hugepages1G}"
# Directory on PATH where `setup` installs the `rigforge` command (a symlink back to this script).
BIN_DIR="${BIN_DIR:-/usr/local/bin}"
# Read-only doctor paths, overridable for tests.
MEMINFO="${MEMINFO:-/proc/meminfo}"
# CPU flags source shared with util/proposed-grub.sh's pdpe1gb probe (#338).
CPUINFO="${CPUINFO:-/proc/cpuinfo}"
MSR_MODULE_DIR="${MSR_MODULE_DIR:-/sys/module/msr}"
GOVERNOR_FILE="${GOVERNOR_FILE:-/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor}"
HUGEPAGES_1G_NR="${HUGEPAGES_1G_NR:-/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages}"
NR_HUGEPAGES_FILE="${NR_HUGEPAGES_FILE:-/proc/sys/vm/nr_hugepages}"
# Hashrate-capping diagnostics (#67): RAM layout and effective loaded CPU clock.
DMIDECODE="${DMIDECODE:-dmidecode}"
RDMSR_BIN="${RDMSR_BIN:-rdmsr}" # msr-tools, for doctor's register-level MSR verification (#66)
CPUFREQ_MAX="${CPUFREQ_MAX:-/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq}"
CPU_SYSFS="${CPU_SYSFS:-/sys/devices/system/cpu}"
MIN_RAM_MTS="${MIN_RAM_MTS:-2666}" # warn below this configured RAM speed (MT/s)
MIN_CLOCK_PCT="${MIN_CLOCK_PCT:-75}" # warn when the loaded clock is below this % of max boost
# BIOS/firmware advisory (#78): board identity and SMT state.
DMI_DIR="${DMI_DIR:-/sys/class/dmi/id}"
SMT_CONTROL="${SMT_CONTROL:-/sys/devices/system/cpu/smt/control}"
# Kernel lockdown state (#333); Secure Boot lockdown blocks every MSR write. Read, never infer.
LOCKDOWN_FILE="${LOCKDOWN_FILE:-/sys/kernel/security/lockdown}"
# systemd service name for the worker.
SERVICE_NAME="${SERVICE_NAME:-xmrig}"
# Detect whether we're being sourced (e.g. by the test suite). When sourced we only define
# functions/constants and skip running main, so functions can be exercised in isolation.
_RIGFORGE_SOURCED=0
if [ "${BASH_SOURCE[0]}" != "${0}" ]; then _RIGFORGE_SOURCED=1; fi
# Report which step failed on an unexpected error (skip when sourced by the test suite).
[ "$_RIGFORGE_SOURCED" = "0" ] && trap on_err ERR
# --- Helpers: idempotent edits, build math & GRUB cmdline ---
# Append a single line to a file only if that exact line is not already present (idempotent).
# Uses sudo so it works on root-owned system files; harmless when the file is user-writable.
append_once() {
local file="$1" line="$2"
grep -qFx "$line" "$file" 2>/dev/null || echo "$line" | sudo tee -a "$file" >/dev/null
}
# Inverse of append_once: remove every exact-match line from a file (idempotent). Used by `uninstall`.
remove_line() { # <file> <line>
local file="$1" line="$2" tmp
[ -f "$file" ] || return 0
grep -qFx "$line" "$file" 2>/dev/null || return 0
tmp=$(mktemp)
grep -vFx "$line" "$file" >"$tmp" 2>/dev/null || true
sudo cp "$tmp" "$file"
rm -f "$tmp"
}
# Choose a safe build parallelism: don't exceed core count, and cap at ~1 job per 2 GB of RAM so the
# heavy XMRig/RandomX C++ translation units don't OOM low-memory hosts. Honors MEMINFO for testing.
compute_build_jobs() { # <ncpu>
local mem_kb mem_gb jobs="$1" max
mem_kb=$(awk '/^MemTotal:/ {print $2}' "${MEMINFO:-/proc/meminfo}" 2>/dev/null || echo 0)
mem_kb="${mem_kb:-0}" # a meminfo with no MemTotal line yields empty (awk exits 0), not the || fallback
mem_gb=$((mem_kb / 1024 / 1024))
# Guard on mem_kb (readable at all), not mem_gb (truncates to 0 for BOTH a real sub-1GB host and an
# unreadable meminfo) — a sub-1GB host must still hit the cap below (falls to the max<1 floor -> 1
# job); only a genuinely unreadable meminfo should skip the cap and use all cores (#277).
if [ "$mem_kb" -gt 0 ]; then
max=$((mem_gb / 2))
[ "$max" -lt 1 ] && max=1
[ "$jobs" -gt "$max" ] && jobs="$max"
fi
[ "$jobs" -lt 1 ] && jobs=1
echo "$jobs"
}
# SHA-256 of a file, portable (Linux sha256sum / macOS shasum).
_sha256() { # <file>
if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1"; else shasum -a 256 "$1"; fi | awk '{print $1}'
}
xmrig_already_built() {
local marker="$WORKER_ROOT/xmrig/.rigforge-commit" sums="$WORKER_ROOT/xmrig/.rigforge-sha256"
[ -x "$WORKER_ROOT/xmrig/build/xmrig" ] && [ -f "$marker" ] && [ "$(cat "$marker" 2>/dev/null)" = "$XMRIG_COMMIT" ] || return 1
[ "$(cat "$WORKER_ROOT/xmrig/.rigforge-donate-min" 2>/dev/null)" = 0 ] || return 1
[ ! -f "$sums" ] || [ "$(cat "$sums" 2>/dev/null)" = "$(_sha256 "$WORKER_ROOT/xmrig/build/xmrig")" ]
}
# Merge the HugePage/MSR kernel params we manage into an existing GRUB cmdline, preserving every
# other parameter and replacing only the ones we own (so re-runs don't accumulate). Echoes the merged
# cmdline. Usage: grub_merge_cmdline "<managed params>" "<current cmdline>"
grub_merge_cmdline() {
local managed="$1" current="$2" preserved="" tok
for tok in $current; do
case "$tok" in
hugepagesz=* | hugepages=* | default_hugepagesz=* | msr.allow_writes=*) ;; # ours — drop, re-added below
*) preserved="${preserved:+$preserved }$tok" ;;
esac
done
echo "${preserved:+$preserved }$managed"
}
# Drop only the kernel params RigForge manages (HugePages/MSR), preserving everything else. The inverse
# of what tune_kernel adds — used by `uninstall` to revert the GRUB cmdline.
grub_strip_managed() { # <current cmdline>
local current="$1" preserved="" tok
for tok in $current; do
case "$tok" in
hugepagesz=* | hugepages=* | default_hugepagesz=* | msr.allow_writes=*) ;; # ours — drop
*) preserved="${preserved:+$preserved }$tok" ;;
esac
done
printf '%s' "$preserved"
}
# Escape a value for use as the REPLACEMENT text of a `sed "s|pat|repl|"` command: backslash-prefix
# `\`, `&` (whole-match backreference) and the `|` delimiter. Without this, a pre-existing kernel
# param containing one of them (e.g. memmap=4G&2M) would silently corrupt /etc/default/grub (#134).
# Implemented with sed, not ${var//}, because bash 5.2's patsub_replacement changed `&` semantics
# in expansion replacements while our floor is bash 3.2.
_sed_escape_replacement() { # <value> -> escaped value on stdout
printf '%s' "$1" | sed -e 's/[\\&|]/\\&/g'
}
# --- Setup: prerequisites & configuration ---
check_prerequisites() {
log "Verifying system prerequisites..."
if ! command -v jq &>/dev/null; then
# Appliance mode never installs packages — same contract as install_dependencies: the read-only
# image bakes everything at build, and an install here would silently violate it (pithead#797 R1).
if [ "$RIGFORGE_APPLIANCE" = 1 ]; then
error "jq is required but missing — appliance mode never installs packages; bake jq into the image."
fi
if [ "$OS_TYPE" == "Darwin" ]; then
if command -v brew &>/dev/null; then
log "Installing prerequisite: jq..."
brew install jq
else
error "Homebrew is required on macOS to install dependencies."
fi
else
log "Installing prerequisite: jq..."
if command -v apt-get &>/dev/null; then
# This is often the FIRST apt call on a fresh boot, so carry the same DPkg::Lock::Timeout
# as install_dependencies (#74) — otherwise an unattended-upgrades lock fails it outright.
sudo apt-get update -qq -o DPkg::Lock::Timeout=300 &&
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq -o DPkg::Lock::Timeout=300 jq
elif command -v dnf &>/dev/null; then
sudo dnf install -y -q jq
elif command -v pacman &>/dev/null; then
sudo pacman -Sy --noconfirm jq
else
error "jq is required and no supported package manager was found. Please install jq manually."
fi
fi
fi
}
ensure_config_exists() {
if [ ! -f "$CONFIG_JSON" ]; then
warn "Configuration file not found: $CONFIG_JSON"
# `|| true`: on a non-interactive stdin (EOF) `read` returns non-zero, which would abort the run
# via the ERR trap; instead fall through to the clear "configuration required" error below.
read -r -p "Create a minimal configuration now? (y/N): " CREATE_CONF || true
if [[ "$CREATE_CONF" =~ ^[Yy] ]]; then
log "Starting interactive setup..."
# Validate before writing; bounded retries also terminate on piped stdin/EOF.
url_tries="${SETUP_URL_TRIES:-3}"
IN_URL=""
for i in $(seq 1 "$url_tries"); do
read -r -p "Enter your pool URL (host:port, e.g. your-stack:3333): " IN_URL || break
if [ -z "$IN_URL" ]; then
warn "A pool URL is required."
IN_URL=""
continue
fi
if ! [[ "$IN_URL" =~ :[0-9]+$ ]]; then
warn "Pool URL must include a port, e.g. $IN_URL:3333."
IN_URL=""
continue
fi
# Validate the host now, the same way parse_config will in a moment — otherwise a
# host-less URL like ":3333" passes the port check, gets written, and then parse_config
# hard-errors on it, leaving a broken config.json on disk that suppresses this prompt on
# the re-run (the file now exists). Failing before the write keeps the user re-promptable.
_host="${IN_URL%:*}"
case "$_host" in
\[*\])
if [[ "$_host" =~ ^\[[0-9A-Fa-f:]+\]$ ]]; then break; fi
warn "Pool URL '$IN_URL' has an invalid IPv6 literal (use [addr]:port)."
;;
*)
if [[ "$_host" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]]; then break; fi
warn "Pool URL host '$_host' is not a valid hostname or IP."
;;
esac
IN_URL=""
done
if [ -z "$IN_URL" ]; then
error "A valid pool URL (host:port) is required — giving up after $url_tries attempt(s)."
fi
# Optional stratum password; validate before creating the config.
IN_PASS=""
read -r -p "Stratum password, if your stack requires one (shown by 'pithead status'; Enter for none): " IN_PASS || true
if [ -n "$IN_PASS" ] && ! [[ "$IN_PASS" =~ ^[[:graph:]]+$ ]]; then
error "Stratum password must have no spaces or control characters."
fi
IN_SOCKS5=""
case "$_host" in
*.[Oo][Nn][Ii][Oo][Nn])
log "An onion pool needs Tor. Using SOCKS proxy 127.0.0.1:9050; install and run Tor yourself."
read -r -p "SOCKS proxy (host:port; Enter for 127.0.0.1:9050): " IN_SOCKS5 || true
IN_SOCKS5="${IN_SOCKS5:-127.0.0.1:9050}"
(_validate_host_port "$IN_SOCKS5" "SOCKS proxy" 9050) >/dev/null 2>&1 || error "SOCKS proxy must be a valid host:port."
;;
esac
# Omit unset fields to preserve the LAN config's minimal shape.
(umask 077 && jq -n --arg url "$IN_URL" --arg pass "$IN_PASS" --arg socks5 "$IN_SOCKS5" \
'{pools: [({url: $url} + (if $pass == "" then {} else {pass: $pass} end) + (if $socks5 == "" then {} else {socks5: $socks5} end))]}' >"$CONFIG_JSON")
# The operator is told (below) to hand-edit this file to add a wallet / ACCESS_TOKEN, and the
# first `apply` may be a long way off — chmod now so those secrets are never world-readable
# in the interim (generate_xmrig_config's chmod 600 only runs on setup/apply). (#131)
chmod 600 "$CONFIG_JSON"
_reown_worker # hand the freshly-created config.json to the operator, even if setup later fails
log "Created $CONFIG_JSON successfully."
# New-user safety net: the minimal config has no wallet, so a PUBLIC-pool miner who stops here
# would credit hashes to the hostname, not themselves — and nothing later (doctor included)
# flags it, since it's a pool-side auth detail. Pithead users correctly need no wallet.
warn "Mining to a PUBLIC pool (SupportXMR, etc.)? Add your Monero wallet as the pool \"user\" in"
warn "$CONFIG_JSON and run 'sudo $0 apply', or your hashes credit '$(hostname)', not you."
warn "Connecting to a Pithead stack instead? You're all set — no wallet needed."
else
error "Configuration file required to proceed."
fi
fi
}
# #412: every numeric range guard in this script was written as
# `! [[ $v =~ ^[0-9]+$ ]] || [ "$v" -gt MAX ]`, and that shape ACCEPTS the value it exists to reject
# when $v carries more digits than bash can evaluate: `[` returns **2** — an error, not "false" — and
# `if` reads any non-zero as false, so the else branch runs and no error fires. The only tell is a
# raw `[: ...: integer expression expected` on stderr, and where the caller redirects stderr there is
# no tell at all. #405 fixed one instance (a pool's port) with a digit-count guard; the shape was
# still live at ten more call sites — nine of them reachable, measured, and accepting a 20-digit
# value; the tenth (the api_allow_from IPv6 prefix) is unreachable because the surrounding regex
# already caps that field at three digits, and routes through here for uniformity. The guard lives
# HERE once rather than being re-derived per key and missed again on the next key somebody adds.
#
# The bound is the digit count of the key's OWN maximum: no value in range can be longer, so nothing
# legal is rejected — except a value zero-padded PAST that width, which the range test used to
# evaluate as decimal and keep. That trade is #405's, made again deliberately. For a key read out of
# JSON it is narrower than it looks: jq normalises a padded NUMBER before bash sees it (`065536` ->
# `65536`), so only a padded STRING (`"065536"`) is newly rejected. Padding within the width
# (`"05120"`, or `:08080` in #405) is untouched. All three shapes are pinned in tests/run.sh.
#
# Callers keep their own `error` messages: each names its key, its units and its range, and a shared
# message could name none of them. `_validate_host_port` keeps its own inline copy of this guard
# because it emits a DISTINCT digit-count message (#405) — the range wording there would quote a port
# that IS in range and tell the operator nothing.
_uint_in_range() { # <value> <min> <max> -> 0 iff a whole number bash can evaluate, within [min,max]
local _v="$1" _min="$2" _max="$3"
[[ "$_v" =~ ^[0-9]+$ ]] || return 1
[ "${#_v}" -le "${#_max}" ] || return 1
[ "$_v" -ge "$_min" ] && [ "$_v" -le "$_max" ]
}
# #400: ONE host:port validator, shared by a pool's `url` and its `socks5` proxy. The issue asks for
# the socks5 value to get "the same rules the pool URL gets" — a second copy of these four checks
# would satisfy that on the day it was written and drift the first time either side is touched, so
# the rules live in one place instead. <value> <label> <example-port>: the label leads the operator
# -facing message, and the example port makes the hint fit its key (a pool is :3333, a SOCKS5 proxy
# is :9050). Every check and every regex below is verbatim from the pool-url path it replaces.
_validate_host_port() { # <value> <label> <example-port>
local _v="$1" _label="$2" _eg="$3" _h _p
if ! [[ "$_v" =~ :[0-9]+$ ]]; then
error "$_label '$_v' must include a port, e.g. $_v:$_eg."
fi
_h="${_v%:*}"
_p="${_v##*:}"
# Bound the digit count before arithmetic; huge values otherwise evade the range check.
if [ "${#_p}" -gt 5 ]; then
error "$_label port '$_p' in '$_v' has more than five digits; a port is 1-65535."
fi
if [ "$_p" -lt 1 ] || [ "$_p" -gt 65535 ]; then
error "$_label port must be between 1 and 65535 (got '$_p' in '$_v')."
fi
# The host must be a valid hostname / FQDN / IPv4, or a bracketed IPv6 literal. This also
# rejects the unfilled template placeholder (<...>), whitespace, and shell/URL metacharacters.
# A v3 .onion is an ordinary hostname to this pattern, which is what makes #400 work at all.
case "$_h" in
\[*\]) [[ "$_h" =~ ^\[[0-9A-Fa-f:]+\]$ ]] || error "$_label '$_v' has an invalid IPv6 literal (use [addr]:port)." ;;
*) [[ "$_h" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]] || error "$_label host '$_h' is not a valid hostname or IP." ;;
esac
}
parse_config() {
log "Parsing configuration..."
# A missing config (e.g. `apply`/`tune` before `setup`) is a different, clearer error than bad JSON.
if [ ! -f "$CONFIG_JSON" ]; then
error "No configuration at $CONFIG_JSON — run 'sudo $0 setup' first (it creates one on first run)."
fi
if ! jq -e . "$CONFIG_JSON" >/dev/null 2>&1; then
error "$CONFIG_JSON is not valid JSON."
fi
# HOME_DIR becomes a filesystem path we mkdir/cd/write under (with sudo), so validate it via the
# shared resolver (the same one the privileged uninstall/backup/restore use, so none of them can act
# on an unvalidated path).
RAW_HOME=$(jq -r '.HOME_DIR // "DYNAMIC_HOME"' "$CONFIG_JSON")
if ! WORKER_ROOT=$(_worker_root_for_home "$RAW_HOME"); then
error "HOME_DIR must be \"DYNAMIC_HOME\" or an absolute path (letters, digits, . _ - /); got: '$RAW_HOME'."
fi
DONATION=$(jq -r '.DONATION // 0' "$CONFIG_JSON")
if ! _uint_in_range "$DONATION" 0 100; then
error "DONATION must be an integer between 0 and 100 (got: $DONATION)."
fi
# Pool(s). The pool target is XMRig's native `pools` array — the same structure XMRig uses
# ({"url","user","pass","keepalive","tls",...}). Each entry needs a `url` of the form `host:port`;
# every other field falls back to a Pithead-friendly default. List multiple entries for failover.
# The pool `user` is left blank here and filled with the rig name in generate_xmrig_config.
if ! jq -e '.pools | type == "array" and length > 0' "$CONFIG_JSON" >/dev/null 2>&1; then
error "No pools configured. Set a 'pools' array in $CONFIG_JSON — e.g. {\"pools\": [{\"url\": \"your-pool-host:3333\"}]}."
fi
POOLS_JSON=$(jq -c '
.pools | map({
url: (.url // ""),
user: (.user // ""),
pass: (.pass // "x"),
keepalive: (.keepalive // true),
tls: (.tls // false),
enabled: (.enabled // true)
})
' "$CONFIG_JSON") || error "Could not parse 'pools' in $CONFIG_JSON."
# tls-fingerprint (#115): re-attach the pin from the raw config, emitted ONLY when set — adding
# it unconditionally (null) would change the generated config shape for every existing rig on
# its next apply. A second single-line pass rather than lines inside the map above: kcov can't
# attribute in-string program lines, and the patch-coverage gate needs every new line hittable.
POOLS_JSON=$(jq -c --argjson base "$POOLS_JSON" '[$base, [.pools[] | ."tls-fingerprint"]] | transpose | map(.[0] + (if (.[1] // null) != null then {"tls-fingerprint": .[1]} else {} end))' "$CONFIG_JSON") || error "Could not parse 'pools' in $CONFIG_JSON."
# Re-attach socks5 only when set, using the same shape/coverage rule as tls-fingerprint.
POOLS_JSON=$(jq -c --argjson base "$POOLS_JSON" '[$base, [.pools[] | .socks5]] | transpose | map(.[0] + (if (.[1] // null) != null then {"socks5": .[1]} else {} end))' "$CONFIG_JSON") || error "Could not parse 'pools' in $CONFIG_JSON."
# #265: jq's `//` treats an explicit false like null/missing, so the map above rewrites an
# operator's "keepalive": false / "enabled": false to the true default. Restore explicit falses
# from the raw config in another single-line pass (same kcov rationale as #115 above). Only a
# boolean false matches `== false`; non-boolean junk flows through `//` untouched to validation.
POOLS_JSON=$(jq -c --argjson base "$POOLS_JSON" '[$base, [.pools[] | [(.keepalive == false), (.enabled == false)]]] | transpose | map(.[0] + (if .[1][0] then {"keepalive": false} else {} end) + (if .[1][1] then {"enabled": false} else {} end))' "$CONFIG_JSON") || error "Could not parse 'pools' in $CONFIG_JSON."
# Validate every pool field — fail fast with a clear message rather than writing a config XMRig
# would choke on. url must be host:port: a valid hostname / IPv4 / bracketed-IPv6 host and a port
# in 1-65535; user/pass reject whitespace and shell/control characters; keepalive/tls/enabled
# must be booleans.
# Iterate one compact JSON object per pool (robust even when a field like user is empty), and read
# each field back with jq.
while IFS= read -r _pool; do
_u=$(jq -r '.url' <<<"$_pool")
_user=$(jq -r '.user' <<<"$_pool")
_pass=$(jq -r '.pass' <<<"$_pool")
[ -n "$_u" ] || error "A pool entry has no url — set 'pools[].url' (host:port) in $CONFIG_JSON."
_validate_host_port "$_u" "Pool url" 3333
# SOCKS5 proxy (#400), per pool, emitted only when set. XMRig dials the pool through it and
# sends the HOSTNAME in the CONNECT request (Client::Socks5::connect uses ATYP 0x03 for
# anything that is not an IP literal), so the proxy resolves the name — which is what lets a
# v3 .onion stratum work with no `socks5h` variant to ask for. Same host:port rules as the
# pool url, via the shared validator, so an onion pool and its proxy cannot be judged by two
# different standards.
# #408: `""` is not "unset", and the two predicates used to disagree about that. The emit
# step above keys on jq truthiness, so an empty string IS written into the generated
# config; `// empty` here makes it indistinguishable from absent, so it skipped validation
# entirely. Reject it rather than dropping the key — dropping is a silent downgrade for
# tls-fingerprint (see below), and one rule for both keys stops them drifting apart again.
if [ "$(jq -r 'has("socks5") and .socks5 == ""' <<<"$_pool")" = "true" ]; then
error "Pool socks5 is an empty string — remove the key, or give it a host:port such as 127.0.0.1:9050."
fi
_s5=$(jq -r '.socks5 // empty' <<<"$_pool")
if [ -n "$_s5" ]; then
_validate_host_port "$_s5" "Pool socks5" 9050
fi
if [ -n "$_user" ] && ! [[ "$_user" =~ ^[A-Za-z0-9._:@+-]+$ ]]; then
error "Pool user '$_user' has invalid characters (allowed: letters, digits, . _ - : @ +)."
fi
# #415: `pass` must be a STRING, checked before the charset rule below can be fooled. The
# feed masks a set password to the {"__secret__": true} sentinel and _control_commit
# resolves it back, but nothing stops a marker — or any other JSON object — reaching
# config.json by a hand edit or by a future writer that forgets to resolve one. `jq -r`
# renders an object as its JSON text, and the empty object `{}` renders as two graph
# characters on one line, so it passes the check below and becomes the rig's literal
# password. Type-check here so the guarantee does not depend on every writer remembering:
# the same posture as the non-string tls-fingerprint rejection below.
if [ "$(jq -r '.pass | type' <<<"$_pool")" != "string" ]; then
error "Pool pass must be a string (got a $(jq -r '.pass | type' <<<"$_pool")). A masked-secret marker from the API feed is not a password — send the real value, or omit 'pass' entirely."
fi
if ! [[ "$_pass" =~ ^[[:graph:]]+$ ]]; then
error "Pool pass must be non-empty with no spaces or control characters."
fi
# TLS fingerprint pin (#115): xmrig does NO cert verification for stratum without a pin
# (v6.26.0 Tls.cpp:186 verifyFingerprint returns true when the pin is a NULL pointer), so
# the fingerprint is the
# ONLY server authentication stratum-TLS has. 64 hex chars, either case (xmrig compares
# case-insensitively); passed verbatim, never normalized. A pin without tls:true is a hard
# error — a silently-ignored pin would leave the operator believing they're protected.
# #408: an EMPTY pin is not an absent one, and the difference is security-relevant. XMRig
# v6.26.0 skips verification only when the pin is a null pointer; `""` parses to a non-null
# empty String (Pool.cpp:129 -> String::operator=(const char *)), so Tls.cpp:186 falls
# through to strncasecmp against every certificate and matches none — the pool never
# connects. That is why this rejects rather than dropping the key: dropping it would turn
# "refuse every cert" into "verify nothing", a silent downgrade introduced by the fix.
if [ "$(jq -r 'has("tls-fingerprint") and ."tls-fingerprint" == ""' <<<"$_pool")" = "true" ]; then
error "Pool tls-fingerprint is an empty string. Remove the key to connect without a pin, or set the cert's SHA-256 as 64 hex chars. An empty pin is not the same as no pin: xmrig compares it against every certificate and matches none, so the pool never connects."
fi
_fp=$(jq -r '."tls-fingerprint" // empty' <<<"$_pool")
if [ -n "$_fp" ]; then
if ! [[ "$_fp" =~ ^[0-9A-Fa-f]{64}$ ]]; then
error "Pool tls-fingerprint must be the cert's SHA-256 as 64 hex chars (no colons). Get it with: echo | openssl s_client -connect $_u 2>/dev/null | openssl x509 -noout -fingerprint -sha256 | cut -d= -f2 | tr -d ':'"
fi
if [ "$(jq -r '.tls' <<<"$_pool")" != "true" ]; then
error "Pool '$_u' sets tls-fingerprint but not \"tls\": true — the pin only applies to a TLS connection; set both or remove the fingerprint."
fi
fi
for _f in keepalive tls enabled; do
_bv=$(jq -r --arg f "$_f" '.[$f]' <<<"$_pool")
case "$_bv" in true | false) ;; *) error "Pool $_f must be true or false (got: $_bv)." ;; esac
done
done < <(jq -c '.[]' <<<"$POOLS_JSON")
# HTTP API token (OPTIONAL). By default the rig's read-only xmrig API is left OPEN — no token,
# matching Pithead's stock no-auth probe of GET http://<rig>:8080/1/summary. Set ACCESS_TOKEN to
# require a Bearer token instead and match it dashboard-side; see docs/pithead-integration.md.
ACCESS_TOKEN=$(jq -r '.ACCESS_TOKEN // empty' "$CONFIG_JSON")
# Opt-in firewall scoping (#142): restrict the read-only API port(s) to one source + loopback.
# Empty (default) = RigForge manages no firewall. Accepts IPv4 or IPv6 address/CIDR (#243): the
# strict per-family charset is the injection guard (the value reaches an nft file), and
# API_ALLOW_FAMILY (ip|ip6) picks the nft match keyword.
API_ALLOW_FROM=$(jq -r '.api_allow_from // empty' "$CONFIG_JSON")
API_ALLOW_FAMILY=ip
if [ -n "$API_ALLOW_FROM" ]; then
if [[ "$API_ALLOW_FROM" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}(/[0-9]{1,2})?$ ]]; then
local _a _b _c _d _p
IFS='./' read -r _a _b _c _d _p <<<"$API_ALLOW_FROM"
if [ "$_a" -gt 255 ] || [ "$_b" -gt 255 ] || [ "$_c" -gt 255 ] || [ "$_d" -gt 255 ] || [ "${_p:-0}" -gt 32 ]; then
error "api_allow_from must be a valid IPv4/IPv6 address or CIDR (e.g. 192.168.1.0/24 or fd00::/64); got: '$API_ALLOW_FROM'."
fi
API_ALLOW_FAMILY=ip
elif [[ "$API_ALLOW_FROM" =~ ^[0-9A-Fa-f:]+(/[0-9]{1,3})?$ ]] && [[ "$API_ALLOW_FROM" == *:*:* ]]; then
# IPv6/CIDR: hex-and-colons charset (the injection guard) + >=2 colons + a 0-128 prefix.
# nft rejects a truly malformed address at load, so this is the guard, not a full RFC
# validator — same posture as the IPv4 octet check above.
local _pfx=""
case "$API_ALLOW_FROM" in */*) _pfx="${API_ALLOW_FROM##*/}" ;; esac
if [ -n "$_pfx" ] && ! _uint_in_range "$_pfx" 0 128; then
error "api_allow_from IPv6 prefix must be 0-128 (got: '$API_ALLOW_FROM')."
fi
API_ALLOW_FAMILY=ip6
else
error "api_allow_from must be an IPv4/IPv6 address or CIDR (e.g. 192.168.1.0/24 or fd00::/64); got: '$API_ALLOW_FROM'."
fi
fi
# When set, the token is sent as an HTTP Authorization header, so keep it to safe, header-clean
# characters. Empty is allowed and means "open API" (the default).
if [ -n "$ACCESS_TOKEN" ] && ! [[ "$ACCESS_TOKEN" =~ ^[A-Za-z0-9._:@+-]+$ ]]; then
error "ACCESS_TOKEN has invalid characters (allowed: letters, digits, . _ - : @ +): '$ACCESS_TOKEN'."
fi
# ACCESS_TOKENS (#516): an OPTIONAL name -> token map, one per consuming Pithead stack, so a bench
# borrow never needs prod's ACCESS_TOKEN. Every entry authenticates :8081/:8082 like it does; only xmrig's :8080 stays master-only.
ACCESS_TOKENS_JSON=$(jq -c '.ACCESS_TOKENS // {}' "$CONFIG_JSON" 2>/dev/null)
jq -e 'type == "object" and all(.[]; type == "string")' <<<"$ACCESS_TOKENS_JSON" >/dev/null 2>&1 || error "ACCESS_TOKENS in $CONFIG_JSON must be a JSON object mapping names to string tokens."
while IFS=$'\t' read -r _atn _att; do
[[ "$_att" =~ ^[A-Za-z0-9._:@+-]+$ ]] || error "ACCESS_TOKENS.\"$_atn\" has invalid characters (allowed: letters, digits, . _ - : @ +): '$_att'."
done < <(jq -r 'to_entries[] | [.key, .value] | @tsv' <<<"$ACCESS_TOKENS_JSON")
# Opt-in periodic live auto-tuning (#46, #95): tri-state. "disabled" (default) installs no timer;
# "performance" schedules a periodic tune for raw H/s; "efficiency" schedules one for hashrate-per-watt.
# Legacy booleans still parse (true -> performance, false -> disabled); a typo hard-errors rather than
# silently disabling tuning. AUTOTUNE_TARGET (perf|efficiency) is what autotune() and the unit consume.
_at=$(jq -r '.autotune // "disabled"' "$CONFIG_JSON")
case "$_at" in
disabled | false | off | none | null | "") AUTOTUNE_MODE=disabled ;;
performance | perf | true | on) AUTOTUNE_MODE=performance ;;
efficiency | eff) AUTOTUNE_MODE=efficiency ;;
*) error "Invalid \"autotune\" value '$_at' in config.json — use \"disabled\", \"performance\", or \"efficiency\"." ;;
esac
case "$AUTOTUNE_MODE" in efficiency) AUTOTUNE_TARGET=efficiency ;; *) AUTOTUNE_TARGET=perf ;; esac
# Opt-in miner watchdog (#139): a periodic health check (same timer machinery as autotune) that
# restarts a WEDGED miner — process alive, 0 H/s or API dead, the case systemd's Restart= can't
# see — and, when max_temp_c is set, stops the miner above that temperature (restarting 5°C
# below). A typo hard-errors: a recovery mechanism must not be silently disabled.
_wd=$(jq -r '.watchdog // "disabled"' "$CONFIG_JSON")
case "$_wd" in
disabled | false | off | none | null | "") WATCHDOG_MODE=disabled ;;
enabled | true | on) WATCHDOG_MODE=enabled ;;
*) error "Invalid \"watchdog\" value '$_wd' in config.json — use \"disabled\" or \"enabled\"." ;;
esac
WATCHDOG_INTERVAL_MIN=$(jq -r '.watchdog_interval_min // 5' "$CONFIG_JSON")
if ! _uint_in_range "$WATCHDOG_INTERVAL_MIN" 1 1440; then
error "watchdog_interval_min must be a whole number of minutes, 1-1440 (got: $WATCHDOG_INTERVAL_MIN)."
fi
# Empty (the default) = no thermal cutoff. Opt-in because thermal_zone0's meaning varies by
# board — a wrong cutoff on an unchecked sensor is worse than none. 40-110: below 40 a loaded
# rig would never restart, above 110 is past any CPU's limit.
MAX_TEMP_C=$(jq -r '.max_temp_c // empty' "$CONFIG_JSON")
if [ -n "$MAX_TEMP_C" ]; then
if ! _uint_in_range "$MAX_TEMP_C" 40 110; then
error "max_temp_c must be empty (no thermal cutoff) or a whole number 40-110 °C (got: $MAX_TEMP_C)."
fi
fi
# Co-resident reservation headroom (#305): MB of HugePages to leave for another workload on the box
# (a co-located pithead stack), ADDED to RigForge's computed mining need when it sizes the GRUB
# reservation and the runtime pool. RigForge stays the sole writer of the reservation; the caller
# just declares "leave this much for the rest of the box." Default 0 (miner-only sizing). The upper
# bound is generous (64 GB of 2MB pages) — this is a deliberate reservation, not a typo guard.
HUGEPAGES_RESERVE_EXTRA_MB=$(jq -r '.hugepages_reserve_extra_mb // 0' "$CONFIG_JSON")
if ! _uint_in_range "$HUGEPAGES_RESERVE_EXTRA_MB" 0 65536; then
error "hugepages_reserve_extra_mb must be a whole number of MB, 0-65536 (got: $HUGEPAGES_RESERVE_EXTRA_MB)."
fi
# Total-pool ceiling (#398): a HARD CAP on the runtime pool RigForge will grow to, expressed in MB —
# unlike hugepages_reserve_extra_mb (which ADDS declared headroom into the computed requirement),
# this bounds the WRITE itself. It exists because no value of hugepages_reserve_extra_mb can fix a
# double-count between that requirement and the grow-only write's availability check (rigforge#398):
# a co-resident consumer's already-held pages land inside the requirement (as declared headroom) and
# are simultaneously excluded from "available," so the shortfall the grow-only write computes counts
# the same pages twice. A ceiling stops the WRITE from ever exceeding the box's declared honest
# capacity, regardless of how that upstream arithmetic comes out. Default 0 = no ceiling (today's
# behavior, unchanged) — only a caller that declares this key opts into the cap. An odd MB value
# FLOORS to the 2MB page below (e.g. 5121 -> 2560 pages, 5120MB effective) — a cap must round
# toward less memory, never more.
HUGEPAGES_POOL_CEILING_MB=$(jq -r '.hugepages_pool_ceiling_mb // 0' "$CONFIG_JSON")
if ! _uint_in_range "$HUGEPAGES_POOL_CEILING_MB" 0 65536; then
error "hugepages_pool_ceiling_mb must be a whole number of MB, 0-65536 (got: $HUGEPAGES_POOL_CEILING_MB)."
fi
# First-class thread cap (#305): a CEILING on the RandomX thread count — min(auto-detected, threads).
# Empty (default) = let XMRig/L3 sizing pick. A co-located miner sets this (e.g. nproc-2) to leave
# the stack cores free. Sizing and the generated cpu.rx both honour it; it never raises the count.
THREADS_CAP=$(jq -r '.threads // empty' "$CONFIG_JSON")
if [ -n "$THREADS_CAP" ]; then
if ! _uint_in_range "$THREADS_CAP" 1 1024; then
error "threads must be empty (auto) or a whole number 1-1024 (got: $THREADS_CAP)."
fi
fi
# Opt-in: install a `rigforge` command on PATH (a symlink in BIN_DIR). Off by default — setup makes
# no system-wide convenience change you didn't ask for.
ADD_TO_PATH=$(jq -r '.add_to_path // false' "$CONFIG_JSON")
# Opt-in read-only sister API (#99): serves XMRig's /2/summary enriched with RigForge state
# (tune/power/health/provenance) on its own port. Same posture as :8080 — read-only, LAN-bound,
# gated by ACCESS_TOKEN or any named ACCESS_TOKENS entry (#516).
_api=$(jq -r '.api // "disabled"' "$CONFIG_JSON")
case "$_api" in
disabled | false | off | none | null | "") API_MODE=disabled ;;
enabled | true | on) API_MODE=enabled ;;
*) error "Invalid \"api\" value '$_api' in config.json — use \"disabled\" or \"enabled\"." ;;
esac
API_PORT=$(jq -r '.api_port // 8081' "$CONFIG_JSON")
if ! _uint_in_range "$API_PORT" 1 65535; then error "api_port must be a port number 1-65535 (got: $API_PORT)."; fi
if [ "$API_PORT" = 8080 ]; then error "api_port 8080 collides with XMRig's own API — pick another port."; fi
API_BIND=$(jq -r '.api_bind // "0.0.0.0"' "$CONFIG_JSON")
[[ "$API_BIND" =~ ^[0-9A-Fa-f.:]+$ ]] || error "api_bind must be an IP address (got: $API_BIND)."
# Opt-in WRITABLE control path (#236): a SEPARATE authenticated endpoint that lets the stack
# apply config changes THROUGH RigForge (see docs/adr/0001), authenticated by ACCESS_TOKEN or any ACCESS_TOKENS entry (#516).
_control=$(jq -r '.control // "disabled"' "$CONFIG_JSON")
case "$_control" in
disabled | false | off | none | null | "") CONTROL_MODE=disabled ;;
enabled | true | on) CONTROL_MODE=enabled API_MODE=enabled ;; # #507: control implies api (unobservable without the read feed) — never the reverse, and api stays out of CONTROL_WRITABLE_KEYS below so it's never remote-settable
*) error "Invalid \"control\" value '$_control' in config.json — use \"disabled\" or \"enabled\"." ;;
esac
CONTROL_PORT=$(jq -r '.control_port // 8082' "$CONFIG_JSON")
if ! _uint_in_range "$CONTROL_PORT" 1 65535; then error "control_port must be a port number 1-65535 (got: $CONTROL_PORT)."; fi
if [ "$CONTROL_PORT" = 8080 ]; then error "control_port 8080 collides with XMRig's own API — pick another port."; fi
CONTROL_BIND=$(jq -r '.control_bind // "0.0.0.0"' "$CONFIG_JSON")
[[ "$CONTROL_BIND" =~ ^[0-9A-Fa-f.:]+$ ]] || error "control_bind must be an IP address (got: $CONTROL_BIND)."
if [ "$CONTROL_MODE" = enabled ]; then
if [ "${API_MODE:-disabled}" = enabled ] && [ "$CONTROL_PORT" = "$API_PORT" ]; then
error "control_port ($CONTROL_PORT) collides with the sister API port — pick another port."
fi
# Fail-closed dual auth: the writable path demands BOTH a Bearer token AND a pinned source —
# missing either would expose an unauthenticated remote config write.
[ -n "${ACCESS_TOKEN:-}" ] || error "control: \"enabled\" requires ACCESS_TOKEN — a writable API with no token is an open remote config write. Set ACCESS_TOKEN in config.json."
[ -n "${API_ALLOW_FROM:-}" ] || error "control: \"enabled\" requires api_allow_from — the writable path must be pinned to the stack host. Set api_allow_from in config.json."
fi
# Opt-in REMOTE UPGRADE (#308, ADR 0002): a SECOND switch, layered on control:"enabled", that lets
# the stack trigger a rig to upgrade its own RigForge to the latest release. Default off, and it
# does NOT come up just because `control` is on. Rides the control channel's own auth, no auth of its own.
_cu=$(jq -r '.control_upgrade // "disabled"' "$CONFIG_JSON")
case "$_cu" in
disabled | false | off | none | null | "") CONTROL_UPGRADE=disabled ;;
enabled | true | on) CONTROL_UPGRADE=enabled ;;
*) error "Invalid \"control_upgrade\" value '$_cu' in config.json — use \"disabled\" or \"enabled\"." ;;
esac
if [ "$CONTROL_UPGRADE" = enabled ] && [ "${CONTROL_MODE:-disabled}" != enabled ]; then
error "control_upgrade requires control: \"enabled\" first — the remote-upgrade path rides the control channel's Bearer token and api_allow_from source pin (ADR 0002)."
fi
# #243/scan: an IPv6 api_allow_from only scopes traffic the APIs actually receive over IPv6, but
# api_bind/control_bind default to IPv4 (0.0.0.0). Warn on the silent no-op — it fails safe (no
# reachable v6 to scope), but the operator's intent isn't met until they bind "::".
if [ "${API_ALLOW_FAMILY:-ip}" = ip6 ] && [[ "$API_BIND" != *:* ]] && [[ "$CONTROL_BIND" != *:* ]]; then
warn "api_allow_from is IPv6 but api_bind/control_bind are IPv4 (0.0.0.0) — the APIs aren't reachable over IPv6, so the v6 scope takes no effect. Set api_bind/control_bind to \"::\" to serve + scope IPv6."
fi
# Privilege separation (#140): run the miner as this dedicated non-root system user. Empty
# (default) keeps today's root behavior exactly. `root` is rejected — it would silently mean
# "no separation". useradd-portable names only.
MINER_USER=$(jq -r '.miner_user // empty' "$CONFIG_JSON")
if [ -n "$MINER_USER" ]; then
if [ "$MINER_USER" = "root" ]; then
error "miner_user must be a non-root system username (empty = run as root, the default)."
fi
[[ "$MINER_USER" =~ ^[a-z_][a-z0-9_-]{0,31}$ ]] || error "miner_user '$MINER_USER' is not a valid system username (lowercase, digits, _ -; max 32 chars)."
fi
_warn_unknown_config_keys
}
# Typo lint (#138): a key parse_config doesn't read is silently ignored — worst case a security key
# ("ACESS_TOKEN") that the operator believes is protecting them. Warn (never error: an unknown key
# is at worst a no-op, and erroring would brick fleet applies on any future rename), name each
# unknown key with a case-insensitive did-you-mean, and point at the reference. Keys starting with
# `_` are the comment convention (config.reference.json's own _docs); RIG_NAME is reserved for the
# #1 image seed. Warn NAMES only, never values — a fat-fingered token must not land in a log.
_warn_unknown_config_keys() {
local known="pools ACCESS_TOKEN ACCESS_TOKENS DONATION autotune add_to_path HOME_DIR api api_port api_bind api_allow_from miner_user RIG_NAME watchdog watchdog_interval_min max_temp_c control control_port control_bind control_upgrade hugepages_reserve_extra_mb hugepages_pool_ceiling_mb threads"
local known_pool="url user pass keepalive tls enabled tls-fingerprint socks5"
local k lk m lm hit hint unknown_seen=0
while IFS= read -r k; do
case "$k" in _*) continue ;; esac
hit=""
hint=""
lk=$(printf '%s' "$k" | tr '[:upper:]' '[:lower:]')
for m in $known; do
if [ "$k" = "$m" ]; then
hit=1
break
fi
lm=$(printf '%s' "$m" | tr '[:upper:]' '[:lower:]')
if [ "$lk" = "$lm" ]; then hint="$m"; fi
done
if [ -n "$hit" ]; then continue; fi
if [ -n "$hint" ]; then warn "config.json: unknown key \"$k\" is ignored — did you mean \"$hint\"?"; else warn "config.json: unknown key \"$k\" is ignored."; fi
unknown_seen=1
done < <(jq -r 'keys[]' "$CONFIG_JSON" 2>/dev/null || true)
while IFS= read -r k; do
case "$k" in _*) continue ;; esac
hit=""
hint=""
lk=$(printf '%s' "$k" | tr '[:upper:]' '[:lower:]')
for m in $known_pool; do
if [ "$k" = "$m" ]; then
hit=1
break
fi
lm=$(printf '%s' "$m" | tr '[:upper:]' '[:lower:]')
if [ "$lk" = "$lm" ]; then hint="$m"; fi
done
if [ -n "$hit" ]; then continue; fi
if [ -n "$hint" ]; then warn "config.json: unknown pool field \"$k\" is ignored — did you mean \"$hint\"?"; else warn "config.json: unknown pool field \"$k\" is ignored."; fi
unknown_seen=1
done < <(jq -r '(.pools // []) | map(keys[]) | unique | .[]' "$CONFIG_JSON" 2>/dev/null || true)
if [ "$unknown_seen" = 1 ]; then warn "See config.reference.json for every supported key."; fi
}
# --- Setup: workspace & dependency install ---
prepare_workspace() {
log "Preparing workspace at $WORKER_ROOT..."
if [ ! -d "$WORKER_ROOT" ]; then
mkdir -p "$WORKER_ROOT" 2>/dev/null || sudo mkdir -p "$WORKER_ROOT"
fi
# Fix permissions to ensure the current user can write
if [ "${EUID:-$(id -u)}" -eq 0 ] || [ ! -w "$WORKER_ROOT" ]; then
if [ "$OS_TYPE" == "Darwin" ]; then
sudo chown -R "$REAL_USER" "$WORKER_ROOT"
else
sudo chown -R "$REAL_USER":"$REAL_USER" "$WORKER_ROOT"
fi
fi
cd "$WORKER_ROOT"
GIT_DIR="xmrig"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# Archive the existing installation only when we're about to rebuild (a no-op re-run keeps it).
if [ "$XMRIG_REBUILD" = true ] && [ -d "$GIT_DIR" ]; then
log "Archiving existing worker installation..."
mv "$GIT_DIR" "${GIT_DIR}-${TIMESTAMP}"
fi
# Prune old build archives so re-runs don't grow the disk without bound (keep the most recent
# few). Override the retention count with KEEP_ARCHIVES. The `|| true` keeps an empty glob (no
# archives yet) from tripping `set -e`/`pipefail`.
local keep="${KEEP_ARCHIVES:-3}" archives
# shellcheck disable=SC2012 # archive names are controlled (xmrig-YYYYmmdd_HHMMSS); ls -t orders by recency
archives="$(ls -dt "${GIT_DIR}-"* 2>/dev/null || true)"
if [ -n "$archives" ]; then
printf '%s\n' "$archives" | tail -n +"$((keep + 1))" | while IFS= read -r old; do
[ -n "$old" ] || continue
log "Pruning old build archive: $(basename "$old")"
rm -rf "$old" 2>/dev/null || sudo rm -rf "$old"
done
fi
}
# Package-manager detection (read-only) — sets DEP_LIST / DEP_CHECK / DEP_INSTALL, rc 1 when no
# manager is found. Split from install_dependencies so the setup --dry-run plan (#146) reuses the
# EXACT list and check — never a second copy to drift.
_detect_pkg_manager() {
DEP_LIST=""
DEP_CHECK=""
DEP_INSTALL=""
if command -v apt-get &>/dev/null; then
DEP_LIST="git build-essential cmake libuv1-dev libssl-dev libhwloc-dev gettext-base python3"
if [ "$OS_TYPE" == "Linux" ]; then
# msr-tools (rdmsr): lets `doctor` verify the prefetcher MSR mod actually applied (#66).
DEP_LIST="$DEP_LIST msr-tools"
# cpupower's package name differs by distro: linux-tools-common on Ubuntu,
# linux-cpupower on Debian. apt's install transaction is all-or-nothing, so one unknown
# name would fail the ENTIRE dependency install — gcc/cmake included (#327). Probe with
# apt-cache show (same guard as the kernel-versioned package below) and add only the
# name this distro actually ships; when neither exists, warn and carry on — cpupower is
# a tuning aid, never worth losing the compiler toolchain over.
if apt-cache show linux-tools-common &>/dev/null; then
DEP_LIST="$DEP_LIST linux-tools-common"
elif apt-cache show linux-cpupower &>/dev/null; then
DEP_LIST="$DEP_LIST linux-cpupower"
else
warn "No cpupower package found (tried linux-tools-common, linux-cpupower) — skipping it. Frequency tuning via cpupower may be unavailable."
fi
if apt-cache show "linux-tools-$(uname -r)" &>/dev/null; then
DEP_LIST="$DEP_LIST linux-tools-$(uname -r)"
fi
fi
# DPkg::Lock::Timeout waits for the apt/dpkg lock instead of failing — fresh boots often have
# unattended-upgrades holding it for a minute or two (#74).
DEP_INSTALL="sudo apt-get update -qq -o DPkg::Lock::Timeout=300 && sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq -o DPkg::Lock::Timeout=300 -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold'"
DEP_CHECK="dpkg -s"
elif command -v dnf &>/dev/null; then
DEP_LIST="git cmake libuv-devel openssl-devel hwloc-devel gettext gcc gcc-c++ make automake kernel-devel msr-tools"
DEP_INSTALL="sudo dnf install -y"
DEP_CHECK="rpm -q"
elif command -v pacman &>/dev/null; then
DEP_LIST="git cmake libuv openssl hwloc gettext base-devel"
DEP_INSTALL="sudo pacman -Sy --noconfirm --needed"
DEP_CHECK="pacman -Qi"
else
return 1
fi
}
# Echo the packages from DEP_LIST that aren't installed (read-only, no root). $DEP_LIST are PACKAGE
# names (build-essential, libuv1-dev — most have no same-named binary), so the package manager is
# the authority for "is it installed", not `command -v` (which would both miss header-only -dev
# packages and let an unrelated PATH binary mask a genuinely-absent package).
_missing_deps() {
local dep missing=""
for dep in $DEP_LIST; do
if ! $DEP_CHECK "$dep" &>/dev/null; then
missing="$missing $dep"
fi
done
printf '%s' "$missing"
}
# Appliance-mode tool check (pithead#797 R1), shared with the setup --dry-run plan (#146 — never a
# second copy to drift). Echoes the tools setup would actually invoke that are absent. `command -v`,
# not the package manager: on the appliance, dpkg/alternatives state rides the volatile /etc overlay
# and can be stale while the tools in /usr are fine (observed on the R0 bench after a reboot).
# envsubst renders the unit on every run; the compiler chain only matters when a build is pending —
# the R0 bench restored mining from a cached build with a half-broken toolchain, and the every-boot
# re-run must keep doing that.
_missing_appliance_tools() {
local t missing="" tools="envsubst"
[ "$XMRIG_REBUILD" = true ] && tools="git cmake make cc envsubst"
for t in $tools; do
command -v "$t" >/dev/null 2>&1 || missing="$missing $t"
done
printf '%s' "$missing"
}
install_dependencies() {
# Appliance mode: the toolchain is baked into the image at build — apt cannot run on the
# read-only root, and even a rw-remounted install leaves its /etc state on the volatile overlay
# (pithead#797 R0 item 1). So never install here: verify and fail naming the missing tools —
# that's an image build bug, not something a rig can fix at runtime.
if [ "$RIGFORGE_APPLIANCE" = 1 ]; then
local _missing
_missing="$(_missing_appliance_tools)"
if [ -n "$_missing" ]; then
error "Appliance mode: required tool(s) missing from the image:$_missing. The toolchain must be baked at image build — package install cannot run on the read-only root."
fi
log "Appliance mode: dependencies are baked into the image — skipping package install."
return 0
fi
if [ "$OS_TYPE" == "Darwin" ]; then
log "Installing macOS dependencies..."
if command -v brew &>/dev/null; then
if [ "${EUID:-$(id -u)}" -eq 0 ]; then
# Drop privileges for Homebrew if running as root
sudo -u "$REAL_USER" brew install cmake libuv openssl hwloc
else
brew install cmake libuv openssl hwloc
fi
else
error "Homebrew not found."
fi
else
if ! _detect_pkg_manager; then
warn "No supported package manager found. Please install dependencies manually."
return
fi
local missing_deps
missing_deps="$(_missing_deps)"
if [ -n "$missing_deps" ]; then
# `setup` is an automated provisioner (often run headless / over the release e2e), so install
# the build dependencies non-interactively rather than prompting — an interactive `read` here
# hit EOF and aborted the whole run under `set -e` on a non-tty stdin (#74).
log "Installing required system dependencies:"
echo -e " ${C_YELLOW}$missing_deps${C_RESET}"
eval "$DEP_INSTALL $missing_deps"
else
log "All system dependencies are already installed."
fi
fi
}
# --- Setup: build XMRig from source ---
compile_xmrig() {
if [ "$XMRIG_REBUILD" != true ]; then
log "XMRig $XMRIG_VERSION (commit ${XMRIG_COMMIT:0:12}) already built — skipping clone/compile."
cd "$WORKER_ROOT/xmrig/build"
return 0
fi
log "Cloning and patching XMRig source code ($XMRIG_VERSION)..."
rm -rf xmrig
git clone --quiet --branch "$XMRIG_VERSION" --depth 1 https://github.com/xmrig/xmrig.git
local actual
actual="$(git -C xmrig rev-parse HEAD)"
[ "$actual" = "$XMRIG_COMMIT" ] || {
rm -rf xmrig
error "XMRig commit mismatch: expected $XMRIG_COMMIT, got $actual"
}
log "Verified XMRig $XMRIG_VERSION at commit $XMRIG_COMMIT"
BUILD_LOG="$WORKER_ROOT/build.log"
: >"$BUILD_LOG" 2>/dev/null || true
sed "s/kDefaultDonateLevel = 1;/kDefaultDonateLevel = $DONATION;/; s/kMinimumDonateLevel = 1;/kMinimumDonateLevel = 0;/" xmrig/src/donate.h >xmrig/src/donate.h.new
mv xmrig/src/donate.h.new xmrig/src/donate.h
grep -Fxq "constexpr const int kDefaultDonateLevel = $DONATION;" xmrig/src/donate.h &&
grep -Fxq 'constexpr const int kMinimumDonateLevel = 0;' xmrig/src/donate.h || error "XMRig donation patch verification failed; refusing to build."
local cores jobs
if [ "$OS_TYPE" == "Darwin" ]; then
cores=$(sysctl -n hw.ncpu)
jobs=$(compute_build_jobs "$cores")
log "Compiling binary ($jobs of $cores cores; output -> $BUILD_LOG)..."
mkdir -p xmrig/build && cd xmrig/build
cmake .. -DWITH_HWLOC=ON -DOPENSSL_ROOT_DIR="$(brew --prefix openssl)" >>"$BUILD_LOG" 2>&1
else
cores=$(nproc)
jobs=$(compute_build_jobs "$cores")
log "Compiling binary ($jobs of $cores cores; output -> $BUILD_LOG)..."
mkdir -p xmrig/build && cd xmrig/build
cmake .. -DWITH_HWLOC=ON >>"$BUILD_LOG" 2>&1
fi
make -j"$jobs" >>"$BUILD_LOG" 2>&1
echo "$XMRIG_COMMIT" >"$WORKER_ROOT/xmrig/.rigforge-commit"
printf '0\n' >"$WORKER_ROOT/xmrig/.rigforge-donate-min"
if [ -f "$WORKER_ROOT/xmrig/build/xmrig" ]; then
_sha256 "$WORKER_ROOT/xmrig/build/xmrig" >"$WORKER_ROOT/xmrig/.rigforge-sha256"
fi
}
# --- Setup: XMRig config generation (CPU / NUMA / MSR layout) ---
generate_xmrig_config() {
log "Generating hardware-optimized XMRig configuration..."
# Identify CPU Topology
if [ "$OS_TYPE" == "Darwin" ]; then