This repository has been archived by the owner on Dec 24, 2021. It is now read-only.
forked from anarsoul/linux-build
-
Notifications
You must be signed in to change notification settings - Fork 6
/
make_rootfs.sh
executable file
·213 lines (167 loc) · 5.43 KB
/
make_rootfs.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
#!/bin/bash
set -e
BUILD="build"
OTHERDIR="otherfiles"
DEST="$1"
OUT_TARBALL="$2"
ROOTFS_PRESET="$3"
BUILD_ARCH=arm64
for i in wget qemu-arm-static qemu-aarch64-static bsdtar chroot; do
hash $i >/dev/null 2>&1 || {
echo >&2 "$i not found. Please install it."; exit 1;
}
done
if [ -f "presets/$ROOTFS_PRESET.sh" ]; then
source "presets/$ROOTFS_PRESET.sh"
else
echo "Preset $ROOTFS_PRESET does not exist! Exiting."
exit 1
fi
export LC_ALL=C
if [ -z "$DEST" ] || [ -z "$OUT_TARBALL" ] || [ -z "$ROOTFS_PRESET" ]; then
echo "Usage: $0 <destination-folder> <destination-tarball> <rootfs-preset>"
exit 1
fi
if [ "$(id -u)" -ne "0" ]; then
echo "This script requires root."
exit 1
fi
DEST=$(readlink -f "$DEST")
if [ ! -d "$DEST" ]; then
mkdir -p $DEST
fi
if [ "$(ls -A -Ilost+found $DEST)" ]; then
echo "Destination $DEST is not empty. Aborting."
exit 1
fi
TEMP=$(mktemp -d)
cleanup() {
if [ -e "$DEST/proc/cmdline" ]; then
umount "$DEST/proc"
fi
if [ -d "$DEST/sys/kernel" ]; then
umount "$DEST/sys"
fi
umount "$DEST/dev" || true
umount "$DEST/tmp" || true
if [ -d "$TEMP" ]; then
rm -rf "$TEMP"
fi
}
trap cleanup EXIT
ROOTFS="http://archlinuxarm.org/os/ArchLinuxARM-aarch64-latest.tar.gz"
TAR_OPTIONS=""
mkdir -p $BUILD
TARBALL="$BUILD/$(basename $ROOTFS)"
mkdir -p "$BUILD"
if [ ! -e "$TARBALL" ]; then
echo "Downloading $DISTRO rootfs tarball ..."
wget -O "$TARBALL" "$ROOTFS"
fi
# Extract with BSD tar
echo -n "Extracting ... "
set -x
bsdtar -xpf $TAR_OPTIONS "$TARBALL" -C "$DEST"
echo "OK"
# Add qemu emulation.
cp /usr/bin/qemu-aarch64-static "$DEST/usr/bin"
cp /usr/bin/qemu-arm-static "$DEST/usr/bin"
do_chroot() {
cmd="$@"
mount -o bind /tmp "$DEST/tmp"
mount -o bind /dev "$DEST/dev"
chroot "$DEST" mount -t proc proc /proc
chroot "$DEST" mount -t sysfs sys /sys
chroot "$DEST" $cmd
chroot "$DEST" umount /sys
chroot "$DEST" umount /proc
umount "$DEST/dev"
umount "$DEST/tmp"
}
mv "$DEST/etc/resolv.conf" "$DEST/etc/resolv.conf.dist"
cp /etc/resolv.conf "$DEST/etc/resolv.conf"
cat $OTHERDIR/pacman.conf > "$DEST/etc/pacman.conf"
if [[ "$ROOTFS_PRESET" = *"barebone"* ]]; then
# Barebone doesn't need more than en_US.
echo "en_US.UTF-8 UTF-8" > "$DEST/etc/locale.gen-all"
else
cp "$DEST/etc/locale.gen" "$DEST/etc/locale.gen-all"
fi
mv "$DEST/etc/pacman.d/mirrorlist" "$DEST/etc/pacman.d/mirrorlist.default"
echo "Server = http://sg.mirror.archlinuxarm.org/\$arch/\$repo" > "$DEST/etc/pacman.d/mirrorlist"
echo "danctnix" > "$DEST/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 "$DEST/usr/share/pacman/keyrings/danctnix.gpg"
wget https://raw.githubusercontent.com/dreemurrs-embedded/Pine64-Arch/master/PKGBUILDS/danctnix/danctnix-keyring/danctnix-trusted \
-O "$DEST/usr/share/pacman/keyrings/danctnix-trusted"
cat > "$DEST/second-phase" <<EOF
#!/bin/sh
pacman-key --init
pacman-key --populate archlinuxarm danctnix
pacman -Rsn --noconfirm linux-aarch64
pacman -Syu --noconfirm --overwrite=*
pacman -S --noconfirm --overwrite=* --disable-download-timeout --needed $PACKAGES_BASE $PACKAGES_UI
killall -KILL gpg-agent
systemctl disable sshd
systemctl disable systemd-networkd
systemctl disable systemd-resolved
systemctl enable zramswap
systemctl enable NetworkManager
usermod -a -G network,video,audio,optical,storage,input,scanner,games,lp,rfkill,wheel alarm
$POST_INSTALL
cp -rv /etc/skel/. /home/alarm
chown -R alarm:alarm /home/alarm
sed -i 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/' /etc/sudoers
cp /etc/locale.gen-all /etc/locale.gen
cd /usr/share/i18n/charmaps
# locale-gen can't spawn gzip when running under qemu-user, so ungzip charmap before running it
# and then gzip it back
gzip -d UTF-8.gz
locale-gen
gzip UTF-8
echo "LANG=en_US.UTF-8" > /etc/locale.conf
yes | pacman -Scc
EOF
chmod +x "$DEST/second-phase"
cp $OTHERDIR/change-alarm $DEST/
do_chroot /second-phase
do_chroot /change-alarm
rm $DEST/second-phase
rm $DEST/change-alarm
# Final touches
rm "$DEST/usr/bin/qemu-aarch64-static"
rm "$DEST/usr/bin/qemu-arm-static"
rm "$DEST/etc/locale.gen-all"
rm -f "$DEST"/*.core
rm "$DEST/etc/resolv.conf.dist" "$DEST/etc/resolv.conf"
touch "$DEST/etc/resolv.conf"
rm "$DEST/etc/pacman.d/mirrorlist"
mv "$DEST/etc/pacman.d/mirrorlist.default" "$DEST/etc/pacman.d/mirrorlist"
rm -rf "$DEST/etc/pacman.d/gnupg"
cp $OTHERDIR/first_time_setup.sh $DEST/usr/local/sbin/
sed -i "s/REPLACEDATE/$(date +%Y%m%d)/g" $DEST/usr/local/sbin/first_time_setup.sh
cp $OTHERDIR/81-blueman.rules $DEST/etc/polkit-1/rules.d/
cp -r $OTHERDIR/systemd/* $DEST/usr/lib/systemd/
do_chroot /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
# Replace Arch's with our own mkinitcpio
rm $DEST/etc/mkinitcpio.conf
cp $OTHERDIR/mkinitcpio.conf $DEST/etc/mkinitcpio.conf
cp $OTHERDIR/mkinitcpio-hooks/resizerootfs-hooks $DEST/usr/lib/initcpio/hooks/resizerootfs
cp $OTHERDIR/mkinitcpio-hooks/resizerootfs-install $DEST/usr/lib/initcpio/install/resizerootfs
if [[ "$ROOTFS_PRESET" = *"barebone"* ]]; then
# Barebone does not come with splash.
sed -i 's/bootsplash-danctnix//g' $DEST/etc/mkinitcpio.conf
fi
do_chroot mkinitcpio -p linux-megi
# Shiny MOTD
cp $OTHERDIR/motd $DEST/etc/motd
echo "Installed rootfs to $DEST"
# Create tarball with BSD tar
echo -n "Creating tarball ... "
pushd .
cd $DEST && bsdtar -czpf ../$OUT_TARBALL .
popd
rm -rf $DEST
set -x
echo "Done"