-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
303 lines (274 loc) · 10.1 KB
/
build.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
#!/bin/bash
# sh0wer
# Exit if user isn't root
[ "$(id -u)" -ne 0 ] && {
echo 'Please run as root'
exit 1
}
# Stage 0: Get download links
# * If any link is filled, the script will download from that link.
# * If empty, get the latest version from the website.
source ./.env
[ -z "$CHECKRA1N_AMD64" ] && {
CHECKRA1N_AMD64=$(curl -s "https://checkra.in/releases/" | grep -Po "https://assets.checkra.in/downloads/linux/cli/x86_64/[0-9a-f]*/checkra1n")
}
[ -z "$CHECKRA1N_I486" ] && {
CHECKRA1N_I486=$(curl -s "https://checkra.in/releases/" | grep -Po "https://assets.checkra.in/downloads/linux/cli/i486/[0-9a-f]*/checkra1n")
}
[ -z "$SILEO" ] && {
SILEO="https://github.com$(curl -s https://github.com/Sileo/Sileo/releases | grep -Po "/Sileo\/Sileo/releases/download/[\d.]+/org\.coolstar\.sileo_[\d.]+_iphoneos-arm\.deb" | head -1)"
}
[ -z "$ZSTD" ] && {
ZSTD="https://github.com$(curl -s https://github.com/facebook/zstd/releases | grep -Po "/facebook\/zstd/releases/download/v[\d.]+/zstd-[\d.]+\.tar\.gz" | head -1)"
}
[ -z "$DEVERSER" ] && {
DEVERSER="https://github.com$(curl -s https://github.com/beerpiss/deverser.py/releases | grep -Po "\/beerpiss\/deverser.py\/releases\/download\/v[\d.%2Bgita-f]+\/deverser-linux_v[\d.%2Bgita-f]+" | head -1)"
}
# Stage 1: User input
# Ask for the version and architecture if variables are empty
while [ -z "$VERSION" ]; do
printf 'Version: '
read -r VERSION
done
until [ "$ARCH" = 'x86_64' ] || [ "$ARCH" = 'x86' ]; do
echo '1 x86_64'
echo '2 x86'
printf 'Which architecture? x86_64 (default) or x86 '
read -r input_arch
[ "$input_arch" = 1 ] || [ -z "$input_arch" ] && {
ARCH='x86_64'
}
[ "$input_arch" = 2 ] && {
ARCH='x86'
}
done
[ "$ARCH" = "x86_64" ] && {
REPO_ARCH="amd64"
KERNEL_ARCH="amd64"
CHECKRA1N="$CHECKRA1N_AMD64"
}
[ "$ARCH" = "x86" ] && {
dpkg --add-architecture i386
apt-get update
apt install -y --no-install-recommends libusb-1.0-0-dev:i386 gcc-multilib
REPO_ARCH="i386"
KERNEL_ARCH="686"
CHECKRA1N="$CHECKRA1N_I486"
}
# Stage 2: Preparations
# * Deletes old build attempts
# * Set some shell options useful for debugging, see set(1p)
# * Starts a stopwatch for build time
{
umount work/chroot/proc
umount work/chroot/sys
umount work/chroot/dev
} > /dev/null 2>&1
rm -rf work
set -e -u -x
start_time="$(date -u +%s)"
# Stage 3: Building
# * Creates a fresh working directory
# * Fetches the alpine minirootfs
# * Configures the base system
# * Install required packages
# * Configure services
# * Stripping unneeded kernel modules
# * Remove unneeded files and directories
mkdir -p work/chroot work/iso/live work/iso/boot/grub
debootstrap --variant=minbase --arch="$REPO_ARCH" sid work/chroot 'http://deb.debian.org/debian/'
mount --bind /proc work/chroot/proc
mount --bind /sys work/chroot/sys
mount --bind /dev work/chroot/dev
cp /etc/resolv.conf work/chroot/etc
cat << ! | chroot work/chroot /bin/bash
# Set debian frontend to noninteractive
export DEBIAN_FRONTEND=noninteractive
# Install required packages
apt-get update
apt-get install -y --no-install-recommends busybox linux-image-$KERNEL_ARCH live-boot \
systemd systemd-sysv usbmuxd libusbmuxd-tools openssh-client sshpass dialog \
build-essential curl ca-certificates
curl -LO $ZSTD
tar xf zstd*.tar.gz -C /opt
(
cd /opt/zstd*
make
make install
)
rm -rf zstd*.tar.gz /opt/zstd*
ln -sf /usr/local/bin/zstd /usr/bin/zstd
ln -sf /usr/local/lib/libzstd.so.*.* /usr/lib/*-linux-gnu/libzstd.so
ln -sf /usr/local/lib/libzstd.so.*.* /usr/lib/*-linux-gnu/libzstd.so.1
!
# Switch compression to zstd 22 for space savings
sed -i 's/COMPRESS=gzip/COMPRESS=zstd/' work/chroot/etc/initramfs-tools/initramfs.conf
sed -i 's/zstd -q -19 -T0/zstd -q --ultra -22 -T0/g' work/chroot/sbin/mkinitramfs
# Debloating Debian
# * Removing unneeded kernel modules (360MB size reduction)
if [ -n "$KERNEL_MODULES" ]; then
cat work/chroot/etc/initramfs-tools/modules >> "$KERNEL_MODULES"
sed -i '/^[[:blank:]]*#/d;s/#.*//;/^$/d' "$KERNEL_MODULES"
modules_to_keep=()
while IFS="" read -r p || [ -n "$p" ]
do
modules_to_keep+=("-not" "-name" "$p")
done < "$KERNEL_MODULES"
find work/chroot/lib/modules/*/kernel/* -type f "${modules_to_keep[@]}" -delete
find work/chroot/lib/modules/*/kernel/* -type d -empty -delete
fi
# * Compress kernel modules
find work/chroot/lib/modules/*/kernel/* -type f -name "*.ko" -exec strip --strip-unneeded {} +
find work/chroot/lib/modules/*/kernel/* -type f -name "*.ko" -exec xz --x86 -e9T0 {} +
depmod -b work/chroot "$(basename "$(find work/chroot/lib/modules/* -maxdepth 0)")"
chroot work/chroot update-initramfs -u
cp work/chroot/vmlinuz work/iso/boot
cp work/chroot/initrd.img work/iso/boot
# Copying scripts & Downloading resources
mkdir -p work/chroot/opt/{odysseyra1n,a9x,pongoOS-latest}
cp scripts/* work/chroot/usr/local/bin
cp assets/.dialogrc work/chroot/root/.dialogrc
cp assets/PongoConsolidated.bin work/chroot/opt/a9x
(
cd work/chroot/usr/local/bin
curl -sLO "$CHECKRA1N"
curl -sLO "$DEVERSER"
)
if [ "$GITHUB_ACTIONS" = true ]; then
cp assets/odysseyra1n/odysseyra1n_resources.tar.zst work/chroot/opt/odysseyra1n
if [ "$PONGO_LATEST" = 'YES' ]; then
chroot work/chroot apt-get install -y --no-install-recommends python3
find assets/pongoOS/build/* -maxdepth 1 -type f -exec cp "{}" work/chroot/opt/pongoOS-latest/ \;
find assets/pongoOS/scripts/* -maxdepth 1 -type f -name '*.py' -exec cp "{}" work/chroot/usr/local/bin \;
rename -f 's/\.py$//' work/chroot/usr/local/bin/*.py
fi
else
(
cd work/chroot/opt/odysseyra1n
curl -sL -O https://github.com/coolstar/Odyssey-bootstrap/raw/master/bootstrap_1500.tar.gz \
-O https://github.com/coolstar/Odyssey-bootstrap/raw/master/bootstrap_1600.tar.gz \
-O https://github.com/coolstar/Odyssey-bootstrap/raw/master/bootstrap_1700.tar.gz \
-O "$SILEO" \
-O https://github.com/coolstar/Odyssey-bootstrap/raw/master/org.swift.libswift_5.0-electra2_iphoneos-arm.deb
# Rolling everything into one zstd-compressed tarball (reduces size hugely)
gzip -dv ./*.tar.gz
tar -vc ./* | zstd -zcT0 --ultra -22 > odysseyra1n_resources.tar.zst
find ./* -not -name "odysseyra1n_resources.tar.zst" -exec rm {} +
)
if [ "$PONGO_LATEST" = 'YES' ]; then
chroot work/chroot apt-get install -y --no-install-recommends python3
(
cd work
git clone --recursive --depth 1 https://github.com/checkra1n/pongoOS
cd pongoOS
make all
)
find work/pongoOS/build/* -maxdepth 1 -type f -exec cp "{}" work/chroot/opt/pongoOS-latest/ \;
find work/pongoOS/scripts/* -maxdepth 1 -type f -name '*.py' -exec cp "{}" work/chroot/usr/local/bin/ \;
rename -f 's/\.py$//' work/chroot/usr/local/bin/*.py
fi
fi
chmod a+x work/chroot/usr/local/bin/*
# Configuring autologin
mkdir -p work/chroot/etc/systemd/system/getty@tty1.service.d
cat << ! > work/chroot/etc/systemd/system/getty@tty1.service.d/override.conf
[Service]
ExecStart=
ExecStart=-/sbin/agetty --noissue --autologin root %I
Type=idle
!
# Configure grub
cat << ! > work/iso/boot/grub/grub.cfg
insmod all_video
echo ''
echo ' .-. '
echo ' ( ). '
echo ' (___(__) '
echo " ' ' ' ' "
echo " ' ' ' ' "
echo ''
echo ' sh0wer '
echo ' by beerpsi '
linux /boot/vmlinuz boot=live quiet
initrd /boot/initrd.img
boot
!
# * Change hostname
# * configure .bashrc
# * configure .dialogrc
echo "$NAME" > work/chroot/etc/hostname
cat << ! > work/chroot/root/.bashrc
export VERSION='$VERSION'
export DIALOGRC=/root/.dialogrc
/usr/local/bin/menu
!
# * Purge a bunch of packages that won't be used anyway
cat << ! | chroot work/chroot /bin/bash
export DEBIAN_FRONTEND=noninteractive
apt-get -y purge make dpkg-dev g++ gcc libc-dev make build-essential curl ca-certificates \
perl-modules-5.32 perl libdpkg-perl
apt-get -y purge libffi8 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0
apt-get -y autoremove
dpkg -P --force-all apt cpio gzip libgpm2
dpkg -P --force-all initramfs-tools initramfs-tools-core
dpkg -P --force-all debconf libdebconfclient0
dpkg -P --force-all init-system-helpers
dpkg -P --force-all perl-base dpkg
!
# * Replacing coreutils with their busybox equivalents
cat << "!" | chroot work/chroot /bin/bash
ln -sfv "$(command -v busybox)" /usr/bin/which
busybox --list | egrep -v "(busybox)|(init)|(sh)" | while read -r line; do
if which $line &> /dev/null; then # If command exists
if [ "$(stat -c%s $(which $line))" -gt 16 ]; then # And we can gain storage space from making a symlink (symlinks are 16 bytes)
ln -sfv "$(which busybox)" "$(which $line)" # Then make one (ignore nonexistent commands /shrug)
fi
fi
done
!
# * Empty unused directories
(
cd work/chroot
rm -f etc/mtab \
etc/fstab \
etc/ssh/ssh_host* \
root/.wget-hsts \
root/.bash_history \
lib/xtables/libip6t_*
rm -rf var/log/* \
var/cache/* \
var/backups/* \
var/lib/apt/* \
var/lib/dpkg/* \
usr/lib/apt/* \
usr/lib/locale/* \
usr/local/include/* \
usr/local/share/man/* \
usr/include/* \
usr/share/doc/* \
usr/share/man/* \
usr/share/fonts/* \
usr/share/info/* \
usr/share/icons/* \
usr/share/locale/* \
usr/share/zoneinfo/* \
usr/share/perl*/* \
usr/lib/modules/* \
boot/*
)
# Stage 4: Build the ISO
# * Make an zstd-compressed squashfs
umount work/chroot/proc
umount work/chroot/sys
umount work/chroot/dev
mksquashfs work/chroot work/iso/live/filesystem.squashfs -noappend -e boot -comp zstd -Xcompression-level 22
## Creates output ISO dir (easier for GitHub Actions)
mkdir -p out
grub-mkrescue -o "out/$NAME-$VERSION-$ARCH.iso" work/iso \
--compress=xz \
--fonts='' \
--locales='' \
--themes=''
end_time="$(date -u +%s)"
elapsed_time="$((end_time - start_time))"
echo "Built $NAME-$VERSION-$ARCH in $((elapsed_time / 60)) minutes and $((elapsed_time % 60)) seconds."