-
Notifications
You must be signed in to change notification settings - Fork 62
/
p2-update
1229 lines (1035 loc) · 53.4 KB
/
p2-update
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
########################################################################
if [[ -z "$script_dir" ]]
then
echo 'do not run this script directly !'
echo 'this script is part of run.sh'
exit -1
fi
########################################################################
########################################################################
echo -e "\e[36msetup variables\e[0m";
########################################################################
########################################################################
. "${script_dir:?}/p2-include-var"
. "${script_dir:?}/c2-custom-var"
########################################################################
########################################################################
. "${script_dir:?}/p2-include-url"
. "${script_dir:?}/c2-custom-url"
echo
echo -e "${KERNEL_MAJOR}.${KERNEL_MINOR} \e[36mis kernel version\e[0m";
echo -e "${INTERFACE_ETH0} \e[36mis used as primary networkadapter for PXE\e[0m";
echo -e "${IP_ETH0} \e[36mis used as primary IP address for PXE\e[0m";
echo -e "${RPI_SN0} \e[36mis used as SN for RPi3 network booting\e[0m";
echo
if [[ -z "${IP_ETH0}" ]]; then
echo -e "\e[1;31mIP address not found. please check your ethernet cable.\e[0m";
exit 1
fi
if [[ -z "${IP_ETH0_ROUTER}" ]]; then
echo -e "\e[1;31mrouter IP address not found. please check your router settings.\e[0m";
exit 1
fi
[[ -z "${SRC_MOUNT}" ]] && sudo umount -f "${SRC_MOUNT}" &>/dev/null;
[[ -z "${SRC_MOUNT}" ]] && sudo mount "${SRC_MOUNT}" &>/dev/null;
########################################################################
compare_last_modification_time() {
python3 - << EOF "${1:?}" "${2:?}"
import sys
import os
import urllib.request
import time
try:
arg_file = sys.argv[1]
stat_file = os.stat(arg_file)
time_file = time.gmtime(stat_file.st_mtime)
arg_url = sys.argv[2]
conn_url = urllib.request.urlopen(arg_url)
time_url = time.strptime(conn_url.headers['last-modified'], '%a, %d %b %Y %H:%M:%S %Z')
if time_url <= time_file:
exit_code = 0
else:
exit_code = 1
print('file:{} <-> url:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time_file), time.strftime("%Y-%m-%d %H:%M:%S", time_url)))
except:
exit_code = 1
sys.exit(exit_code)
EOF
}
########################################################################
_unhandle_img() {
if [[ -z "${1}" ]] || [[ -z "${!1}" ]]; then return 0; fi
####################################################################
# ${!1} : name of VARIABLE -> $VARIABLE, $VARIABLE_URL, $VARIABLE_SUM, $VARIABLE_SUM_TYPE
local NAME="${!1}"
local URL="${1}_URL"; URL="${!URL}"
local SUM="${1}_SUM"; SUM="${!SUM}"
local SUM_TYPE="${1}_SUM_TYPE"; SUM_TYPE="${!SUM_TYPE}"
# ${2..} : optional options
local OPT=${2}
####################################################################
local FILE_URL="${NAME:?}.url"
local FILE_IMG="${NAME:?}.img"
####################################################################
echo -e "\e[32m_unhandle_img(\e[0m${NAME:?}\e[32m)\e[0m";
sudo exportfs -u "*:${DST_NFS_ETH0:?}/${NAME:?}" 2> /dev/null;
sudo umount -f "${DST_NFS_ETH0:?}/${NAME:?}" 2> /dev/null;
sudo rm -f "${DST_IMG:?}/${FILE_URL:?}";
sudo rm -f "${DST_IMG:?}/${FILE_IMG:?}";
sudo rm -rf "${DST_NFS_ETH0:?}/${NAME:?}";
sudo sed /etc/fstab -i -e "/${NAME:?}/d"
sudo sed /etc/exports -i -e "/${NAME:?}/d"
if [[ -d "${SRC_IMG:?}" ]] \
&& ( \
! [[ -s "${SRC_IMG:?}/${FILE_IMG:?}" ]] \
|| ! grep -q "${URL}" "${SRC_IMG:?}/${FILE_URL:?}" 2> /dev/null \
|| ([[ "${OPT}" == "timestamping" ]] && ! compare_last_modification_time "${SRC_IMG:?}/${FILE_IMG:?}" "${URL:?}") \
); \
then
echo -e "\e[36m download image to backup location\e[0m";
sudo rm -f "${SRC_IMG:?}/${FILE_URL:?}";
sudo rm -f "${SRC_IMG:?}/${FILE_IMG:?}";
sudo wget -O "${SRC_IMG:?}/${FILE_IMG:?}" "${URL:?}";
sudo sh -c "echo '${URL}' > ${SRC_IMG:?}/${FILE_URL:?}";
sudo touch -r "${SRC_IMG:?}/${FILE_IMG:?}" "${SRC_IMG:?}/${FILE_URL:?}";
fi
}
########################################################################
_unhandle_iso() {
if [[ -z "${1}" ]] || [[ -z "${!1}" ]]; then return 0; fi
####################################################################
# ${!1} : name of VARIABLE -> $VARIABLE, $VARIABLE_URL, $VARIABLE_SUM, $VARIABLE_SUM_TYPE
local NAME="${!1}"
local URL="${1}_URL"; URL="${!URL}"
local SUM="${1}_SUM"; SUM="${!SUM}"
local SUM_TYPE="${1}_SUM_TYPE"; SUM_TYPE="${!SUM_TYPE}"
# ${2..} : optional options
# local OPT=${2}
####################################################################
local FILE_URL="${NAME:?}.url"
local FILE_ISO="${NAME:?}.iso"
####################################################################
echo -e "\e[32m_unhandle_iso(\e[0m${NAME:?}\e[32m)\e[0m";
local timestamping;
local bindfs;
local vblade;
local vblade_shelf;
local vblade_slot;
local fstab_options;
# collect optional options
shift;
while [[ $# -gt 0 ]]; do
case "${1}" in
timestamping)
timestamping=yes;
;;
bindfs)
bindfs=yes;
;;
vbladed)
vblade=yes;
vblade_shelf="${2}";
vblade_slot="${3}";
shift 2;
;;
*)
fstab_options="${1}"
;;
esac
shift;
done
####################################################################
if [[ "${vblade}" == "yes" ]]; then
sudo systemctl stop vblade@$(systemd-escape "${NAME:?}").service &>/dev/null;
sudo rm -f "/etc/vblade.conf.d/${NAME:?}.conf" &>/dev/null;
sudo systemctl daemon-reload &>/dev/null;
sudo systemctl restart vblade.service &>/dev/null;
fi
sudo exportfs -u "*:${DST_NFS_ETH0:?}/${NAME:?}" &>/dev/null;
sudo umount -f "${DST_NFS_ETH0:?}/${NAME:?}" &>/dev/null;
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
sudo rm -f "${DST_ISO:?}/${FILE_ISO:?}";
sudo rm -rf "${DST_NFS_ETH0:?}/${NAME:?}";
sudo sed /etc/fstab -i -e "/${NAME:?}/d"
sudo sed /etc/exports -i -e "/${NAME:?}/d"
}
########################################################################
_unhandle_kernel() {
if [[ -z "${1}" ]] || [[ -z "${!1}" ]]; then return 0; fi
####################################################################
# ${!1} : name of VARIABLE -> $VARIABLE, $VARIABLE_URL, $VARIABLE_SUM, $VARIABLE_SUM_TYPE
local NAME="${!1}"
local URL="${1}_URL"; URL="${!URL}"
local SUM="${1}_SUM"; SUM="${!SUM}"
local SUM_TYPE="${1}_SUM_TYPE"; SUM_TYPE="${!SUM_TYPE}"
####################################################################
local FILE_URL="${NAME:?}.url"
local FILE_KERNEL="${NAME:?}.kernel"
####################################################################
echo -e "\e[32m_unhandle_kernel(\e[0m${NAME:?}\e[32m)\e[0m";
sudo exportfs -u "*:${DST_NFS_ETH0:?}/${NAME:?}" 2> /dev/null;
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
sudo rm -f "${DST_ISO:?}/${FILE_KERNEL:?}";
sudo rm -rf "${DST_NFS_ETH0:?}/${NAME:?}";
sudo sed /etc/exports -i -e "/${NAME:?}/d"
}
########################################################################
_unhandle_zip_img() {
if [[ -z "${1}" ]] || [[ -z "${!1}" ]]; then return 0; fi
####################################################################
# ${!1} : name of VARIABLE -> $VARIABLE, $VARIABLE_URL, $VARIABLE_SUM, $VARIABLE_SUM_TYPE
local NAME="${!1}"
local URL="${1}_URL"; URL="${!URL}"
local SUM="${1}_SUM"; SUM="${!SUM}"
local SUM_TYPE="${1}_SUM_TYPE"; SUM_TYPE="${!SUM_TYPE}"
# ${2} : optional options
local OPT=${2}
####################################################################
local RAW_FILENAME=$(basename "${URL:?}" .zip)
local RAW_FILENAME_ZIP="${RAW_FILENAME:?}.zip"
local NAME_BOOT="${NAME:?}-boot"
local NAME_ROOT="${NAME:?}-root"
local DST_NFS_BOOT="${DST_NFS_ETH0:?}/${NAME_BOOT:?}"
local DST_NFS_ROOT="${DST_NFS_ETH0:?}/${NAME_ROOT:?}"
local FILE_URL="${NAME:?}.url"
local FILE_IMG="${NAME:?}.img"
####################################################################
echo -e "\e[32m_unhandle_zip_img(\e[0m${NAME:?}\e[32m)\e[0m";
## boot
sudo exportfs -u "*:${DST_NFS_BOOT:?}" 2> /dev/null;
sudo umount -f "${DST_NFS_BOOT:?}" 2> /dev/null;
sudo rm -rf "${DST_NFS_BOOT:?}";
sudo sed /etc/fstab -i -e "/${NAME_BOOT:?}/d"
sudo sed /etc/exports -i -e "/${NAME_BOOT:?}/d"
## root
sudo exportfs -u "*:${DST_NFS_ROOT:?}" 2> /dev/null;
sudo umount -f "${DST_NFS_ROOT:?}" 2> /dev/null;
sudo rm -rf "${DST_NFS_ROOT:?}";
sudo sed /etc/fstab -i -e "/${NAME_ROOT:?}/d"
sudo sed /etc/exports -i -e "/${NAME_ROOT:?}/d"
## img
sudo rm -f "${DST_IMG:?}/${FILE_IMG:?}";
sudo rm -f "${DST_IMG:?}/${FILE_URL:?}";
}
########################################################################
handle_img() {
if [[ -z "${1}" ]] || [[ -z "${!1}" ]]; then return 0; fi
####################################################################
# ${!1} : name of VARIABLE -> $VARIABLE, $VARIABLE_URL, $VARIABLE_SUM, $VARIABLE_SUM_TYPE
local NAME="${!1}"
local URL="${1}_URL"; URL="${!URL}"
local SUM="${1}_SUM"; SUM="${!SUM}"
local SUM_TYPE="${1}_SUM_TYPE"; SUM_TYPE="${!SUM_TYPE}"
# ${2..} : optional options
local OPT=${2}
####################################################################
local FILE_URL="${NAME:?}.url"
local FILE_IMG="${NAME:?}.img"
####################################################################
echo -e "\e[32mhandle_img(\e[0m${NAME:?}\e[32m)\e[0m";
if ! [[ -d "${DST_IMG:?}/" ]]; then sudo mkdir -p "${DST_IMG:?}/"; fi
if ! [[ -d "${DST_NFS_ETH0:?}/" ]]; then sudo mkdir -p "${DST_NFS_ETH0:?}/"; fi
sudo exportfs -u "*:${DST_NFS_ETH0:?}/${NAME:?}" 2> /dev/null;
# sudo umount -f "${DST_NFS_ETH0:?}/${NAME:?}" 2> /dev/null;
sudo umount -f "${DST_NFS_ETH0:?}/${NAME:?}";
if [[ -z "${URL}" ]]; then
if ! [[ -s "${DST_IMG:?}/${FILE_IMG:?}" ]] \
&& [[ -s "${SRC_IMG:?}/${FILE_IMG:?}" ]] \
&& [[ -f "${SRC_IMG:?}/${FILE_URL:?}" ]]; \
then
echo -e "\e[36m copy img from usb-stick\e[0m";
sudo rm -f "${FILE_IMG:?}/${FILE_URL:?}";
sudo rsync -xa --info=progress2 "${SRC_IMG:?}/${FILE_IMG:?}" "${DST_IMG:?}";
sudo rsync -xa --info=progress2 "${SRC_IMG:?}/${FILE_URL:?}" "${DST_IMG:?}";
fi
else
if [[ -s "${SRC_IMG:?}/${FILE_IMG:?}" ]] \
&& [[ -f "${SRC_IMG:?}/${FILE_URL:?}" ]] \
&& grep -q "${URL}" "${SRC_IMG:?}/${FILE_URL:?}" 2> /dev/null \
&& ! grep -q "${URL}" "${DST_IMG:?}/${FILE_URL:?}" 2> /dev/null; \
then
echo -e "\e[36m copy img from usb-stick\e[0m";
sudo rm -f "${FILE_IMG:?}/${FILE_URL:?}";
sudo rsync -xa --info=progress2 "${SRC_IMG:?}/${FILE_IMG:?}" "${DST_IMG:?}";
sudo rsync -xa --info=progress2 "${SRC_IMG:?}/${FILE_URL:?}" "${DST_IMG:?}";
fi
if ! [[ -s "${DST_IMG:?}/${FILE_IMG:?}" ]] \
|| ! grep -q "${URL}" "${DST_IMG:?}/${FILE_URL:?}" 2> /dev/null \
|| ([[ "${OPT}" == "timestamping" ]] && ! compare_last_modification_time "${DST_IMG:?}/${FILE_URL:?}" "${URL:?}"); \
then
echo -e "\e[36m download image\e[0m";
sudo rm -f "${DST_IMG:?}/${FILE_IMG:?}";
sudo rm -f "${DST_IMG:?}/${FILE_URL:?}";
sudo wget -O "${DST_IMG:?}/${FILE_IMG:?}" "${URL:?}";
sudo sh -c "echo '${URL}' > ${DST_IMG:?}/${FILE_URL:?}";
sudo touch -r "${DST_IMG:?}/${FILE_IMG:?}" "${DST_IMG:?}/${FILE_URL:?}";
fi
fi
if ! [[ -s "${DST_IMG:?}/${FILE_IMG:?}" ]]; then
sudo rm -f "${DST_IMG:?}/${FILE_IMG:?}";
sudo rm -f "${DST_IMG:?}/${FILE_URL:?}";
fi
if [[ -s "${DST_IMG:?}/${FILE_IMG:?}" ]]; then
local OFFSET_PART1=$((512*$(sfdisk -d "${DST_IMG:?}/${FILE_IMG:?}" | grep "${DST_IMG:?}/${FILE_IMG:?}"\1 | awk '{print $4}' | sed 's/,//')))
local SIZE_PART1=$((512*$(sfdisk -d "${DST_IMG:?}/${FILE_IMG:?}" | grep "${DST_IMG:?}/${FILE_IMG:?}"\1 | awk '{print $6}' | sed 's/,//')))
#sfdisk -d "${DST_IMG:?}/${FILE_IMG:?}"
## partition1
if ! [[ -d "${DST_NFS_ETH0:?}/${NAME:?}" ]]; then
echo -e "\e[36m create image folder\e[0m";
sudo mkdir -p "${DST_NFS_ETH0:?}/${NAME:?}";
fi
if ! grep -q "${DST_NFS_ETH0:?}/${NAME:?}" /etc/fstab; then
echo -e "\e[36m add image to fstab\e[0m";
sudo sh -c "echo '${DST_IMG:?}/${FILE_IMG:?} ${DST_NFS_ETH0:?}/${NAME:?} auto ro,nofail,auto,loop,offset=${OFFSET_PART1:?},sizelimit=${SIZE_PART1:?} 0 11' >> /etc/fstab";
fi
if ! grep -q "${DST_NFS_ETH0:?}/${NAME:?}" /etc/exports; then
echo -e "\e[36m add image folder to exports\e[0m";
sudo sh -c "echo '${DST_NFS_ETH0:?}/${NAME:?} *(ro,async,no_subtree_check,root_squash,mp,fsid=$(uuid))' >> /etc/exports";
fi
sudo mount "${DST_NFS_ETH0:?}/${NAME:?}";
sudo exportfs "*:${DST_NFS_ETH0:?}/${NAME:?}";
else
## partition1
sudo sed /etc/fstab -i -e "/${NAME:?}/d"
sudo sed /etc/exports -i -e "/${NAME:?}/d"
fi
}
########################################################################
handle_iso() {
if [[ -z "${1}" ]] || [[ -z "${!1}" ]]; then return 0; fi
####################################################################
# ${!1} : name of VARIABLE -> $VARIABLE, $VARIABLE_URL, $VARIABLE_SUM, $VARIABLE_SUM_TYPE
local NAME="${!1}"
local URL="${1}_URL"; URL="${!URL}"
local SUM="${1}_SUM"; SUM="${!SUM}"
local SUM_TYPE="${1}_SUM_TYPE"; SUM_TYPE="${!SUM_TYPE}"
# ${2..} : optional options
# local OPT=${2}
####################################################################
local FILE_URL="${NAME:?}.url"
local FILE_ISO="${NAME:?}.iso"
local DST_ORIGINAL="/srv/tmp/original/${NAME:?}"
####################################################################
echo -e "\e[32mhandle_iso(\e[0m${NAME:?}\e[32m)\e[0m";
local timestamping;
local bindfs;
local vblade;
local vblade_shelf;
local vblade_slot;
local fstab_options;
# collect optional options
shift;
while [[ $# -gt 0 ]]; do
case "${1}" in
timestamping)
timestamping=yes;
echo -e "\e[36m timestamping\e[0m";
;;
bindfs)
bindfs=yes;
echo -e "\e[36m bindfs\e[0m";
;;
vbladed)
vblade=yes;
vblade_shelf="${2}";
vblade_slot="${3}";
shift 2;
echo -e "\e[36m vblade_shelf=${vblade_shelf}, vblade_slot=${vblade_slot}\e[0m";
;;
*)
fstab_options="${1}"
echo -e "\e[36m fstab_options=${fstab_options}\e[0m";
;;
esac
shift;
done
####################################################################
if ! [[ -d "${DST_ISO:?}/" ]]; then sudo mkdir -p "${DST_ISO:?}/"; fi
if ! [[ -d "${DST_NFS_ETH0:?}/" ]]; then sudo mkdir -p "${DST_NFS_ETH0:?}/"; fi
if [[ "${vblade}" == "yes" ]]; then
sudo systemctl stop vblade@$(systemd-escape "${NAME:?}").service &>/dev/null;
fi
sudo exportfs -u "*:${DST_NFS_ETH0:?}/${NAME:?}" &>/dev/null;
sudo umount -f "${DST_NFS_ETH0:?}/${NAME:?}" &>/dev/null;
if [[ -z "${URL}" ]]; then
if ! [[ -s "${DST_ISO:?}/${FILE_ISO:?}" ]] \
&& [[ -s "${SRC_ISO:?}/${FILE_ISO:?}" ]] \
&& [[ -f "${SRC_ISO:?}/${FILE_URL:?}" ]]; \
then
echo -e "\e[36m copy iso from usb-stick\e[0m";
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
sudo rsync -xa --info=progress2 "${SRC_ISO:?}/${FILE_ISO:?}" "${DST_ISO:?}";
sudo rsync -xa --info=progress2 "${SRC_ISO:?}/${FILE_URL:?}" "${DST_ISO:?}";
fi
else
if [[ -s "${SRC_ISO:?}/${FILE_ISO:?}" ]] \
&& [[ -f "${SRC_ISO:?}/${FILE_URL:?}" ]] \
&& grep -q "${URL}" "${SRC_ISO:?}/${FILE_URL:?}" 2> /dev/null \
&& ! grep -q "${URL}" "${DST_ISO:?}/${FILE_URL:?}" 2> /dev/null; \
then
echo -e "\e[36m copy iso from usb-stick\e[0m";
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
sudo rsync -xa --info=progress2 "${SRC_ISO:?}/${FILE_ISO:?}" "${DST_ISO:?}";
sudo rsync -xa --info=progress2 "${SRC_ISO:?}/${FILE_URL:?}" "${DST_ISO:?}";
fi
if ! [[ -s "${DST_ISO:?}/${FILE_ISO:?}" ]] \
|| ! grep -q "${URL}" "${DST_ISO:?}/${FILE_URL:?}" 2> /dev/null \
|| ([[ "${timestamping}" == "yes" ]] && ! compare_last_modification_time "${DST_ISO:?}/${FILE_ISO:?}" "${URL:?}"); \
then
echo -e "\e[36m download iso image\e[0m";
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
sudo rm -f "${DST_ISO:?}/${FILE_ISO:?}";
sudo wget -O "${DST_ISO:?}/${FILE_ISO:?}" "${URL:?}";
sudo sh -c "echo '${URL}' > ${DST_ISO:?}/${FILE_URL:?}";
sudo touch -r "${DST_ISO:?}/${FILE_ISO:?}" "${DST_ISO:?}/${FILE_URL:?}";
fi
fi
if ! [[ -s "${DST_ISO:?}/${FILE_ISO:?}" ]]; then
sudo rm -f "${DST_ISO:?}/${FILE_ISO:?}";
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
fi
if [[ -s "${DST_ISO:?}/${FILE_ISO:?}" ]]; then
if ! [[ -d "${DST_NFS_ETH0:?}/${NAME:?}" ]]; then
echo -e "\e[36m create nfs folder\e[0m";
sudo mkdir -p "${DST_NFS_ETH0:?}/${NAME:?}";
fi
if ! grep -q "${DST_NFS_ETH0:?}/${NAME:?}" /etc/fstab; then
echo -e "\e[36m add iso image to fstab\e[0m";
sudo sh -c "echo '${DST_ISO:?}/${FILE_ISO:?} ${DST_NFS_ETH0:?}/${NAME:?} auto ro,nofail,auto,loop${fstab_options} 0 10' >> /etc/fstab";
fi
if ! grep -q "${DST_NFS_ETH0:?}/${NAME:?}" /etc/exports; then
echo -e "\e[36m add nfs folder to exports\e[0m";
sudo sh -c "echo '${DST_NFS_ETH0:?}/${NAME:?} *(ro,async,no_subtree_check,root_squash,mp,fsid=$(uuid))' >> /etc/exports";
fi
sudo mount "${DST_NFS_ETH0:?}/${NAME:?}";
sudo exportfs "*:${DST_NFS_ETH0:?}/${NAME:?}";
if [[ "${vblade}" == "yes" ]]; then
echo -e "\e[36m setup vblade-persistence\e[0m";
##cat << EOF | sudo tee "/etc/vblade.conf.d/e${vblade_shelf}${vblade_slot}.conf" &>/dev/null
cat << EOF | sudo tee "/etc/vblade.conf.d/${NAME:?}.conf" &>/dev/null
shelf=${vblade_shelf}
slot=${vblade_slot}
netif=${INTERFACE_ETH0:?}
filename=${DST_ISO:?}/${FILE_ISO:?}
options='-r'
ionice='--class best-effort --classdata 7'
EOF
sudo systemctl daemon-reload;
sudo systemctl restart vblade.service;
fi
else
sudo sed /etc/fstab -i -e "/${NAME:?}/d"
sudo sed /etc/exports -i -e "/${NAME:?}/d"
if [[ "${vblade}" == "yes" ]]; then
sudo systemctl stop vblade@$(systemd-escape "${NAME:?}").service &>/dev/null;
sudo rm -f "/etc/vblade.conf.d/${NAME:?}.conf" &>/dev/null;
sudo systemctl daemon-reload &>/dev/null;
sudo systemctl restart vblade.service &>/dev/null;
fi
fi
}
########################################################################
handle_zip_img() {
if [[ -z "${1}" ]] || [[ -z "${!1}" ]]; then return 0; fi
####################################################################
# ${!1} : name of VARIABLE -> $VARIABLE, $VARIABLE_URL, $VARIABLE_SUM, $VARIABLE_SUM_TYPE
local NAME="${!1}"
local URL="${1}_URL"; URL="${!URL}"
local SUM="${1}_SUM"; SUM="${!SUM}"
local SUM_TYPE="${1}_SUM_TYPE"; SUM_TYPE="${!SUM_TYPE}"
# ${2} : optional options
local OPT=${2}
####################################################################
local RAW_FILENAME=$(basename "${URL:?}" .zip)
local RAW_FILENAME_ZIP="${RAW_FILENAME:?}.zip"
local NAME_BOOT="${NAME:?}-boot"
local NAME_ROOT="${NAME:?}-root"
local DST_NFS_BOOT="${DST_NFS_ETH0:?}/${NAME_BOOT:?}"
local DST_NFS_ROOT="${DST_NFS_ETH0:?}/${NAME_ROOT:?}"
local FILE_URL="${NAME:?}.url"
local FILE_IMG="${NAME:?}.img"
####################################################################
echo -e "\e[32mhandle_zip_img(\e[0m${NAME:?}\e[32m)\e[0m";
if ! [[ -d "${DST_IMG:?}/" ]]; then sudo mkdir -p "${DST_IMG:?}/"; fi
if ! [[ -d "${DST_NFS_ETH0:?}/" ]]; then sudo mkdir -p "${DST_NFS_ETH0:?}/"; fi
sudo exportfs -u "*:${DST_NFS_BOOT:?}" 2> /dev/null;
sudo umount -f "${DST_NFS_BOOT:?}" 2> /dev/null;
sudo exportfs -u "*:${DST_NFS_ROOT:?}" 2> /dev/null;
sudo umount -f "${DST_NFS_ROOT:?}" 2> /dev/null;
if [[ -z "${URL}" ]]; then
if ! [[ -s "${DST_IMG:?}/${FILE_IMG:?}" ]] \
&& [[ -s "${SRC_IMG:?}/${FILE_IMG:?}" ]] \
&& [[ -f "${SRC_IMG:?}/${FILE_URL:?}" ]]; \
then
echo -e "\e[36m copy img from usb-stick\e[0m";
sudo rm -f "${FILE_IMG:?}/${FILE_URL:?}";
sudo rsync -xa --info=progress2 "${SRC_IMG:?}/${FILE_IMG:?}" "${DST_IMG:?}";
sudo rsync -xa --info=progress2 "${SRC_IMG:?}/${FILE_URL:?}" "${DST_IMG:?}";
fi
else
if [[ -s "${SRC_IMG:?}/${FILE_IMG:?}" ]] \
&& [[ -f "${SRC_IMG:?}/${FILE_URL:?}" ]] \
&& grep -q "${URL}" "${SRC_IMG:?}/${FILE_URL:?}" 2> /dev/null \
&& ! grep -q "${URL}" "${DST_IMG:?}/${FILE_URL:?}" 2> /dev/null; \
then
echo -e "\e[36m copy img from usb-stick\e[0m";
sudo rm -f "${FILE_IMG:?}/${FILE_URL:?}";
sudo rsync -xa --info=progress2 "${SRC_IMG:?}/${FILE_IMG:?}" "${DST_IMG:?}";
sudo rsync -xa --info=progress2 "${SRC_IMG:?}/${FILE_URL:?}" "${DST_IMG:?}";
fi
if ! [[ -s "${DST_IMG:?}/${FILE_IMG:?}" ]] \
|| ! grep -q "${URL}" "${DST_IMG:?}/${FILE_URL:?}" 2> /dev/null \
|| ([[ "${OPT}" == "timestamping" ]] && ! compare_last_modification_time "${DST_IMG:?}/${FILE_URL:?}" "${URL:?}"); \
then
echo -e "\e[36m download image\e[0m";
sudo rm -f "${DST_IMG:?}/${FILE_IMG:?}";
sudo rm -f "${DST_IMG:?}/${FILE_URL:?}";
sudo wget -O "${DST_IMG:?}/${RAW_FILENAME_ZIP:?}" "${URL:?}";
sudo sh -c "echo '${URL}' > ${DST_IMG:?}/${FILE_URL:?}";
sudo touch -r "${DST_IMG:?}/${RAW_FILENAME_ZIP:?}" "${DST_IMG:?}/${FILE_URL:?}";
echo -e "\e[36m extract image\e[0m";
sudo unzip "${DST_IMG:?}/${RAW_FILENAME_ZIP:?}" -d "${DST_IMG:?}" > /tmp/output.tmp;
sudo rm -f "${DST_IMG:?}/${RAW_FILENAME_ZIP:?}";
local RAW_FILENAME_IMG=$(grep 'inflating' /tmp/output.tmp | cut -d':' -f2 | xargs basename)
sudo mv "${DST_IMG:?}/${RAW_FILENAME_IMG:?}" "${DST_IMG:?}/${FILE_IMG:?}";
rm /tmp/output.tmp
fi
fi
if ! [[ -s "${DST_IMG:?}/${FILE_IMG:?}" ]]; then
sudo rm -f "${DST_IMG:?}/${FILE_IMG:?}";
sudo rm -f "${DST_IMG:?}/${FILE_URL:?}";
fi
if [[ -s "${DST_IMG:?}/${FILE_IMG:?}" ]] && [[ $(sfdisk -d ${DST_IMG:?}/${FILE_IMG:?}) ]]; then
local OFFSET_BOOT=$((512*$(sfdisk -d ${DST_IMG:?}/${FILE_IMG:?} | grep ${DST_IMG:?}/${FILE_IMG:?}\1 | awk '{print $4}' | sed 's/,//')))
local SIZE_BOOT=$((512*$(sfdisk -d ${DST_IMG:?}/${FILE_IMG:?} | grep ${DST_IMG:?}/${FILE_IMG:?}\1 | awk '{print $6}' | sed 's/,//')))
local OFFSET_ROOT=$((512*$(sfdisk -d ${DST_IMG:?}/${FILE_IMG:?} | grep ${DST_IMG:?}/${FILE_IMG:?}\2 | awk '{print $4}' | sed 's/,//')))
local SIZE_ROOT=$((512*$(sfdisk -d ${DST_IMG:?}/${FILE_IMG:?} | grep ${DST_IMG:?}/${FILE_IMG:?}\2 | awk '{print $6}' | sed 's/,//')))
#sfdisk -d "${DST_IMG:?}/${FILE_IMG:?}"
sudo sed /etc/fstab -i -e "/${NAME_BOOT:?}/d"
sudo sed /etc/fstab -i -e "/${NAME_ROOT:?}/d"
## boot
if ! [[ -d "${DST_NFS_BOOT:?}" ]]; then
echo -e "\e[36m create image-boot folder\e[0m";
sudo mkdir -p "${DST_NFS_BOOT:?}";
fi
if ! grep -q "${DST_NFS_BOOT:?}" /etc/fstab; then
echo -e "\e[36m add image-boot to fstab\e[0m";
sudo sh -c "echo '${DST_IMG:?}/${FILE_IMG:?} ${DST_NFS_BOOT:?} auto ro,nofail,auto,loop,offset=${OFFSET_BOOT:?},sizelimit=${SIZE_BOOT:?} 0 11' >> /etc/fstab";
fi
if ! grep -q "${DST_NFS_BOOT:?}" /etc/exports; then
echo -e "\e[36m add image-boot folder to exports\e[0m";
sudo sh -c "echo '${DST_NFS_BOOT:?} *(ro,async,no_subtree_check,root_squash,mp,fsid=$(uuid))' >> /etc/exports";
fi
## root
if ! [[ -d "${DST_NFS_ROOT:?}" ]]; then
echo -e "\e[36m create image-root folder\e[0m";
sudo mkdir -p "${DST_NFS_ROOT:?}";
fi
if ! grep -q "${DST_NFS_ROOT:?}" /etc/fstab; then
echo -e "\e[36m add image-root to fstab\e[0m";
sudo sh -c "echo '${DST_IMG:?}/${FILE_IMG:?} ${DST_NFS_ROOT:?} auto ro,nofail,auto,loop,offset=${OFFSET_ROOT:?},sizelimit=${SIZE_ROOT:?} 0 11' >> /etc/fstab";
fi
if ! grep -q "${DST_NFS_ROOT:?}" /etc/exports; then
echo -e "\e[36m add image-root folder to exports\e[0m";
sudo sh -c "echo '${DST_NFS_ROOT:?} *(ro,async,no_subtree_check,root_squash,mp,fsid=$(uuid))' >> /etc/exports";
fi
sudo mount "${DST_NFS_BOOT:?}";
sudo exportfs "*:${DST_NFS_BOOT:?}";
sudo mount "${DST_NFS_ROOT:?}";
sudo exportfs "*:${DST_NFS_ROOT:?}";
else
## boot
sudo sed /etc/fstab -i -e "/${NAME_BOOT:?}/d"
sudo sed /etc/exports -i -e "/${NAME_BOOT:?}/d"
## root
sudo sed /etc/fstab -i -e "/${NAME_ROOT:?}/d"
sudo sed /etc/exports -i -e "/${NAME_ROOT:?}/d"
fi
}
########################################################################
handle_kernel() {
if [[ -z "${1}" ]] || [[ -z "${!1}" ]]; then return 0; fi
####################################################################
# ${!1} : name of VARIABLE -> $VARIABLE, $VARIABLE_URL, $VARIABLE_SUM, $VARIABLE_SUM_TYPE
local NAME="${!1}"
local URL="${1}_URL"; URL="${!URL}"
local SUM="${1}_SUM"; SUM="${!SUM}"
local SUM_TYPE="${1}_SUM_TYPE"; SUM_TYPE="${!SUM_TYPE}"
# ${2} : optional options
local OPT=${2}
####################################################################
local FILE_URL="${NAME:?}.url"
local FILE_KERNEL="${NAME:?}.kernel"
local DST_ORIGINAL="/srv/tmp/original/${NAME:?}"
####################################################################
echo -e "\e[32mhandle_kernel(\e[0m${NAME:?}\e[32m)\e[0m";
if ! [[ -d "${DST_NFS_ETH0:?}/" ]]; then sudo mkdir -p "${DST_NFS_ETH0:?}/"; fi
sudo exportfs -u "*:${DST_NFS_ETH0:?}/${NAME:?}" 2> /dev/null;
if [[ -z "${URL}" ]]; then
if ! [[ -s "${DST_ISO:?}/${FILE_KERNEL:?}" ]] \
&& [[ -s "${SRC_ISO:?}/${FILE_KERNEL:?}" ]] \
&& [[ -f "${SRC_ISO:?}/${FILE_URL:?}" ]]; \
then
echo -e "\e[36m copy kernel from usb-stick\e[0m";
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
sudo rsync -xa --info=progress2 "${SRC_ISO:?}/${FILE_KERNEL:?}" "${DST_ISO:?}";
sudo rsync -xa --info=progress2 "${SRC_ISO:?}/${FILE_URL:?}" "${DST_ISO:?}";
fi
else
if [[ -s "${SRC_ISO:?}/${FILE_KERNEL:?}" ]] \
&& [[ -f "${SRC_ISO:?}/${FILE_URL:?}" ]] \
&& grep -q "${URL}" "${SRC_ISO:?}/${FILE_URL:?}" 2> /dev/null \
&& ! grep -q "${URL}" "${DST_ISO:?}/${FILE_URL:?}" 2> /dev/null; \
then
echo -e "\e[36m copy kernel from usb-stick\e[0m";
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
sudo rsync -xa --info=progress2 "${SRC_ISO:?}/${FILE_KERNEL:?}" "${DST_ISO:?}";
sudo rsync -xa --info=progress2 "${SRC_ISO:?}/${FILE_URL:?}" "${DST_ISO:?}";
fi
if ! [[ -s "${DST_ISO:?}/${FILE_KERNEL:?}" ]] \
|| ! grep -q "${URL}" "${DST_ISO:?}/${FILE_URL:?}" 2> /dev/null \
|| ([[ "${OPT}" == "timestamping" ]] && ! compare_last_modification_time "${DST_ISO:?}/${FILE_KERNEL:?}" "${URL:?}"); \
then
echo -e "\e[36m download kernel image\e[0m";
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
sudo rm -f "${DST_ISO:?}/${FILE_KERNEL:?}";
sudo wget -O "${DST_ISO:?}/${FILE_KERNEL:?}" "${URL:?}";
sudo sh -c "echo '${URL}' > ${DST_ISO:?}/${FILE_URL:?}";
sudo touch -r "${DST_ISO:?}/${FILE_KERNEL:?}" "${DST_ISO:?}/${FILE_URL:?}";
fi
fi
if ! [[ -s "${DST_ISO:?}/${FILE_KERNEL:?}" ]]; then
sudo rm -f "${DST_ISO:?}/${FILE_KERNEL:?}";
sudo rm -f "${DST_ISO:?}/${FILE_URL:?}";
fi
if [[ -s "${DST_ISO:?}/${FILE_KERNEL:?}" ]]; then
if ! [[ -d "${DST_NFS_ETH0:?}/${NAME:?}" ]]; then
echo -e "\e[36m create nfs folder\e[0m";
sudo mkdir -p "${DST_NFS_ETH0:?}/${NAME:?}";
fi
sudo cp "${DST_ISO:?}/${FILE_KERNEL:?}" "${DST_NFS_ETH0:?}/${NAME:?}/kernel"
if ! grep -q "${DST_NFS_ETH0:?}/${NAME:?}" /etc/exports; then
echo -e "\e[36m add nfs folder to exports\e[0m";
sudo sh -c "echo '${DST_NFS_ETH0:?}/${NAME:?} *(ro,async,no_subtree_check,root_squash,mp,fsid=$(uuid))' >> /etc/exports";
fi
sudo exportfs "*:${DST_NFS_ETH0:?}/${NAME:?}";
else
sudo sed /etc/exports -i -e "/${NAME:?}/d"
fi
}
########################################################################
handle_item() {
local ITEM_ACTION="${1}"
local ITEM_TYPE="${2}"
shift;
shift;
case "${ITEM_ACTION:?}" in
'+')
case "${ITEM_TYPE:?}" in
img)
handle_img $*;
;;
iso)
handle_iso $*;
;;
kernel)
handle_kernel $*;
;;
zip_img)
handle_zip_img $*;
;;
rpi_pxe)
handle_rpi_pxe $*;
;;
*)
;;
esac
;;
'-')
case "${ITEM_TYPE:?}" in
img)
_unhandle_img $*;
;;
iso)
_unhandle_iso $*;
;;
kernel)
_unhandle_kernel $*;
;;
zip_img)
_unhandle_zip_img $*;
;;
rpi_pxe)
#_unhandle_rpi_pxe $*;
;;
*)
;;
esac
;;
'#')
;;
*)
;;
esac
}
########################################################################
handle_ipxe() {
echo -e "\e[32mhandle_ipxe()\e[0m";
local FILE_MENU="${DST_TFTP_ETH0:?}/${DST_IPXE:?}/menu.ipxe"
local FILE_BASE="http://${IP_ETH0:?}/srv"
if ! [[ -d "${DST_TFTP_ETH0:?}/${DST_IPXE:?}" ]]; then sudo mkdir -p "${DST_TFTP_ETH0:?}/${DST_IPXE:?}"; fi
####################################################################
# http://ipxe.org/docs
# http://ipxe.org/howto/chainloading
####################################################################
if (! compare_last_modification_time "${DST_TFTP_ETH0:?}/${DST_IPXE:?}/ipxe.efi" https://boot.ipxe.org/ipxe.efi); then
echo -e "\e[36m download iPXE stuff\e[0m";
sudo wget --quiet -O "${DST_TFTP_ETH0:?}/${DST_IPXE:?}/ipxe.efi" https://boot.ipxe.org/ipxe.efi;
sudo wget --quiet -O "${DST_TFTP_ETH0:?}/${DST_IPXE:?}/undionly.kpxe" https://boot.ipxe.org/undionly.kpxe;
[[ -f "${DST_TFTP_ETH0:?}/${DST_IPXE:?}/wimboot" ]] || (\
wget -O /tmp/wimboot.tar.gz https://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz; \
tar -xf /tmp/wimboot.tar.gz --wildcards *wimboot -O | sudo tee "${DST_TFTP_ETH0:?}/${DST_IPXE:?}/wimboot" &>/dev/null);
sudo wget --quiet -O "${DST_TFTP_ETH0:?}/${DST_IPXE:?}/ipxe.pxe" https://boot.ipxe.org/ipxe.pxe;
sudo wget --quiet -O "${DST_TFTP_ETH0:?}/${DST_IPXE:?}/ipxe.iso" https://boot.ipxe.org/ipxe.iso;
fi
sudo touch "${FILE_MENU:?}"
. "${script_dir:?}/p2-include-menu" ipxe
}
########################################################################
handle_pxe_menu() {
# $1 : menu short name
# $2 : menu file name
local MENU_PXE="${1}"
local FILE_NAME="${2}"
####################################################################
local FILE_MENU="${DST_TFTP_ETH0:?}/${MENU_PXE:?}/pxelinux.cfg/${FILE_NAME:?}"
local FILE_BASE="http://${IP_ETH0:?}/srv"
####################################################################
## INFO:
## The entry before -- means that it will be used by the live system / the installer
## The entry after -- means that it will be carried to and used by the installed system
## https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/admin-guide/kernel-parameters.txt
##
## some debian/ubuntu parameter
## https://www.debian.org/releases/stretch/example-preseed.txt
## https://www.debian.org/releases/stretch/amd64/apb.html.en
## https://www.debian.org/releases/stretch/amd64/ch05s03.html.en
## https://manpages.debian.org/stretch/live-config-doc/live-config.7.en.html
## http://manpages.ubuntu.com/manpages/precise/man7/live-config.7.html
####################################################################
echo -e "\e[32mhandle_pxe_menu(\e[0m${MENU_PXE:?}\e[32m)\e[0m";
echo -e "\e[36m setup sys menu for pxe\e[0m";
if ! [[ -d "${DST_TFTP_ETH0:?}/${MENU_PXE:?}/pxelinux.cfg" ]]; then sudo mkdir -p "${DST_TFTP_ETH0:?}/${MENU_PXE:?}/pxelinux.cfg"; fi
if [[ -d "${DST_TFTP_ETH0:?}/${MENU_PXE:?}/pxelinux.cfg" ]]; then
cat << EOF | sudo tee "${FILE_MENU:?}" &>/dev/null
########################################
# ${FILE_MENU:?}
# http://www.syslinux.org/wiki/index.php?title=Menu
UI menu.c32
PROMPT 0
NOESCAPE 1
MENU CLEAR
MENU MARGIN 0
MENU ROWS 17
MENU TABMSGROW 21
MENU COLOR title 1;37;44 * * all
MENU COLOR border 22;36;44 * * std
MENU COLOR scrollbar 22;36;44 * * std
MENU COLOR disabled 22;36;44 * * std
MENU COLOR sel 1;37;41 * * all
MENU COLOR hotsel 1;37;41 * * all
MENU COLOR tabmsg 22;36;44 * * std
MENU TITLE PXE-Linux Boot Menu (${MENU_PXE:?})
MENU TABMSG [Enter]=boot, [Tab]=edit, [Esc]=return
EOF
fi
####################################################################
####################################################################
. "${script_dir:?}/p2-include-menu"
. "${script_dir:?}/c2-custom-menu"
}
########################################################################
handle_pxe() {
echo -e "\e[32mhandle_pxe()\e[0m";
####################################################################
[[ -d "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}" ]] || sudo mkdir -p "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}";
if [[ -d "${SRC_TFTP_ETH0:?}" ]]; then
echo -e "\e[36m copy win-pe stuff\e[0m";
if ! [[ -f "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/pxeboot.n12" ]] && [[ -f "${SRC_TFTP_ETH0:?}/pxeboot.n12" ]]; then sudo rsync -xa --info=progress2 "${SRC_TFTP_ETH0:?}/pxeboot.n12" "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/"; fi
if ! [[ -f "${DST_TFTP_ETH0:?}/bootmgr.exe" ]] && [[ -f "${SRC_TFTP_ETH0:?}/bootmgr.exe" ]]; then sudo rsync -xa --info=progress2 "${SRC_TFTP_ETH0:?}/bootmgr.exe" "${DST_TFTP_ETH0:?}/"; fi
if ! [[ -d "${DST_TFTP_ETH0:?}/Boot" ]] && [[ -d "${SRC_TFTP_ETH0:?}/Boot" ]]; then sudo rsync -xa --info=progress2 "${SRC_TFTP_ETH0:?}/Boot" "${DST_TFTP_ETH0:?}/"; fi
fi
[[ -h "${DST_TFTP_ETH0:?}/sources" ]] || sudo ln -s "${DST_NFS_ETH0:?}/${WIN_PE_X86:?}/sources/" "${DST_TFTP_ETH0:?}/sources";
####################################################################
echo -e "\e[36m setup sys menu files for pxe bios\e[0m";
[[ -d "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}" ]] || sudo mkdir -p "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/lpxelinux.0" ]] || sudo ln -s /usr/lib/PXELINUX/lpxelinux.0 "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/ldlinux.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/bios/ldlinux.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/menu.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/bios/menu.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/libutil.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/bios/libutil.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/poweroff.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/bios/poweroff.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/reboot.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/bios/reboot.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/libcom32.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/bios/libcom32.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/nfs" ]] || sudo ln -s "${DST_NFS_ETH0:?}/" "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/nfs";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/iso" ]] || sudo ln -s "${DST_ISO:?}/" "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/iso";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/memdisk" ]] || sudo ln -s /usr/lib/syslinux/memdisk "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/linux.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/bios/linux.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/";
[[ -f "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/wimboot" ]] || ( \
wget -O /tmp/wimboot.tar.gz https://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz; \
tar -xf /tmp/wimboot.tar.gz --wildcards *wimboot -O | sudo tee "${DST_TFTP_ETH0:?}/${DST_PXE_BIOS:?}/wimboot" > /dev/null);
handle_pxe_menu "${DST_PXE_BIOS:?}" default;
####################################################################
echo -e "\e[36m setup sys menu files for pxe efi32\e[0m";
[[ -d "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}" ]] || sudo mkdir -p "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/bootia32.efi" ]] || sudo ln -s /usr/lib/SYSLINUX.EFI/efi32/syslinux.efi "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/bootia32.efi";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/ldlinux.e32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi32/ldlinux.e32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/menu.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi32/menu.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/libutil.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi32/libutil.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/poweroff.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi32/poweroff.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/reboot.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi32/reboot.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/libcom32.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi32/libcom32.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/nfs" ]] || sudo ln -s "${DST_NFS_ETH0:?}/" "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/nfs";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/iso" ]] || sudo ln -s "${DST_ISO:?}/" "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/iso";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/linux.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi32/linux.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/";
[[ -f "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/wimboot" ]] || ( \
wget -O /tmp/wimboot.tar.gz https://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz; \
tar -xf /tmp/wimboot.tar.gz --wildcards *wimboot -O | sudo tee "${DST_TFTP_ETH0:?}/${DST_PXE_EFI32:?}/wimboot" &>/dev/null);
handle_pxe_menu "${DST_PXE_EFI32:?}" default;
####################################################################
echo -e "\e[36m setup sys menu files for pxe efi64\e[0m";
[[ -d "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}" ]] || sudo mkdir -p "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/bootx64.efi" ]] || sudo ln -s /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/bootx64.efi";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/ldlinux.e64" ]] || sudo ln -s /usr/lib/syslinux/modules/efi64/ldlinux.e64 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/menu.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi64/menu.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/libutil.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi64/libutil.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/reboot.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi64/reboot.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/libcom32.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi64/libcom32.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/nfs" ]] || sudo ln -s "${DST_NFS_ETH0:?}/" "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/nfs";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/iso" ]] || sudo ln -s "${DST_ISO:?}/" "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/iso";
[[ -h "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/linux.c32" ]] || sudo ln -s /usr/lib/syslinux/modules/efi64/linux.c32 "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/";
[[ -f "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/wimboot" ]] || (\
wget -O /tmp/wimboot.tar.gz https://git.ipxe.org/releases/wimboot/wimboot-latest.tar.gz; \
tar -xf /tmp/wimboot.tar.gz --wildcards *wimboot -O | sudo tee "${DST_TFTP_ETH0:?}/${DST_PXE_EFI64:?}/wimboot" &>/dev/null);
handle_pxe_menu "${DST_PXE_EFI64:?}" default;
}
########################################################################
handle_rpi_pxe_customization() {
echo -e "\e[36m handle_rpi_pxe_customization()\e[0m";
####################################################################
local DST_CUSTOM_BOOT="${1:?}"
local DST_CUSTOM_ROOT="${2:?}"
local FLAGS="${3:?}"
####################################################################
if (echo "${FLAGS:?}" | grep -q redo); then
################################################################
if (echo "${FLAGS:?}" | grep -q cmdline); then
echo -e "\e[36m add cmdline file\e[0m";
sudo sh -c "echo 'dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 plymouth.ignore-serial-consoles root=/dev/nfs nfsroot=${IP_ETH0:?}:${DST_NFS_ROOT:?},vers=3 rootwait rw ip=dhcp elevator=deadline net.ifnames=0 consoleblank=0' > ${DST_CUSTOM_BOOT:?}/cmdline.txt";
fi
################################################################
if (echo "${FLAGS:?}" | grep -q config); then
echo -e "\e[36m add config file\e[0m";
cat << EOF | sudo tee "${DST_CUSTOM_BOOT:?}/config.txt" &>/dev/null
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2
[all]
#dtoverlay=vc4-fkms-v3d