forked from ataraxialinux/ataraxia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk
executable file
·418 lines (361 loc) · 10.6 KB
/
mk
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
#!/bin/sh
set -e
export RELEASE="$(date +%y%m%d)"
printmsg() {
local msg=$(echo $1 | tr -s / /)
printf "\e[1m\e[32m==>\e[0m $msg\n"
sleep 1
}
printmsgerror() {
local msg=$(echo $1 | tr -s / /)
printf "\e[1m\e[31m==!\e[0m $msg\n"
sleep 1
exit 1
}
pkginstall() {
local pkg="$@"
for mergepkg in $pkg; do
printmsg "Building and installing $mergepkg"
cd $REPO/$mergepkg
pkgmk -d -if -im -is -ns -cf $REPO/pkgmk.conf
pkgadd $PACKAGES/$mergepkg#*.pkg.tar.xz --root $ROOTFS
done
}
pkginstallstage() {
local pkg="$@"
for mergepkg in $pkg; do
printmsg "Installing $mergepkg"
pkgadd $PACKAGES/$mergepkg#*.pkg.tar.xz --root $STAGE
done
}
pkginstallinitrd() {
local pkg="$@"
for mergepkg in $pkg; do
printmsg "Installing $mergepkg"
pkgadd $PACKAGES/$mergepkg#*.pkg.tar.xz --root $INITRD
done
}
toolpkginstall() {
local pkg="$@"
for mergepkg in $pkg; do
printmsg "Building and installing $mergepkg"
cd $TCREPO/$mergepkg
pkgmk -d -if -im -is -ns -cf $TCREPO/pkgmk.conf
pkgadd $PACKAGES/$mergepkg#*.pkg.tar.xz --root $TOOLS -f
done
}
pkgupdate() {
local pkg="$@"
for mergepkg in $pkg; do
printmsg "Building and updating $mergepkg"
cd $REPO/$mergepkg
pkgmk -d -if -im -is -ns -cf $REPO/pkgmk.conf
pkgadd $PACKAGES/$mergepkg#*.pkg.tar.xz --root $ROOTFS -u
done
}
toolpkgupdate() {
local pkg="$@"
for mergepkg in $pkg; do
printmsg "Building and updating $mergepkg"
cd $TCREPO/$mergepkg
pkgmk -d -if -im -is -ns -cf $TCREPO/pkgmk.conf
pkgadd $PACKAGES/$mergepkg#*.pkg.tar.xz --root $TOOLS -f -u
done
}
initdb() {
local dir="$@"
for dbindir in $dir; do
mkdir -p $dbindir/var/lib/pkg
touch $dbindir/var/lib/pkg/db
done
}
rmpkg() {
local rmpkg="$@"
for rmpack in $rmpkg; do
rm -rf $PACKAGES/$rmpack#*
done
}
check_for_root() {
if [[ $EUID -ne 0 ]]; then
printmsgerror "This script must be run as root"
fi
}
setup_architecture() {
case $BARCH in
x86_64)
printmsg "Using configuration for x86_64"
export XHOST="$(echo ${MACHTYPE} | sed -e 's/-[^-]*/-cross/')"
export XTARGET="x86_64-linux-musl"
export XKARCH="x86_64"
export GCCOPTS=
;;
aarch64)
printmsg "Using configuration for aarch64"
export XHOST="$(echo ${MACHTYPE} | sed -e 's/-[^-]*/-cross/')"
export XTARGET="aarch64-linux-musl"
export XKARCH="arm64"
export GCCOPTS="--with-arch=armv8-a --with-abi=lp64"
;;
armv7h)
printmsg "Using configuration for armv7h"
export XHOST="$(echo ${MACHTYPE} | sed -e 's/-[^-]*/-cross/')"
export XTARGET="armv7l-linux-musleabihf"
export XKARCH="arm"
export GCCOPTS="--with-arch=armv7-a --with-fpu=vfpv3 --with-float=hard"
;;
armv6h)
printmsg "Using configuration for armv6h"
export XHOST="$(echo ${MACHTYPE} | sed -e 's/-[^-]*/-cross/')"
export XTARGET="arm-linux-musleabihf"
export XKARCH="arm"
export GCCOPTS="--with-arch=armv6 --with-fpu=vfp --with-float=hard"
;;
mipsel)
printmsg "Using configuration for mipsel"
export XHOST="$(echo ${MACHTYPE} | sed -e 's/-[^-]*/-cross/')"
export XTARGET="mipsel-linux-musl"
export XKARCH="mips"
export GCCOPTS="--with-arch=mips32r2 --with-float=soft --with-linker-hash-style=sysv"
;;
mips)
printmsg "Using configuration for mips"
export XHOST="$(echo ${MACHTYPE} | sed -e 's/-[^-]*/-cross/')"
export XTARGET="mips-linux-musl"
export XKARCH="mips"
export GCCOPTS="--with-arch=mips32r2 --with-float=soft --with-linker-hash-style=sysv"
;;
*)
printmsgerror "BARCH variable isn't set!"
esac
}
setup_environment() {
printmsg "Setting up build environment"
export CWD="$(pwd)"
export KEEP="$CWD/KEEP"
export BUILD="$CWD/build"
export REPO="$CWD/packages"
export TCREPO="$CWD/toolchain"
export SOURCES="$BUILD/sources"
export ROOTFS="$BUILD/rootfs"
export TOOLS="$BUILD/tools"
export PACKAGES="$BUILD/packages"
export IMAGE="$BUILD/image"
export INITRD="$BUILD/initrd"
export STAGE="$BUILD/stage"
export LC_ALL="POSIX"
export PATH="$KEEP/bin:$TOOLS/bin:$PATH"
export HOSTCC="gcc"
export HOSTCXX="g++"
export MKOPTS="-j$(expr $(nproc) + 1)"
export CPPFLAGS="-D_FORTIFY_SOURCE=2"
export CFLAGS="-Os -pipe -fstack-protector-strong"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-s"
}
make_environment() {
umount $ROOTFS/usr/janus/KEEP $ROOTFS/usr/janus/packages $ROOTFS/usr/janus/toolchain || true
umount $ROOTFS/proc $ROOTFS/sys $ROOTFS/dev $ROOTFS/tmp || true
umount $ROOTFS/output/sources $ROOTFS/output/packages || true
umount $ROOTFS/output/stage $ROOTFS/output/initrd || true
rm -rf $BUILD
mkdir -p $BUILD $SOURCES $ROOTFS $INITRD $STAGE $TOOLS $PACKAGES $IMAGE
initdb $ROOTFS $TOOLS $INITRD $STAGE
}
build_toolchain() {
printmsg "Building cross-toolchain for $BARCH"
pkginstall filesystem
toolpkginstall file
toolpkginstall pkgconf
toolpkginstall binutils
toolpkginstall gcc-static
pkginstall linux-headers musl-bootstrap
toolpkginstall gcc
printmsg "Cleaning"
rmpkg file pkgconf binutils gcc gcc-static
}
bootstrap_rootfs() {
printmsg "Bootstraping root filesystem"
pkginstall zlib-bootstrap binutils-bootstrap gcc-bootstrap make-bootstrap busybox-bootstrap ncurses-bootstrap bash-bootstrap file-bootstrap perl-bootstrap xz-bootstrap libarchive-bootstrap libressl-bootstrap curl-bootstrap npkg-bootstrap patch-bootstrap bootstrap-scripts
}
clean_packages() {
printmsg "Cleaning"
rmpkg linux-headers-bootstrap musl-bootstrap zlib-bootstrap binutils-bootstrap gcc-bootstrap make-bootstrap busybox-bootstrap ncurses-bootstrap bash-bootstrap file-bootstrap perl-bootstrap xz-bootstrap libarchive-bootstrap libressl-bootstrap curl-bootstrap npkg-bootstrap patch-bootstrap bootstrap-scripts
}
mountall() {
mount --bind $BUILD/packages $ROOTFS/output/packages || true
mount --bind $BUILD/sources $ROOTFS/output/sources || true
mount --bind $BUILD/stage $ROOTFS/output/stage || true
mount --bind $BUILD/initrd $ROOTFS/output/initrd || true
}
umountall() {
umount $ROOTFS/output/sources $ROOTFS/output/packages || true
umount $ROOTFS/output/stage $ROOTFS/output/initrd || true
}
enter_chroot() {
set +e
mkdir -p $ROOTFS/output/{stage,initrd}
mkdir -p $ROOTFS/usr/janus/{KEEP,packages,toolchain}
mount --bind /proc $ROOTFS/proc
mount --bind /sys $ROOTFS/sys
mount --bind /dev $ROOTFS/dev
mount --bind /tmp $ROOTFS/tmp
mount --bind $CWD/KEEP $ROOTFS/usr/janus/KEEP
mount --bind $CWD/packages $ROOTFS/usr/janus/packages
mount --bind $CWD/toolchain $ROOTFS/usr/janus/toolchain
mount --bind $BUILD/packages $ROOTFS/output/packages
mount --bind $BUILD/sources $ROOTFS/output/sources
mount --bind $BUILD/stage $ROOTFS/output/stage
mount --bind $BUILD/initrd $ROOTFS/output/initrd
chroot $ROOTFS /busybox/bin/env -i \
TERM="$TERM" \
PS1='(januslinux chroot) \u:\w\$ ' \
PATH="/busybox/bin:/usr/local/sbin:/usr/local/bin:/usr/bin" \
/usr/bin/bash --login +h
umount $ROOTFS/usr/janus/KEEP $ROOTFS/usr/janus/packages $ROOTFS/usr/janus/toolchain
umount $ROOTFS/proc $ROOTFS/sys $ROOTFS/dev $ROOTFS/tmp
umount $ROOTFS/output/sources $ROOTFS/output/packages
umount $ROOTFS/output/stage $ROOTFS/output/initrd
}
generate_stage_archive() {
set +e
printmsg "Building stage archive"
pkginstallstage filesystem linux-headers musl zlib m4 bison flex libelf binutils gmp mpfr mpc isl gcc attr acl libcap sed pkgconf ncurses shadow util-linux e2fsprogs libtool bzip2 perl readline autoconf automake bash bc diffutils file gettext kbd make xz kmod patch busybox libressl ca-certificates dosfstools gperf eudev linux nano vim lzip lzo zstd btrfs-progs xfsprogs curl wget libarchive git npkg prt-get
cd $STAGE
tar -cJf $CWD/januslinux-$RELEASE-$BARCH.tar.xz .
}
generate_initrd() {
set +e
printmsg "Building initrd archive"
pkginstallinitrd filesystem linux-headers musl zlib attr acl libcap sed ncurses shadow util-linux e2fsprogs bzip2 readline bash file kbd xz kmod busybox libressl ca-certificates dosfstools eudev linux lzo zstd btrfs-progs xfsprogs nano libnl wpa_supplicant curl lzip
cd $INITRD
ln -sf busybox usr/bin/wget
rm -rf usr/include
find . | cpio -R root:root -H newc -o | xz -9 --check=none > $IMAGE/rootfs.cpio.xz
cp boot/vmlinuz* $IMAGE/vmlinuz
}
generate_iso_x86() {
set +e
printmsg "Building *.iso image"
cd $SOURCES
curl -C - -O -L https://mirrors.edge.kernel.org/pub/linux/utils/boot/syslinux/syslinux-6.03.tar.xz
tar -xf syslinux-6.03.tar.xz
cp syslinux-6.03/bios/core/isolinux.bin $IMAGE/isolinux.bin
cp syslinux-6.03/bios/com32/elflink/ldlinux/ldlinux.c32 $IMAGE/ldlinux.c32
cp syslinux-6.03/bios/com32/libutil/libutil.c32 $IMAGE/libutil.c32
cp syslinux-6.03/bios/com32/menu/menu.c32 $IMAGE/menu.c32
cat << CEOF > $IMAGE/syslinux.cfg
UI menu.c32
PROMPT 0
TIMEOUT 10
DEFAULT default
MENU TITLE januslinux boot menu:
MENU MARGIN 10
MENU VSHIFT 5
MENU ROWS 5
MENU TABMSGROW 14
MENU TABMSG Press ENTER to boot, TAB to edit, or press F1 for more information.
MENU HELPMSGROW 15
MENU HELPMSGENDROW -3
MENU AUTOBOOT BIOS default device boot in # second{,s}...
LABEL default
MENU LABEL januslinux
TEXT HELP
Fast and compact Linux distribution which uses musl libc.
Coded and built by januslinux Inc. 2016-2018
ENDTEXT
LINUX vmlinuz
INITRD rootfs.cpio.xz
APPEND quiet
CEOF
mkdir -p $IMAGE/efi/boot
cat << CEOF > $IMAGE/efi/boot/startup.nsh
echo -off
echo januslinux starting...
\\vmlinuz quiet initrd=\\rootfs.cpio.xz
CEOF
xorriso \
-as mkisofs \
-J -r -o $CWD/januslinux-$RELEASE-$BARCH.iso \
-b isolinux.bin \
-c boot.cat \
-input-charset UTF-8 \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
$IMAGE
isohybrid $CWD/januslinux-$RELEASE-$BARCH.iso
}
generate_iso_arm() {
mkdir -p $IMAGE/efi/boot
cat << CEOF > $IMAGE/efi/boot/startup.nsh
echo -off
echo januslinux starting...
\\vmlinuz quiet initrd=\\rootfs.cpio.xz
CEOF
xorriso \
-as mkisofs \
-J -r -o $CWD/januslinux-$RELEASE-$BARCH.iso \
-input-charset UTF-8 \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
$IMAGE
isohybrid -u $CWD/januslinux-$RELEASE-$BARCH.iso 2>/dev/null || true
}
generate_iso() {
case $BARCH in
x86_64*|i686*)
generate_iso_x86
;;
aarch64|armv7h)
generate_iso_arm
;;
*)
printmsgerror "Unsupported for $BARCH"
esac
}
OPT="$1"
JPKG="$2"
case "$OPT" in
toolchain)
check_for_root
setup_architecture
setup_environment
make_environment
build_toolchain
;;
bootstrap)
check_for_root
setup_architecture
setup_environment
make_environment
build_toolchain
bootstrap_rootfs
clean_packages
;;
enter-chroot)
check_for_root
setup_environment
enter_chroot
;;
stage)
check_for_root
setup_architecture
setup_environment
mountall
generate_stage_archive
umountall
;;
image)
check_for_root
setup_architecture
setup_environment
mountall
generate_initrd
generate_iso
umountall
;;
usage|*)
mkusage
esac
exit 0