@dotpi/tools 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/DOC.md +66 -0
  2. package/LICENSE +28 -0
  3. package/NOTES.md +81 -0
  4. package/README.md +341 -0
  5. package/dotpi-tools.js +35 -0
  6. package/dotpi_root/bin/dotpi +66 -0
  7. package/dotpi_root/bin/dotpi_firstrun +193 -0
  8. package/dotpi_root/bin/dotpi_prepare_sd_card +616 -0
  9. package/dotpi_root/bin/dotpi_prepare_system +244 -0
  10. package/dotpi_root/etc/dotpi_environment_default.bash +69 -0
  11. package/dotpi_root/etc/dotpi_tarignore +3 -0
  12. package/dotpi_root/share/dotpi-install/update_to_v1.0.0_manager +39 -0
  13. package/dotpi_root/share/dotpi-install/update_to_v1.0.0_pi +39 -0
  14. package/dotpi_root/share/dotpi_audio_bluetooth/dotpi_audio_bluetooth.service +23 -0
  15. package/dotpi_root/share/dotpi_audio_bluetooth/dotpi_audio_bluetooth_command +134 -0
  16. package/dotpi_root/share/dotpi_audio_bluetooth/dotpi_audio_pipewire_relink +132 -0
  17. package/dotpi_root/share/dotpi_avahi_monitor/dotpi_avahi_monitor.service +22 -0
  18. package/dotpi_root/share/dotpi_avahi_monitor/dotpi_avahi_monitor_command +14 -0
  19. package/dotpi_root/share/dotpi_environment.bash +5 -0
  20. package/dotpi_root/share/dotpi_functions/dotpi_functions_audio_bluetooth.bash +346 -0
  21. package/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device.bash +504 -0
  22. package/dotpi_root/share/dotpi_functions/dotpi_functions_configuration.bash +435 -0
  23. package/dotpi_root/share/dotpi_functions/dotpi_functions_connection.bash +199 -0
  24. package/dotpi_root/share/dotpi_functions/dotpi_functions_dotpi.bash +36 -0
  25. package/dotpi_root/share/dotpi_functions/dotpi_functions_service.bash +111 -0
  26. package/dotpi_root/share/dotpi_functions/dotpi_functions_system.bash +175 -0
  27. package/dotpi_root/share/dotpi_functions/dotpi_functions_utils.bash +154 -0
  28. package/dotpi_root/share/dotpi_functions.bash +14 -0
  29. package/dotpi_root/share/dotpi_init.bash +18 -0
  30. package/dotpi_root/share/jackd/dotpi_jackd_bluetooth.bash +18 -0
  31. package/dotpi_root/share/jackd/dotpi_jackd_command +26 -0
  32. package/dotpi_root/share/jackd/dotpi_jackd_headphones.bash +21 -0
  33. package/dotpi_root/share/jackd/dotpi_jackd_hifiberry.bash +17 -0
  34. package/dotpi_root/share/jackd/jackd.service +22 -0
  35. package/dotpi_root/share/limits/zzz_dotpi_audio.conf +11 -0
  36. package/dotpi_root/share/wireplumber/bluetooth.lua.d/8zzz-dotpi-wireplumber-config.lua +4 -0
  37. package/examples/example-development/configuration/dotpi_project.bash +40 -0
  38. package/examples/example-headphones/configuration/dotpi_project.bash +18 -0
  39. package/examples/example-hifiberry/configuration/dotpi_instance_012.bash +4 -0
  40. package/examples/example-hifiberry/configuration/dotpi_project.bash +18 -0
  41. package/examples/example-network/configuration/dotpi_project.bash +21 -0
  42. package/package.json +35 -0
  43. package/src/configure-host.js +187 -0
  44. package/src/constants.js +30 -0
  45. package/src/create-project.js +438 -0
  46. package/src/install-rpi.js +173 -0
  47. package/src/templates/README.md +16 -0
  48. package/src/templates/ap.nmconnection +25 -0
  49. package/src/templates/dotpi_project.bash +31 -0
  50. package/src/templates/dotpi_secrets.bash +4 -0
  51. package/src/templates/infrastructure.nmconnection +29 -0
  52. package/src/templates/ssh_config +6 -0
  53. package/src/utils.js +175 -0
  54. package/test/constants.spec.js +37 -0
  55. package/test/create-project.test.js +57 -0
  56. package/test/install-rpi.test.js +8 -0
  57. package/test/utils.spec.js +48 -0
