forked from paolosabatino/multitool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_image.sh
More file actions
executable file
·1382 lines (915 loc) · 36.5 KB
/
create_image.sh
File metadata and controls
executable file
·1382 lines (915 loc) · 36.5 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
#!/bin/bash
function round_sectors() {
SECTORS="$1"
ROUNDED=$(((($SECTORS / 8) + 1) * 8))
echo $ROUNDED
}
BACKTITLE="TUI Multitool Image Builder"
FINAL_MESSAGE=""
RUN_START_EPOCH="$(date +%s)"
LOGS_DIR=""
LOG_FILE=""
CURRENT_STAGE="startup"
LAST_CMD_STATUS=""
LAST_CMD_TEXT=""
#------------------------------------------------------------------------------
# Function: show_error
# Description: Shows a blocking error dialog, logs the error context, and exits.
# Parameters: $1=error_message, $2=optional_status_code_for_log
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function show_error() {
local explicit_status="$2"
local status_to_log="${explicit_status:-${LAST_CMD_STATUS:-$?}}"
log_error "$1" "$status_to_log"
dialog \
--backtitle "$BACKTITLE" \
--title "Error" \
--ok-label "Exit" \
--msgbox "\n$1" 8 50
exit 1
}
#------------------------------------------------------------------------------
# Function: show_wait
# Description: Updates current stage and shows a non-blocking progress dialog.
# Parameters: $1=stage_or_progress_message
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function show_wait(){
log_stage "$1"
dialog \
--backtitle "$BACKTITLE" \
--title "Please wait" \
--infobox "\n$1" 8 50
}
#------------------------------------------------------------------------------
# Function: show_info
# Description: Shows an informational blocking dialog requiring user acknowledgment.
# Parameters: $1=information_message
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function show_info() {
dialog \
--backtitle "$BACKTITLE" \
--title "Info" \
--ok-label "OK" \
--msgbox "\n$1" 8 50
}
#------------------------------------------------------------------------------
# Function: show_warning
# Description: Shows a warning dialog for non-fatal events.
# Parameters: $1=warning_message
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function show_warning() {
dialog \
--backtitle "$BACKTITLE" \
--title "Warning" \
--ok-label "OK" \
--msgbox "\n$1" 8 50
}
#------------------------------------------------------------------------------
# Function: log_write
# Description: Writes a timestamped structured line to LOG_FILE when available.
# Parameters: $1=level, $2=message
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function log_write() {
local level="$1"
local message="$2"
if [ -z "$LOG_FILE" ]; then
return 0
fi
printf "%s [%s] %s\n" "$(date "+%Y-%m-%d %H:%M:%S")" "$level" "$message" >> "$LOG_FILE" 2>/dev/null
}
#------------------------------------------------------------------------------
# Function: log_stage
# Description: Updates CURRENT_STAGE and logs a stage transition.
# Parameters: $1=stage_name
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function log_stage() {
CURRENT_STAGE="$1"
log_write "INFO" "stage: $1"
}
#------------------------------------------------------------------------------
# Function: log_error
# Description: Logs an error entry bound to CURRENT_STAGE and status code.
# Parameters: $1=error_message, $2=optional_status_code
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function log_error() {
local message="$1"
local status_code="$2"
log_write "ERROR" "stage: $CURRENT_STAGE | status: ${status_code:-unknown} | message: $message"
}
#------------------------------------------------------------------------------
# Function: log_vars
# Description: Logs scoped variable snapshots for debugging and traceability.
# Parameters: $1=scope, $2..$n=details_key_value_pairs
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function log_vars() {
local scope="$1"
shift
local details="$*"
log_write "VARS" "$scope | $details"
}
#------------------------------------------------------------------------------
# Function: run_logged
# Description: Executes command with logging of text, output markers, and exit.
# Parameters: $@=command_and_arguments
# Returns: command_exit_status
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function run_logged() {
local status
log_write "CMD" "$*"
LAST_CMD_TEXT="$*"
if [ -n "$LOG_FILE" ]; then
log_write "CMD_OUT_START" "$*"
"$@" >> "$LOG_FILE" 2>&1
log_write "CMD_OUT_END" "$*"
else
"$@" >/dev/null 2>&1
fi
status=$?
LAST_CMD_STATUS="$status"
log_write "CMD_RET" "exit=$status :: $*"
return $status
}
#------------------------------------------------------------------------------
# Function: run_logged_capture
# Description: Executes command, captures stdout in a variable, and logs status.
# Parameters: $1=destination_var_name, $2..$n=command_and_arguments
# Returns: command_exit_status
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function run_logged_capture() {
local output_var="$1"
shift
local output
local status
log_write "CMD" "$*"
LAST_CMD_TEXT="$*"
if [ -n "$LOG_FILE" ]; then
output="$("$@" 2>> "$LOG_FILE")"
else
output="$("$@" 2>/dev/null)"
fi
status=$?
LAST_CMD_STATUS="$status"
log_write "CMD_RET" "exit=$status :: $*"
if [ -n "$output" ]; then
log_write "RAW_CAPTURE" "$output"
fi
printf -v "$output_var" '%s' "$output"
return $status
}
#------------------------------------------------------------------------------
# Function: run_logged_to_file
# Description: Executes command with stdout redirected to file and full logging.
# Parameters: $1=destination_file, $2..$n=command_and_arguments
# Returns: command_exit_status
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function run_logged_to_file() {
local dest_file="$1"
shift
local status
log_write "CMD" "$* > $dest_file"
LAST_CMD_TEXT="$* > $dest_file"
if [ -n "$LOG_FILE" ]; then
log_write "CMD_OUT_START" "$* > $dest_file"
"$@" > "$dest_file" 2>> "$LOG_FILE"
log_write "CMD_OUT_END" "$* > $dest_file"
else
"$@" > "$dest_file" 2>/dev/null
fi
status=$?
LAST_CMD_STATUS="$status"
log_write "CMD_RET" "exit=$status :: $* > $dest_file"
return $status
}
#------------------------------------------------------------------------------
# Function: rotate_logs
# Description: Keeps the 10 newest build logs and removes older log files.
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function rotate_logs() {
local files=()
if [ ! -d "$LOGS_DIR" ]; then
return 0
fi
mapfile -t files < <(ls -1t "$LOGS_DIR"/build-*.log 2>/dev/null)
if [ "${#files[@]}" -le 10 ]; then
return 0
fi
for old_log in "${files[@]:10}"; do
rm -f "$old_log" >/dev/null 2>&1
done
}
#------------------------------------------------------------------------------
# Function: init_logs
# Description: Initializes log directory/file and applies log retention policy.
# Returns: 0=success, 1=initialization_failure
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function init_logs() {
local run_timestamp
mkdir -p "$LOGS_DIR" >/dev/null 2>&1
if [ $? -ne 0 ]; then
return 1
fi
run_timestamp="$(date "+%Y%m%d-%H%M%S")"
LOG_FILE="$LOGS_DIR/build-${run_timestamp}-unknown.log"
touch "$LOG_FILE" >/dev/null 2>&1
if [ $? -ne 0 ]; then
return 1
fi
rotate_logs
log_write "INFO" "build started"
log_write "INFO" "cwd: $CWD"
return 0
}
#------------------------------------------------------------------------------
# Function: log_summary
# Description: Writes final build summary fields (status, image, duration, UUIDs).
# Parameters: $1=final_status
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function log_summary() {
local status="$1"
local elapsed_seconds="$(($(date +%s) - RUN_START_EPOCH))"
log_write "SUMMARY" "status: $status"
log_write "SUMMARY" "image: $DEST_IMAGE"
log_write "SUMMARY" "duration_seconds: $elapsed_seconds"
if [ -n "$SQUASHFS_PARTITION_PARTUUID" ]; then
log_write "SUMMARY" "squashfs_partuuid: $SQUASHFS_PARTITION_PARTUUID"
fi
if [ -n "$FAT_PARTITION_PARTUUID" ]; then
log_write "SUMMARY" "fat_partuuid: $FAT_PARTITION_PARTUUID"
fi
}
#------------------------------------------------------------------------------
# Function: generate_checksum
# Description: Builds checksum metadata for auto-restore validation payload.
# Strategy: full SHA256 for small files; sampled hashes for large files.
# Parameters: $1=file_path
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function generate_checksum() {
local FILE="$1"
local FILE_SIZE=$(stat -c%s "$FILE")
local FILE_SIZE_MB=$(( $FILE_SIZE / 1048576 ))
# Full checksum for files under 500MB
if [ $FILE_SIZE_MB -lt 500 ]; then
local MODE="full"
local FULL_HASH=$(sha256sum "$FILE" | awk '{print $1}')
printf "%s\n%s\n%s" "$FILE_SIZE" "$MODE" "$FULL_HASH"
return 0
fi
# Partial checksum for files 500MB and above
local MODE="partial"
local SAMPLE_MB=100
# For very large files (>3GB), increase sample size
[ $FILE_SIZE_MB -gt 3072 ] && SAMPLE_MB=200
local MID_OFFSET=$(( ($FILE_SIZE_MB / 2) - ($SAMPLE_MB / 2) ))
local HEAD_HASH=$(head -c ${SAMPLE_MB}M "$FILE" | sha256sum | awk '{print $1}')
local TAIL_HASH=$(tail -c ${SAMPLE_MB}M "$FILE" | sha256sum | awk '{print $1}')
local MID_HASH=$(dd if="$FILE" bs=1M skip=$MID_OFFSET count=$SAMPLE_MB 2>/dev/null | sha256sum | awk '{print $1}')
printf "%s\n%s\n%s\n%s\n%s\n%s" "$FILE_SIZE" "$MODE" "$SAMPLE_MB" "$HEAD_HASH" "$TAIL_HASH" "$MID_HASH"
return 0
}
CWD=$(pwd)
SOURCES_PATH="$CWD/sources"
TOOLS_PATH="$CWD/tools"
LOGS_DIR="$CWD/logs"
USERID=$(id -u)
if [ "$USERID" != "0" ]; then
echo "This script can only work with root permissions"
exit 26
fi
MOUNTED_DEVICES=()
LOOP_DEVICES=()
MOUNTED_POINTS=()
#------------------------------------------------------------------------------
# Function: cleanup
# Description: Best-effort cleanup for mounts, loop devices, and temp paths.
# Side Effects: unmounts tracked mount points, detaches tracked loop devices,
# removes tracked temporary directories.
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function cleanup() {
log_write "INFO" "cleanup started"
for device in "${MOUNTED_DEVICES[@]}"; do
log_vars "cleanup-mount" "candidate=$device"
if mountpoint -q "$device"; then
log_write "INFO" "cleanup unmounting mountpoint=$device"
umount "$device" >/dev/null 2>&1
fi
done
for loop in "${LOOP_DEVICES[@]}"; do
log_vars "cleanup-loop" "candidate=$loop"
if losetup -l | grep -q "$loop"; then
log_write "INFO" "cleanup detaching loop=$loop"
losetup -d "$loop" >/dev/null 2>&1
fi
done
for point in "${MOUNTED_POINTS[@]}"; do
log_vars "cleanup-temp" "removing=$point"
rm -rf "$point" >/dev/null 2>&1
done
clear
echo "Script finished. All temporary devices cleaned up."
log_write "INFO" "cleanup finished"
}
trap cleanup EXIT
#------------------------------------------------------------------------------
# Function: mount_device
# Description: Mounts a device/path into a mount point and tracks it for cleanup.
# Parameters: $1=device_or_partition, $2=mount_point
# Returns: mount command exit status
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function mount_device() {
local device="$1"
local mount_point="$2"
run_logged mount "$device" "$mount_point"
if [ $? -ne 0 ]; then
return $?
fi
MOUNTED_DEVICES+=("$mount_point")
MOUNTED_POINTS+=("$mount_point")
}
#------------------------------------------------------------------------------
# Function: unmount_device
# Description: Unmounts a tracked mount point when currently mounted.
# Parameters: $1=mount_point
# Returns: umount command exit status when executed, otherwise 0
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function unmount_device() {
local device="$1"
if mountpoint -q "$device"; then
run_logged umount "$device"
if [ $? -ne 0 ]; then
return $?
fi
for i in "${!MOUNTED_DEVICES[@]}"; do
if [ "${MOUNTED_DEVICES[$i]}" == "$device" ]; then
unset 'MOUNTED_DEVICES[i]'
break
fi
done
fi
}
#------------------------------------------------------------------------------
# Function: attach_loop
# Description: Attaches an image file to a free loop device and tracks it.
# Parameters: $1=image_file_path
# Returns: 0 on success, non-zero on failure
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function attach_loop() {
local file="$1"
local loop=""
run_logged_capture loop losetup -fP --show "$file"
if [ $? -ne 0 ]; then
return $?
fi
LOOP_DEVICE="$loop"
LOOP_DEVICES+=("$loop")
return 0
}
#------------------------------------------------------------------------------
# Function: detach_loop
# Description: Detaches a loop device if present and removes it from tracking.
# Parameters: $1=loop_device_path
# Returns: losetup detach exit status when executed, otherwise 0
# Author: Pedro Rigolin
#------------------------------------------------------------------------------
function detach_loop() {
local loop="$1"
if losetup -l | grep -q "$loop"; then
run_logged losetup -d "$loop"
if [ $? -ne 0 ]; then
return $?
fi
for i in "${!LOOP_DEVICES[@]}"; do
if [ "${LOOP_DEVICES[$i]}" == "$loop" ]; then
unset 'LOOP_DEVICES[i]'
break
fi
done
fi
}
if ! init_logs; then
LOG_FILE=""
show_warning "Could not initialize log file in $LOGS_DIR"
fi
shopt -s nullglob
conf_files=(sources/*.conf)
log_vars "config-discovery" "found_conf_files=${#conf_files[@]}"
if [ "${#conf_files[@]}" -eq 0 ]; then
show_error "No configuration files found in sources/ directory"
exit 1
fi
options=()
for i in "${!conf_files[@]}"; do
file="${conf_files[$i]}"
base="$(basename "$file" .conf)"
pretty="$(echo "$base" | sed -E 's/[_-]+/ /g')"
options+=("$i" "$pretty")
done
choice=$(dialog \
--clear \
--stdout \
--backtitle "$BACKTITLE" \
--title "Choose" \
--ok-label "Select" \
--cancel-label "Exit" \
--menu "\nChoose a configuration" 15 70 12 "${options[@]}")
status=$?
log_vars "config-selection" "dialog_status=$status selected_index=$choice"
if [ "$status" -ne 0 ]; then
log_write "INFO" "configuration selection canceled by user"
echo "Please specify a target configuration"
exit 40
fi
TARGET_CONF="$CWD/${conf_files[$choice]}"
log_vars "config-selection" "target_conf=$TARGET_CONF"
if [ ! -f "$TARGET_CONF" ]; then
show_error "Could not find ${conf_files[$choice]} target configuration file"
exit 42
fi
. "${TARGET_CONF}"
if [ $? -ne 0 ]; then
show_error "Could not source ${TARGET_CONF}"
exit 41
fi
BOARD_NAME=$(echo "$TARGET_CONF" | sed -E 's/.*sources\/(.*)\.conf/\1/')
log_write "INFO" "target conf: $TARGET_CONF"
log_vars "board" "board_name=$BOARD_NAME"
NEW_LOG_FILENAME="${LOG_FILE/-unknown/-${BOARD_NAME}}"
mv "$LOG_FILE" "$NEW_LOG_FILENAME" >/dev/null 2>&1
if [ $? -ne 0 ]; then
log_write "WARNING" "Could not rename log file to include board name"
else
LOG_FILE="$NEW_LOG_FILENAME"
log_write "INFO" "Log file renamed to $LOG_FILE"
fi
EMBEDDED_IMAGE=""
log_stage "embedded-image-selection"
# Embedded image selection state machine.
# Author: Pedro Rigolin
#
# Flow summary:
# 1) First screen (no file selected yet):
# - Yes => open Zenity file picker
# - No => continue build without embedded image
# 2) Second screen (file already selected):
# - Yes => proceed with current selection
# - No => reset selection and return to first screen
#
# The loop only exits in two valid terminal states:
# - User chooses to skip embedding (no file selected + press No)
# - User confirms the selected file (file selected + press Yes)
while true; do
HAS_EMBEDDED_IMAGE=1
YES_LABEL="Select file"
NO_LABEL="Skip"
YESNO="\nDo you want to embed an additional gz image into the backup folder?"
HEIGHT=10
WIDTH=60
# If a file is already selected, switch dialog semantics from
# "select or skip" to "proceed or reset" and expand dialog size
# to better display the full selected path.
if [ -n "$EMBEDDED_IMAGE" ]; then
YES_LABEL="Proceed"
NO_LABEL="Reset"
YESNO="\nCurrently selected embedded image:\n\n$EMBEDDED_IMAGE\n\nDo you want to proceed?"
HAS_EMBEDDED_IMAGE=0
HEIGHT=12
WIDTH=70
fi
log_vars "embedded-image-dialog" "current_selection=${EMBEDDED_IMAGE:-none} has_embedded_image=$HAS_EMBEDDED_IMAGE"
dialog \
--backtitle "$BACKTITLE" \
--title " Embedded Image " \
--yes-label "$YES_LABEL" \
--no-label "$NO_LABEL" \
--yesno "$YESNO" $HEIGHT $WIDTH
DIALOG_RC=$?
log_vars "embedded-image-dialog" "dialog_rc=$DIALOG_RC yes_label=$YES_LABEL no_label=$NO_LABEL"
# Case A: User pressed Yes and there is no selected file yet.
# Open file picker and validate that the chosen file is a valid .gz.
if [ "$DIALOG_RC" -eq 0 ] && [ "$HAS_EMBEDDED_IMAGE" -eq 1 ]; then
EMBEDDED_IMAGE=$(zenity --file-selection --title="Select gz image to embed in backup folder" --file-filter="*.gz" 2>/dev/null)
log_vars "embedded-image-selection" "zenity_selected=${EMBEDDED_IMAGE:-none}"
# If user picked a file, perform a lightweight gzip integrity check.
# On failure, show error and force a new selection cycle.
if [ -n "$EMBEDDED_IMAGE" ]; then
run_logged pigz -l "$EMBEDDED_IMAGE"
if [ $? -ne 0 ]; then
log_write "WARNING" "invalid embedded image selected: $EMBEDDED_IMAGE"
show_warning "The selected file is not a valid gz file"
EMBEDDED_IMAGE=""
fi
fi
# Case B: User pressed No while a file is already selected.
# This means "Reset" in this state, so clear selection and loop again.
elif [ "$DIALOG_RC" -ne 0 ] && [ "$HAS_EMBEDDED_IMAGE" -eq 0 ]; then
log_write "INFO" "embedded image selection reset by user"
EMBEDDED_IMAGE=""
# Case C: terminal conditions.
# - No selected file + No pressed => continue without embedded image.
# - Selected file + Yes pressed => continue with selected image.
else
log_vars "embedded-image-selection" "final_selection=${EMBEDDED_IMAGE:-none}"
break
fi
done
AUTO_RESTORE_ENABLED=1
log_vars "auto-restore" "default_enabled=$AUTO_RESTORE_ENABLED"
# Auto-restore toggle for embedded image workflow.
# Default value is 1 (disabled/no) to keep a safe behavior when:
# - there is no embedded image selected, or
# - the prompt is skipped/cancelled for any reason.
if [ -n "$EMBEDDED_IMAGE" ]; then
# Ask whether the generated image should boot with auto-restore enabled.
# If enabled, Multitool will automatically restore the latest backup from
# the NTFS partition to the target eMMC on startup.
dialog \
--backtitle "$BACKTITLE" \
--title " Auto restore " \
--yes-label "Yes" \
--no-label "No" \
--yesno "\nDo you want to enable auto-restore for this image?\n\nThis will automatically restore the latest backup found in the NTFS partition to the device's eMMC when the image is booted." 12 70
# Store dialog return code directly:
# 0 = Yes (enable auto-restore)
# 1 = No (keep disabled)
# 255 = Esc/Cancel (treated as disabled by downstream checks)
AUTO_RESTORE_ENABLED=$?
log_vars "auto-restore" "dialog_rc=$AUTO_RESTORE_ENABLED embedded_image=$EMBEDDED_IMAGE"
fi
# Target-specific sources path
TS_SOURCES_PATH="$CWD/sources/${BOARD_NAME}"
# Destination path and image
DIST_PATH="${CWD}/dist-${BOARD_NAME}"
DEST_IMAGE="${DIST_PATH}/multitool.img"
log_vars "paths" "ts_sources_path=$TS_SOURCES_PATH dist_path=$DIST_PATH dest_image=$DEST_IMAGE"
run_logged mkdir -p "$DIST_PATH"
if [ ! -f "$DIST_PATH/root.img" ]; then
show_wait "Creating debian base rootfs. This will take a while..."
cd "${SOURCES_PATH}/multistrap"
run_logged multistrap -f multistrap.conf
if [ $? -ne 0 ]; then
show_error "Failed to run multistrap. Check log file for details"
fi
show_wait "Creating squashfs from rootfs..."
run_logged mksquashfs rootfs "$DIST_PATH/root.img" -noappend -all-root
if [ $? -ne 0 ]; then
show_error "Failed to create squashfs from rootfs"
fi
fi
ROOTFS_SIZE=$(du -k "$DIST_PATH/root.img" 2>/dev/null | awk '{print $1}')
# Validate size right after collection (must be a positive integer).
if [[ ! "$ROOTFS_SIZE" =~ ^[0-9]+$ ]] || [ "$ROOTFS_SIZE" -le 0 ]; then
show_error "Could not determine size of squashfs root filesystem"
fi
ROOTFS_SECTORS_RAW=$((ROOTFS_SIZE * 2))
ROOTFS_SECTORS=$(round_sectors "$ROOTFS_SECTORS_RAW")
# Validate rounded sectors as well (defensive check).
if [[ ! "$ROOTFS_SECTORS" =~ ^[0-9]+$ ]] || [ "$ROOTFS_SECTORS" -le 0 ]; then
show_error "Could not calculate rootfs sectors"
fi
log_vars "rootfs" "rootfs_size_kb=$ROOTFS_SIZE rootfs_sectors_raw=$ROOTFS_SECTORS_RAW rootfs_sectors_rounded=$ROOTFS_SECTORS"
log_write "RAW" "rootfs_size_kb=$ROOTFS_SIZE"
cd "$CWD"
show_wait "Creating empty image in $DEST_IMAGE"
# Define the base image size in Megabytes, same as the original script
# @author: Pedro Rigolin
BASE_SIZE_MB=512
# Convert the base size to Kilobytes for calculations
# @author: Pedro Rigolin
BASE_SIZE_KB=$((BASE_SIZE_MB * 1024))
# Add an extra security buffer (e.g.: 50MB) to ensure everything fits
# @author: Pedro Rigolin
BUFFER_KB=51200
# Initialize the size of the file to be embedded as zero
# @author: Pedro Rigolin
EMBED_FILE_SIZE_KB=0
EMBED_FILE_PRESENT="no"
EMBED_FILE_NAME="none"
# If the user specified a file to embed...
# @author: Pedro Rigolin
if [ -n "$EMBEDDED_IMAGE" ]; then
# ...calculate its size in Kilobytes
# @author: Pedro Rigolin
EMBED_FILE_SIZE_KB=$(du "$EMBEDDED_IMAGE" | cut -f 1)
EMBED_FILE_PRESENT="yes"
EMBED_FILE_NAME="$(basename "$EMBEDDED_IMAGE")"
log_vars "embedded-image" "embed_file=$EMBEDDED_IMAGE embed_file_size_kb=$EMBED_FILE_SIZE_KB"
fi
# Calculate the final image size by adding base + embedded file + buffer
# @author: Pedro Rigolin
FINAL_IMAGE_SIZE_KB=$((BASE_SIZE_KB + EMBED_FILE_SIZE_KB + BUFFER_KB))
log_vars "image-size" "base_size_mb=$BASE_SIZE_MB base_size_kb=$BASE_SIZE_KB buffer_kb=$BUFFER_KB embed_file_present=$EMBED_FILE_PRESENT embed_file_name=$EMBED_FILE_NAME embed_file_size_kb=$EMBED_FILE_SIZE_KB final_image_size_kb=$FINAL_IMAGE_SIZE_KB"
# Create the image with the calculated final size (using 'K' for Kilobytes)
run_logged fallocate -l ${FINAL_IMAGE_SIZE_KB}K "$DEST_IMAGE"
if [ $? -ne 0 ]; then
show_error "Error while creating $DEST_IMAGE empty file"
fi
show_wait "Mounting as loop device"
LOOP_DEVICE=""
attach_loop "$DEST_IMAGE"
if [ $? -ne 0 ]; then
show_error "Could not loop mount $DEST_IMAGE"
fi
log_vars "loop" "loop_device=$LOOP_DEVICE"
show_wait "Creating partition table and partitions..."
run_logged parted -s -- "$LOOP_DEVICE" mktable msdos
if [ $? -ne 0 ]; then
show_error "Could not create partitions table"
fi
START_ROOTFS=$BEGIN_USER_PARTITIONS
END_ROOTFS=$(($START_ROOTFS + $ROOTFS_SECTORS - 1))
START_FAT=$(round_sectors $END_ROOTFS)
END_FAT=$(($START_FAT + 131072 - 1)) # 131072 sectors = 64Mb
START_NTFS=$(round_sectors $END_FAT)
log_vars "partition-layout" "begin_user_partitions=$BEGIN_USER_PARTITIONS start_rootfs=$START_ROOTFS end_rootfs=$END_ROOTFS start_fat=$START_FAT end_fat=$END_FAT start_ntfs=$START_NTFS"
run_logged parted -s -- "$LOOP_DEVICE" unit s mkpart primary ntfs $START_NTFS -1s
if [ $? -ne 0 ]; then
show_error "Could not create ntfs partition"
fi
run_logged parted -s -- "$LOOP_DEVICE" unit s mkpart primary fat32 $START_FAT $END_FAT
if [ $? -ne 0 ]; then
show_error "Could not create fat partition"
fi
run_logged parted -s -- "$LOOP_DEVICE" unit s mkpart primary $START_ROOTFS $END_ROOTFS
if [ $? -ne 0 ]; then
show_error "Could not create rootfs partition"
fi
run_logged parted -s -- "$LOOP_DEVICE" set 1 boot off set 2 boot on set 3 boot off
if [ $? -ne 0 ]; then
show_error "Could not set partition flags"
fi
sync
sleep 1
# First check: in containers, it may happen that loop device partitions
# spawns as soon as they are created. We check their presence. If they already
# are there, we don't remount the device
SQUASHFS_PARTITION="${LOOP_DEVICE}p3"
NTFS_PARTITION="${LOOP_DEVICE}p1"
FAT_PARTITION="${LOOP_DEVICE}p2"
log_vars "partitions-initial" "squashfs_partition=$SQUASHFS_PARTITION fat_partition=$FAT_PARTITION ntfs_partition=$NTFS_PARTITION"
if [ ! -b "$SQUASHFS_PARTITION" -o ! -b "$FAT_PARTITION" -o ! -b "$NTFS_PARTITION" ]; then
show_wait "Remounting loop device with partitions..."
detach_loop "$LOOP_DEVICE"
sleep 1
if [ $? -ne 0 ]; then
show_error "Could not umount loop device $LOOP_DEVICE"
fi
LOOP_DEVICE=""
attach_loop "$DEST_IMAGE"
if [ $? -ne 0 ]; then
show_error "Could not remount loop device $LOOP_DEVICE"
fi
SQUASHFS_PARTITION="${LOOP_DEVICE}p3"
NTFS_PARTITION="${LOOP_DEVICE}p1"
FAT_PARTITION="${LOOP_DEVICE}p2"
log_vars "partitions-remount" "loop_device=$LOOP_DEVICE squashfs_partition=$SQUASHFS_PARTITION fat_partition=$FAT_PARTITION ntfs_partition=$NTFS_PARTITION"
run_logged lsblk "$LOOP_DEVICE"
sleep 1
else
log_write "INFO" "remount not required; partitions already present"
fi
if [ ! -b "$SQUASHFS_PARTITION" ]; then
show_error "Could not find expected partition $SQUASHFS_PARTITION"
fi