-
Notifications
You must be signed in to change notification settings - Fork 11
/
common-functions
2158 lines (1787 loc) · 48.5 KB
/
common-functions
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/sh
# shellcheck disable=SC1117,SC2039
#############################################################################
##
## common-functions
##
## Copyright 2021-2023 Dermot Bradley <dermot_bradley@yahoo.com>
##
## Licensed via GPL version 2.0
##
#############################################################################
#
# Add more Alpine repos to the repositories file.
#
add_additional_repos() {
cat <<-'EOF' >> "$run_script"
write_log "Adding additional repos"
{
EOF
if [ "$image_alpine_release" = "edge-special" ]; then
add_alpine_repo "$alpine_mirror_site" "edge" "community"
add_alpine_repo "$alpine_mirror_site" "edge" "testing"
if [ -n "$local_alpine_repository" ]; then
add_alpine_repo "$local_alpine_repository" "edge"
fi
else
add_alpine_repo "$alpine_mirror_site" "$image_alpine_release" "community"
case $image_alpine_release in
edge )
add_alpine_repo "$alpine_mirror_site" "$image_alpine_release" "testing" ;;
esac
if [ -n "$local_alpine_repository" ]; then
add_alpine_repo "$local_alpine_repository" "$image_alpine_release"
fi
fi
cat <<-'EOF' >> "$run_script"
}
EOF
}
#
# Enable base Alpine init.d services.
#
add_base_init_d() {
cat <<-'EOF' >> "$run_script"
write_log "Enable init.d scripts"
{
EOF
add_init_d_service "devfs" "sysinit" 2
add_init_d_service "dmesg" "sysinit" 2
cat <<-'EOF' >> "$run_script"
EOF
add_init_d_service "bootmisc" "boot" 2
add_init_d_service "hostname" "boot" 2
add_init_d_service "modules" "boot" 2
add_init_d_service "swap" "boot" 2
case $image_alpine_release in
v3.13 | v3.14 | v3.15 | v3.16 )
add_init_d_service "urandom" "boot" 2 ;;
* )
add_init_d_service "seedrng" "boot" 2 ;;
esac
case $image_class in
cloud | virtual )
add_init_d_service "osclock" "boot" 2 ;;
physical )
case $image_physical_type in
rpi* )
if [ -n "$rpi_use_rtc" ]; then
# With hardware RTC
add_init_d_service "osclock" "boot" 2
else
# Without hardware RTC
add_init_d_service "swclock" "boot" 2
fi
;;
* )
add_init_d_service "osclock" "boot" 2 ;;
esac
;;
esac
cat <<-'EOF' >> "$run_script"
EOF
add_init_d_service "networking" "default" 2
cat <<-'EOF' >> "$run_script"
EOF
add_init_d_service "killprocs" "shutdown" 2
add_init_d_service "mount-ro" "shutdown" 2
add_init_d_service "savecache" "shutdown" 2
cat <<-'EOF' >> "$run_script"
} >> /chroot.log 2>&1
EOF
}
#
# Add an entry to /etc/apk/repositories file.
#
add_alpine_repo() {
local _alpine_mirror=$1
local _alpine_release=$2
local _alpine_repo=${3:-local}
cat <<EOF >> "$run_script"
write_log "Adding ${_alpine_repo} repo to /etc/apk/repositories" 2
cat <<-_SCRIPT_ >> /etc/apk/repositories
EOF
if [ "$_alpine_repo" = "local" ]; then
cat <<EOF >> "$run_script"
${_alpine_mirror}/${_alpine_release}
EOF
else
cat <<EOF >> "$run_script"
${_alpine_mirror}/${_alpine_release}/${_alpine_repo}
EOF
fi
cat <<'EOF' >> "$run_script"
_SCRIPT_
EOF
}
#
# Enable the specified service on the specified runlevel.
#
add_init_d_service() {
local _service_name=$1
local _run_level=$2
local _indent=${3:-0}
cat <<-EOF >> "$run_script"
$(printf "%${_indent}s")rc-update add ${_service_name} ${_run_level}
EOF
}
#
# Returns all entries from a comma-separated string
# except the specified entry.
#
all_entries_in_comma_list_except() {
local _comma_list="$1"
local _cl_except="$2"
all_entries_in_list_except "$_comma_list" "$_cl_except" ","
}
#
# Returns all entries from a string (which uses the specified
# separator) except the specified entry.
#
all_entries_in_list_except() {
local _list="$1"
local _keep_item="$2"
local _separator="$3"
local _check _check_item _resulting_list=""
_check="$_list"
while true; do
_check_item="$(first_entry_in_list "$_check" "$_separator")"
case $_check_item in
generic )
: ;;
* )
if [ "$_check_item" != "$_keep_item" ]; then
if [ -n "$_resulting_list" ]; then
_resulting_list="${_resulting_list}${_separator}${_check_item}"
else
_resulting_list="${_check_item}"
fi
fi
;;
esac
if [ "${_check%$_separator*}" = "$_check" ]; then
# No more entries
break
else
_check="${_check#$_check_item$_separator}"
fi
done
echo "$_resulting_list"
}
#
# Returns all entries from a space-separated string
# except the specified entry.
#
all_entries_in_space_list_except() {
local _space_list="$1"
local _sl_except="$2"
all_entries_in_list_except "$_space_list" "$_sl_except" " "
}
#
# Change the extension of a filename.
#
change_filename_extension() {
local _original_full_filepath="$1"
local _new_ext="$2"
local _filename_portion _new_full_filepath _old_ext _path_portion
_path_portion="$(dirname "$_original_full_filepath")"
if [ "$_path_portion" != "." ]; then
_filename_portion="${_original_full_filepath#$_path_portion}"
else
_filename_portion="${_original_full_filepath}"
_path_portion=""
fi
if [ "${_filename_portion%.*}" != "$_filename_portion" ]; then
# Replace existing extension
_old_ext="${_filename_portion##*.}"
_new_full_filepath="${_path_portion}${_filename_portion%.$_old_ext}.${_new_ext}"
else
# No existing extension so add one
_new_full_filepath="${_path_portion}${_filename_portion}.${_new_ext}"
fi
echo "$_new_full_filepath"
}
#
# Display error as command option is not supported, and exit.
#
cli_option_not_handled() {
error_message "This option does not apply in this situation."
}
#
# Display error as command option value is not valid, and exit.
#
cli_option_unknown_value() {
error_message "Unknown $1!"
}
#
# Copy /etc/resolv.conf from host system to chroot.
#
copy_host_resolv_conf_to_chroot() {
cat <<-'EOF' >> "$run_script"
write_log "Copying system's /etc/resolv.conf into chroot filesystem"
mkdir -p "$chroot_dir"/etc
cp /etc/resolv.conf "$chroot_dir"/etc/
EOF
}
#
# Copy local repository signing key to chroot.
#
copy_local_repo_key_to_chroot() {
cat <<-EOF >> "$run_script"
write_log "Copy local repo key to /etc/apk/keys/ directory inside chroot"
cp "${local_repo_signature_file}" "\$chroot_dir"/etc/apk/keys/
EOF
}
#
# Create MBR or GPT disk label.
#
create_disk_label() {
local _label_type="$1"
cat <<-EOF >> "$run_script"
write_debug_log "Creating ${_label_type} disk label" 2
parted --machine --script --align=optimal \$image_full_filename \\
mklabel ${_label_type} >> "\$logfile" 2>&1
EOF
}
#
# Create a disk partition.
#
create_disk_partition() {
local _part_number=$1
local _part_start=$2
local _part_end=$3
local _size=$4
local _part_desc=$5
local _flags="${6:-}"
local _fs_type=${7:-}
local _flag
cat <<-EOF >> "$run_script"
write_debug_log "Creating ${_size}MiB ${_part_desc} partition" 2
parted --machine --script --align=optimal \$image_full_filename \\
unit MiB mkpart primary ${_fs_type} ${_part_start} ${_part_end} >> "\$logfile" 2>&1
EOF
for _flag in $_flags; do
cat <<-EOF >> "$run_script"
write_debug_log "Setting partition ${_flag} flag on" 2
parted --machine --script --align=optimal \$image_full_filename \\
set ${_part_number} ${_flag} on >> "\$logfile" 2>&1
EOF
done
}
#
# Create basic /etc/apk/repositories file inside chroot.
#
create_repositories_file_inside_chroot() {
cat <<-EOF >> "$run_script"
write_log "Creating /etc/apk/repositories file inside chroot"
mkdir -p "\$chroot_dir"/etc/apk/keys
{
EOF
if [ "$image_alpine_release" = "edge-special" ]; then
cat <<-EOF >> "$run_script"
printf '%s/edge/main\n' "$alpine_mirror_site"
printf '%s/edge/community\n' "$alpine_mirror_site"
printf '%s/edge/testing\n' "$alpine_mirror_site"
EOF
if [ -n "$local_alpine_repository" ]; then
cat <<-EOF >> "$run_script"
printf '%s/edge\n' "$local_alpine_repository"
EOF
fi
else
cat <<-EOF >> "$run_script"
printf '%s/%s/main\n' "$alpine_mirror_site" "$image_alpine_release"
printf '%s/%s/community\n' "$alpine_mirror_site" "$image_alpine_release"
EOF
case $image_alpine_release in
edge )
cat <<-EOF >> "$run_script"
printf '%s/%s/testing\n' "$alpine_mirror_site" "$image_alpine_release"
EOF
;;
esac
if [ -n "$local_alpine_repository" ]; then
cat <<-EOF >> "$run_script"
printf '%s/%s\n' "$local_alpine_repository" "$image_alpine_release"
EOF
fi
fi
cat <<-EOF >> "$run_script"
} > "\$chroot_dir"/etc/apk/repositories
EOF
}
#
# Determine the Linux distro that this script is being run on.
#
detect_host_os() {
grep "^ID=" /etc/os-release | sed -e 's/^ID=//'
}
#
# Output one line per entry in list using specified line formatting.
#
echo_lines_from_comma_list() {
local _output_formatting="$1"
local _input_cl_list="$2"
local _cl_list _cl_list_item
_cl_list="$_input_cl_list"
while true; do
_cl_list_item="$(first_entry_in_comma_list "$_cl_list")"
printf "$_output_formatting\n" "$_cl_list_item"
if [ "${_cl_list%,*}" = "$_cl_list" ]; then
# No more entries
break
else
_cl_list="${_cl_list#$_cl_list_item,}"
fi
done
}
#
# Name a GPT disk partition.
#
label_gpt_disk_partition() {
local _part_number=$1
local _part_name="${2:-}"
cat <<-EOF >> "$run_script"
write_debug_log "Labelling GPT partition ${_part_number} as '${_part_name}'" 2
parted --machine --script \$image_full_filename \\
name ${_part_number} "${_part_name}" >> "\$logfile" 2>&1
EOF
}
#
# Output list of options, one per line.
#
options_wrap() {
local _options_list="$1"
local _current_option _option_name _option_value
while [ ${#_options_list} -gt 0 ] && \
[ "${_options_list#*--}" != "$_options_list" ]; do
_options_list="${_options_list#*--}"
_current_option="${_options_list%% --*}"
# Extract the name of the option
_option_name="${_current_option%% *}"
if [ "$_option_name" != "$_current_option" ]; then
# There is an option value also, extract it
_option_value="${_current_option##$_option_name }"
if [ "${_option_value%% *}" != "$_option_value" ]; then
# option value contains multiple items so place quotes around it
_option_value="\"$_option_value\""
fi
fi
if [ -n "$_option_value" ]; then
cat <<-EOF >> "$run_script"
# --${_option_name} ${_option_value}
EOF
else
cat <<-EOF >> "$run_script"
# --${_option_name}
EOF
fi
unset _option_value
done
}
#
# Create the comment block at the start of the runscript.
#
create_runscript_header() {
cat <<-EOF > "$run_script"
#!/bin/sh -eu
# shellcheck disable=SC2039
#############################################################################
# Generated on $(date +%c) by ${script_name}
# version ${script_version} using the following options:
#
EOF
options_wrap "$full_command_options"
cat <<-'EOF' >> "$run_script"
#
#############################################################################
if [ "$(id -u)" -ne 0 ]; then
printf '\nThis script must be run as the root user!\n\n'
exit 1
fi
EOF
}
#
# Download tarball of statically compiled apk tool.
#
download_static_apk() {
if [ "$image_script_host_arch" = "all" ] || \
[ "$image_script_host_arch" != "$image_arch" ]; then
cat <<-'EOF' >> "$run_script"
_host_arch="$(detect_host_arch)"
EOF
else
cat <<-EOF >> "$run_script"
_host_arch="${image_arch}"
EOF
fi
cat <<-EOF >> "$run_script"
write_log "Downloading statically built APK tool for \${_host_arch} arch"
{
wget -q -O ${TMPDIR}/apk.static \\
${apk_static_url}/"\${_host_arch}"/apk.static \\
2>> "\$logfile"
_rc=\$?
case \$_rc in
5 )
error_message "Error with wget HTTPS certificate validation!" ;;
1 | 3 | 4 | 7 | 8 )
error_message "A wget error (\$_rc) occurred during download!" ;;
esac
chmod +x ${TMPDIR}/apk.static
if ! echo "$apk_static_checksum ${TMPDIR}/apk.static" | sha256sum -c >/dev/null; then
error_message "The checksum of the downloaded APK tool does not match the expected checksum!"
fi
}
EOF
}
#
# Display an error message and exit the script.
#
error_message() {
printf '\n%s\n\n' "$1"
exit 2
}
#
# Returns first entry from a comma-separated string.
#
first_entry_in_comma_list() {
local _comma_list="$1"
first_entry_in_list "$_comma_list" ","
}
#
# Returns first entry from a string which uses the specified separator.
#
first_entry_in_list() {
local _list="$1"
local _separator="$2"
echo "${_list%%$_separator*}"
}
#
# Returns first entry from a space-separated string.
#
first_entry_in_space_list() {
local _space_list="$1"
first_entry_in_list "$_space_list" " "
}
#
# Create an ESP FAT filesystem.
#
format_esp_fs() {
local _uefi_info_type _uefi_mkfs_options
case $image_esp_type in
fat12 )
_uefi_info_type="FAT12"
_uefi_mkfs_options="-F12 -s 1 -S $image_os_device_sector_size"
;;
fat16 )
_uefi_info_type="FAT16"
_uefi_mkfs_options="-F16 -s 1 -S $image_os_device_sector_size"
;;
fat32 )
_uefi_info_type="FAT32"
_uefi_mkfs_options="-F32 -S $image_os_device_sector_size"
;;
esac
if [ -n "$debug_enabled" ]; then
_uefi_mkfs_options="$_uefi_mkfs_options -v"
fi
cat <<-EOF >> "$run_script"
write_log \\
"Formatting ${_uefi_info_type} filesystem with ${image_os_device_sector_size}-byte sectors on ESP partition" 2
mkfs.fat ${_uefi_mkfs_options} -n "${CADI_ESP_FS_LABEL}" "\$uefi_part_device" \\
>> "\$logfile" 2>&1
EOF
if [ -n "$debug_enabled" ]; then
cat <<-EOF >> "$run_script"
# Debug
write_debug_log "FAT filesystem info:" 2
file -s "\$uefi_part_device" >> "\$logfile" 2>&1
EOF
fi
}
#
# Create an EXT4 filesystem.
#
format_ext4_fs() {
local _ext4_device=$1
local _ext4_fs_use="$2"
local _ext4_label=$3
local _formatting_where="$4"
local _ext4_fs_options
_ext4_fs_options="-L ${_ext4_label}"
# Ensure EXT4 inode size is 256 bytes rather than 128
# in order to avoid Year 2038 problems/warning.
_ext4_fs_options="$_ext4_fs_options -I 256"
case $image_bootloader in
syslinux )
# Syslinux requires Ext4 64bit flag be disabled
# for whichever filesystem it boots from.
case $_ext4_fs_use in
boot )
_ext4_fs_options="$_ext4_fs_options -O ^64bit" ;;
root )
if [ "$image_create_boot_partition" != "true" ] && \
[ -z "$image_lvm_rootfs" ]; then
_ext4_fs_options="$_ext4_fs_options -O ^64bit"
fi
;;
esac
;;
esac
case $image_encryption_type in
both | fscrypt )
_ext4_fs_options="$_ext4_fs_options -O encrypt"
;;
esac
if [ -z "$debug_enabled" ]; then
_ext4_fs_options="$_ext4_fs_options -q"
fi
cat <<-EOF >> "$run_script"
write_log "Formatting Ext4 ${_ext4_fs_use} filesystem on ${_formatting_where}" 2
mkfs.ext4 ${_ext4_fs_options} "${_ext4_device}" >> "\$logfile" 2>&1
EOF
if [ -n "$debug_enabled" ]; then
cat <<-EOF >> "$run_script"
# Debug
write_debug_log "EXT4 filesystem info:" 2
tune2fs -l "$_ext4_device" >> "\$logfile" 2>&1
EOF
fi
}
#
# Indicate which kernel package to use based on cloud/physical/vm type,
# whether Edge kernel is specified, and the Alpine release.
#
get_kernel_type() {
local _kernel_pkg_type
if [ -n "$CADI_KERNEL_PACKAGE" ]; then
# If config file defines kernel package then use that
_kernel_pkg_type="$(echo "$CADI_KERNEL_PACKAGE" | sed -e 's/^linux\-//')"
else
case $image_class in
cloud | virtual )
case $image_alpine_release in
v3.13 | v3.14 )
_kernel_pkg_type="virt" ;;
v3.15 )
# This Alpine release has linux-virt
# and linux-edge4virt packages.
case $image_kernel_type in
edge | edge-special )
_kernel_pkg_type="edge4virt" ;;
* )
_kernel_pkg_type="virt" ;;
esac
;;
* )
# These Alpine releases no longer have linux-edge4virt
# package so use either linux-virt or linux-edge
# (rather than linux-edge4virt).
case $image_kernel_type in
edge | edge-special )
_kernel_pkg_type="edge" ;;
* )
_kernel_pkg_type="virt" ;;
esac
;;
esac
;;
physical )
case $image_physical_type in
rpi2 | rpi3 )
case $image_arch in
aarch64 )
_kernel_pkg_type="rpi" ;;
armv7 )
_kernel_pkg_type="rpi2" ;;
esac
;;
rpi4 )
_kernel_pkg_type="rpi4" ;;
* )
case $image_kernel_type in
edge | edge-special )
_kernel_pkg_type="edge" ;;
* )
_kernel_pkg_type="lts" ;;
esac
;;
esac
;;
esac
fi
echo "$_kernel_pkg_type"
}
#
# Returns the device name for specified storage.
#
get_storage_device_name() {
local _device_type=$1
local _device_name
case $_device_type in
nvme )
_device_name="/dev/nvme0n1" ;;
sd )
_device_name="/dev/mmcblk0" ;;
virtio_blk )
_device_name="/dev/vda" ;;
* )
_device_name="/dev/sda" ;;
esac
echo "$_device_name"
}
#
# Returns the device name for specified storage partition.
#
get_storage_partition_name() {
local _device_type=$1
local _partition_number=$2
local _partition_name
_partition_name=$(get_storage_device_name "$_device_type")
case $_device_type in
nvme | sd )
_partition_name="${_partition_name}p" ;;
esac
_partition_name="${_partition_name}${_partition_number}"
echo "$_partition_name"
}
#
# Insert function add_fstab_entry into created script.
#
insert_chroot_function_add_fstab_entry() {
cat <<-'EOF' >> "$run_script"
add_fstab_entry() {
local _entry_type="\$1"
local _entry_value="\$2"
local _mount_point="\$3"
local _fs_type="\$4"
local _fs_options="\${5:-}"
local _entry_log="\${6:-}"
local _fstab_entry
if [ "\$_entry_type" = "BIND" ]; then
_fs_options="bind,\${_fs_options}"
local _fs_passno="0"
elif [ "\$_fs_type" = "swap" ]; then
_mount_point="none"
_fs_options="sw"
local _fs_passno="0"
_entry_log="Swap partition"
elif [ "\$_fs_type" = "tmpfs" ]; then
local _fs_passno="0"
elif [ "\$_mount_point" = "/" ]; then
local _fs_passno="1"
else
local _fs_passno="2"
fi
if [ "\$_entry_type" = "BIND" ] || [ "\$_entry_type" = "DEVICE" ]; then
_fstab_entry="\${_entry_value}"
else
_fstab_entry="\${_entry_type}=\${_entry_value}"
fi
_fstab_entry="\${_fstab_entry}\t\${_mount_point}\t\${_fs_type}\t\${_fs_options} 0 \${_fs_passno}"
write_log "Add \${_entry_log} entry" 2
# shellcheck disable=SC2059
printf "\${_fstab_entry}\n" >> /etc/fstab
}
EOF
}
#
# Insert function get_kernel_version into created script.
#
insert_chroot_function_get_kernel_version() {
local _kernel_type
_kernel_type="$(get_kernel_type)"
cat <<-EOF >> "$run_script"
get_kernel_version() {
apk info linux-${_kernel_type} | head -n 1 | sed -e "s/^linux-${_kernel_type}-//" \\
-e 's/-r/-/' -e 's/ .*//' -Ee "s/^(.*)\$/\\1-${_kernel_type}/"
}
EOF
}
#
# Insert function write_debug_log into created script.
#
insert_chroot_function_write_debug_log() {
cat <<-'EOF' >> "$run_script"
write_debug_log() {
local _log_entry="\$1"
local _indent=\${2:-0}
local _current_time
EOF
if [ -n "$debug_enabled" ]; then
cat <<-'EOF' >> "$run_script"
_current_time=\$(printf "[%s]" "\$(date -u "+%Y-%m-%d %H:%M:%S")")
# shellcheck disable=SC1117
printf "\$_current_time chroot: %\${_indent}s\${_log_entry}\n" >> /chroot.log
EOF
else
cat <<-'EOF' >> "$run_script"
# Debug not enabled so do nothing
true
EOF
fi
cat <<-'EOF' >> "$run_script"
}
EOF
}
#
# Insert function write_log into created script.
#
insert_chroot_function_write_log() {
cat <<-'EOF' >> "$run_script"
write_log() {
local _log_entry="\$1"
local _indent=\${2:-0}
local _current_time
_current_time=\$(printf "[%s]" "\$(date -u "+%Y-%m-%d %H:%M:%S")")
# shellcheck disable=SC1117
printf "\$_current_time chroot: %\${_indent}s\${_log_entry}\n" >> /chroot.log
# shellcheck disable=SC1117
printf "chroot: %\${_indent}s\${_log_entry}\n"
}
EOF
}
#
# Insert function check_binfmt_packages into the run script.
#
insert_runscript_function_check_binfmt_packages() {
local _alpine_arch_package _binfmt_file
case $image_arch in
aarch64 )
_alpine_arch_package="qemu-aarch64"
_binfmt_file="/proc/sys/fs/binfmt_misc/qemu-aarch64"
;;
armv7 )
_alpine_arch_package="qemu-arm"
_binfmt_file="/proc/sys/fs/binfmt_misc/qemu-arm"
;;
x86 )
_alpine_arch_package="qemu-i386"
_binfmt_file="/proc/sys/fs/binfmt_misc/qemu-i386"
;;
x86_64 )
_alpine_arch_package="qemu-x86_64"
_binfmt_file="/proc/sys/fs/binfmt_misc/qemu-x86_64"
;;
esac
cat <<-EOF >> "$run_script"
#
# Check that the host OS has necessary packages installed for
# running user-mode QEMU via binfmt and that it is configured.
#
check_binfmt_packages() {
EOF
case $image_script_host_os in
alpine | all )
cat <<-EOF >> "$run_script"
local _alpine_arch_package="$_alpine_arch_package"
EOF
;;
esac
cat <<-EOF >> "$run_script"
local _arch="$image_arch"
local _binfmt_file="$_binfmt_file"
local _binfmt_arch_enabled _host_arch _host_os _required_packages
_host_arch=\$(detect_host_arch)
if [ "\$_host_arch" != "\$_arch" ]; then
_host_os=\$(detect_host_os)
case \$_host_os in
EOF
case $image_script_host_os in
alpine | all )
cat <<-'EOF' >> "$run_script"
alpine )
EOF
cat <<-'EOF' >> "$run_script"
_required_packages="qemu-openrc $_alpine_arch_package"
# shellcheck disable=SC2086
if ! apk info -e -q $_required_packages; then
printf '\nThe following Alpine packages need to be installed:\n\n'
printf ' %s\n\n' "$_required_packages"
exit 1
fi
;;
EOF
;;
esac
case $image_script_host_os in
debian | ubuntu | all )
case $image_script_host_os in
debian )
cat <<-'EOF' >> "$run_script"
debian )
EOF