-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild_rootfs_tar.sh
executable file
·204 lines (165 loc) · 5.01 KB
/
build_rootfs_tar.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
#!/bin/bash
if ! pacman -Q musl &>/dev/null; then
echo "Error: package 'musl' not found" >&2
exit 1
fi
if ! command -v make &>/dev/null; then
echo "Error: command 'make' not found" >&2
exit 1
fi
if ! command -v strip &>/dev/null; then
echo "Error: package 'binutils' not found" >&2
exit 1
fi
if ! command -v tar &>/dev/null; then
echo "Error: command 'tar' not found" >&2
exit 1
fi
if ! command -v curl &>/dev/null; then
echo "Error: command 'curl' not found" >&2
exit 1
fi
if [ -e /usr/include/linux ] && \
[ -e /usr/include/asm ] && \
[ -e /usr/include/asm-generic ]; then
:
else
echo "Error: package 'linux-api-headers' not found" >&2
exit 1
fi
if [ -e /usr/lib/musl/include/linux ] && \
[ -e /usr/lib/musl/include/asm ] && \
[ -e /usr/lib/musl/include/asm-generic ]; then
:
else
echo "Error: package 'kernel-headers-musl' not found" >&2
exit 1
fi
if [ ! -d ./work ]; then
echo "Error: directory 'work' does not exists" >&2
exit 1
fi
if [ ! -d ./skuf_src ]; then
echo "Error: directory 'skuf_src' does not exists" >&2
exit 1
fi
if [ ! -f ./busybox_config ]; then
echo "Error: file 'busybox_config' does not exists" >&2
exit 1
fi
for __patches in ./busybox_*.patch ./kexec-tools_*.patch; do
[ -e "$__patches" ] || continue
if command -v patch &>/dev/null; then
break
else
echo "Error: .patch files are found, but the 'patch' command is not" >&2
exit 1
fi
done
set -e
set -x
rm -r -f ./work/{busybox,kexec,rootfs}
rm -f ./skuf_src/rootfs.tar
ver_busybox=1.36.1
ver_kexec=2.0.29
if [ -n "$CC" ]; then
if ! command -v "$CC" &>/dev/null; then
echo "Error: command '$CC' not found" >&2
exit 1
fi
if [ -n "$muslCC" ] && command -v "$muslCC" &>/dev/null; then
:
elif command -v "musl-$CC" &>/dev/null; then
muslCC="musl-$CC"
else
muslCC="$CC"
fi
elif command -v musl-clang &>/dev/null && command -v clang &>/dev/null; then
CC=clang
muslCC=musl-clang
elif command -v musl-gcc &>/dev/null && command -v gcc &>/dev/null; then
CC=gcc
muslCC=musl-gcc
else
echo "Error: commands 'musl-gcc' and 'gcc' not found" >&2
exit 1
fi
backtome="$(realpath .)"
##################################################
echo "[] Creating directory structure"
sleep 1
install -d -m 755 ./work/rootfs
install -d -m 755 ./work/rootfs/bin
install -d -m 755 ./work/rootfs/dev
install -d -m 755 ./work/rootfs/etc
install -d -m 755 ./work/rootfs/proc
install -d -m 700 ./work/rootfs/root
install -d -m 755 ./work/rootfs/run
install -d -m 755 ./work/rootfs/sys
install -d -m 755 ./work/rootfs/tmp
install -d -m 755 ./work/rootfs/var
ln -s /run ./work/rootfs/var/run
: > ./work/rootfs/init
chmod 755 ./work/rootfs/init
cat <<EOF > ./work/rootfs/init
#!/bin/ash
EOF
##################################################
echo "[] Building busybox $ver_busybox"
sleep 1
install -d -m 755 ./work/busybox
pushd ./work/busybox
curl -L -o ./busybox-$ver_busybox.tar.bz2 "https://busybox.net/downloads/busybox-$ver_busybox.tar.bz2"
tar -x -p -f ./busybox-$ver_busybox.tar.bz2
pushd ./busybox-$ver_busybox
install -m 644 "$backtome"/busybox_config ./.config
for bpatch in "$backtome"/busybox_*.patch; do
[ -e "$bpatch" ] || continue
patch -Np1 -i "$bpatch"
done
KCONFIG_NOTIMESTAMP=1 make CC="$muslCC"
install -m 755 ./busybox "$backtome"/work/rootfs/bin/busybox
popd
popd
##################################################
echo "[] Building kexec $ver_kexec"
sleep 1
install -d -m 755 ./work/kexec
pushd ./work/kexec
curl -L -o ./kexec-$ver_kexec.tar.gz "https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/snapshot/kexec-tools-$ver_kexec.tar.gz"
tar -x -p -f ./kexec-$ver_kexec.tar.gz
pushd ./kexec-tools-$ver_kexec
for kpatch in "$backtome"/kexec-tools_*.patch; do
[ -e "$kpatch" ] || continue
patch -Np1 -i "$kpatch"
done
./bootstrap
CFLAGS+=" -mtune=generic -pipe"
CFLAGS="$CFLAGS" LDFLAGS=-static CC="$CC" ./configure
make
install -m 755 ./build/sbin/kexec "$backtome"/work/rootfs/bin/kexec
popd
popd
##################################################
echo "[] Striping"
sleep 1
pushd ./work/rootfs/bin
strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag ./busybox
strip -S --strip-unneeded --remove-section=.note.gnu.gold-version --remove-section=.comment --remove-section=.note --remove-section=.note.gnu.build-id --remove-section=.note.ABI-tag ./kexec
popd
##################################################
echo "[] Creating busybox symlinks"
sleep 1
pushd ./work/rootfs/bin
for applet in $(./busybox --list); do
ln -s busybox ./"$applet"
done
popd
echo "[] rootfs.tar creation"
sleep 1
pushd ./work/rootfs
tar --create * --numeric-owner --owner=0 --group=0 --format=ustar -f "$backtome"/skuf_src/rootfs.tar
popd
##################################################
echo "Done."
# vim: set ft=sh ts=4 sw=4 et: