-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinuxrcd_builder.sh
executable file
·1007 lines (864 loc) · 42.7 KB
/
linuxrcd_builder.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/sudo /bin/bash
# Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 nerdopolis (or n3rdopolis) <[email protected]>
#
# This file is part of LinuxRCD
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#This function retrives the PID, and some of the child PIDs, to eventually get the pid 1 of the namespace for the build
function GetJobPIDs
{
if [[ -d /proc/$SUBSHELLPID ]]
then
PYTHONPID=$(pgrep -oP $SUBSHELLPID 2>/dev/null)
else
ROOTPID=-1
return
fi
if [[ -d /proc/$PYTHONPID ]]
then
UNSHAREPID=$(pgrep -oP $PYTHONPID 2>/dev/null)
else
ROOTPID=-1
return
fi
if [[ -d /proc/$UNSHAREPID ]]
then
ROOTPID=$(pgrep -oP $UNSHAREPID 2>/dev/null)
else
ROOTPID=-1
fi
}
#This function takes the same arguments as echo, behaves like echo, except all the text is saved in a variable to be written to a log later
function echolog
{
LOGTEXTADD=$(echo "$@")
LOGTEXT+=$LOGTEXTADD
if [[ $1 != -n ]]
then
LOGTEXT+=$'\n'
fi
echo "$@"
}
#This function takes the same arguments as echo, like echolog, but then writes to a failure log, and exits the script
#THis is for when the script itself cannot initailize phase0, 1, 2, or 3, not for when an ISO fails to build
function faillog
{
echolog "$@"
echo "$LOGTEXT" > "$BUILDLOCATION"/logs/failedlogs/Failed_"$STARTDATE"_"$BUILDARCH".log
rm "$BUILDLOCATION"/logs/failedlogs/latest-failedlog_"$BUILDARCH".log 2>/dev/null
ln -s "$BUILDLOCATION"/logs/failedlogs/Failed_"$STARTDATE"_"$BUILDARCH".log "$BUILDLOCATION"/logs/failedlogs/latest-failedlog_"$BUILDARCH".log
exit 1
}
function setup_buildprocess
{
#unset most varaibles
unset VARLIST
VARLIST=$(env | awk -F = '{print $1}' | grep -Ev "^PATH$|^HOME$|^SUDO_USER$" )
for var in $VARLIST
do
unset "$var" &> /dev/null
done
#Detect the best Python command to use
PYTHONTESTCOMMANDS=(python3 python2 python2.7 python)
for PYTHONTESTCOMMAND in ${PYTHONTESTCOMMANDS[@]}
do
type $PYTHONTESTCOMMAND &> /dev/null
if [[ $? == 0 ]]
then
export PYTHONCOMMAND=$PYTHONTESTCOMMAND
break
fi
done
export TERM=linux
PATH=$(getconf PATH):/sbin:/usr/sbin
export LANG=en_US.UTF-8
STARTDATE=$(date +"%Y-%m-%d_%H-%M-%S")
#If user presses CTRL+C, kill any namespace, remove the lock file, exit the script
trap 'if [[ $BUILD_RUNNING == 0 ]]; then exit 2; fi; if [[ -z $ROOTPID ]]; then GetJobPIDs; fi; if [[ -e /proc/"$ROOTPID" && $ROOTPID != "" && $ROOTPID != -1 ]]; then kill -9 $ROOTPID; rm "$BUILDLOCATION"/build/"$BUILDARCH"/lockfile; echo -e "\nCTRL+C pressed, exiting..."; exit 2; fi' 2
#Handle when the script is resumed
trap 'if [[ -e /proc/"$SUBSHELLPID" && $SUBSHELLPID != "" ]]; then kill -CONT $SUBSHELLPID; fi; if [[ -e /proc/"$ROOTPID" && $ROOTPID != "" && $ROOTPID != -1 ]]; then pkill -CONT --nslist pid --ns $ROOTPID ""; fi' 18
#Stop the background process that the script is waiting on when CTRL+Z is sent
trap 'echo "CTRL+Z pressed, pausing..."; if [[ -z $ROOTPID ]]; then GetJobPIDs; fi; if [[ -e /proc/"$SUBSHELLPID" && $SUBSHELLPID != "" && $ROOTPID != -1 ]]; then kill -STOP $SUBSHELLPID; fi; if [[ -e /proc/"$ROOTPID" && $ROOTPID != "" ]]; then pkill -STOP --nslist pid --ns $ROOTPID ""; fi' 20
}
#Function to start a command and all arguments, starting from the third one, as a command in a seperate PID and mount namespace. The first argument determines if the namespace should have network connectivity or not (1 = have network connectivity, 0 = no network connectivity). The second argument states where the log output will be written.
function NAMESPACE_EXECUTE {
HASNETWORK=$1
shift
LOGFILE="$1"
shift
if [[ $HASNETWORK == 0 ]]
then
UNSHAREFLAGS="-f --pid --mount --net --mount-proc"
else
UNSHAREFLAGS="-f --pid --mount --mount-proc"
fi
#Create the PID and Mount namespaces to start the command in
($PYTHONCOMMAND -c 'import pty, sys; from signal import signal, SIGPIPE, SIG_DFL; signal(SIGPIPE,SIG_DFL); pty.spawn(sys.argv[1:])' bash -c "stty cols 80 rows 24; exec unshare $UNSHAREFLAGS "$@"" |& tee "$LOGFILE" ) &
SUBSHELLPID=$!
#Get the PID of the unshared process, which is pid 1 for the namespace, wait at the very most 1 minute for the process to start, 120 attempts with half 1 second intervals.
#Abort if not started in 1 minute
for (( element = 0 ; element < 120 ; element++ ))
do
read -t .5
GetJobPIDs
if [[ ! -z $ROOTPID ]]
then
break
fi
done
if [[ -z $ROOTPID ]]
then
faillog "The main namespace process failed to start, in 1 minute. This should not take that long"
fi
#Wait for the PID to complete
if [[ $ROOTPID != -1 ]]
then
read < <(tail -f /dev/null --pid=$UNSHAREPID)
fi
unset SUBSHELLPID
unset PYTHONPID
unset UNSHAREPID
unset ROOTPID
}
#Declare most of the script as a function, to protect against the script from any changes when running, from causing the build process to be inconsistant
function run_buildprocess {
BUILD_RUNNING=0
SCRIPTFILEPATH=$(readlink -f "$0")
SCRIPTFOLDERPATH=$(dirname "$SCRIPTFILEPATH")
#Begin config options
HOMELOCATION=$HOME
OLDBUILDROOT=$HOME
BUILDROOT=/var/cache
BUILDFOLDERNAME=LRCD_Build_Files
export BUILDUNIXNAME=linuxrcd
export BUILDFRIENDLYNAME=LinuxRCD
#Values for determining how much free disk/ramdisk space is needed
GIGABYTE=1048576
STORAGESIZE_TOTALSIZE=0
STORAGESIZE_PADDING=$((2 * $GIGABYTE ))
STORAGESIZE_TMPBASEBUILD=$((1 * $GIGABYTE ))
STORAGESIZE_TMPSRCBUILDOVERLAY=$((1 * $GIGABYTE ))
STORAGESIZE_TMPSRCBUILDOFULL=$((1 * $GIGABYTE ))
STORAGESIZE_TMPPHASE3=$((1 * $GIGABYTE ))
STORAGESIZE_TMPREMASTERSYS=$((1 * $GIGABYTE ))
STORAGESIZE_ISOOUT=$((1 * $GIGABYTE ))
STORAGESIZE_BUILDOUTPUT=$((1 * $GIGABYTE ))
STORAGESIZE_PHASE1=$((2 * $GIGABYTE ))
STORAGESIZE_PHASE2=$((3 * $GIGABYTE ))
STORAGESIZE_ARCHIVES=$((1 * $GIGABYTE ))
STORAGESIZE_SRCBUILD=$((1 * $GIGABYTE ))
#End config options
#Move to /var/cache if an existing build folder exists. With systemd-homed and the problems caused by UIDs moved in a migrated home directory, it's best to store in /var/cache
if [[ -d "$OLDBUILDROOT"/$BUILDFOLDERNAME && ! -d "$BUILDROOT"/$BUILDFOLDERNAME ]]
then
echolog "Moving "$OLDBUILDROOT"/$BUILDFOLDERNAME to "$BUILDROOT"/$BUILDFOLDERNAME"
mv "$OLDBUILDROOT"/$BUILDFOLDERNAME "$BUILDROOT"/$BUILDFOLDERNAME
fi
export BUILDLOCATION="$BUILDROOT"/$BUILDFOLDERNAME
#make a folder containing the live cd tools in the users local folder
mkdir -p "$BUILDLOCATION"
#Create the logs folder for any logs the script may need to write if it aborts early
mkdir -p "$BUILDLOCATION"/logs/failedlogs
#switch to that folder
cd "$BUILDLOCATION"
echolog "Build script for "$BUILDFRIENDLYNAME". The build process requires no user interaction, apart from specifing the build architecture, and sending a keystroke to confirm to starting the build process.
"
export BUILDARCH=$(echo $1| awk -F = '{print $2}')
if [[ -z "$BUILDARCH" ]]
then
echolog "Select Arch. Enter 1 for i386, 2 for amd64, 3 for custom. Default=i386."
echolog "The arch can also be selected by passing BUILDARCH=(architecture) as the first argument."
read archselect
if [[ $archselect == 2 ]]
then
export BUILDARCH=amd64
elif [[ $archselect == 3 ]]
then
echolog "Enter custom CPU arch. Please ensure your processor is capable of running the selected architecture."
read BUILDARCH
export BUILDARCH
else
export BUILDARCH=i386
fi
SKIPPROMPT=0
else
SKIPPROMPT=1
fi
#Checkinstall needs overlayfs
HASOVERLAYFS=$(grep -c overlay$ /proc/filesystems)
if [[ $HASOVERLAYFS == 0 ]]
then
HASOVERLAYFSMODULE=$(modprobe -n overlay; echo $?)
if [[ $HASOVERLAYFSMODULE == 0 ]]
then
HASOVERLAYFS=1
else
faillog "Building without overlayfs is no longer supported"
fi
fi
#Detect another instance, by creating a testing a lockfile, which is a symlink to /proc/pid/cmdline, and making sure the second line of /proc/pid/cmdline matches (as it's the path to the script).
ls $(readlink -f "$BUILDLOCATION"/build/"$BUILDARCH"/lockfile) &> /dev/null
result=$?
if [[ $result != 0 || ! -e "$BUILDLOCATION"/build/"$BUILDARCH"/lockfile ]]
then
rm "$BUILDLOCATION"/build/"$BUILDARCH"/lockfile &> /dev/null
"$BUILDLOCATION"/build/"$BUILDARCH"/lockfile 2>/dev/null
ln -s /proc/"$$"/cmdline "$BUILDLOCATION"/build/"$BUILDARCH"/lockfile
else
LockPID=$(readlink -f "$BUILDLOCATION"/build/"$BUILDARCH"/lockfile | awk -F "/" '{print $3}')
faillog "Error: Another instance is already running for $BUILDARCH (pid $LockPID)"
fi
#Create the placeholder for the revisions import, so that it's easy for the user to get the name correct. It is only used if it's more than 0 bytes
if [[ ! -e "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt ]]
then
touch "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt
fi
#Create the placeholder for the list of packages to delete
if [[ ! -e "$BUILDLOCATION"/RestartPackageList_"$BUILDARCH".txt ]]
then
touch "$BUILDLOCATION"/RestartPackageList_"$BUILDARCH".txt
fi
#####Tell User what script does
echolog "
The following files will be generated by the script. The listed files will be overwritten. (file names and folder names are case sensitive)
Folder: $BUILDLOCATION
File: ${HOMELOCATION}/"$BUILDFRIENDLYNAME"_"$BUILDARCH".iso
File: ${HOMELOCATION}/"$BUILDFRIENDLYNAME"_Revisions_"$BUILDARCH".txt
File: ${HOMELOCATION}/"$BUILDFRIENDLYNAME"_Source_"$BUILDARCH".tar.gz
"
echolog "----------------------------------------------------------------------------------------
If you want to build revisions specified in a list file from a previous build, overwrite
"$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt
with the requested revisions file generated from a previous build, or a downloaded instance.
You may also specify a list of pacakges in
"$BUILDLOCATION"/RestartPackageList_"$BUILDARCH".txt
to batch restart, one package per line.
Although the files have the CPU architecture as the suffix in the file name, there is nothing CPU dependant in them, and the suffix only exists to identify them.
For example buildcore_revisions_amd64.txt can be used in "$BUILDLOCATION"/buildcore_revisions_i386.txt
and
RestartPackageList_amd64.txt can be used in RestartPackageList_i386.txt
Ensure the file(s) are copied, and not moved, as they are treated as a one time control file, and deleted after the next run.
-----------------------------------------------------------------------------------------------------------------------------"
if [[ $SKIPPROMPT != 1 ]]
then
echolog "Most users can ignore this message. Press Enter to continue..."
read wait
else
echolog "Most users can ignore this message. The build process will start in 5 seconds"
sleep 5
fi
STARTTIME=$(date +%s)
PREPARE_STARTTIME=$(date +%s)
#prepare debootstrap
if [[ ! -e "$BUILDLOCATION"/debootstrap/debootstrap || ! -e "$BUILDLOCATION"/DontDownloadDebootstrapScript ]]
then
echolog "Control file for debootstrap removed, or non existing. Deleting downloaded debootstrap folder"
touch "$BUILDLOCATION"/DontDownloadDebootstrapScript
rm -rf "$BUILDLOCATION"/debootstrap/*
rm "$BUILDLOCATION"/debootstrap/debootstrap.tar.gz
mkdir -p "$BUILDLOCATION"/debootstrap
DEBOOTSTRAPURL=$(wget -O - http://packages.debian.org/source/sid/debootstrap 2>/dev/null|grep .tar.gz | awk -F \" '{print $2}')
wget "$DEBOOTSTRAPURL" -O "$BUILDLOCATION"/debootstrap/debootstrap.tar.gz
tar xaf "$BUILDLOCATION"/debootstrap/debootstrap.tar.gz -C "$BUILDLOCATION"/debootstrap --strip 1
make -C "$BUILDLOCATION"/debootstrap/ devices.tar.gz
fi
#If debootstrap fails
if [[ ! -e "$BUILDLOCATION"/debootstrap/debootstrap ]]
then
faillog "Download of debootstrap failed, this script needs to be able to download debootstrap from ftp.debian.org in order to be able to continue."
fi
#Determine how much free disk space is neeed
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_PADDING))
#Determine how much space is in use by the ISOs already, and take that value away from the space allocated towards the output ISO, and append it
CURRENT_ISOOUT=$(du -c "$HOMELOCATION"/"$BUILDFRIENDLYNAME"*_"$BUILDARCH".iso 2>/dev/null | tail -1 | awk '{print $1}')
((STORAGESIZE_TOTALSIZE_OUTPUT=(STORAGESIZE_ISOOUT-CURRENT_ISOOUT)))
#ISOs will be built in the build location, and moved out into the destination directory. Account for the temporary space used
((STORAGESIZE_TOTALSIZE+=(STORAGESIZE_ISOOUT-CURRENT_ISOOUT)))
echolog "Starting the build process..."
REBUILT="to update"
#Delete any stale files
echolog "Cleaning up any stale remaining files from any incomplete build..."
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/phase_3/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/remastersys/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/vartmp/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/buildhome/sourcehome/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/buildhome/config/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/exportsource/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild_overlay/*
#Only run phase0 if phase1 and phase2 are going to be reset. phase0 only resets
RUN_PHASE_0=0
if [[ ! -f "$BUILDLOCATION"/DontForceSnapshotBuild"$BUILDARCH" && -f "$BUILDLOCATION"/DontStartFromScratch"$BUILDARCH" ]]
then
FORCE_SNAPSHOT_BUILD=1
touch "$BUILDLOCATION"/DontForceSnapshotBuild"$BUILDARCH"
else
FORCE_SNAPSHOT_BUILD=0
fi
if [[ -s "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt || $FORCE_SNAPSHOT_BUILD == 1 ]]
then
if [[ -s "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt ]]
then
APTFETCHDATESECONDS=$(grep APTFETCHDATESECONDS= "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt | head -1 | sed 's/APTFETCHDATESECONDS=//g')
fi
export PHASE1_PATHNAME=snapshot_phase_1
export PHASE2_PATHNAME=snapshot_phase_2
export BUILD_SNAPSHOT_SYSTEMS=1
RUN_PHASE_0=1
echolog "Clearing phase1 and phase2 snapshot build systems..."
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_1/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_2/*
else
export PHASE1_PATHNAME=phase_1
export PHASE2_PATHNAME=phase_2
export BUILD_SNAPSHOT_SYSTEMS=0
fi
if [[ ! -f "$BUILDLOCATION"/DontStartFromScratch"$BUILDARCH" || ! -f "$BUILDLOCATION"/DontRestartPhase1"$BUILDARCH" || ! -f "$BUILDLOCATION"/DontRestartPhase2"$BUILDARCH" ]]
then
#If set to clean up all files
if [ ! -f "$BUILDLOCATION"/DontStartFromScratch"$BUILDARCH" ]
then
echolog "Control file for all of $BUILDARCH removed, or non existing. Deleting phase_1, phase_2, archives, built deb files, and downloaded sources"
#clean up old files
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/phase_1/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/phase_2/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/phase_3/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/workdir/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/buildoutput/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/archives/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_1/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_2/*
rm "$BUILDLOCATION"/DontRestartPhase1"$BUILDARCH"
rm "$BUILDLOCATION"/DontRestartPhase2"$BUILDARCH"
rm "$BUILDLOCATION"/DontRestartBuildoutput"$BUILDARCH"
rm "$BUILDLOCATION"/DontRestartArchives"$BUILDARCH"
rm "$BUILDLOCATION"/DontRestartSourceDownload"$BUILDARCH"
touch "$BUILDLOCATION"/DontStartFromScratch"$BUILDARCH"
touch "$BUILDLOCATION"/DontForceSnapshotBuild"$BUILDARCH"
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/phase_2/tmp
touch "$BUILDLOCATION"/build/"$BUILDARCH"/phase_2/tmp/INSTALLS.txt.lastrun
REBUILT="to rebuild from scratch"
fi
#if set to rebuild phase 1
if [[ ! -f "$BUILDLOCATION"/DontRestartPhase1"$BUILDARCH" && $BUILD_SNAPSHOT_SYSTEMS == 0 ]]
then
echolog "Control file for phase_1 removed, or non existing. Deleting phase_1 system for $BUILDARCH"
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/phase_1/*
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_PHASE1))
fi
#if set to rebuild phase 2
if [[ ! -f "$BUILDLOCATION"/DontRestartPhase2"$BUILDARCH" && $BUILD_SNAPSHOT_SYSTEMS == 0 ]]
then
echolog "Control file for phase_2 removed, or non existing. Deleting phase_2 system for $BUILDARCH"
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/phase_2/*
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/phase_2/tmp
touch "$BUILDLOCATION"/build/"$BUILDARCH"/phase_2/tmp/INSTALLS.txt.lastrun
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_PHASE2))
fi
RUN_PHASE_0=1
fi
#Delete buildoutput based on a control file
if [[ ! -f "$BUILDLOCATION"/DontRestartBuildoutput"$BUILDARCH" ]]
then
echolog "Control file for buildoutput removed, or non existing. Deleting compiled .deb files for $BUILDARCH"
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/buildoutput/*
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/buildoutput
touch "$BUILDLOCATION"/DontRestartBuildoutput"$BUILDARCH"
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_BUILDOUTPUT))
fi
#Delete archives based on a control file
if [[ ! -f "$BUILDLOCATION"/DontRestartArchives"$BUILDARCH" ]]
then
echolog "Control file for archives removed, or non existing. Deleting downloaded cached .deb files for $BUILDARCH"
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/archives/*
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/archives
touch "$BUILDLOCATION"/DontRestartArchives"$BUILDARCH"
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_ARCHIVES))
fi
#Delete downloaded source based on a control file
if [[ ! -f "$BUILDLOCATION"/DontRestartSourceDownload"$BUILDARCH" ]]
then
echolog "Control file for srcbuild removed, or non existing. Deleting downloaded sources for $BUILDARCH"
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/*
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild
touch "$BUILDLOCATION"/DontRestartSourceDownload"$BUILDARCH"
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_SRCBUILD))
fi
#create the folders for the build systems, and for any folder that will be bind mounted in
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/phase_1
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/phase_2
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/phase_3
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_1
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_2
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/buildoutput
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/buildoutput
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/workdir
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/archives
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/remastersys
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/vartmp
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/importdata
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/exportsource
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild_overlay
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/unionwork
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/unionwork_srcbuild
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk
RAMDISKED_TEMP_FOLDERS=0
TOTAL_TEMP_FOLDERS=0
FREERAM=$(grep MemAvailable: /proc/meminfo | awk '{print $2}')
RAMDISKSIZE=$STORAGESIZE_PADDING
#Determine the size of the ram disk, determine if enough free ram for base in ramdisk
((TOTAL_TEMP_FOLDERS++))
RAMDISKTESTSIZE=$((RAMDISKSIZE+STORAGESIZE_TMPBASEBUILD))
OPTIMAL_FREE_RAM=$((RAMDISKSIZE+STORAGESIZE_TMPBASEBUILD))
if [[ $FREERAM -gt $RAMDISKTESTSIZE ]]
then
echolog "More than $(( ((RAMDISKTESTSIZE+1023) /1024 + 1023) /1024 ))GB of RAM Free. Using a ramdisk for base temporary folders."
((RAMDISKED_TEMP_FOLDERS++))
RAMDISK_FOR_BASE=1
RAMDISKSIZE=$RAMDISKTESTSIZE
else
echolog "Not enough free RAM to use a ramdisk for base temporary folders."
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_TMPBASEBUILD))
RAMDISKTESTSIZE=$RAMDISKSIZE
fi
#Determine if enough free RAM for srcbuild_overlay in ramdisk
((TOTAL_TEMP_FOLDERS++))
RAMDISKTESTSIZE=$((RAMDISKTESTSIZE+STORAGESIZE_TMPSRCBUILDOVERLAY))
OPTIMAL_FREE_RAM=$((OPTIMAL_FREE_RAM+STORAGESIZE_TMPSRCBUILDOVERLAY))
if [[ $FREERAM -gt $RAMDISKTESTSIZE ]]
then
echolog "More than $(( ((RAMDISKTESTSIZE+1023) /1024 + 1023) /1024 ))GB of RAM Free. Using a ramdisk for srcbuild overlay"
((RAMDISKED_TEMP_FOLDERS++))
RAMDISK_FOR_SRCBUILD=1
RAMDISKSIZE=$RAMDISKTESTSIZE
else
echolog "Not enough free RAM to use a ramdisk for srcbuild overlay."
RAMDISKTESTSIZE=$RAMDISKSIZE
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_TMPSRCBUILDOVERLAY))
fi
#Determine if enough free RAM for phase3 in ramdisk
((TOTAL_TEMP_FOLDERS++))
RAMDISKTESTSIZE=$((RAMDISKTESTSIZE+STORAGESIZE_TMPPHASE3))
OPTIMAL_FREE_RAM=$((OPTIMAL_FREE_RAM+STORAGESIZE_TMPPHASE3))
if [[ $FREERAM -gt $RAMDISKTESTSIZE ]]
then
echolog "More than $(( ((RAMDISKTESTSIZE+1023) /1024 + 1023) /1024 ))GB of RAM Free. Using a ramdisk for phase 3"
((RAMDISKED_TEMP_FOLDERS++))
RAMDISK_FOR_PHASE3=1
RAMDISKSIZE=$RAMDISKTESTSIZE
else
echolog "Not enough free RAM to use a ramdisk for phase 3."
RAMDISKTESTSIZE=$RAMDISKSIZE
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_TMPPHASE3))
fi
if [[ $BUILD_SNAPSHOT_SYSTEMS == 1 ]]
then
((TOTAL_TEMP_FOLDERS++))
RAMDISKTESTSIZE=$((RAMDISKSIZE+STORAGESIZE_PHASE1))
OPTIMAL_FREE_RAM=$((OPTIMAL_FREE_RAM+STORAGESIZE_PHASE1))
if [[ $FREERAM -gt $RAMDISKTESTSIZE ]]
then
echolog "More than $(( ((RAMDISKTESTSIZE+1023) /1024 + 1023) /1024 ))GB of RAM Free. Using a ramdisk for phase 1"
((RAMDISKED_TEMP_FOLDERS++))
RAMDISK_FOR_PHASE1=1
RAMDISKSIZE=$RAMDISKTESTSIZE
else
echolog "Not enough free RAM to use a ramdisk for phase 2."
RAMDISKTESTSIZE=$RAMDISKSIZE
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_PHASE1))
fi
((TOTAL_TEMP_FOLDERS++))
RAMDISKTESTSIZE=$((RAMDISKSIZE+STORAGESIZE_PHASE2-STORAGESIZE_PHASE1))
OPTIMAL_FREE_RAM=$((OPTIMAL_FREE_RAM+STORAGESIZE_PHASE2-STORAGESIZE_PHASE1))
if [[ $FREERAM -gt $RAMDISKTESTSIZE ]]
then
echolog "More than $(( ((RAMDISKTESTSIZE+1023) /1024 + 1023) /1024 ))GB of RAM Free. Using a ramdisk for phase 2"
((RAMDISKED_TEMP_FOLDERS++))
RAMDISK_FOR_PHASE2=1
RAMDISKSIZE=$RAMDISKTESTSIZE
else
echolog "Not enough free RAM to use a ramdisk for phase 2."
RAMDISKTESTSIZE=$RAMDISKSIZE
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_PHASE2))
#If this is snapshot, the phase 1 is temporary, and deleted once it completes all downloads, in favor of phase 2
((STORAGESIZE_TOTALSIZE-=STORAGESIZE_PHASE1))
fi
fi
#Determine if enough free RAM for Remastersys in ramdisk
((TOTAL_TEMP_FOLDERS++))
RAMDISKTESTSIZE=$((RAMDISKSIZE+STORAGESIZE_TMPREMASTERSYS))
OPTIMAL_FREE_RAM=$((OPTIMAL_FREE_RAM+STORAGESIZE_TMPREMASTERSYS))
if [[ $FREERAM -gt $RAMDISKTESTSIZE ]]
then
echolog "More than $(( ((RAMDISKTESTSIZE+1023) /1024 + 1023) /1024 ))GB of RAM Free. Using a ramdisk for remastersys"
((RAMDISKED_TEMP_FOLDERS++))
RAMDISK_FOR_REMASTERSYS=1
RAMDISKSIZE=$RAMDISKTESTSIZE
else
echolog "Not enough free RAM to use a ramdisk for remastersys."
RAMDISKTESTSIZE=$RAMDISKSIZE
((STORAGESIZE_TOTALSIZE+=STORAGESIZE_TMPREMASTERSYS))
fi
#get the size of the output location
FREEDISKSPACE_OUTPUT=$(df --output=avail $HOMELOCATION | tail -1)
#if there is less than the required amount of space, then exit.
if [[ $FREEDISKSPACE_OUTPUT -le $STORAGESIZE_TOTALSIZE_OUTPUT ]]
then
echolog "You have less then $(( ((STORAGESIZE_TOTALSIZE_OUTPUT+1023) /1024 + 1023) /1024 ))GB of free space on the filesystem $(df --output=target $HOMELOCATION | tail -1) for $HOMELOCATION. Please free up some space."
echolog "The script will now abort."
faillog "free space: $FREEDISKSPACE_OUTPUT"
else
echolog -e "\n\nFree disk space needed for ISO output: $(( ((STORAGESIZE_TOTALSIZE_OUTPUT+1023) /1024 + 1023) /1024 ))GB, Free disk space for ISO output: $(( ((FREEDISKSPACE_OUTPUT+1023) /1024 + 1023) /1024 ))GB"
fi
#get the size of the build location
FREEDISKSPACE_BUILDSPACE=$(df --output=avail $BUILDLOCATION | tail -1)
#if there is less than the required amount of space, then exit.
if [[ $FREEDISKSPACE_BUILDSPACE -le $STORAGESIZE_TOTALSIZE ]]
then
echolog "You have less then $(( ((STORAGESIZE_TOTALSIZE+1023) /1024 + 1023) /1024 ))GB of free space on the filesystem $(df --output=target $BUILDLOCATION | tail -1) for $BUILDLOCATION. Please free up some space."
echolog "The script will now abort."
faillog "free space: $FREEDISKSPACE_BUILDSPACE"
else
echolog -e "Free RAM: $(( ((FREERAM+1023) /1024 + 1023) /1024 ))GB; RAM disk maximum size: $(( ((RAMDISKSIZE+1023) /1024 + 1023) /1024 ))GB, Free disk space needed for build: $(( ((STORAGESIZE_TOTALSIZE+1023) /1024 + 1023) /1024 ))GB, Free disk space for build: $(( ((FREEDISKSPACE_BUILDSPACE+1023) /1024 + 1023) /1024 ))GB"
echolog -e "Temporary folders in ramdisk: $RAMDISKED_TEMP_FOLDERS / Total temporary folders: $TOTAL_TEMP_FOLDERS . For all temporary folders to be in RAM, $(( ((OPTIMAL_FREE_RAM+1023) /1024 + 1023) /1024 ))GB is needed.\n"
fi
#Mount the ramdisk
mount --make-rprivate /
if [[ $RAMDISK_STATUS != 0 ]]
then
mount -t tmpfs -o size=${RAMDISKSIZE}k tmpfs "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk
RAMDISK_STATUS=$?
else
RAMDISK_STATUS=1
fi
#Create the folders in the ramdisk
if [[ $RAMDISK_FOR_BASE == 1 && $RAMDISK_STATUS == 0 ]]
then
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/buildlogs
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/vartmp
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/importdata
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/externalbuilders
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/exportsource
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/buildlogs "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/vartmp "$BUILDLOCATION"/build/"$BUILDARCH"/vartmp
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/importdata "$BUILDLOCATION"/build/"$BUILDARCH"/importdata
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/externalbuilders "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/exportsource "$BUILDLOCATION"/build/"$BUILDARCH"/exportsource
fi
if [[ $BUILD_SNAPSHOT_SYSTEMS == 1 && $RAMDISK_FOR_PHASE1 == 1 && $RAMDISK_STATUS == 0 ]]
then
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/snapshot_phase_1
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/unionwork_phase1
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/snapshot_phase_1 "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_1
fi
if [[ $BUILD_SNAPSHOT_SYSTEMS == 1 && $RAMDISK_FOR_PHASE2 == 1 && $RAMDISK_STATUS == 0 ]]
then
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/snapshot_phase_2
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/snapshot_phase_2 "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_2
fi
if [[ $RAMDISK_FOR_SRCBUILD == 1 && $RAMDISK_STATUS == 0 ]]
then
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/unionwork
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/srcbuild_overlay
fi
if [[ $RAMDISK_FOR_PHASE3 == 1 && $RAMDISK_STATUS == 0 ]]
then
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/unionwork_srcbuild
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/phase_3
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/phase_3 "$BUILDLOCATION"/build/"$BUILDARCH"/phase_3
fi
if [[ $RAMDISK_FOR_REMASTERSYS == 1 && $RAMDISK_STATUS == 0 ]]
then
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/remastersys
mount --bind "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk/remastersys "$BUILDLOCATION"/build/"$BUILDARCH"/remastersys
fi
#Create a miniature /dev so that, for instance LVM doesn't try to create a backup file
#on the chroots
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/minidev
mount -t tmpfs none "$BUILDLOCATION"/build/"$BUILDARCH"/minidev
if [[ $? != 0 ]]
then
faillog "Failed to create mini devtmpfs"
fi
#Use #http://www.linuxfromscratch.org/lfs/view/6.1/chapter06/devices.html
#for the list of devices, permissions, and makor and minor numbers
mknod -m 622 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/console c 5 1
mknod -m 666 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/null c 1 3
mknod -m 666 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/zero c 1 5
mknod -m 666 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/ptmx c 5 2
mknod -m 666 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/tty c 5 0
mknod -m 444 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/random c 1 8
mknod -m 444 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/urandom c 1 9
chown root:tty "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/console
chown root:tty "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/ptmx
chown root:tty "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/tty
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/pts
mount -t devpts -o gid=4,mode=620 none "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/pts
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/shm
mount -t tmpfs none "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/shm
ln -s /proc/self/fd "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/fd
ln -s /proc/self/fd/0 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/stdin
ln -s /proc/self/fd/1 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/stdout
ln -s /proc/self/fd/2 "$BUILDLOCATION"/build/"$BUILDARCH"/minidev/stderr
#Copy external builders into thier own directory, make them executable
cp "$SCRIPTFOLDERPATH"/externalbuilders/* "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders
chmod +x "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders/*
#Copy all external files before they are used
rsync "$SCRIPTFOLDERPATH"/"$BUILDUNIXNAME"_files/* -CKr "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/
rsync "$SCRIPTFOLDERPATH"/* -CKr "$BUILDLOCATION"/build/"$BUILDARCH"/exportsource
#Support importing the control file to use fixed revisions of the source code
rm "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/tmp/buildcore_revisions.txt > /dev/null 2>&1
rm "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE1_PATHNAME/tmp/buildcore_revisions.txt > /dev/null 2>&1
rm "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE2_PATHNAME/tmp/buildcore_revisions.txt > /dev/null 2>&1
rm "$BUILDLOCATION"/build/"$BUILDARCH"/phase_3/tmp/buildcore_revisions.txt > /dev/null 2>&1
if [ -s "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt ]
then
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/tmp/
cp "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/tmp/buildcore_revisions.txt
rm "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt
touch "$BUILDLOCATION"/buildcore_revisions_"$BUILDARCH".txt
fi
#If there is a date specified in the revisions file, create the Debian snapshot sources.list for that time
#If using a revisions file, force downloading a snapshot from the time specified
if [[ $APTFETCHDATESECONDS == [0-9]* ]]
then
APTFETCHDATE=$(date -d @$APTFETCHDATESECONDS -u +%Y%m%dT%H%M%SZ 2>/dev/null)
APTFETCHDATERESULT=$?
if [[ $APTFETCHDATERESULT == 0 ]]
then
DEBIANREPO="http://snapshot.debian.org/archive/debian/$APTFETCHDATE/"
sed "s|http://httpredir.debian.org/debian|$DEBIANREPO|g" "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/etc/apt/sources.list > "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/tmp/etc_apt_sources.list
else
echolog "Invalid APTFETCHDATESECONDS set. Falling back"
fi
fi
#Delete the list of pacakges specified in RestartPackageList_"$BUILDARCH".txt
cat "$BUILDLOCATION"/RestartPackageList_"$BUILDARCH".txt | while read -r RESETPACKAGE
do
#Dont allow path tampering, stop at the first / for path.
IFS=/
RESETPACKAGE=($RESETPACKAGE)
unset IFS
RESETPACKAGE=${RESETPACKAGE[0]}
#Delete the file, don't allow path clobbering for the current directory, or parent directory. rm doesnt delete directories by default, but be paranoid anyway
if [[ ! -z $RESETPACKAGE && $RESETPACKAGE != '.' && $RESETPACKAGE != '..' ]]
then
rm "$BUILDLOCATION"/build/"$BUILDARCH"/buildoutput/control/"$RESETPACKAGE"
echolog " Marked package: $RESETPACKAGE for rebuild."
fi
done
echo -n > "$BUILDLOCATION"/RestartPackageList_"$BUILDARCH".txt
#make the imported files executable
chmod 0755 -R "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/
chown root -R "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/
chgrp root -R "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/
PREPARE_ENDTIME=$(date +%s)
#Create a log folder for the phase logs
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs/externallogs
#Force buildcore to redownload cargo if the control file is gone
if [[ ! -e "$BUILDLOCATION"/DontRestartCargoDownload"$BUILDARCH" ]]
then
if [[ -e "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/buildhome/cargocache ]]
then
echolog "Deleting downloaded Cargo files..."
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/buildhome/cargocache
fi
touch "$BUILDLOCATION"/DontRestartCargoDownload"$BUILDARCH"
fi
BUILD_RUNNING=1
#run the build scripts
if [[ $RUN_PHASE_0 == 1 ]]
then
PHASE0_STARTTIME=$(date +%s)
echolog "Starting phase0..."
NAMESPACE_EXECUTE 1 "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs/externallogs/phase0.log "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders/"$BUILDUNIXNAME"_phase0.sh
PHASE0_ENDTIME=$(date +%s)
fi
PHASE1_STARTTIME=$(date +%s)
echolog "Starting phase1..."
NAMESPACE_EXECUTE 1 "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs/externallogs/phase1.log "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders/"$BUILDUNIXNAME"_phase1.sh
PHASE1_ENDTIME=$(date +%s)
#copy the installs data copied in phase 1 into phase 2
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE2_PATHNAME/var/lib/apt/lists/*
cp "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE1_PATHNAME/tmp/INSTALLS.txt "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE2_PATHNAME/tmp/INSTALLS.txt
cp -a "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE1_PATHNAME/var/cache/apt/*.bin "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE2_PATHNAME/var/cache/apt
cp -a "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE1_PATHNAME/var/lib/apt/lists "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE2_PATHNAME/var/lib/apt
#If a sources.list was created for Debian Snapshots, import it in
cp ""$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE1_PATHNAME/tmp/APTFETCHDATE" ""$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE2_PATHNAME/tmp/APTFETCHDATE"
#Remove Phase 1 if it's a snapshot
if [[ $BUILD_SNAPSHOT_SYSTEMS == 1 ]]
then
echolog "Clearing phase1 snapshot build system..."
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_1/*
mkdir -p "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_2/tmp
touch "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_2/tmp/INSTALLS.txt.lastrun
fi
PHASE2_STARTTIME=$(date +%s)
echolog "Starting phase2..."
NAMESPACE_EXECUTE 0 "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs/externallogs/phase2.log "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders/"$BUILDUNIXNAME"_phase2.sh
PHASE2_ENDTIME=$(date +%s)
PHASE3_STARTTIME=$(date +%s)
echolog "Starting phase3..."
NAMESPACE_EXECUTE 0 "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs/externallogs/phase3.log "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders/"$BUILDUNIXNAME"_phase3.sh
PHASE3_ENDTIME=$(date +%s)
#Main Build Complete, Extract ISO and logs
#If the live cd did not build then tell user
echolog "Moving built ISO files..."
EXPORT_STARTTIME=$(date +%s)
#Take a snapshot of the source
rm "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_Source_"$BUILDARCH".tar.gz
#Pack the source into a tar file, trying to always get the same filehash
TARDATESTAMP=$(cat ""$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE2_PATHNAME/tmp/APTFETCHDATE" | grep -v ^$| awk -F = '{print $2}')
GZIP=-n tar -czvf "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_Source_"$BUILDARCH".tar.gz -C "$BUILDLOCATION"/build/"$BUILDARCH"/exportsource/ . --mtime=@$TARDATESTAMP &>/dev/null
if [[ ! -f "$BUILDLOCATION"/build/"$BUILDARCH"/remastersys/remastersys/custom.iso ]]
then
ISOFAILED=1
else
mv "$BUILDLOCATION"/build/"$BUILDARCH"/remastersys/remastersys/custom.iso "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_"$BUILDARCH".iso
fi
#Before the rest of the files are cleaned, export the logs, that are now also generated by the cleanup of the build source.
#Create a date string for unique log folder names
ENDDATE=$(date +"%Y-%m-%d_%H-%M-%S")
#Create a folder contain the revisions files
mkdir -p ""$BUILDLOCATION"/revisions_history"
#Create a folder for the log files with the date string
mkdir -p ""$BUILDLOCATION"/logs/"$ENDDATE"_"$BUILDARCH""
#Export the log files to the location
cp -a "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs/* ""$BUILDLOCATION"/logs/"$ENDDATE"_"$BUILDARCH""
rm ""$BUILDLOCATION"/logs/latest-"$BUILDARCH"" 2>/dev/null
ln -s ""$BUILDLOCATION"/logs/"$ENDDATE"_"$BUILDARCH"" ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""
cp -a ""$BUILDLOCATION"/build/"$BUILDARCH"/phase_3/usr/share/buildcore_revisions.txt" ""$BUILDLOCATION"/revisions_history/"$BUILDFRIENDLYNAME"_Revisions_"$BUILDARCH"_"$ENDDATE".txt"
cp -a ""$BUILDLOCATION"/build/"$BUILDARCH"/phase_3/usr/share/buildcore_revisions.txt" ""$HOMELOCATION"/"$BUILDFRIENDLYNAME"_Revisions_"$BUILDARCH".txt"
#allow the user to actually read the iso
chown $SUDO_USER "$HOMELOCATION"/"$BUILDFRIENDLYNAME"*_"$BUILDARCH".iso "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_*_"$BUILDARCH".txt "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_*_"$BUILDARCH".tar.gz
chgrp $SUDO_USER "$HOMELOCATION"/"$BUILDFRIENDLYNAME"*_"$BUILDARCH".iso "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_*_"$BUILDARCH".txt "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_*_"$BUILDARCH".tar.gz
chmod 777 "$HOMELOCATION"/"$BUILDFRIENDLYNAME"*_"$BUILDARCH".iso "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_*_"$BUILDARCH".txt "$HOMELOCATION"/"$BUILDFRIENDLYNAME"_*_"$BUILDARCH".tar.gz
EXPORT_ENDTIME=$(date +%s)
echolog "Cleaning up non reusable build data..."
POSTCLEANUP_STARTTIME=$(date +%s)
#Unmount the ramdisks, and bind mounts
if [[ $RAMDISK_FOR_BASE == 1 && $RAMDISK_STATUS == 0 ]]
then
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/vartmp
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/importdata
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/exportsource
fi
if [[ $RAMDISK_FOR_PHASE3 == 1 && $RAMDISK_STATUS == 0 ]]
then
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/phase_3
fi
if [[ $RAMDISK_FOR_PHASE1 == 1 && $RAMDISK_STATUS == 0 ]]
then
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE1_PATHNAME
fi
if [[ $RAMDISK_FOR_PHASE2 == 1 && $RAMDISK_STATUS == 0 ]]
then
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/$PHASE2_PATHNAME
fi
if [[ $RAMDISK_FOR_REMASTERSYS == 1 && $RAMDISK_STATUS == 0 ]]
then
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/remastersys
fi
umount -lf "$BUILDLOCATION"/build/"$BUILDARCH"/ramdisk
#Continue cleaning non reusable files
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/vartmp/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/remastersys/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/buildhome/sourcehome/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild/buildhome/config/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/phase_3/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/importdata/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/buildlogs/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/exportsource/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/externalbuilders/*
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/srcbuild_overlay/*
if [[ $BUILD_SNAPSHOT_SYSTEMS == 1 ]]
then
rm -rf "$BUILDLOCATION"/build/"$BUILDARCH"/snapshot_phase_2/*
fi
rm "$BUILDLOCATION"/build/"$BUILDARCH"/lockfile
POSTCLEANUP_ENDTIME=$(date +%s)
#If the live cd did build then tell user
if [[ $ISOFAILED != 1 ]];
then
echolog "Live CD image build was successful."
else
echolog "The Live CD did not succesfuly build. The script could have been modified, or a network connection could have failed to one of the servers preventing the installation packages for Debian, or Remstersys from installing. There could also be a problem with the selected architecture for the build, such as an incompatible kernel or CPU, or a misconfigured qemu-system bin_fmt"
fi
ENDTIME=$(date +%s)
#Summarize cleanup time
echolog "build of $BUILDARCH finished in $((ENDTIME-STARTTIME)) seconds $REBUILT"
echolog -n "Prepare run time: $((PREPARE_ENDTIME-PREPARE_STARTTIME)) seconds, "
if [[ $RUN_PHASE_0 == 1 ]]
then
echolog -n "Phase 0 build time: $((PHASE0_ENDTIME-PHASE0_STARTTIME)) seconds, "
fi
echolog -n "Phase 1 build time: $((PHASE1_ENDTIME-PHASE1_STARTTIME)) seconds, "
echolog -n "Phase 2 build time: $((PHASE2_ENDTIME-PHASE2_STARTTIME)) seconds, "
echolog -n "Phase 3 build time: $((PHASE3_ENDTIME-PHASE3_STARTTIME)) seconds, "
echolog -n "Export time: $((EXPORT_ENDTIME-EXPORT_STARTTIME)) seconds, "
echolog "Cleanup time: $((POSTCLEANUP_ENDTIME-POSTCLEANUP_STARTTIME)) seconds"
if [[ -e ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/package_operations/Downloads/failedpackages.log ]]
then
echolog -e "\nPackages that failed to download:"
LIST=$(cat ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/package_operations/Downloads/failedpackages.log | tr '\n' '|' | sed 's/|/. /g')
echolog "$LIST"
echolog " "
fi
if [[ -e ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/package_operations/Installs/failedpackages.log ]]
then
echolog -e "\nPackages that failed to install:"
LIST=$(cat ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/package_operations/Installs/failedpackages.log | tr '\n' '|' | sed 's/|/. /g')
echolog "$LIST"
echolog " "
fi
if [[ -e ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/build_core/faileddownloads ]]
then
echolog -e "\nPackages that failed to download source:"
LIST=$(cat ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/build_core/faileddownloads | tr '\n' ' ')
echolog "$LIST"
echolog " "
fi
if [[ -e ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/build_core/failedcompiles ]]
then
echolog -e "\nPackages that failed to compile:"
LIST=$(cat ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/build_core/failedcompiles | tr '\n' ' ')
echolog "$LIST"
echolog " "
fi
echolog "ISO sizes:"
if [[ $(compgen -G "$HOMELOCATION"/"$BUILDFRIENDLYNAME"*_"$BUILDARCH".iso &> /dev/null; echo $?) == 0 ]]
then
echolog "$(ls -lh "$HOMELOCATION"/"$BUILDFRIENDLYNAME"*_"$BUILDARCH".iso)"
fi
#Write specially logged messages to the mainlog
echo "$LOGTEXT" > ""$BUILDLOCATION"/logs/latest-"$BUILDARCH""/externallogs/mainlog.log
exit
}
#Start the build process
if [[ $BUILDER_IS_UNSHARED != 1 ]]