-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-iso.sh
More file actions
executable file
·2635 lines (2276 loc) · 76.3 KB
/
Copy pathbuild-iso.sh
File metadata and controls
executable file
·2635 lines (2276 loc) · 76.3 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
set -euo pipefail
require_cmd() {
local cmd="$1"
command -v "$cmd" >/dev/null 2>&1 || {
echo "Missing required command: $cmd" >&2
exit 1
}
}
fatal() {
echo "$*" >&2
exit 1
}
read_lock_json_string() {
local key="$1"
local content="$2"
printf '%s' "$content" | sed -n "s/.*\"${key}\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p"
}
load_version_lock() {
local lock_file="${VERSION_LOCK_FILE:-$REPO_ROOT/build/version-lock.json}"
local lock_content=""
local lock_kernel_series=""
local lock_kernel_version=""
local lock_zfs_tag=""
if [ ! -f "$lock_file" ]; then
return 0
fi
lock_content="$(tr -d '\r\n' < "$lock_file")"
lock_kernel_series="$(read_lock_json_string "kernel_series" "$lock_content")"
lock_kernel_version="$(read_lock_json_string "kernel_version" "$lock_content")"
lock_zfs_tag="$(read_lock_json_string "zfs_tag" "$lock_content")"
if [ -n "$lock_kernel_series" ]; then
KERNEL_SERIES="$lock_kernel_series"
fi
if [ -n "$lock_kernel_version" ]; then
KERNEL_VERSION="$lock_kernel_version"
fi
if [ -n "$lock_zfs_tag" ]; then
ZFS_TAG="$lock_zfs_tag"
fi
VERSION_LOCK_HASH="$(sha256sum "$lock_file" | awk '{print $1}')"
echo "Using version lock file: $lock_file"
echo "Version lock hash: $VERSION_LOCK_HASH"
[ -n "$lock_kernel_version" ] && echo "Locked kernel version: $lock_kernel_version"
[ -n "$lock_zfs_tag" ] && echo "Locked OpenZFS tag: $lock_zfs_tag"
}
kernel_config_overrides() {
cat <<'EOF'
CONFIG_EFI=y
CONFIG_EFI_STUB=y
CONFIG_DRM=y
CONFIG_DRM_FBDEV_EMULATION=y
CONFIG_SYSFB=y
CONFIG_SYSFB_SIMPLEFB=y
CONFIG_DRM_SIMPLEDRM=y
CONFIG_FB=y
CONFIG_FRAMEBUFFER_CONSOLE=y
CONFIG_FB_EFI=y
CONFIG_PCI=y
CONFIG_PCI_MSI=y
CONFIG_PCIEPORTBUS=y
CONFIG_HOTPLUG_PCI_PCIE=y
CONFIG_BLK_DEV_NVME=y
CONFIG_NVME_CORE=y
CONFIG_VMD=y
EOF
}
apply_kernel_config_overrides() {
local fragment_path="$WORKDIR/kernel-config-overrides.conf"
local config_source="${KERNEL_CONFIG_FILE:-}"
kernel_config_overrides > "$fragment_path"
if [ -n "$config_source" ] && [ -f "$config_source" ]; then
cp "$config_source" .config
make olddefconfig
else
make "$KERNEL_CONFIG_TARGET"
fi
if [ -x "./scripts/kconfig/merge_config.sh" ]; then
./scripts/kconfig/merge_config.sh -m .config "$fragment_path"
else
cat "$fragment_path" >> .config
fi
make olddefconfig
}
MOUNTS_TO_CLEAN=()
register_mount() {
MOUNTS_TO_CLEAN+=("$1")
}
bind_mount() {
local source_path="$1"
local mount_point="$2"
sudo mkdir -p "$mount_point"
if ! mountpoint -q "$mount_point"; then
sudo mount --bind "$source_path" "$mount_point"
register_mount "$mount_point"
fi
}
cleanup() {
local status=$?
local idx mp
for ((idx=${#MOUNTS_TO_CLEAN[@]}-1; idx>=0; idx--)); do
mp="${MOUNTS_TO_CLEAN[$idx]}"
if mountpoint -q "$mp"; then
sudo umount "$mp" || sudo umount -l "$mp" || true
fi
done
trap - EXIT INT TERM
exit "$status"
}
trap cleanup EXIT INT TERM
unmount_rootfs_runtime_mounts() {
local mount_path
for mount_path in "$ROOTFS/run" "$ROOTFS/sys" "$ROOTFS/proc" "$ROOTFS/dev"; do
if mountpoint -q "$mount_path"; then
sudo umount "$mount_path" || sudo umount -l "$mount_path" || true
fi
done
MOUNTS_TO_CLEAN=()
}
KERNEL_SERIES="${KERNEL_SERIES:-6.18}"
KERNEL_VERSION="${KERNEL_VERSION:-}"
ZFS_TAG="${ZFS_TAG:-}"
VERSION_LOCK_HASH=""
CLEAN_BUILD="${CLEAN_BUILD:-0}"
MODE="full"
INSTALL_PROFILE="${INSTALL_PROFILE:-user}"
MENU_UI="${MENU_UI:-gui}"
MENU_BACKEND_DEFAULT="${MENU_BACKEND_DEFAULT:-}"
BOOT_PERSIST_FS="${BOOT_PERSIST_FS:-}"
BOOT_PERSIST_RECREATE_ON_RESIZE_FAIL="${BOOT_PERSIST_RECREATE_ON_RESIZE_FAIL:-0}"
UBUNTU_CODENAME="${UBUNTU_CODENAME:-resolute}"
UBUNTU_MIRROR="${UBUNTU_MIRROR:-https://archive.ubuntu.com/ubuntu/}"
UBUNTU_SECURITY_MIRROR="${UBUNTU_SECURITY_MIRROR:-https://security.ubuntu.com/ubuntu/}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
UNGRUB_SRC=""
UNGRUB_THEME_SRC=""
UNGRUB_WEBGUI_REPO="${UNGRUB_WEBGUI_REPO:-https://github.com/unraid/webgui.git}"
UNGRUB_WEBGUI_REF="${UNGRUB_WEBGUI_REF:-}"
DEFAULT_KERNEL_CONFIG_FILE="$SCRIPT_DIR/config"
DISPLAY_NAME="${DISPLAY_NAME:-Internal Boot Setup}"
STATIC_HOSTNAME="${STATIC_HOSTNAME:-internal-boot-setup}"
PRETTY_HOSTNAME="${PRETTY_HOSTNAME:-Internal Boot Setup}"
WORKDIR="${WORKDIR:-$REPO_ROOT/zfs-live-build}"
UNGRUB_WORKTREE="$WORKDIR/webgui"
ROOTFS="$WORKDIR/rootfs"
ISO="$WORKDIR/iso"
ISO_THEME_DIR="$ISO/boot/grub/themes/unraid"
KERNEL_STAGING_DIR="$WORKDIR/kernel-staging"
ROOTFS_MIN_FREE_KB="${ROOTFS_MIN_FREE_KB:-4194304}"
KERNEL_CONFIG_TARGET="defconfig"
KERNEL_CONFIG_FILE="${KERNEL_CONFIG_FILE:-}"
ZFS_SRC_DIR="$WORKDIR/zfs"
ZFS_USERSPACE_STAGING_DIR="$WORKDIR/zfs-userspace-staging"
PUBLISH_ISO="${PUBLISH_ISO:-$WORKDIR/onboarding.iso}"
MEDIA_LABEL="${MEDIA_LABEL:-INSTALLER}"
ONBOARDING_ASSET_DIR="$ROOTFS/boot/install"
while (($#)); do
case "$1" in
full|grub-iso|cache-only)
MODE="$1"
shift
;;
--clean)
CLEAN_BUILD=1
shift
;;
--menu-ui)
[ "$#" -ge 2 ] || { echo "Missing value for --menu-ui" >&2; exit 1; }
MENU_UI="$2"
shift 2
;;
--menu-backend)
[ "$#" -ge 2 ] || { echo "Missing value for --menu-backend" >&2; exit 1; }
MENU_BACKEND_DEFAULT="$2"
shift 2
;;
--persist-fs)
[ "$#" -ge 2 ] || { echo "Missing value for --persist-fs" >&2; exit 1; }
BOOT_PERSIST_FS="$2"
shift 2
;;
-h|--help)
cat <<'EOF'
Usage:
./build-iso.sh [full|grub-iso|cache-only] [--clean] [--menu-ui gui] [--menu-backend auto|whiptail|dialog|text] [--persist-fs ext4|fat32|vfat] [--persist-recreate-on-resize-fail]
Options:
--clean Remove kernel/ZFS build artifacts and force recompilation (full/cache-only mode).
--menu-ui Select default onboarding menu implementation (gui only).
--menu-backend Set default menu backend (auto, whiptail, dialog, text).
--persist-fs Set default persistence filesystem for auto-create at boot.
--persist-recreate-on-resize-fail Enable vfat backup+recreate fallback when resize fails.
EOF
exit 0
;;
--persist-recreate-on-resize-fail)
BOOT_PERSIST_RECREATE_ON_RESIZE_FAIL=1
shift
;;
*)
echo "Unknown argument: $1" >&2
echo "Mode must be 'full', 'grub-iso', or 'cache-only'" >&2
exit 1
;;
esac
done
UBUNTU_MIRROR="${UBUNTU_MIRROR%/}/"
UBUNTU_SECURITY_MIRROR="${UBUNTU_SECURITY_MIRROR%/}/"
mkdir -p "$WORKDIR"
cd "$WORKDIR"
load_version_lock
if [ -n "$VERSION_LOCK_HASH" ]; then
KERNEL_VERSION_FILE="$WORKDIR/.kernel-version-$VERSION_LOCK_HASH"
ZFS_TAG_FILE="$WORKDIR/.zfs-tag-$VERSION_LOCK_HASH"
else
KERNEL_VERSION_FILE="$WORKDIR/.kernel-version"
ZFS_TAG_FILE="$WORKDIR/.zfs-tag"
fi
if [ -z "$KERNEL_CONFIG_FILE" ] && [ -f "$DEFAULT_KERNEL_CONFIG_FILE" ]; then
KERNEL_CONFIG_FILE="$DEFAULT_KERNEL_CONFIG_FILE"
fi
if [ -n "$KERNEL_CONFIG_FILE" ] && [ ! -f "$KERNEL_CONFIG_FILE" ]; then
echo "KERNEL_CONFIG_FILE does not exist: $KERNEL_CONFIG_FILE" >&2
exit 1
fi
if [ -n "$KERNEL_CONFIG_FILE" ]; then
echo "Using kernel config file: $KERNEL_CONFIG_FILE"
else
echo "Using kernel config target: $KERNEL_CONFIG_TARGET"
fi
case "$MODE" in
full|grub-iso|cache-only) ;;
*)
echo "Mode must be 'full', 'grub-iso', or 'cache-only'" >&2
exit 1
;;
esac
if [ "$CLEAN_BUILD" = "1" ] && [ "$MODE" = "grub-iso" ]; then
echo "--clean is supported only with 'full' or 'cache-only' mode." >&2
exit 1
fi
if [ "$INSTALL_PROFILE" != "user" ]; then
echo "INSTALL_PROFILE must be 'user' in the build chain (got: $INSTALL_PROFILE)" >&2
exit 1
fi
case "$MENU_UI" in
gui) ;;
*)
echo "MENU_UI must be 'gui' (got: $MENU_UI)" >&2
exit 1
;;
esac
case "${MENU_BACKEND_DEFAULT,,}" in
""|auto)
MENU_BACKEND_DEFAULT=""
;;
whiptail|dialog|text)
MENU_BACKEND_DEFAULT="${MENU_BACKEND_DEFAULT,,}"
;;
*)
echo "--menu-backend must be auto, whiptail, dialog, or text (got: $MENU_BACKEND_DEFAULT)" >&2
exit 1
;;
esac
if [ -n "$BOOT_PERSIST_FS" ]; then
case "${BOOT_PERSIST_FS,,}" in
ext4|fat32|vfat)
BOOT_PERSIST_FS="${BOOT_PERSIST_FS,,}"
;;
*)
echo "--persist-fs must be ext4, fat32, or vfat (got: $BOOT_PERSIST_FS)" >&2
exit 1
;;
esac
fi
BOOT_PERSIST_KERNEL_ARGS=""
if [ -n "$BOOT_PERSIST_FS" ]; then
BOOT_PERSIST_KERNEL_ARGS=" persist_fs=$BOOT_PERSIST_FS"
fi
if [ "$BOOT_PERSIST_RECREATE_ON_RESIZE_FAIL" = "1" ]; then
BOOT_PERSIST_KERNEL_ARGS+=" persist_recreate_on_resize_fail=1"
fi
require_cmd tar
require_cmd xz
require_cmd cpio
require_cmd gzip
require_cmd depmod
require_cmd mountpoint
require_cmd xorriso
require_cmd grub-mkstandalone
require_cmd grub-mkimage
require_cmd mkfs.vfat
require_cmd mmd
require_cmd mcopy
require_cmd git
if [ "$MODE" = "full" ]; then
require_cmd debootstrap
require_cmd dpkg-deb
fi
download_to_file() {
local url="$1"
local out="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$out"
return
fi
if command -v wget >/dev/null 2>&1; then
wget -qO "$out" "$url"
return
fi
echo "Missing required command: curl or wget" >&2
exit 1
}
sync_ungrub_from_webgui() {
echo "Syncing ungrub from $UNGRUB_WEBGUI_REPO..."
rm -rf "$UNGRUB_WORKTREE"
if [ -n "$UNGRUB_WEBGUI_REF" ]; then
git clone --depth 1 --branch "$UNGRUB_WEBGUI_REF" "$UNGRUB_WEBGUI_REPO" "$UNGRUB_WORKTREE"
else
git clone --depth 1 "$UNGRUB_WEBGUI_REPO" "$UNGRUB_WORKTREE"
fi
UNGRUB_SRC="$UNGRUB_WORKTREE/ungrub"
UNGRUB_THEME_SRC="$UNGRUB_SRC/themes/unraid"
[ -d "$UNGRUB_SRC" ] || {
echo "Missing ungrub directory in webgui repository checkout: $UNGRUB_SRC" >&2
exit 1
}
echo "Using ungrub source: $UNGRUB_SRC"
}
sync_ungrub_from_webgui
resolve_isohybrid_mbr_path() {
local candidate
for candidate in \
/usr/lib/ISOLINUX/isohdpfx.bin \
/usr/lib/syslinux/isohdpfx.bin \
/usr/lib/syslinux/bios/isohdpfx.bin; do
if [ -f "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
return 1
}
resolve_grub_cdboot_path() {
local candidate
for candidate in \
/usr/lib/grub/i386-pc/cdboot.img \
/usr/lib/grub/i386-pc/eltorito.img; do
if [ -f "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
return 1
}
resolve_latest_zfs_tag() {
local tmp_metadata tag
tmp_metadata="$(mktemp)"
if ! download_to_file "https://api.github.com/repos/openzfs/zfs/releases/latest" "$tmp_metadata"; then
rm -f "$tmp_metadata"
fatal "Unable to download latest OpenZFS release metadata"
fi
tag="$(sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$tmp_metadata" | head -n1)"
rm -f "$tmp_metadata"
case "$tag" in
zfs-[0-9]*.[0-9]*.[0-9]*)
printf '%s\n' "$tag"
;;
*)
fatal "Unable to resolve latest OpenZFS release tag"
;;
esac
}
normalize_zfs_tag() {
local raw_tag="$1"
case "$raw_tag" in
zfs-[0-9]*.[0-9]*.[0-9]*)
printf '%s\n' "$raw_tag"
return 0
;;
[0-9]*.[0-9]*.[0-9]*)
printf 'zfs-%s\n' "$raw_tag"
return 0
;;
esac
return 1
}
resolve_zfs_tag() {
local requested_tag="${ZFS_TAG:-}"
local cached_tag=""
local normalized_tag=""
if [ -n "$requested_tag" ]; then
if normalized_tag="$(normalize_zfs_tag "$requested_tag")"; then
printf '%s\n' "$normalized_tag"
return 0
fi
fatal "Invalid ZFS_TAG '$requested_tag'. Expected X.Y.Z or zfs-X.Y.Z"
fi
if [ -f "$ZFS_TAG_FILE" ]; then
cached_tag="$(head -n1 "$ZFS_TAG_FILE" | tr -d '[:space:]')"
if normalized_tag="$(normalize_zfs_tag "$cached_tag")"; then
printf '%s\n' "$normalized_tag"
return 0
fi
echo "Ignoring invalid cached OpenZFS tag in $ZFS_TAG_FILE: ${cached_tag:-<empty>}" >&2
fi
resolve_latest_zfs_tag
}
resolve_kernel_version() {
local requested_version="${KERNEL_VERSION:-}"
local series="$KERNEL_SERIES"
local cached_version=""
local tmp_metadata versions latest_version
if [ -f "$KERNEL_VERSION_FILE" ]; then
cached_version="$(head -n1 "$KERNEL_VERSION_FILE" | tr -d '[:space:]')"
case "$cached_version" in
*.*.*)
printf '%s\n' "$cached_version"
return
;;
esac
fi
if [ -n "$requested_version" ]; then
case "$requested_version" in
*.*.*)
printf '%s\n' "$requested_version"
return
;;
*.*)
series="$requested_version"
;;
*)
echo "Invalid KERNEL_VERSION '$requested_version'. Use X.Y for a series or X.Y.Z for an exact version." >&2
exit 1
;;
esac
fi
tmp_metadata="$(mktemp)"
if ! download_to_file "https://www.kernel.org/releases.json" "$tmp_metadata"; then
rm -f "$tmp_metadata"
echo "Unable to download kernel release metadata from kernel.org" >&2
exit 1
fi
versions="$(grep -o '"version":[[:space:]]*"'"$series"'\.[0-9][0-9]*"' "$tmp_metadata" | sed -E 's/.*"([0-9.]+)"/\1/' | sort -V | uniq)"
rm -f "$tmp_metadata"
latest_version="$(printf '%s\n' "$versions" | tail -n1)"
if [ -z "$latest_version" ]; then
echo "Unable to find a kernel.org release for series $series" >&2
exit 1
fi
printf '%s\n' "$latest_version" > "$KERNEL_VERSION_FILE"
printf '%s\n' "$latest_version"
}
if [ "$MODE" = "full" ] || [ "$MODE" = "cache-only" ]; then
cache_args=()
if [ "$CLEAN_BUILD" = "1" ]; then
cache_args+=(--clean)
fi
/bin/bash "$SCRIPT_DIR/build-kernel-zfs-cache.sh" "${cache_args[@]}"
fi
[ -f "$WORKDIR/.kernel-version-current" ] || { echo "Missing cache metadata: $WORKDIR/.kernel-version-current" >&2; exit 1; }
[ -f "$WORKDIR/.kernel-release-current" ] || { echo "Missing cache metadata: $WORKDIR/.kernel-release-current" >&2; exit 1; }
[ -f "$WORKDIR/.kernel-src-dir-current" ] || { echo "Missing cache metadata: $WORKDIR/.kernel-src-dir-current" >&2; exit 1; }
[ -f "$WORKDIR/.kernel-staging-dir-current" ] || { echo "Missing cache metadata: $WORKDIR/.kernel-staging-dir-current" >&2; exit 1; }
KERNEL_VERSION="$(cat "$WORKDIR/.kernel-version-current")"
KERNEL_RELEASE="$(cat "$WORKDIR/.kernel-release-current")"
KERNEL_SRC_DIR="$(cat "$WORKDIR/.kernel-src-dir-current")"
KERNEL_STAGING_DIR="$(cat "$WORKDIR/.kernel-staging-dir-current")"
KERNEL_RELEASE_FILE="$WORKDIR/.kernel-release-$KERNEL_VERSION"
ZFS_TAG_CURRENT_FILE="$WORKDIR/.zfs-tag-current"
echo "Using kernel version: $KERNEL_VERSION"
echo "Using Ubuntu mirror: $UBUNTU_MIRROR"
echo "Using Ubuntu security mirror: $UBUNTU_SECURITY_MIRROR"
echo "Using install profile: $INSTALL_PROFILE"
echo "Clean build: $CLEAN_BUILD"
if [ "$MODE" = "cache-only" ]; then
echo "cache-only mode complete: kernel and OpenZFS artifacts are prepared in $WORKDIR"
exit 0
fi
if [ "$MODE" = "full" ]; then
echo "Creating minimal rootfs..."
ROOTFS_PARENT="$(dirname "$ROOTFS")"
FREE_KB="$(df -Pk "$ROOTFS_PARENT" | awk 'NR==2 {print $4}')"
if [ -z "$FREE_KB" ] || [ "$FREE_KB" -lt "$ROOTFS_MIN_FREE_KB" ]; then
echo "Insufficient free space for debootstrap at $ROOTFS_PARENT (have ${FREE_KB:-0} KB, need >= $ROOTFS_MIN_FREE_KB KB)." >&2
exit 1
fi
if [ -e "$ROOTFS" ]; then
echo "Removing existing rootfs at $ROOTFS before debootstrap..."
for stale in "$ROOTFS/dev" "$ROOTFS/proc" "$ROOTFS/sys" "$ROOTFS/run"; do
if mountpoint -q "$stale"; then
sudo umount "$stale" || sudo umount -l "$stale" || true
fi
done
sudo rm -rf "$ROOTFS"
fi
sudo debootstrap --variant=minbase "$UBUNTU_CODENAME" "$ROOTFS" "$UBUNTU_MIRROR"
bind_mount /dev "$ROOTFS/dev"
bind_mount /proc "$ROOTFS/proc"
bind_mount /sys "$ROOTFS/sys"
bind_mount /run "$ROOTFS/run"
sudo tee "$ROOTFS/usr/sbin/policy-rc.d" <<'EOF' >/dev/null
#!/bin/sh
exit 101
EOF
sudo chmod +x "$ROOTFS/usr/sbin/policy-rc.d"
sudo tee "$ROOTFS/etc/apt/sources.list" >/dev/null <<EOF
deb ${UBUNTU_MIRROR} ${UBUNTU_CODENAME} main restricted universe multiverse
deb ${UBUNTU_MIRROR} ${UBUNTU_CODENAME}-updates main restricted universe multiverse
deb ${UBUNTU_MIRROR} ${UBUNTU_CODENAME}-backports main restricted universe multiverse
deb ${UBUNTU_SECURITY_MIRROR} ${UBUNTU_CODENAME}-security main restricted universe multiverse
EOF
sudo chroot "$ROOTFS" /bin/bash <<'EOF'
export DEBIAN_FRONTEND=noninteractive
apt update
apt install -y --no-install-recommends \
bash \
coreutils \
ca-certificates \
curl \
dosfstools \
fatresize \
e2fsprogs \
findutils \
gawk \
gdisk \
grep \
grub-common \
grub-efi-amd64-bin \
grub-pc-bin \
iw \
iproute2 \
iputils-ping \
network-manager \
busybox \
dialog \
kmod \
nano \
parted \
pciutils \
php-cli \
sed \
udev \
unzip \
usbutils \
util-linux \
wpasupplicant \
whiptail \
wget
# Optional: include memtest when available in the configured Ubuntu release.
if apt-cache show memtest86+ >/dev/null 2>&1; then
apt install -y --no-install-recommends memtest86+
fi
apt clean
rm -rf /var/lib/apt/lists/* /usr/share/doc/* /usr/share/man/* /usr/share/info/* /usr/share/locale/*
rm -f /usr/sbin/policy-rc.d
EOF
unmount_rootfs_runtime_mounts
else
echo "grub-iso mode: reusing existing kernel build, rootfs, and runtime assets"
[ -d "$KERNEL_SRC_DIR" ] || { echo "Missing kernel source directory for grub-iso mode: $KERNEL_SRC_DIR" >&2; exit 1; }
[ -d "$ROOTFS" ] || { echo "Missing rootfs for grub-iso mode: $ROOTFS" >&2; exit 1; }
if [ -f "$KERNEL_RELEASE_FILE" ]; then
KERNEL_RELEASE="$(cat "$KERNEL_RELEASE_FILE")"
else
cd "$KERNEL_SRC_DIR"
KERNEL_RELEASE="$(make -s kernelrelease)"
cd "$WORKDIR"
fi
if [ -f "$ZFS_TAG_CURRENT_FILE" ]; then
echo "Reusing OpenZFS release tag: $(cat "$ZFS_TAG_CURRENT_FILE")"
fi
fi
STAGED_KERNEL_IMAGE_PATH="$KERNEL_SRC_DIR/arch/x86/boot/bzImage"
STAGED_KERNEL_MODULES_PATH="$KERNEL_STAGING_DIR/lib/modules/$KERNEL_RELEASE"
[ -f "$STAGED_KERNEL_IMAGE_PATH" ] || { echo "Missing staged kernel image: $STAGED_KERNEL_IMAGE_PATH" >&2; exit 1; }
[ -d "$STAGED_KERNEL_MODULES_PATH" ] || { echo "Missing staged module tree: $STAGED_KERNEL_MODULES_PATH" >&2; exit 1; }
if ! find "$STAGED_KERNEL_MODULES_PATH" -type f \( -name 'zfs.ko' -o -name 'zfs.ko.xz' -o -name 'zfs.ko.zst' \) -print -quit | grep -q .; then
echo "Missing staged ZFS module in $STAGED_KERNEL_MODULES_PATH" >&2
exit 1
fi
sudo mkdir -p "$ROOTFS/mnt/persist" "$ROOTFS/boot" "$ONBOARDING_ASSET_DIR" "$ROOTFS/usr/local" "$ROOTFS/etc/rc.d"
# Avoid cross-profile leftovers when reusing rootfs (for example in grub-iso mode).
sudo rm -f \
"$ONBOARDING_ASSET_DIR"/menu.sh \
"$ONBOARDING_ASSET_DIR"/menu_*.sh \
"$ONBOARDING_ASSET_DIR"/menu_gui_common.sh \
"$ONBOARDING_ASSET_DIR"/create_internal_boot.sh \
"$ONBOARDING_ASSET_DIR"/convert_internal_boot_to_dedicated.sh \
"$ONBOARDING_ASSET_DIR"/create_flash_boot.sh \
"$ONBOARDING_ASSET_DIR"/zip.sh \
"$ONBOARDING_ASSET_DIR"/version_check.sh \
"$ONBOARDING_ASSET_DIR"/installer-version \
"$ONBOARDING_ASSET_DIR"/install-profile \
"$ONBOARDING_ASSET_DIR"/menu-backend
[ -f "$SCRIPT_DIR/create_internal_boot_user.sh" ] || {
echo "Missing required onboarding script: $SCRIPT_DIR/create_internal_boot_user.sh" >&2
exit 1
}
[ -f "$SCRIPT_DIR/zip.sh" ] || {
echo "Missing required onboarding script: $SCRIPT_DIR/zip.sh" >&2
exit 1
}
[ -f "$SCRIPT_DIR/version_check.sh" ] || {
echo "Missing required onboarding script: $SCRIPT_DIR/version_check.sh" >&2
exit 1
}
[ -f "$SCRIPT_DIR/create_flash_boot.sh" ] || {
echo "Missing required onboarding script: $SCRIPT_DIR/create_flash_boot.sh" >&2
exit 1
}
if [ "$MENU_UI" = "gui" ]; then
[ -f "$SCRIPT_DIR/menu_gui_user.sh" ] || {
echo "Missing required onboarding script: $SCRIPT_DIR/menu_gui_user.sh" >&2
exit 1
}
[ -f "$SCRIPT_DIR/menu_gui_common.sh" ] || {
echo "Missing required onboarding script: $SCRIPT_DIR/menu_gui_common.sh" >&2
exit 1
}
[ -f "$SCRIPT_DIR/menu_recovery.sh" ] || {
echo "Missing required onboarding script: $SCRIPT_DIR/menu_recovery.sh" >&2
exit 1
}
sudo cp "$SCRIPT_DIR/menu_gui_common.sh" "$ONBOARDING_ASSET_DIR/menu_gui_common.sh"
sudo cp "$SCRIPT_DIR/menu_gui_user.sh" "$ONBOARDING_ASSET_DIR/menu_gui_user.sh"
sudo cp "$SCRIPT_DIR/menu_recovery.sh" "$ONBOARDING_ASSET_DIR/menu_recovery.sh"
sudo chmod +x "$ONBOARDING_ASSET_DIR/menu_gui_common.sh"
sudo chmod +x "$ONBOARDING_ASSET_DIR/menu_gui_user.sh"
sudo chmod +x "$ONBOARDING_ASSET_DIR/menu_recovery.sh"
fi
sudo cp "$SCRIPT_DIR/create_internal_boot_user.sh" "$ONBOARDING_ASSET_DIR/create_internal_boot.sh"
sudo cp "$SCRIPT_DIR/create_flash_boot.sh" "$ONBOARDING_ASSET_DIR/create_flash_boot.sh"
sudo cp "$SCRIPT_DIR/zip.sh" "$ONBOARDING_ASSET_DIR/zip.sh"
sudo cp "$SCRIPT_DIR/version_check.sh" "$ONBOARDING_ASSET_DIR/version_check.sh"
installer_version="$(read_lock_json_string "version" "$(cat "$REPO_ROOT/build/unraid-release-lock.json")")"
if [[ ! "$installer_version" =~ ^[0-9]+(\.[0-9]+)+$ ]]; then
echo "Invalid installer version in build/unraid-release-lock.json: $installer_version" >&2
exit 1
fi
printf '%s\n' "$installer_version" | sudo tee "$ONBOARDING_ASSET_DIR/installer-version" >/dev/null
sudo cp "$SCRIPT_DIR/menu_gui_common.sh" "$ONBOARDING_ASSET_DIR/menu_gui_common.sh"
sudo cp "$SCRIPT_DIR/menu_gui_user.sh" "$ONBOARDING_ASSET_DIR/menu.sh"
sudo chmod +x "$ONBOARDING_ASSET_DIR/menu.sh" "$ONBOARDING_ASSET_DIR/menu_gui_common.sh" "$ONBOARDING_ASSET_DIR/menu_recovery.sh" "$ONBOARDING_ASSET_DIR/create_internal_boot.sh" "$ONBOARDING_ASSET_DIR/create_flash_boot.sh" "$ONBOARDING_ASSET_DIR/zip.sh" "$ONBOARDING_ASSET_DIR/version_check.sh"
if [ -n "$MENU_BACKEND_DEFAULT" ]; then
printf '%s\n' "$MENU_BACKEND_DEFAULT" | sudo tee "$ONBOARDING_ASSET_DIR/menu-backend" >/dev/null
sudo chmod 0644 "$ONBOARDING_ASSET_DIR/menu-backend"
echo "Using default menu backend: $MENU_BACKEND_DEFAULT"
fi
# Ensure legacy alternate menu/helpers are not present in user builds.
sudo rm -f "$ONBOARDING_ASSET_DIR/menu_user.sh" "$ONBOARDING_ASSET_DIR/menu_gui.sh"
# Assets are no longer required for either profile.
sudo rm -rf "$ONBOARDING_ASSET_DIR/assets"
[ -d "$UNGRUB_SRC" ] || {
echo "Missing ungrub runtime assets: $UNGRUB_SRC" >&2
exit 1
}
sudo rm -rf "$ROOTFS/usr/local/ungrub"
sudo cp -a "$UNGRUB_SRC" "$ROOTFS/usr/local/ungrub"
for ungrub_tool in mkbootable mkgrub mktheme update_grub_from_syslinux; do
if [ -f "$ROOTFS/usr/local/ungrub/$ungrub_tool" ]; then
sudo chmod +x "$ROOTFS/usr/local/ungrub/$ungrub_tool"
fi
done
sudo tee "$ROOTFS/etc/rc.d/rc.runlog" <<'EOF' >/dev/null
#!/bin/sh
log() {
printf '%s\n' "$*"
}
EOF
sudo chmod +x "$ROOTFS/etc/rc.d/rc.runlog"
sudo mkdir -p "$ROOTFS/usr/local/sbin"
sudo mkdir -p "$ROOTFS/usr/share/udhcpc"
sudo tee "$ROOTFS/usr/share/udhcpc/default.script" <<'EOF' >/dev/null
#!/bin/sh
RESOLV_CONF="/etc/resolv.conf"
mask_to_prefix() {
local mask="$1" prefix=0 octet
OLDIFS="$IFS"
IFS=.
set -- $mask
IFS="$OLDIFS"
for octet in "$@"; do
case "$octet" in
255) prefix=$((prefix + 8)) ;;
254) prefix=$((prefix + 7)) ;;
252) prefix=$((prefix + 6)) ;;
248) prefix=$((prefix + 5)) ;;
240) prefix=$((prefix + 4)) ;;
224) prefix=$((prefix + 3)) ;;
192) prefix=$((prefix + 2)) ;;
128) prefix=$((prefix + 1)) ;;
0) ;;
*) return 1 ;;
esac
done
printf '%s\n' "$prefix"
}
flush_interface() {
ip addr flush dev "$interface" 2>/dev/null || true
ip route flush dev "$interface" 2>/dev/null || true
}
case "$1" in
deconfig)
flush_interface
;;
renew|bound)
flush_interface
ip link set "$interface" up || true
prefix="$(mask_to_prefix "${subnet:-255.255.255.0}")"
ip addr add "$ip/$prefix" dev "$interface"
if [ -n "${router:-}" ]; then
metric=0
for gateway in $router; do
ip route add default via "$gateway" dev "$interface" metric "$metric" 2>/dev/null || true
metric=$((metric + 1))
done
fi
: > "$RESOLV_CONF"
if [ -n "${domain:-}" ]; then
printf 'search %s\n' "$domain" >> "$RESOLV_CONF"
fi
for nameserver in $dns; do
printf 'nameserver %s\n' "$nameserver" >> "$RESOLV_CONF"
done
;;
esac
exit 0
EOF
sudo chmod +x "$ROOTFS/usr/share/udhcpc/default.script"
sudo tee "$ROOTFS/usr/local/sbin/udhcpc" <<'EOF' >/dev/null
#!/bin/sh
exec /bin/busybox udhcpc -s /usr/share/udhcpc/default.script "$@"
EOF
sudo chmod +x "$ROOTFS/usr/local/sbin/udhcpc"
sudo tee "$ROOTFS/usr/local/sbin/resize_persistence.sh" <<'EOF' >/dev/null
#!/bin/bash
set -euo pipefail
log() {
printf '%s\n' "[resize-persist] $*"
}
on_error() {
local line_no="$1"
local cmd="$2"
local rc="$3"
log "error at line ${line_no}: ${cmd} (exit=${rc})"
exit "$rc"
}
trap 'on_error "$LINENO" "$BASH_COMMAND" "$?"' ERR
find_persist_dev() {
local link candidate resolved
for link in \
/dev/disk/by-partlabel/INSTALL-PERSIST \
/dev/disk/by-partlabel/ONBOARDING-PERSIST \
/dev/disk/by-label/INSTALL-PERSIST \
/dev/disk/by-label/INSTALLPERS; do
if [ -L "$link" ] || [ -e "$link" ]; then
resolved=""
if resolved="$(readlink -f -- "$link" 2>/dev/null)"; then
if [ -n "$resolved" ] && [ -b "$resolved" ]; then
printf '%s\n' "$resolved"
return 0
fi
fi
fi
done
candidate="$(blkid -L INSTALL-PERSIST 2>/dev/null || true)"
if [ -n "$candidate" ] && [ -b "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
candidate="$(blkid -L INSTALLPERS 2>/dev/null || true)"
if [ -n "$candidate" ] && [ -b "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
return 0
}
partition_parent_disk() {
local part_dev="$1"
local pkname=""
pkname="$(lsblk -nro PKNAME "$part_dev" 2>/dev/null | head -n1 || true)"
if [ -n "$pkname" ] && [ -b "/dev/$pkname" ]; then
printf '%s\n' "/dev/$pkname"
return 0
fi
case "$part_dev" in
/dev/nvme*n*p[0-9]*|/dev/mmcblk*p[0-9]*) printf '%s\n' "${part_dev%p[0-9]*}" ;;
/dev/sd[a-z][0-9]*|/dev/vd[a-z][0-9]*|/dev/xvd[a-z][0-9]*|/dev/hd[a-z][0-9]*) printf '%s\n' "${part_dev%[0-9]*}" ;;
*) return 1 ;;
esac
}
partition_number() {
local part_dev="$1"
local pn=""
pn="$(lsblk -nro PARTN "$part_dev" 2>/dev/null | head -n1 || true)"
if [ -n "$pn" ]; then
printf '%s\n' "$pn"
return 0
fi
case "$part_dev" in
*p[0-9]*) printf '%s\n' "${part_dev##*p}" ;;
*[0-9]) printf '%s\n' "${part_dev##*[!0-9]}" ;;
*) return 1 ;;
esac
}
refresh_partition_path() {
local disk="$1"
local pn="$2"
if [ -b "${disk}p${pn}" ]; then
printf '%s\n' "${disk}p${pn}"
elif [ -b "${disk}${pn}" ]; then
printf '%s\n' "${disk}${pn}"
else
return 1
fi
}
normalize_vfat_label() {
local raw_label="$1"
local normalized_label=""
normalized_label="$(printf '%s' "$raw_label" | tr '[:lower:]' '[:upper:]' | tr -cd 'A-Z0-9' | cut -c1-11)"
if [ -z "$normalized_label" ]; then
normalized_label="INSTALLPERS"
fi
printf '%s\n' "$normalized_label"
}
copy_tree_with_progress() {
local src_root="$1"
local dst_root="$2"
local phase="$3"
local total=""
local done_count=0
local pct=0
local last_pct=-1
local item=""
local rel=""
local target_dir=""
local item_list_file=""
total="$(find "$src_root" -mindepth 1 -print 2>/dev/null | wc -l | tr -d '[:space:]')"
if [ -z "$total" ] || [ "$total" -eq 0 ] 2>/dev/null; then
log "${phase}: 100% (nothing to copy)"
return 0
fi
log "${phase}: 0% (0/${total})"
item_list_file="$(mktemp /tmp/persist-copy-list.XXXXXX)"
find "$src_root" -mindepth 1 -print0 > "$item_list_file"
while IFS= read -r -d '' item; do
rel="${item#"$src_root"/}"
if [ "$rel" = "$item" ]; then
continue
fi
if [ -d "$item" ]; then
mkdir -p "$dst_root/$rel"