-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdragonos-builder.sh
More file actions
executable file
·490 lines (430 loc) · 20.9 KB
/
dragonos-builder.sh
File metadata and controls
executable file
·490 lines (430 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/bin/bash
#
# DragonOS
#
# MIT License
#
# Copyright (c) 2020 Lukas Yoder <lukas@lukasyoder.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
##########################################################################
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
#
# .-._
# \ '-._
# ______/___ '.
# `'--.___ _\ / DragonOS Builder, Version 0.9.9
# /_.-' _\ \ _:,_
# .'__ _.' \'-/,`-~`
# '. ___.> /=, , ,
# / .-'/_ ) / \/ \
# )' ( /(/ by Lukas Yoder (/ //_ \_
# \\ " \|| . \
# '==' _,:__.-"/---\_ \
# '~-'--.)__( , )\ \
# ,' \)|\ `\|
# " || (
# /
VERSION=0.9.9
LAST_MODIFIED=6Jun2020
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
##########################################################################
#------------------------------------------------------------------------#
# #
# Making all scripts executable... #
# #
#------------------------------------------------------------------------#
chmod -R +x parts dragonos-builder.sh
#------------------------------------------------------------------------#
# #
# Setting build system variables... #
# #
#------------------------------------------------------------------------#
export LFS=$(pwd)/sysbuild
export WORDIR=`pwd`
THREADS=$(cat /proc/cpuinfo | grep processor | wc -l)
export MAKEFLAGS="-j $THREADS"
export PLATFORM=$(bash parts/00-system-type-guesser.sh)
# by default, always start a new build
CONTINUESTATUS="true"
#------------------------------------------------------------------------#
# #
# This script only works as root... #
# #
#------------------------------------------------------------------------#
if [ "$EUID" -ne 0 ]
then echo "Please run as root."
exit
fi
#------------------------------------------------------------------------#
# #
# Check for suitable build environment... #
# #
#------------------------------------------------------------------------#
function CHECKER {
time bash parts/01-checker.sh
}
#------------------------------------------------------------------------#
# #
# Prepare build environment by making and mounting the disk as well #
# as downloading all packages and caching them for future builds. #
# #
#------------------------------------------------------------------------#
function PREPARE {
time bash parts/02-prepare.sh
}
#------------------------------------------------------------------------#
# #
# Set up the unprivileged user "lfs" for a safer build... #
# #
#------------------------------------------------------------------------#
function SETUPLFS {
time bash parts/03-setup-lfs.sh
}
#------------------------------------------------------------------------#
# #
# Set up a bootstrapped environment to build the system... #
# #
#------------------------------------------------------------------------#
function BOOTSTRAPSETUP {
cp parts/04-bootstrap-setup.sh $LFS
chown -v lfs $LFS/tools
chown -v lfs $LFS/sources
cp parts/extract.sh $LFS/sources/
time su lfs -c /bin/bash << "EOF"
exec env HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' LFS=$LFS LC_ALL=POSIX \
LFS_TGT=$(uname -m)-lfs-linux-gnu PATH=/tools/bin:/bin:/usr/bin bash \
$LFS/04-bootstrap-setup.sh
EOF
rm -f $LFS/04-bootstrap-setup.sh
}
#------------------------------------------------------------------------#
# #
# Set configuration variables and virtual kernel filesystems. #
# #
#------------------------------------------------------------------------#
function FILESYSTEMSETUP {
time bash parts/05-filesystem-setup.sh
}
#------------------------------------------------------------------------#
# #
# Part One of the DragonOS system build... #
# #
#------------------------------------------------------------------------#
function BUILDPARTONE {
cp parts/06-stage-one.sh $LFS
time chroot "$LFS" /tools/bin/env -i \
HOME=/root TERM="$TERM" PS1='\u:\w\$ ' LFS=$LFS MAKEFLAGS="$MAKEFLAGS" \
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
/tools/bin/bash +h -c "bash /06-stage-one.sh"
rm -f $LFS/06-stage-one.sh
# copying over finished status from chroot directory
cp $LFS/sources/buildlogs/06-buildpartone-finished.txt \
logs/06-buildpartone-finished.txt
}
#------------------------------------------------------------------------#
# #
# Part Two of the DragonOS system build... #
# #
#------------------------------------------------------------------------#
function BUILDPARTTWO {
cp parts/07-stage-two.sh $LFS
cp parts/kernelconfig.txt $LFS/sources/
time chroot "$LFS" /usr/bin/env -i HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
LFS=$LFS MAKEFLAGS="$MAKEFLAGS" PATH=/bin:/usr/bin:/sbin:/usr/sbin \
/bin/bash +h -c "bash /07-stage-two.sh"
rm $LFS/07-stage-two.sh
# copying over finished status from chroot directory
cp $LFS/sources/buildlogs/07-buildparttwo-finished.txt \
logs/07-buildparttwo-finished.txt
rm $LFS/sources/extract.sh
}
#------------------------------------------------------------------------#
# #
# Finish setting up the DragonOS system... #
# #
#------------------------------------------------------------------------#
function FINALIZE {
# suppress errors
rm -rf $LFS/sources/
# unmount virtual filesystems
umount -l $LFS/dev/pts 2>/dev/null
umount -l $LFS/dev 2>/dev/null
umount -l $LFS/run 2>/dev/null
umount -l $LFS/proc 2>/dev/null
umount -l $LFS/sys 2>/dev/null
# unmount LFS itself
umount -l /dev/loop0p1 2>/dev/null
losetup -d /dev/loop0 2>/dev/null
partprobe
userdel lfs 2>/dev/null
rm -rf /home/lfs/
rm -rf $LFS
# show completion status of this step
touch logs/finalize-finished.txt
}
#------------------------------------------------------------------------#
# #
# Copy system over to a smaller disk (to make more redistributable). #
# #
#------------------------------------------------------------------------#
function NEWDISK {
set -x
time bash parts/08-newdisk.sh
}
#------------------------------------------------------------------------#
# #
# Set up the GRUB2 bootloader on the new disk. #
# #
#------------------------------------------------------------------------#
function NEWGRUBINSTALL {
mkdir -p $LFS
losetup /dev/loop0 disk.img
partprobe /dev/loop0
umount $LFS 2>/dev/null
mount /dev/loop0p1 $LFS
rm -f $LFS/09-newgrubinstall.sh
if [ -z $(which grub-install 2>/dev/null) ]
then grub2-install --target i386-pc --boot-directory $LFS/boot/ \
/dev/loop0
else grub-install --target i386-pc --boot-directory $LFS/boot/ \
/dev/loop0
fi
cp parts/09-newgrubinstall.sh $LFS
time chroot "$LFS" /usr/bin/env -i HOME=/root TERM=$TERM \
PS1='\u:\w\$ ' PATH=/bin:/usr/bin:/sbin:/usr/sbin /bin/bash << "EOF"
bash /09-newgrubinstall.sh
EOF
# removing the newgrubinstall script
rm -rf $LFS/09-newgrubinstall.sh
# copying over finished status from chroot directory
mv $LFS/sources/buildlogs/09-newgrubinstall-finished.txt \
logs/09-newgrubinstall-finished.txt
# remove the sources from the image
rm -rf $LFS/sources/
# unmount the image and destroy the loopback device
umount /dev/loop0p1
losetup -d /dev/loop0
}
#------------------------------------------------------------------------#
# #
# Optional Command: Resets the build process but keeps package caches. #
# #
#------------------------------------------------------------------------#
if [ "$1" == "restart" ]
then FINALIZE
cd $WORDIR
rm -f disk.img
rm -rf logs
rm -rf cache/meta/
exit
fi
#------------------------------------------------------------------------#
# #
# Optional Command: Clears all build material, including package caches. #
# #
#------------------------------------------------------------------------#
if [ "$1" == "clean" ]
then FINALIZE 2> /dev/null
cd $WORDIR
rm -f disk.img
rm -rf logs
rm -rf cache
exit
fi
#------------------------------------------------------------------------#
# #
# Optional Command: Compresses free space to facilitate redistirbution. #
# #
#------------------------------------------------------------------------#
if [ "$1" == "dist" ]
then xz -z9 -e -k -C sha256 disk.img &
progress -m
exit
fi
#------------------------------------------------------------------------#
# #
# Optional Command: Continues an existing build. #
# #
#------------------------------------------------------------------------#
if [ "$1" == "continue" ]
then CONTINUESTATUS="true"
fi
#------------------------------------------------------------------------#
# #
# Checks to see if build is possible and exits the script if not. #
# #
#------------------------------------------------------------------------#
# creates a log directory for all subsequent log files
mkdir -p logs && chmod -R 777 logs
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/01-checker-finished.txt
fi
# checks whether system dependencies are met; skips if already done
if [ ! -f logs/01-checker-finished.txt ]; then
CHECKER 2>&1 | tee logs/01-checker-output.txt
fi
# stops script if dependencies are not met
if [ ! -f logs/01-checker-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Sets up the target filesystem and source tarballs. Exits the script #
# upon encountering any errors. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/02-prepare-finished.txt
fi
# sets up filesystem and source tarballs; skips if already done
if [ ! -f logs/02-prepare-finished.txt ]; then
PREPARE 2>&1 | tee logs/02-prepare-output.txt
fi
# stops script if filesystem creation had errors
if [ ! -f logs/02-prepare-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Sets up the 'lfs' user, who owns the sources and tools directories. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/03-setup-lfs-finished.txt
fi
# sets up the unprivileged 'lfs' user with ownership of tools and sources;
# skips if finished
if [ ! -f logs/03-setup-lfs-finished.txt ]; then
SETUPLFS 2>&1 | tee logs/03-setuplfs-output.txt
fi
# stops the script if the unprivileged user setup had errors
if [ ! -f logs/03-setup-lfs-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Builds the bootstrapping system used to build the target system. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/04-bootstrap-setup-finished.txt
fi
# sets up the bootstrapping system; skips if finished
if [ ! -f logs/04-bootstrap-setup-finished.txt ]; then
BOOTSTRAPSETUP 2>&1 | tee logs/04-bootstrapsetup-output.txt
fi
# stops the script if the bootstrapping system was not successfully built
if [ ! -f logs/04-bootstrap-setup-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Set up the target system's filesystem. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/05-filesystem-setup-finished.txt
fi
# sets up the bootstrapping system; skips if finished
if [ ! -f logs/05-filesystem-setup-finished.txt ]; then
FILESYSTEMSETUP 2>&1 | tee logs/05-filesystemsetup-output.txt
fi
# stops the script if the bootstrapping system was not successfully built
if [ ! -f logs/05-filesystem-setup-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Build the target system libraries and executables. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/06-buildpartone-finished.txt
fi
# sets up the bootstrapping system; skips if finished
if [ ! -f logs/06-buildpartone-finished.txt ]; then
BUILDPARTONE 2>&1 | tee logs/06-buildpartone-output.txt
fi
# stops the script if the bootstrapping system was not successfully built
if [ ! -f logs/06-buildpartone-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Build the target system kernel and config files. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/07-buildparttwo-finished.txt
fi
# sets up the bootstrapping system; skips if finished
if [ ! -f logs/07-buildparttwo-finished.txt ]; then
BUILDPARTTWO 2>&1 | tee logs/07-buildparttwo-output.txt
fi
# stops the script if the bootstrapping system was not successfully built
if [ ! -f logs/07-buildparttwo-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Finalizing the target system build. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/finalize-finished.txt
fi
# sets up the bootstrapping system; skips if finished
if [ ! -f logs/finalize-finished.txt ]; then
FINALIZE 2>&1 | tee logs/finalize-output.txt
fi
# stops the script if the bootstrapping system was not successfully built
if [ ! -f logs/finalize-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Set up a new, smaller disk and filesystem. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/08-newdisk-finished.txt
fi
# sets up the bootstrapping system; skips if finished
if [ ! -f logs/08-newdisk-finished.txt ]; then
NEWDISK 2>&1 | tee logs/08-newdisk-output.txt
fi
# stops the script if the bootstrapping system was not successfully built
if [ ! -f logs/08-newdisk-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# Install GRUB to the new disk. #
# #
#------------------------------------------------------------------------#
# cleans previous checker status if not continuing
if [ $CONTINUESTATUS == "false" ]
then rm -f logs/09-newgrubinstall-finished.txt
fi
# sets up the bootstrapping system; skips if finished
if [ ! -f logs/09-newgrubinstall-finished.txt ]; then
NEWGRUBINSTALL 2>&1 | tee logs/09-newgrubinstall-output.txt
fi
# stops the script if the bootstrapping system was not successfully built
if [ ! -f logs/09-newgrubinstall-finished.txt ]; then exit 1; fi
#------------------------------------------------------------------------#
# #
# If the build process reached this point, it's complete. #
# #
#------------------------------------------------------------------------#
echo -e "\nCongrats on your new DragonOS build!"