@@ -0,0 +1,616 @@
1
+ #!/bin/bash
2
+
3
+ set +e
4
+
5
+ _dotpi_command="$(basename -- "$0")"
6
+
7
+ ####### Bootstrap
8
+
9
+ dotpi_echo_error() {
10
+ printf 'ERROR: %s\n' "$1" >&2
11
+ }
12
+
13
+ dotpi_echo_warning() {
14
+ printf 'WARNING: %s\n' "$1" >&2
15
+ }
16
+
17
+ ####### Command-line options
18
+
19
+ _dotpi_usage() {
20
+ echo "Usage: ${_dotpi_command} --project <project_path> [--instance-number <integer>] [--bootfs-drive-letter <letter>]"
21
+ }
22
+
23
+ _echo_empty_option_error() {
24
+ dotpi_echo_error "$1 requires a non-empty option argument."
25
+ }
26
+
27
+ while true ; do
28
+ case $1 in
29
+
30
+ -h|-\?|--help)
31
+ _dotpi_usage # Display a usage synopsis.
32
+ exit 0
33
+ ;;
34
+
35
+ -p|--project) # Takes an option argument; ensure it has been specified.
36
+ if [ -z "$2" ] ; then
37
+ _echo_empty_option_error "$1"
38
+ _dotpi_usage
39
+ exit 1
40
+ else
41
+ project="$2"
42
+ shift
43
+ fi
44
+ ;;
45
+
46
+ -i|--instance-number) # Takes an option argument; ensure it has been specified.
47
+ if [ -z "$2" ] ; then
48
+ _echo_empty_option_error "$1"
49
+ _dotpi_usage
50
+ exit 1
51
+ else
52
+ instance_number="$2"
53
+ shift
54
+ fi
55
+ ;;
56
+
57
+ -d|--bootfs-drive-letter) # Takes an option argument; ensure it has been specified.
58
+ if [ -z "$2" ] ; then
59
+ _echo_empty_option_error "$1"
60
+ _dotpi_usage
61
+ exit 1
62
+ else
63
+ bootfs_drive_letter="$2"
64
+ shift
65
+ fi
66
+ ;;
67
+
68
+ --) # End of all options.
69
+ shift
70
+ break
71
+ ;;
72
+
73
+ -?*) # Unknown
74
+ dotpi_echo_error "Unknown option: $1"
75
+ _dotpi_usage
76
+ exit 1
77
+ ;;
78
+
79
+ *) # Default case: No more options, so break out of the loop.
80
+ break
81
+
82
+ esac
83
+
84
+ shift
85
+ done
86
+
87
+ if [ -z ${project+x} ] ; then
88
+ dotpi_echo_error "missing project option"
89
+ _dotpi_usage
90
+ exit 1
91
+ fi
92
+
93
+ project_path="$( (cd -- "$project" && pwd) 2> /dev/null || {
94
+ dotpi_echo_error "unable to access project ${project}"
95
+ } )"
96
+ if [ -z "$project_path" ] ; then
97
+ exit 1
98
+ fi
99
+
100
+ local_path="$( (cd -- "$(dirname -- "$0")" && pwd) 2> /dev/null || {
101
+ dotpi_echo_error "unable to access local path $(dirname -- "$0")"
102
+ } )"
103
+ if [ -z "$local_path" ] ; then
104
+ exit 1
105
+ fi
106
+
107
+ # script in "bin"
108
+ DOTPI_ROOT="${local_path}/.."
109
+
110
+ source "${DOTPI_ROOT}/share/dotpi_init.bash"
111
+
112
+ tmp_path="${project_path}/tmp"
113
+ mkdir -p -- "$tmp_path"
114
+
115
+ # self log
116
+ log_file="${tmp_path}/${_dotpi_command}_$(date +"%Y%m%d-%H%M%S").log"
117
+ dotpi_echo_info "Log file: ${log_file}"
118
+ exec &> >(dotpi_log "$log_file")
119
+
120
+ file_system_mirror="${tmp_path}/file-system"
121
+ rm -rf "$file_system_mirror"
122
+ mkdir -p -- "$file_system_mirror"
123
+
124
+ dotpi_echo_info "Filesystem mirror: ${file_system_mirror}"
125
+
126
+ tar_options=()
127
+ while read pattern ; do
128
+
129
+ # avoid empty pattern
130
+ if [ -z "$pattern" ] ; then
131
+ continue
132
+ fi
133
+
134
+ tar_options+=( '--exclude' "${pattern}" )
135
+ done < "${DOTPI_ROOT}/etc/dotpi_tarignore"
136
+
137
+ # regular files only
138
+ if [[ "$(uname)" == "Darwin" ]] ; then
139
+ tar_options+=( '--no-mac-metadata' )
140
+ fi
141
+ tar_options+=( '--no-xattrs' )
142
+
143
+ #### copy defaults from dotpi_root
144
+
145
+ source_path="$DOTPI_ROOT"
146
+ destination_path="${file_system_mirror}/opt/dotpi"
147
+ mkdir -p -- "$destination_path"
148
+ (set -o pipefail ; \
149
+ cd -- "$source_path" && tar c "${tar_options[@]}" '.' \
150
+ | tar x -C "$destination_path") || {
151
+ dotpi_echo_error "Error while copying defaults from ${source_path} to ${destination_path}"
152
+ exit 1
153
+ }
154
+
155
+ #### copy project configuration
156
+
157
+ source_path="$project_path"
158
+ destination_path="${file_system_mirror}/opt/dotpi/etc"
159
+ mkdir -p -- "$destination_path"
160
+ source_file_path="${source_path}/dotpi_project.bash"
161
+ destination_file_path="${destination_path}/dotpi_environment_project.bash"
162
+ if [ -r "$source_file_path" ] ; then
163
+ cp -- "$source_file_path" "$destination_file_path"
164
+ fi
165
+
166
+ #### select instance
167
+
168
+ # keep reference of file to increment at the end
169
+ environment_tmp_file="${tmp_path}/dotpi_tmp.bash"
170
+ dotpi_source_if_available "$environment_tmp_file"
171
+
172
+ if [ -n "$instance_number" ] ; then
173
+ # from command-line
174
+ dotpi_instance_number="$instance_number"
175
+ else
176
+ # from environment_tmp
177
+ if [ -z "$dotpi_instance_number" ] ; then
178
+ dotpi_instance_number=1
179
+ fi
180
+
181
+ echo ''
182
+ echo '##########################################################'
183
+ echo ''
184
+ echo " Please type instance number, or type enter for ${dotpi_instance_number}"
185
+ echo -n ' '
186
+
187
+ read -r user_instance_number
188
+ if [ -n "$user_instance_number" ] ; then
189
+ dotpi_instance_number="${user_instance_number}"
190
+ fi
191
+
192
+ echo ''
193
+ echo '##########################################################'
194
+ echo ''
195
+ fi
196
+
197
+ # remove any padding zero to avoid octal interpretation (sic)
198
+ dotpi_instance_number="$((10#${dotpi_instance_number}))"
199
+
200
+ #### copy instance over-rides
201
+
202
+ source_path="$project_path"
203
+ destination_path="${file_system_mirror}/opt/dotpi/etc"
204
+ mkdir -p -- "$destination_path"
205
+ source_file_path="${source_path}/dotpi_instance_$(printf "%03d" "${dotpi_instance_number}").bash"
206
+ destination_file_path="${destination_path}/dotpi_environment_instance.bash"
207
+ if [ -r "$source_file_path" ] ; then
208
+ dotpi_echo_info "Using instance-specific configuration ${source_file_path}"
209
+ cp -- "$source_file_path" "$destination_file_path"
210
+ fi
211
+
212
+
213
+ ####################################################
214
+ # mirror should be complete, now: switch to mirror #
215
+ ####################################################
216
+
217
+ DOTPI_ROOT="${file_system_mirror}/opt/dotpi"
218
+ source "${DOTPI_ROOT}/share/dotpi_init.bash"
219
+
220
+ ######## project name
221
+
222
+ destination_path="${DOTPI_ROOT}/etc"
223
+ destination_file_path="${destination_path}/dotpi_environment_project.bash"
224
+
225
+ # default project name is directory of project
226
+ if [[ -z "$dotpi_project_name" ]] ; then
227
+ dotpi_project_name="$(basename "$project_path")"
228
+ dotpi_echo_warning "Project name not found. Using project folder name: ${dotpi_project_name}"
229
+ dotpi_configuration_write --file "$destination_file_path" --key "dotpi_project_name" --value "$dotpi_project_name"
230
+ fi
231
+
232
+ ######## read extra files
233
+
234
+ if [ -z "$dotpi_hostname_prefix" ] ; then
235
+ dotpi_echo_error "Bad dotpi_hostname_prefix: ${dotpi_hostname_prefix}"
236
+ echo "Check ${project_path}/dotpi_project.bash"
237
+ exit 1
238
+ fi
239
+
240
+ dotpi_instance_hostname="${dotpi_hostname_prefix}-${dotpi_project_name}-$(printf "%03d" "${dotpi_instance_number}")"
241
+ if (( $? != 0 )) ; then
242
+ dotpi_echo_error "Bad dotpi_instance_number: ${dotpi_instance_number}"
243
+ exit 1
244
+ fi
245
+ dotpi_echo_info "Instance hostname: ${dotpi_instance_hostname}"
246
+
247
+ destination_file="${DOTPI_ROOT}/etc/dotpi_environment_instance.bash"
248
+ dotpi_configuration_write --file "$destination_file" --key 'dotpi_instance_number' --value "$dotpi_instance_number"
249
+ dotpi_configuration_write --file "$destination_file" --key 'dotpi_instance_hostname' --value "$dotpi_instance_hostname"
250
+
251
+
252
+ ### secrets for password
253
+
254
+ source_file="${project_path}/dotpi_secrets.bash"
255
+ destination_file="${DOTPI_ROOT}/etc/dotpi_environment_project.bash"
256
+ if ! [ -r "$source_file" ] ; then
257
+ dotpi_echo_error "unable to read secrets file ${source_file}"
258
+ exit 1
259
+ else
260
+ source "$source_file"
261
+
262
+ if [ -z "$dotpi_password" ] ; then
263
+ dotpi_echo_error "You must define a password: set dotpi_password in ${source_file} to a non-empty string."
264
+ exit 1
265
+ else
266
+ hash="$(openssl passwd -1 "$dotpi_password")"
267
+ dotpi_configuration_write --file "$destination_file" --key 'dotpi_password_hash' --value "$hash"
268
+ fi
269
+ fi
270
+
271
+ ### secrets for ssh public keys
272
+
273
+ source_path="${project_path}/ssh"
274
+ destination_path="${DOTPI_ROOT}/etc/ssh"
275
+ mkdir -p -- "$destination_path"
276
+ if [ -d "$source_path" ] && [ -r "$source_path" ] ; then
277
+ (set -o pipefail ; \
278
+ cd -- "$source_path" && tar c "${tar_options[@]}" *.pub \
279
+ | tar x -C "$destination_path") || {
280
+ dotpi_echo_error "No public key found in ${source_path}"
281
+ exit 1
282
+ }
283
+ fi
284
+
285
+ ### secrets for NetworkManager connections from project
286
+
287
+ source_path="${project_path}/network"
288
+ destination_path="${DOTPI_ROOT}/etc/network"
289
+ mkdir -p -- "$destination_path"
290
+ if [ -d "$source_path" ] && [ -r "$source_path" ] ; then
291
+ (set -o pipefail ; \
292
+ cd -- "$source_path" && tar c "${tar_options[@]}" *.nmconnection \
293
+ | tar x -C "$destination_path") || {
294
+ dotpi_echo_error "No network connection found in ${source_path}"
295
+ exit 1
296
+ }
297
+ fi
298
+
299
+ # update connections in DOTPI_ROOT
300
+
301
+ if ! $(wpa_passphrase test 12345678 > /dev/null 2>&1) ; then
302
+ dotpi_echo_warning "Network password won't be salted, 'wpa_passphrase' is needed to salt passwords."
303
+ else
304
+
305
+ for file in "${destination_path}"/*.nmconnection ; do
306
+ key='wifi-security.psk'
307
+ psk="$(dotpi_connection_read --file "$file" --key "$key")"
308
+
309
+ # psk present and not salted
310
+ if (( ${#psk} > 0 && ${#psk} < 64 )) ; then
311
+ key='wifi.ssid'
312
+ ssid="$(dotpi_connection_read --file "$file" --key "$key")"
313
+ passphrase="$(wpa_passphrase "$ssid" "$psk" | dotpi_configuration_read --key 'psk' )"
314
+ key='wifi-security.psk'
315
+ dotpi_connection_write --file "$file" --key "$key" --value "$passphrase"
316
+ fi
317
+
318
+ done
319
+ fi
320
+
321
+
322
+ ######## SD card
323
+
324
+ bootfs_name="bootfs"
325
+
326
+ if [[ "$(uname)" =~ "Darwin" ]] ; then
327
+ echo ""
328
+ echo "MacOS host detected"
329
+ echo ""
330
+
331
+ # try to mount if unmounted (but not ejected)
332
+ if type -P diskutil &> /dev/null ; then
333
+ diskutil mount "$bootfs_name"
334
+ fi
335
+
336
+ # avoid spotlight errors
337
+ bootfs_path="$(find /Volumes -name "$bootfs_name" 2> /dev/null)"
338
+ elif [[ "$(uname -a)" =~ "Microsoft" ]] ; then
339
+ echo ""
340
+ echo "Windows Subsystem for Linux system detected"
341
+ echo ""
342
+
343
+ bootfs_mount_reference=($(findmnt -lo source,target | grep "$bootfs_name"))
344
+ if ! [[ -n "$bootfs_mount_reference" ]] ; then
345
+ # not there, mount
346
+
347
+ bootfs_mount_source="${bootfs_drive_letter^^}:\\"
348
+ bootfs_mount_target="${tmp_path}/${bootfs_name}"
349
+ mkdir -p "$bootfs_mount_target"
350
+ mount -t drvfs "$bootfs_mount_source" "$bootfs_mount_target" || {
351
+ dotpi_echo_error "Unable to mount ${bootfs_mount_source} to ${bootfs_mount_target}"
352
+ }
353
+ fi
354
+
355
+ bootfs_mount_reference=($(findmnt -lo source,target | grep "$bootfs_name"))
356
+ if [[ -n "$bootfs_mount_reference" ]] ; then
357
+ bootfs_mount_source="${bootfs_mount_reference[0]}"
358
+ bootfs_mount_target="${bootfs_mount_reference[1]}"
359
+ bootfs_path="$bootfs_mount_target"
360
+ else
361
+ bootfs_path=""
362
+ rmdir "$bootfs_mount_target"
363
+ fi
364
+
365
+ elif [[ "$(uname)" =~ "Linux" ]] ; then
366
+ echo ""
367
+ echo "Linux system detected"
368
+ echo ""
369
+
370
+ # try to mount if unmounted (but not ejected)
371
+ if type -P udisksctl &> /dev/null ; then
372
+ udisksctl mount --block-device "/dev/disk/by-label/${bootfs_name}"
373
+ fi
374
+
375
+ bootfs_path="$(lsblk --output MOUNTPOINT | grep "/${bootfs_name}")"
376
+ else
377
+ echo ""
378
+ echo "Unknown system detected"
379
+ echo ""
380
+ echo
381
+ echo "Please adapt for your environment and send us feedback."
382
+ exit 1
383
+ fi
384
+
385
+ if [ ! -d "$bootfs_path" ] ; then
386
+ echo ""
387
+ echo "############################################################"
388
+ echo ""
389
+ echo " Please mount SD card and try again."
390
+ echo ""
391
+ echo "############################################################"
392
+ echo ""
393
+ exit 1
394
+ fi
395
+
396
+
397
+ ######### config.txt
398
+
399
+ config_filename='config.txt'
400
+ config_file="${project}/${config_filename}"
401
+ destination_path="${DOTPI_ROOT}/etc"
402
+
403
+ mkdir -p -- "$destination_path"
404
+
405
+ if [ -r "$config_file" ] ; then
406
+ echo "using project config file ${config_file}"
407
+ else
408
+ config_file="${bootfs_path}/${config_filename}"
409
+ echo "# copy config file from imager"
410
+ fi
411
+
412
+ cp -- "$config_file" "$destination_path"
413
+
414
+ config_file="${destination_path}/${config_filename}"
415
+
416
+ echo "# edit ${config_file}"
417
+
418
+ # keep quotes here to keep quotes hereafter
419
+ cat >> "${config_file}" << EOF
420
+
421
+ ############
422
+ # dotpi
423
+ #
424
+
425
+ [all]
426
+
427
+ EOF
428
+
429
+ if [[ "$dotpi_word_size" == "32" ]] ; then
430
+ word_size="$(dotpi_file_word_size "${bootfs_path}/start.elf" )"
431
+ if [[ "$word_size" == "32" ]] ; then
432
+
433
+ file="$config_file"
434
+ key="arm_64bits"
435
+
436
+ # comment original value
437
+ dotpi_configuration_comment --file "$file" --key "$key"
438
+ dotpi_configuration_write \
439
+ --file "$file" --key "$key" \
440
+ --value "0" --prepend-line-if-new '# true 32bit system'
441
+ fi
442
+ fi
443
+
444
+ ######### audio device
445
+
446
+ # set model before, as it will select jackd configuration
447
+ dotpi_audio_device_select --model "$dotpi_audio_device" --config "$config_file"
448
+
449
+ # copy configuration files from project for over-rides
450
+ source_path="${project_path}/jackd"
451
+ destination_path="${DOTPI_ROOT}/share/jackd"
452
+ mkdir -p -- "$destination_path"
453
+ if [ -d "$source_path" ] && [ -r "$source_path" ] ; then
454
+ (set -o pipefail ; \
455
+ cd -- "$source_path" && tar c "${tar_options[@]}" '.' \
456
+ | tar x -C "$destination_path") || {
457
+ dotpi_echo_error "Unable to install files from ${source_path}"
458
+ exit 1
459
+ }
460
+ fi
461
+
462
+
463
+ # back-up imager config
464
+
465
+ destination_filename="imager_${config_filename}"
466
+
467
+ # install config.txt to /boot to be ready for first run
468
+ cp -- "$config_file" "$bootfs_path"
469
+
470
+ ######### configure first run
471
+
472
+ echo "# set-up first run"
473
+
474
+ source_file_path="${bootfs_path}/firstrun.sh"
475
+ if [ -f "$source_file_path" ] ; then
476
+ # back-up imager firstrun.sh
477
+ destination_filename='imager_firstrun.sh'
478
+ # keep a copy on bootfs
479
+ destination_path="${bootfs_path}/dotpi"
480
+
481
+ mkdir -p -- "$destination_path"
482
+
483
+ if [ -r "${destination_path}/${destination_filename}" ] ; then
484
+ dotpi_echo_warning "Keep existing imager first-run script: ${destination_path}/${destination_filename}"
485
+ else
486
+ cp -- "$source_file_path" "${destination_path}/${destination_filename}"
487
+
488
+ destination_path="${DOTPI_ROOT}/etc"
489
+ mkdir -p -- "$destination_path"
490
+ cp -- "$source_file_path" "${destination_path}/${destination_filename}"
491
+ fi
492
+ else
493
+ dotpi_echo_warning "firstrun.sh not found"
494
+ fi
495
+
496
+ # configure cmdline.txt for firstun script, if not already here
497
+ source_file_name='cmdline.txt'
498
+ source_file_path="${bootfs_path}/${source_file_name}"
499
+ destination_path="${bootfs_path}/dotpi"
500
+ pattern='systemd.run=/boot/firstrun.sh'
501
+ pattern_escaped="$(dotpi_configuration_escape_string "$pattern")"
502
+ first_run_configured="$(perl -ne 'if (m/'"$pattern_escaped"'/) { print "1"; }' "$source_file_path")"
503
+ if (( first_run_configured != 1 )) ; then
504
+ dotpi_echo_info "Configuring firstrun in ${source_file_path}"
505
+ # keep a copy of originalc command-line
506
+ mkdir -p -- "$destination_path"
507
+ cp -- "$source_file_path" "${destination_path}/imager_${source_file_name}"
508
+ # append:run script and reboot on success
509
+ arguments=(
510
+ 'systemd.run=/boot/firstrun.sh'
511
+ 'systemd.run_success_action=reboot'
512
+ 'systemd.unit=kernel-command-line.target'
513
+ )
514
+ pattern="${arguments[*]}"
515
+ pattern_escaped="$(dotpi_configuration_escape_string "$pattern")"
516
+ # do not append to a new line
517
+ # append a space to separate from previous arguments (extra does not hurt)
518
+
519
+ # windows error: Cannot complete in-place edit of tmp/bootfs/cmdline.txt
520
+ source_file_path_tmp="${tmp_path}/${source_file_name}"
521
+ cp "$source_file_path" "$source_file_path_tmp"
522
+ dotpi_sed 's/$/${1} '"$pattern_escaped"'/' -i -- "$source_file_path_tmp"
523
+ mv "$source_file_path_tmp" "$source_file_path"
524
+ fi
525
+
526
+ # replace first run with custom, that will over-ride imager settings
527
+ # run dotpi_firstrun.bash
528
+ cat > "${bootfs_path}/firstrun.sh" << "EOF"
529
+ #!/bin/bash
530
+
531
+ set +e
532
+
533
+ # since Raspberry Pi OS Bookworm
534
+ local_path='/boot/firmware'
535
+
536
+ if ! [ -d "$local_path" ] ; then
537
+ # for Raspberry Pi OS Bullseye
538
+ local_path='/boot'
539
+ fi
540
+
541
+ # log to boot partition to access it even on errors
542
+ log_file="${local_path}/firstrun-$(date +"%Y%m%d-%H%M%S").log"
543
+
544
+ clean_up() {
545
+ rm -f "${local_path}/firstrun.sh"
546
+ sed -i 's| systemd.run.*||g' "${local_path}/cmdline.txt"
547
+ }
548
+
549
+ (cd -- / && tar xz -m --no-same-owner --no-same-permissions --no-overwrite-dir -f "${local_path}/dotpi/file_system.tgz")
550
+
551
+ source_path="/opt/dotpi"
552
+ cd -- "$source_path" || { clean_up ; exit 1; }
553
+ "bin/dotpi_firstrun" "$log_file"
554
+
555
+ clean_up
556
+
557
+ exit 0
558
+
559
+ EOF
560
+
561
+
562
+ ########## archive mirror to bootfs
563
+
564
+ echo "# archive mirror"
565
+
566
+ source_path="$file_system_mirror"
567
+ destination_path="${bootfs_path}/dotpi"
568
+ mkdir -p -- "$destination_path"
569
+
570
+ # create archive for installing, later
571
+ # this overcomes the limitiations of the FAT32 boot volume
572
+ tar cz "${tar_options[@]}" -C "${source_path}" '.' > "${destination_path}/file_system.tgz"
573
+
574
+ # un-mount
575
+ if type -P diskutil &> /dev/null ; then
576
+ # MacOS
577
+
578
+ diskutil umount "$bootfs_name" || :
579
+ elif [[ -n "$bootfs_mount_source" ]] && [[ -n "$bootfs_mount_target" ]] ; then
580
+ # WSL
581
+
582
+ umount "$bootfs_mount_source"
583
+ rmdir "$bootfs_mount_target"
584
+
585
+ echo ""
586
+ echo "####################################################"
587
+ echo ""
588
+ echo " Please eject SD card from Windows host."
589
+ echo ""
590
+ echo "####################################################"
591
+ echo ""
592
+
593
+ elif type -P udisksctl &> /dev/null ; then
594
+ # Linux
595
+
596
+ udisksctl unmount -b "/dev/disk/by-label/${bootfs_name}"
597
+ fi
598
+
599
+ dotpi_echo_info "Mirror of file-system is here: ${file_system_mirror}"
600
+
601
+ (( dotpi_instance_number++ ))
602
+ dotpi_configuration_write --file "$environment_tmp_file" --key dotpi_instance_number --value "$dotpi_instance_number"
603
+
604
+ echo ""
605
+ echo "####################################################"
606
+ echo ""
607
+ echo " Please put SD card in Raspberry Pi and power on."
608
+ echo ""
609
+ echo "####################################################"
610
+ echo ""
611
+
612
+ dotpi_echo_info "You can monitor the preparation of the system with the following command"
613
+ dotpi_echo_info "(You will have to wait until the network is ready.)"
614
+ dotpi_echo_info ''
615
+ dotpi_echo_info "ssh ${dotpi_user}@${dotpi_instance_hostname}.local 'tail -f /opt/dotpi/var/log/dotpi_prepare_system_*.log'"
616
+