-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.sh
executable file
·370 lines (294 loc) · 11.4 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
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
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-only
# Copyright 2023 Dang Huynh <danct12@disroot.org>
set -e
SUPPORTED_ARCHES=(aarch64 armv7)
NOCONFIRM=0
OSK_SDL=0
NO_BOOTLOADER=0
use_pipewire=0
output_folder="build"
mkdir -p "$output_folder"
cachedir="$output_folder/pkgcache"
temp=$(mktemp -p $output_folder -d)
date=$(date +%Y%m%d)
error() { echo -e "\e[41m\e[5mERROR:\e[49m\e[25m $1" && exit 1; }
check_dependency() { [ $(which $1) ] || error "$1 not found. Please make sure it is installed and on your PATH."; }
usage() { error "$0 [-a ARCHITECTURE] [-d device] [-u ui] [-h hostname] [--osk-sdl] [--noconfirm] [--cachedir directory] [--no-cachedir]"; }
cleanup() {
trap '' EXIT
trap '' INT
if [ -d "$temp" ]; then
unmount_chroot
rm -r "$temp"
fi
}
trap cleanup EXIT
trap cleanup INT
pre_check() {
check_dependency wget
check_dependency bsdtar
check_dependency fallocate
check_dependency fdisk
check_dependency losetup
check_dependency mkfs.vfat
check_dependency mkfs.ext4
check_dependency genfstab
check_dependency lsof
chmod 755 "$temp"
}
parse_args() {
while [ $# -gt 0 ]; do
case $1 in
-a|--arch) arch=$2; shift ;;
-d|--device) device=$2; shift ;;
-u|--ui) ui=$2; shift ;;
-h|--hostname) hostname=$2; shift ;;
--noconfirm) NOCONFIRM=1;;
--osk-sdl) OSK_SDL=1;;
--cachedir) cachedir=$2; shift ;;
--no-cachedir) cachedir= ;;
*) usage ;;
esac
shift
done
}
parse_presets() {
[ ! -e "devices/$device" ] && error "Device \"$device\" is unknown!"
[ ! -e "ui/$ui" ] && error "User Interface \"$ui\" is unknown!"
[ ! -e "devices/$device/config" ] && error "\"$device\" doesn't have a config file!" \
|| source "devices/$device/config"
for i in $(cat "devices/$device/packages"); do
packages_device+=( $i )
done
for i in $(cat "ui/$ui/packages"); do
[ $use_pipewire -gt 0 ] && packages_ui+=( pipewire-audio pipewire-alsa pipewire-jack pipewire-pulse )
packages_ui+=( $i )
done
if [ -e "devices/$device/packages-$ui-extra" ]; then
for i in $(cat "devices/$device/packages-$ui-extra"); do
packages_ui+=( $i )
done
fi
if [ -e "ui/$ui/postinstall" ]; then
while IFS= read -r postinstall_line; do
postinstall+=("$postinstall_line\n")
done < ui/$ui/postinstall
fi
}
check_arch() {
echo ${SUPPORTED_ARCHES[@]} | grep -q $arch || { echo "$arch is not supported. Supported architecture are: ${SUPPORTED_ARCHES[@]}" && exit 1; }
}
download_rootfs() {
[ $NOCONFIRM -gt 0 ] && [ -f "$output_folder/ArchLinuxARM-$arch-latest.tar.gz" ] && return
[ -f "$output_folder/ArchLinuxARM-$arch-latest.tar.gz" ] && {
read -rp "Stock rootfs already exist, delete it? (y/n) " yn
case $yn in
[Yy]*) rm "$output_folder/ArchLinuxARM-$arch-latest.tar.gz" ;;
[Nn]*) return ;;
*) echo "Aborting." && exit 1 ;;
esac; }
wget -O "$output_folder/ArchLinuxARM-$arch-latest.tar.gz" http://os.archlinuxarm.org/os/ArchLinuxARM-$arch-latest.tar.gz
pushd .
cd $output_folder && { curl -s -L http://os.archlinuxarm.org/os/ArchLinuxARM-$arch-latest.tar.gz.md5 | md5sum -c \
|| { rm ArchLinuxARM-$arch-latest.tar.gz && error "Rootfs checksum failed!"; } }
popd
}
extract_rootfs() {
[ -f "$output_folder/ArchLinuxARM-$arch-latest.tar.gz" ] || error "Rootfs not found"
bsdtar -xpf "$output_folder/ArchLinuxARM-$arch-latest.tar.gz" -C "$temp"
}
mount_chroot() {
mount -o bind /dev "$temp/dev"
mount -t proc proc "$temp/proc"
mount -t sysfs sys "$temp/sys"
mount -t tmpfs tmpfs "$temp/tmp"
}
unmount_chroot() {
for i in $(lsof +D "$temp" | tail -n+2 | tr -s ' ' | cut -d ' ' -f 2 | sort -nu); do
kill -9 $i
done
for i in $(cat /proc/mounts | awk '{print $2}' | grep "^$(readlink -f $temp)"); do
[ $i ] && umount -l $i
done
}
mount_cache() {
if [ -n "$cachedir" ]; then
mkdir -p "$cachedir"
mount --bind "$cachedir" "$temp/var/cache/pacman/pkg" || error "Failed to mount pkg cache!";
fi
}
unmount_cache() {
if [[ $(findmnt "$temp/var/cache/pacman/pkg") ]]; then
umount -l "$temp/var/cache/pacman/pkg" || error "Failed to unmount pkg cache!";
fi
}
do_chroot() {
chroot "$temp" "$@"
}
init_rootfs() {
if [ $OSK_SDL -gt 0 ]; then
rootfs_tarball="rootfs-$device-$ui-$date-osksdl.tar.gz"
packages_ui+=( osk-sdl )
else
rootfs_tarball="rootfs-$device-$ui-$date.tar.gz"
fi
[ $NOCONFIRM -gt 0 ] && [ -f "$output_folder/$rootfs_tarball" ] && rm "$output_folder/$rootfs_tarball"
[ -f "$output_folder/$rootfs_tarball" ] && {
read -rp "Rootfs seems to have generated before, delete it? (y/n) " yn
case $yn in
[Yy]*) rm "$output_folder/$rootfs_tarball" ;;
[Nn]*) return ;;
*) echo "Aborting." && exit 1 ;;
esac; }
download_rootfs
extract_rootfs
mount_chroot
mount_cache
rm "$temp/etc/resolv.conf"
cat /etc/resolv.conf > "$temp/etc/resolv.conf"
cp "overlays/base/etc/pacman.conf" "$temp/etc/pacman.conf"
if [[ $ui = "barebone" ]]; then
sed -i 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' "$temp/etc/locale.gen"
fi
echo "${hostname:-danctnix}" > "$temp/etc/hostname"
# Download our gpg key and install it first, this however will be overwritten with our package later.
wget https://raw.githubusercontent.com/dreemurrs-embedded/Pine64-Arch/master/PKGBUILDS/danctnix/danctnix-keyring/danctnix.gpg \
-O "$temp/usr/share/pacman/keyrings/danctnix.gpg"
wget https://raw.githubusercontent.com/dreemurrs-embedded/Pine64-Arch/master/PKGBUILDS/danctnix/danctnix-keyring/danctnix-trusted \
-O "$temp/usr/share/pacman/keyrings/danctnix-trusted"
cat > "$temp/second-phase" <<EOF
#!/bin/bash
set -e
pacman-key --init
pacman-key --populate archlinuxarm danctnix
pacman-key --lsign-key 68B3537F39A313B3E574D06777193F152BDBE6A6
pacman -Rsn --noconfirm linux-$arch
pacman -Syu --noconfirm --overwrite=*
pacman -S --noconfirm --overwrite=* --needed ${packages_device[@]} ${packages_ui[@]}
systemctl disable sshd
systemctl disable systemd-networkd
systemctl disable systemd-resolved
systemctl enable zramswap
systemctl enable NetworkManager
usermod -a -G network,video,audio,rfkill,wheel alarm
$(echo -e "${postinstall[@]}")
cp -rv /etc/skel/. /home/alarm
chown -R alarm:alarm /home/alarm
if [ -e /etc/sudoers ]; then
sed -i 's/# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /etc/sudoers
fi
cat << FOE | passwd alarm
123456
123456
FOE
locale-gen
echo "LANG=en_US.UTF-8" > /etc/locale.conf
ln -s ../usr/share/zoneinfo/Europe/London /etc/localtime
# remove pacman gnupg keys post generation
rm -rf /etc/pacman.d/gnupg
rm /second-phase
EOF
chmod +x "$temp/second-phase"
do_chroot /second-phase || error "Failed to run the second phase rootfs build!"
cp -r overlays/base/* "$temp/"
[ -d "overlays/$ui" ] && cp -r overlays/$ui/* "$temp/"
[ -d "devices/$device/overlays/base" ] && cp -r devices/$device/overlays/base/* "$temp/"
[ -d "devices/$device/overlays/$ui" ] && cp -r devices/$device/overlays/$ui/* "$temp/"
if [ -e "$temp/usr/lib/initcpio/hooks/resizerootfs" ] && [ $OSK_SDL -gt 0 ]; then
rm -f $temp/usr/lib/initcpio/hooks/resizerootfs
rm -f $temp/usr/lib/initcpio/install/resizerootfs
fi
[ -e "$temp/usr/lib/initcpio/hooks/resizerootfs" ] && sed -i '/^HOOKS=/s/fsck/resizerootfs fsck/g' "$temp/etc/mkinitcpio.conf"
[ -e "$temp/usr/lib/initcpio/hooks/osk-sdl" ] && sed -i '/^HOOKS=/s/fsck/osk-sdl fsck/g' "$temp/etc/mkinitcpio.conf"
sed -i "s/REPLACEDATE/$date/g" "$temp/usr/local/sbin/first_time_setup.sh"
[[ "$ui" != "barebone" ]] && do_chroot passwd -dl root
[ -d "$temp/usr/share/glib-2.0/schemas" ] && do_chroot /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
do_chroot mkinitcpio -P
unmount_cache
yes | do_chroot pacman -Scc
unmount_chroot
echo "Creating tarball: $rootfs_tarball ..."
pushd .
cd $temp && bsdtar -czpf ../$rootfs_tarball .
popd
rm -rf $temp
}
make_image() {
[ ! -e "$output_folder/$rootfs_tarball" ] && \
error "Rootfs not found! (how did you get here?)"
[ $NOCONFIRM -gt 0 ] && [ -f "$output_folder/archlinux-$device-$ui-$date.img" ] && \
rm "$output_folder/archlinux-$device-$ui-$date.img"
[ -f "$output_folder/archlinux-$device-$ui-$date.img" ] && {
read -rp "Disk image already exist, delete it? (y/n) " yn
case $yn in
[Yy]*) rm "$output_folder/archlinux-$device-$ui-$date.img" ;;
[Nn]*) return ;;
*) echo "Aborting." && exit 1 ;;
esac; }
disk_size="$(eval "echo \${size_ui_$ui}")"
disk_output="$output_folder/archlinux-$device-$ui-$date.img"
echo "Generating a blank disk image ($disk_size)"
fallocate -l $disk_size $disk_output
boot_part_start=${boot_part_start:-1}
boot_part_size=${boot_part_size:-128}
echo "Boot partition start: ${boot_part_start}MB"
echo "Boot partition size: ${boot_part_size}MB"
parted -s $disk_output mktable gpt
parted -s $disk_output mkpart boot fat32 ${boot_part_start}MB $[boot_part_start+boot_part_size]MB
parted -s $disk_output set 1 esp on
parted -s $disk_output mkpart rootfs ext4 $[boot_part_start+boot_part_size]MB '100%'
echo "Attaching loop device"
loop_device=$(losetup -f)
losetup -P $loop_device "$output_folder/archlinux-$device-$ui-$date.img"
echo "Creating filesystems"
mkfs.vfat ${loop_device}p1
mkfs.ext4 ${loop_device}p2
mkdir -p $temp
echo "Mounting disk image"
mount ${loop_device}p2 $temp
mkdir -p $temp/boot
mount ${loop_device}p1 $temp/boot
echo "Extracting rootfs to image"
bsdtar -xpf "$output_folder/$rootfs_tarball" -C "$temp" || true
[ $NO_BOOTLOADER -lt 1 ] && {
echo "Installing bootloader"
case $platform in
"rockchip")
dd if=$temp/boot/idbloader.img of=$loop_device seek=64 conv=notrunc,fsync
dd if=$temp/boot/u-boot.itb of=$loop_device seek=16384 conv=notrunc,fsync
;;
*)
dd if=$temp/boot/$bootloader of=$loop_device bs=128k seek=1
;;
esac; }
echo "Generating fstab"
genfstab -U $temp | grep UUID | grep -v "swap" | tee -a $temp/etc/fstab
echo "Unmounting disk image"
umount -R $temp
rm -rf $temp
losetup -d $loop_device
}
make_squashfs() {
check_dependency mksquashfs
[ $NOCONFIRM -gt 0 ] && [ -f "$output_folder/archlinux-$device-$ui-$date.sqfs" ] && \
rm "$output_folder/archlinux-$device-$ui-$date.sqfs"
[ -f "$output_folder/archlinux-$device-$ui-$date.sqfs" ] && {
read -rp "Squashfs image already exist, delete it? (y/n) " yn
case $yn in
[Yy]*) rm "$output_folder/archlinux-$device-$ui-$date.sqfs" ;;
[Nn]*) return ;;
*) echo "Aborting." && exit 1 ;;
esac; }
mkdir -p "$temp"
bsdtar -xpf "$output_folder/$rootfs_tarball" -C "$temp"
mksquashfs "$temp" "$output_folder/archlinux-$device-$ui-$date.sqfs"
rm -rf "$temp"
}
pre_check
parse_args $@
[[ "$arch" && "$device" && "$ui" ]] || usage
check_arch
parse_presets
init_rootfs
[ $OSK_SDL -gt 0 ] && make_squashfs || make_image