-
Notifications
You must be signed in to change notification settings - Fork 21
/
generate-cpio-rootfs.sh
executable file
·306 lines (263 loc) · 9.12 KB
/
generate-cpio-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
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
#!/bin/bash
#
# Usage: generate_cpio_rootfs.sh <platform_name>
# generate_cpio_rootfs.sh <platform_name> clean
# generate_cpio_rootfs.sh <platform_name> nocpio
#
# All credits to Linus Walleij that wrote this initially.
# exit as soon as an error is encoutered
set -e
echo "Generator for a simple initramfs root filesystem for some ARM targets"
CURDIR="$(dirname "$(readlink -f "$0")")"
STAGEDIR=${CURDIR}/stage
BUILDDIR=${CURDIR}/build
OUTFILE=${CURDIR}/rootfs-$1.cpio
BUSYBOXDIR=${CURDIR}/../busybox
if [ "$2" = "clean" -o "$2" = "distclean" ]; then
echo "Cleaning"
rm -rf ${STAGEDIR} ${BUILDDIR}
rm -f ${OUTFILE} ${CURDIR}/etc/hostname ${CURDIR}/etc/inittab
rm -f ${CURDIR}/filelist-tmp.txt ${CURDIR}/filelist-final.txt
exit 0
fi
STRACEVER=strace-4.7
STRACE=${CURDIR}/${STRACEVER}
# Helper function to copy one level of files and then one level
# of links from a directory to another directory.
function clone_dir()
{
SRCDIR=$1
DSTDIR=$2
FILES=`find ${SRCDIR} -maxdepth 1 -type f`
for file in ${FILES} ; do
BASE=`basename $file`
cp $file ${DSTDIR}/${BASE}
# ${STRIP} -s ${DSTDIR}/${BASE}
done;
# Clone links from the toolchain binary library dir
LINKS=`find ${SRCDIR} -maxdepth 1 -type l`
cd ${DSTDIR}
for file in ${LINKS} ; do
BASE=`basename $file`
TARGET=`readlink $file`
ln -sf ${TARGET} ${BASE}
done;
cd ${CURDIR}
}
CC_PREFIX=`${CROSS_COMPILE}gcc -print-multiarch`
case "${CC_PREFIX}" in
aarch64-linux-gnu)
LIBCBASE=$(dirname $(${CROSS_COMPILE}gcc -print-file-name=ld-linux-aarch64.so.1))/..
export ARCH=arm64
;;
arm-linux-gnueabihf)
LIBCBASE=$(dirname $(${CROSS_COMPILE}gcc -print-file-name=ld-linux-armhf.so.3))/..
export ARCH=arm
;;
*)
echo "Unsupported arch"
exit 1
;;
esac
function generate_inittab()
{
# Expand variables ${...} taking value from the environment
# http://superuser.com/a/302847
cat $1 | awk '{while(match($0,"[$]{[^}]*}")) {var=substr($0,RSTART+2,RLENGTH -3);gsub("[$]{"var"}",ENVIRON[var])}}1'>$2
}
case $1 in
"vexpress")
CFLAGS=${CFLAGS-"-Wno-strict-aliasing -Wno-unused-result -marm -mabi=aapcs-linux -mthumb -mthumb-interwork -mcpu=cortex-a15"}
cp etc/inittab-vexpress etc/inittab
echo "Vexpress" > etc/hostname
;;
"fvp")
echo "Building FVP root filesystem"
cp etc/inittab-vexpress etc/inittab
echo "FVP" > etc/hostname
;;
"rpi3")
echo "Building RPi3 root filesystem"
cp etc/inittab-rpi3 etc/inittab
echo "RPi3" > etc/hostname
;;
"poplar")
echo "Building Poplar root filesystem"
cp etc/inittab-poplar etc/inittab
echo "poplar" > etc/hostname
;;
"hikey")
echo "Building HiKey root filesystem"
export CFG_NW_CONSOLE_UART=${CFG_NW_CONSOLE_UART:-3}
generate_inittab etc/inittab-hikey etc/inittab
echo "HiKey" > etc/hostname
;;
"hikey960")
echo "Building HiKey960 root filesystem"
export CFG_CONSOLE_UART=${CFG_CONSOLE_UART:-6}
generate_inittab etc/inittab-hikey960 etc/inittab
echo "HiKey960" > etc/hostname
;;
"mt8173-evb")
cp etc/inittab-mt8173 etc/inittab
echo "MT8173_EVB" > etc/hostname
;;
"dra7xx")
echo "Building DRA7xx root filesystem"
cp etc/inittab-dra7xx etc/inittab
echo "DRA7xx" > etc/hostname
;;
"am43xx")
echo "Building AM43xx root filesystem"
cp etc/inittab-am43xx etc/inittab
echo "AM43xx" > etc/hostname
;;
*)
echo "Usage: $0 [versatile|vexpress|fvp|rpi3|poplar|hikey|hikey960|mt8173-evb|dra7xx|am43xx]"
exit 1
;;
esac
# Define more tools
STRIP=${CROSS_COMPILE}strip
echo "OUTFILE = ${OUTFILE}"
echo "Check prerequisites..."
echo -n "Check crosscompiler ... "
${CROSS_COMPILE}gcc --version > /dev/null ; if [ ! $? -eq 0 ] ; then
echo "ERROR: cross-compiler ${CROSS_COMPILE}gcc cannot be run!"
echo "ABORTING."
exit 1
else
echo "OK"
fi
echo -n "Check # of CPUs ..."
_NPROCESSORS_ONLN=`getconf _NPROCESSORS_ONLN`
echo $_NPROCESSORS_ONLN
if [ "$2" != "nocpio" ]; then
echo -n "gen_init_cpio ... "
which gen_init_cpio > /dev/null ; if [ ! $? -eq 0 ] ; then
echo "ERROR: gen_init_cpio not in PATH=$PATH!"
echo "Copy this binary from the Linux build tree."
echo "Or set your PATH into the Linux kernel tree, I don't care..."
echo "ABORTING."
exit 1
else
echo "OK"
fi
fi
# Clone the busybox git if we don't have it...
if [ ! -d ${BUSYBOXDIR} ] ; then
echo "It appears we're missing a busybox git"
echo "Failed. ABORTING."
exit 1
fi
# Copy the template of static files to be used
cp filelist.txt filelist-tmp.txt
# Prep dirs
mkdir -p ${STAGEDIR}
mkdir -p ${STAGEDIR}/lib
mkdir -p ${STAGEDIR}/sbin
mkdir -p ${BUILDDIR}
mkdir -p ${STAGEDIR}/etc/udhcp
# For using the git version
cd ${BUSYBOXDIR}
if [ ! -e ${BUILDDIR}/.config ]; then
make O=${BUILDDIR} defconfig
echo "Configuring cross compiler etc..."
# Comment in this line to create a statically linked busybox
#sed -i "s/^#.*CONFIG_STATIC.*/CONFIG_STATIC=y/" ${BUILDDIR}/.config
sed -i -e "s|CONFIG_CROSS_COMPILER_PREFIX=\"\"|CONFIG_CROSS_COMPILER_PREFIX=\"${CROSS_COMPILE}\"|g" ${BUILDDIR}/.config
sed -i -e "s/CONFIG_EXTRA_CFLAGS=\"\"/CONFIG_EXTRA_CFLAGS=\"${CFLAGS}\"/g" ${BUILDDIR}/.config
sed -i -e "s/CONFIG_PREFIX=\".*\"/CONFIG_PREFIX=\"..\/stage\"/g" ${BUILDDIR}/.config
# Turn off "eject" command, we don't have a CDROM
sed -i -e "s/CONFIG_EJECT=y/\# CONFIG_EJECT is not set/g" ${BUILDDIR}/.config
sed -i -e "s/CONFIG_FEATURE_EJECT_SCSI=y/\# CONFIG_FEATURE_EJECT_SCSI is not set/g" ${BUILDDIR}/.config
#make O=${BUILDDIR} menuconfig
fi
make -j${_NPROCESSORS_ONLN} O=${BUILDDIR}
make O=${BUILDDIR} install
# copy udhcp simple script to the stage
cp ${BUSYBOXDIR}/examples/udhcp/simple.script ${STAGEDIR}/etc/udhcp/
cd ${CURDIR}
# First the flat library where arch-independent stuff will
# end up
clone_dir ${LIBCBASE}/lib ${STAGEDIR}/lib
# The C library may be in a per-arch subdir (multiarch)
# OR it may not...
if [ -d ${LIBCBASE}/lib/${CC_PREFIX} ] ; then
mkdir -p ${STAGEDIR}/lib/${CC_PREFIX}
echo "dir /lib/${CC_PREFIX} 755 0 0" >> filelist-tmp.txt
clone_dir ${LIBCBASE}/lib/${CC_PREFIX} ${STAGEDIR}/lib/${CC_PREFIX}
fi
# Add files by searching stage directory
BINFILES=`find ${STAGEDIR}/bin -maxdepth 1 -type f`
for file in ${BINFILES} ; do
BASE=`basename $file`
echo "file /bin/${BASE} $file 755 0 0" >> filelist-tmp.txt
done;
SBINFILES=`find ${STAGEDIR}/sbin -maxdepth 1 -type f`
for file in ${SBINFILES} ; do
BASE=`basename $file`
echo "file /sbin/${BASE} $file 755 0 0" >> filelist-tmp.txt
done;
LIBFILES=`find ${STAGEDIR}/lib -maxdepth 1 -type f -a -not -name "*.a" \
-a -not -name "libstdc++*" -a -not -name "libasan*" \
-a -not -name "libubsan*" -a -not -name "libgfortran*" \
-a -not -name "libnss_nis*" -a -not -name "liblsan*" \
-a -not -name "libtsan*" -a -not -name "libitm*"`
for file in ${LIBFILES} ; do
BASE=`basename $file`
echo "file /lib/${BASE} $file 755 0 0" >> filelist-tmp.txt
done;
LIBLINKS=`find ${STAGEDIR}/lib -maxdepth 1 -type l`
for file in ${LIBLINKS} ; do
BASE=`basename $file`
TARGET=`readlink $file`
echo "slink /lib/${BASE} ${TARGET} 755 0 0" >> filelist-tmp.txt
done;
# Add multiarch libarary dir
if [ -d ${STAGEDIR}/lib/${CC_PREFIX} ] ; then
echo "dir /lib/${CC_PREFIX} 755 0 0" >> filelist-tmp.txt
CLIBFILES=`find ${STAGEDIR}/lib/${CC_PREFIX} -maxdepth 1 -type f`
for file in ${CLIBFILES} ; do
BASE=`basename $file`
echo "file /lib/${CC_PREFIX}/${BASE} $file 755 0 0" >> filelist-tmp.txt
done;
CLIBLINKS=`find ${STAGEDIR}/lib/${CC_PREFIX} -maxdepth 1 -type l`
for file in ${CLIBLINKS} ; do
BASE=`basename $file`
TARGET=`readlink $file`
echo "slink /lib/${CC_PREFIX}/${BASE} ${TARGET} 755 0 0" >> filelist-tmp.txt
done;
fi
# Add links by searching stage directory
LINKSBIN=`find ${STAGEDIR}/bin -maxdepth 1 -type l`
for file in ${LINKSBIN} ; do
BASE=`basename $file`
TARGET=`readlink $file`
echo "slink /bin/${BASE} ${TARGET} 755 0 0" >> filelist-tmp.txt
done;
LINKSSBIN=`find ${STAGEDIR}/sbin -maxdepth 1 -type l`
for file in ${LINKSSBIN} ; do
BASE=`basename $file`
TARGET=`readlink $file`
echo "slink /sbin/${BASE} ${TARGET} 755 0 0" >> filelist-tmp.txt
done;
LINKSUSRBIN=`find ${STAGEDIR}/usr/bin -maxdepth 1 -type l`
for file in ${LINKSUSRBIN} ; do
BASE=`basename $file`
TARGET=`readlink $file`
echo "slink /usr/bin/${BASE} ${TARGET} 755 0 0" >> filelist-tmp.txt
done;
LINKSUSRSBIN=`find ${STAGEDIR}/usr/sbin -maxdepth 1 -type l`
for file in ${LINKSUSRSBIN} ; do
BASE=`basename $file`
TARGET=`readlink $file`
echo "slink /sbin/${BASE} ${TARGET} 755 0 0" >> filelist-tmp.txt
done;
diff filelist-final.txt filelist-tmp.txt >/dev/null 2>&1 || mv filelist-tmp.txt filelist-final.txt
if [ "$2" != "nocpio" ]; then
gen_init_cpio filelist-final.txt > ${OUTFILE}
if [ -f ${OUTFILE} ] ; then
echo "New rootfs ready in ${OUTFILE}"
fi
fi