-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·329 lines (268 loc) · 11.5 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
#!/usr/bin/env bash
set -e
KERNEL_VERSION_RPI1=3.18.0-trunk-rpi
KERNEL_VERSION_RPI2=3.18.0-trunk-rpi2
INSTALL_MODULES="kernel/fs/f2fs/f2fs.ko"
INSTALL_MODULES="$INSTALL_MODULES kernel/fs/btrfs/btrfs.ko"
INSTALL_MODULES="$INSTALL_MODULES kernel/drivers/usb/storage/usb-storage.ko"
INSTALL_MODULES="$INSTALL_MODULES kernel/drivers/scsi/sg.ko"
INSTALL_MODULES="$INSTALL_MODULES kernel/drivers/scsi/sd_mod.ko"
# checks if first parameter is contained in the array passed as the second parameter
# use: contains_element "search_for" "${some_array[@]}" || do_if_not_found
function contains_element {
local elem
for elem in "${@:2}"; do [[ "${elem}" == "$1" ]] && return 0; done
return 1
}
# expects an array with kernel modules as a parameter, checks each module for dependencies
# and if a dependency isn't already in the $modules array, adds it to it (through a temporary
# local array).
# in addition sets the global $new_count variable to the number of dependencies added, so
# that the newly added dependencies can be checked as well
# use: check_dependencies "${modules[@]:${index}}"
function check_dependencies {
# collect the parameters into an array
mods=("${@}")
# temp array to hold the newly found dependencies
local -a new_found
# temp array to hold the found dependencies for a single module
local -a deps
local mod
local dep
# iterate over the passed modules
for mod in ${mods[@]}; do
# find the modules dependencies, convert into array
deps=($(cat "${depmod_file}" | grep "^${mod}" | cut -d':' -f2))
# iterate over the found dependencies
for dep in ${deps[@]}; do
# check if the dependency is in $modules, if not, add to temp array
contains_element "${dep}" "${modules[@]}" || new_found[${#new_found[@]}]="${dep}"
done
done
# add the newly found dependencies to the end of the $modules array
modules=("${modules[@]}" "${new_found[@]}")
# set the global variable to the number of newly found dependencies
new_count=${#new_found[@]}
}
# creates the file passed as an argument and sets permissions
function touch_tempfile {
[[ -z "${1}" ]] && return 1
touch "${1}" && chmod 600 "${1}"
echo "${1}"
}
# creates a temporary file and returns (echos) its filename
# the function checks for different commands and uses the appropriate one
# it will fallback to creating a file in /tmp
function create_tempfile {
local tmp_ptrn="/tmp/$(basename "${0}").${$}"
if type mktemp &> /dev/null; then
mktemp 2> /dev/null || \
mktemp -t raspbian-ua-netinst 2> /dev/null || \
touch_tempfile "${tmp_ptrn}"
else
if type tempfile &> /dev/null; then
tempfile
else
touch_tempfile "${tmp_ptrn}"
fi
fi
}
function create_cpio {
local KERNEL_VERSION=""
local target_system
if [ "$1" = "rpi1" ] ; then
KERNEL_VERSION=$KERNEL_VERSION_RPI1
target_system="rpi1"
elif [ "$1" = "rpi2" ] ; then
KERNEL_VERSION=$KERNEL_VERSION_RPI2
target_system="rpi2"
else
echo "Invalid parameter to 'create_cpio' function!"
return 1
fi
# initialize rootfs
rm -rf rootfs
mkdir -p rootfs
# create all the directories needed to copy the various components into place
mkdir -p rootfs/bin/
mkdir -p rootfs/etc/{default,network/if-up.d/}
mkdir -p rootfs/lib/lsb/init-functions.d/
mkdir -p rootfs/lib/modules/${KERNEL_VERSION}
mkdir -p rootfs/sbin/
mkdir -p rootfs/usr/bin/
mkdir -p rootfs/usr/lib/openssl-1.0.0/engines/
mkdir -p rootfs/usr/sbin/
mkdir -p rootfs/usr/share/keyrings/
mkdir -p rootfs/var/lib/ntpdate
cp -a tmp/lib/modules/${KERNEL_VERSION}/modules.{builtin,order} rootfs/lib/modules/${KERNEL_VERSION}
# calculate module dependencies
depmod_file=$(create_tempfile)
/sbin/depmod -nab tmp ${KERNEL_VERSION} > ${depmod_file}
modules=(${INSTALL_MODULES})
# new_count contains the number of new elements in the $modules array for each iteration
new_count=${#modules[@]}
# repeat the hunt for dependencies until no new ones are found (the loop takes care
# of finding nested dependencies)
until [ "${new_count}" == 0 ]; do
# check the dependencies for the modules in the last $new_count elements
check_dependencies "${modules[@]:$((${#modules[@]}-${new_count}))}"
done
# do some cleanup
rm -f ${depmod_file}
# copy the needed kernel modules to the rootfs (create directories as needed)
for module in ${modules[@]}; do
# calculate the target dir, just so the following line of code is shorter :)
dstdir="rootfs/lib/modules/${KERNEL_VERSION}/$(dirname ${module})"
# check if destination dir exist, create it otherwise
[ -d "${dstdir}" ] || mkdir -p "${dstdir}"
cp -a "tmp/lib/modules/${KERNEL_VERSION}/${module}" "${dstdir}"
done
/sbin/depmod -a -b rootfs ${KERNEL_VERSION}
# install scripts
cp -r filesystem/* rootfs/
# update version and date
sed -i "s/__VERSION__/git~`git rev-parse --short @{0}`/" rootfs/etc/init.d/rcS
sed -i "s/__DATE__/`date`/" rootfs/etc/init.d/rcS
# btrfs-tools components
cp tmp/sbin/mkfs.btrfs rootfs/sbin/
cp tmp/usr/lib/*/libbtrfs.so.0 rootfs/lib/
# busybox-static components
cp tmp/bin/busybox rootfs/bin
cd rootfs && ln -s bin/busybox init; cd ..
# cdebootstrap-static components
cp -r tmp/usr/share/cdebootstrap-static rootfs/usr/share/
cp tmp/usr/bin/cdebootstrap-static rootfs/usr/bin/
# dosfstools components
cp tmp/sbin/mkfs.vfat rootfs/sbin/
# e2fslibs components
cp tmp/lib/*/libe2p.so.2.3 rootfs/lib/libe2p.so.2
cp tmp/lib/*/libext2fs.so.2.4 rootfs/lib/libext2fs.so.2
# e2fsprogs components
cp tmp/sbin/mkfs.ext4 rootfs/sbin/
# f2fs-tools components
cp tmp/sbin/mkfs.f2fs rootfs/sbin/
cp tmp/lib/*/libf2fs.so.0 rootfs/lib/
# gpgv components
cp tmp/usr/bin/gpgv rootfs/usr/bin/
# lsb-base components
cp tmp/lib/lsb/init-functions rootfs/lib/lsb/
cp tmp/lib/lsb/init-functions.d/20-left-info-blocks rootfs/lib/lsb/init-functions.d/
# netbase components
cp tmp/etc/protocols rootfs/etc/
cp tmp/etc/rpc rootfs/etc/
cp tmp/etc/services rootfs/etc/
# ntpdate components
cp tmp/etc/default/ntpdate rootfs/etc/default/
# don't use /etc/ntp.conf since we don't have it
sed -i s/NTPDATE_USE_NTP_CONF=yes/NTPDATE_USE_NTP_CONF=no/ rootfs/etc/default/ntpdate
cp tmp/etc/network/if-up.d/ntpdate rootfs/etc/network/if-up.d/
cp tmp/usr/sbin/ntpdate rootfs/usr/sbin/
cp tmp/usr/sbin/ntpdate-debian rootfs/usr/sbin/
# raspberrypi.org GPG key
cp packages/raspberrypi.gpg.key rootfs/usr/share/keyrings/
# raspbian-archive-keyring components
cp tmp/usr/share/keyrings/raspbian-archive-keyring.gpg rootfs/usr/share/keyrings/
# libblkid1 components
cp tmp/lib/*/libblkid.so.1.1.0 rootfs/lib/libblkid.so.1
# libbz2-1.0 components
cp tmp/lib/*/libbz2.so.1.0.* rootfs/lib/libbz2.so.1.0
# libc6 components
cp tmp/lib/*/ld-*.so rootfs/lib/ld-linux-armhf.so.3
cp tmp/lib/*/libanl-*.so rootfs/lib/libanl.so.1
cp tmp/lib/*/libBrokenLocale-*.so rootfs/lib/libBrokenLocale.so.1
cp tmp/lib/*/libc-*.so rootfs/lib/libc.so.6
cp tmp/lib/*/libcidn-*.so rootfs/lib/libcidn.so.1
cp tmp/lib/*/libcrypt-*.so rootfs/lib/libcrypt.so.1
cp tmp/lib/*/libdl-*.so rootfs/lib/libdl.so.2
cp tmp/lib/*/libm-*.so rootfs/lib/libm.so.6
cp tmp/lib/*/libmemusage.so rootfs/lib/
cp tmp/lib/*/libnsl-*.so rootfs/lib/libnsl.so.1
cp tmp/lib/*/libnss_compat-*.so rootfs/lib/libnss_compat.so.2
cp tmp/lib/*/libnss_dns-*.so rootfs/lib/libnss_dns.so.2
cp tmp/lib/*/libnss_files-*.so rootfs/lib/libnss_files.so.2
cp tmp/lib/*/libnss_hesiod-*.so rootfs/lib/libnss_hesiod.so.2
cp tmp/lib/*/libnss_nis-*.so rootfs/lib/libnss_nis.so.2
cp tmp/lib/*/libpcprofile.so rootfs/lib/
cp tmp/lib/*/libpthread-*.so rootfs/lib/libpthread.so.0
cp tmp/lib/*/libresolv-*.so rootfs/lib/libresolv.so.2
cp tmp/lib/*/librt-*.so rootfs/lib/librt.so.1
cp tmp/lib/*/libSegFault.so rootfs/lib/
cp tmp/lib/*/libthread_db-*.so rootfs/lib/libthread_db.so.1
cp tmp/lib/*/libutil-*.so rootfs/lib/libutil.so.1
# libcomerr2 components
cp tmp/lib/*/libcom_err.so.2.1 rootfs/lib/libcom_err.so.2
# libgcc1 components
cp tmp/lib/*/libgcc_s.so.1 rootfs/lib/
# liblzo2-2 components
cp tmp/lib/*/liblzo2.so.2 rootfs/lib/
# libssl1.0.0 components
cp tmp/usr/lib/*/libcrypto.so.1.0.0 rootfs/usr/lib/
cp tmp/usr/lib/*/libssl.so.1.0.0 rootfs/usr/lib/
cp tmp/usr/lib/*/openssl-1.0.0/engines/lib4758cca.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libaep.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libatalla.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libcapi.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libchil.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libcswift.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libgmp.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libgost.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libnuron.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libpadlock.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libsureware.so rootfs/usr/lib/openssl-1.0.0/engines/
cp tmp/usr/lib/*/openssl-1.0.0/engines/libubsec.so rootfs/usr/lib/openssl-1.0.0/engines/
# libuuid1 components
cp tmp/lib/*/libuuid.so.1.3.0 rootfs/lib/libuuid.so.1
# zlib1g components
cp tmp/lib/*/libz.so.1 rootfs/lib/
INITRAMFS="../installer-${target_system}.cpio.gz"
(cd rootfs && find . | cpio -H newc -ov | gzip --best > $INITRAMFS)
rm -rf rootfs
}
if [ ! -d packages ]; then
. ./update.sh
fi
rm -rf tmp
mkdir tmp
# extract debs
for i in packages/*.deb; do
cd tmp && ar x ../$i && tar -xf data.tar.*; rm data.tar.*; cd ..
done
# initialize bootfs
rm -rf bootfs
mkdir bootfs
# raspberrypi-bootloader-nokernel components and kernel
cp -r tmp/boot/* bootfs/
rm bootfs/System*
rm bootfs/config-*
mv bootfs/vmlinuz-${KERNEL_VERSION_RPI1} bootfs/kernel-rpi1_install.img
mv bootfs/vmlinuz-${KERNEL_VERSION_RPI2} bootfs/kernel-rpi2_install.img
if [ ! -f bootfs/config.txt ] ; then
touch bootfs/config.txt
fi
create_cpio "rpi1"
cp installer-rpi1.cpio.gz bootfs/
echo "[pi1]" >> bootfs/config.txt
echo "kernel=kernel-rpi1_install.img" >> bootfs/config.txt
echo "initramfs installer-rpi1.cpio.gz" >> bootfs/config.txt
echo "device_tree=" >> bootfs/config.txt
create_cpio "rpi2"
cp installer-rpi2.cpio.gz bootfs/
echo "[pi2]" >> bootfs/config.txt
echo "kernel=kernel-rpi2_install.img" >> bootfs/config.txt
echo "initramfs installer-rpi2.cpio.gz" >> bootfs/config.txt
# clean up
rm -rf tmp
echo "consoleblank=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1" > bootfs/cmdline.txt
if [ -f installer-config.txt ]; then
cp installer-config.txt bootfs/
fi
if [ -f post-install.txt ]; then
cp post-install.txt bootfs/
fi
if [ -d config ] ; then
mkdir bootfs/config
cp -r config/* bootfs/config
fi
ZIPFILE=raspbian-ua-netinst-`date +%Y%m%d`-git`git rev-parse --short @{0}`.zip
rm -f $ZIPFILE
cd bootfs && zip -r -9 ../$ZIPFILE *; cd ..