forked from debian-pi/raspbian-ua-netinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·212 lines (176 loc) · 6.92 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
#!/bin/bash
set -e
KERNEL_VERSION=3.12-1-rpi
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
}
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 -p bootfs
cp tmp/boot/* bootfs/
rm bootfs/System*
rm bootfs/config-*
mv bootfs/vmlinuz* bootfs/kernel_install.img
# initialize rootfs
rm -rf rootfs
mkdir -p rootfs/bin/
mkdir -p rootfs/lib/
mkdir -p rootfs/lib/modules/${KERNEL_VERSION}
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 scripts/* rootfs/
# update version
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
# install busybox
cp tmp/bin/busybox rootfs/bin
cd rootfs && ln -s bin/busybox init; cd ..
# install libc6 (for DNS and filesystem utils)
cp tmp/lib/*/ld-*.so rootfs/lib/ld-linux-armhf.so.3
cp tmp/lib/*/libc-*.so rootfs/lib/libc.so.6
cp tmp/lib/*/libresolv-*.so rootfs/lib/libresolv.so.2
cp tmp/lib/*/libnss_dns-*.so rootfs/lib/libnss_dns.so.2
cp tmp/lib/*/libpthread-*.so rootfs/lib/libpthread.so.0
# install cdebootstrap
mkdir -p rootfs/usr/share/
mkdir -p rootfs/usr/bin/
cp -r tmp/usr/share/cdebootstrap-static rootfs/usr/share/
cp tmp/usr/bin/cdebootstrap-static rootfs/usr/bin/
# install raspbian-archive-keyring (for cdebootstrap)
mkdir -p rootfs/usr/share/keyrings/
cp tmp/usr/share/keyrings/raspbian-archive-keyring.gpg rootfs/usr/share/keyrings/
# install gpgv (for cdebootstrap)
cp tmp/usr/bin/gpgv rootfs/usr/bin/
# install raspberrypi.org GPG key (for apt-key add)
cp packages/raspberrypi.gpg.key rootfs/usr/share/keyrings/
# install libbz2-1.0 (for gpgv)
cp tmp/lib/*/libbz2.so.1.0.* rootfs/lib/libbz2.so.1.0
# libs for mkfs
cp tmp/lib/*/libcom_err.so.2.1 rootfs/lib/libcom_err.so.2
cp tmp/lib/*/libe2p.so.2.3 rootfs/lib/libe2p.so.2
cp tmp/lib/*/libuuid.so.1.3.0 rootfs/lib/libuuid.so.1
cp tmp/lib/*/libblkid.so.1.1.0 rootfs/lib/libblkid.so.1
cp tmp/lib/*/libgcc_s.so.1 rootfs/lib/
# filesystem utils
mkdir -p rootfs/sbin/
cp tmp/sbin/mkfs.vfat rootfs/sbin/
cp tmp/sbin/mkfs.ext4 rootfs/sbin/
cp tmp/sbin/mkfs.f2fs rootfs/sbin/
cp tmp/sbin/mkfs.btrfs rootfs/sbin/
cp tmp/lib/*/libext2fs.so.2.4 rootfs/lib/libext2fs.so.2
cp tmp/lib/*/libf2fs.so.0 rootfs/lib/libf2fs.so.0
cp tmp/usr/lib/*/libbtrfs.so.0 rootfs/lib/libbtrfs.so.0
cp tmp/lib/*/libm.so.6 rootfs/lib/libm.so.6
cp tmp/lib/*/libz.so.1 rootfs/lib/libz.so.1
cp tmp/lib/*/liblzo2.so.2 rootfs/lib/liblzo2.so.2
cd rootfs && find . | cpio -H newc -ov > ../installer.cpio
cd ..
rm -rf tmp
rm -rf rootfs
cp installer.cpio bootfs/
echo "kernel=kernel_install.img" > bootfs/config.txt
echo "initramfs installer.cpio" >> bootfs/config.txt
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/installer-config.txt
fi
if [ -f post-install.txt ]; then
cp post-install.txt bootfs/post-install.txt
fi
ZIPFILE=raspbian-ua-netinst-`date +%Y%m%d`-git`git rev-parse --short @{0}`.zip
rm -f $ZIPFILE
cd bootfs && zip -9 ../$ZIPFILE *; cd ..