forked from strongly-typed/image-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.sh
executable file
·246 lines (174 loc) · 6.21 KB
/
bootstrap.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
#!/bin/bash
# More debugging
set -x
abort()
{
echo >&2 '
***************
*** ABORTED ***
***************
'
echo "Some error occurred before." >&2
echo "Now trying to clean up" >&2
sync; sleep 1
umount -l $rootfs/dev
umount -l $rootfs/proc
# this does all the heavy lifting
unmount_image $image
# ToDo
exit 1
}
trap 'abort' 0
# These linear scripts without error handling shall not continue when an error occurred.
set -e
applications_confd=${0%/*}/applications.conf.d
boards_confd=${0%/*}/boards.conf.d
distributions_confd=${0%/*}/distributions.conf.d
systems_confd=${0%/*}/systems.conf.d
tools_d=${0%/*}/tools.d
source ${tools_d}/functions.sh
# Root privileges are needed
check_root || exit 1
# Check for requirements
check_requirements || exit 1
# Load configuration for this system
source ${systems_confd}/${1}.d/bootstrap.sh
# Get board configuration
board=${board?Board not provided}
source ${boards_confd}/${board}.d/${board}.conf
# Get distribution configuration
distribution=${distribution?Distribution not provided.}
source ${distributions_confd}/${distribution}.d/${distribution}.conf
echo "***************************************************************************"
echo " Building for board=${board} "
echo " Building with distribution=${distribution} "
echo "***************************************************************************"
################################# Directories ##################################
buildenv="$(readlink -f ${0%/*}/build)"
rootfs="${buildenv}/rootfs"
bootfs="${rootfs}/boot"
mydate=`date +%Y-%m-%d_%H%M`
# path to final image
image="${buildenv}/${hostname}_${board}_${distribution}_${mydate}.img"
################################# Packages #####################################
# if not provided by config file
if [ -z "$packages" ]; then
packages=""
fi
# Every package line need a whitespace at its end
packages+="ntp ntpdate openssh-server "
# convenience and/or needed for using some tools interactively
packages+="dialog less nano bzip2 "
# convenience
packages+="bash-completion htop usbutils picocom mc ack-grep "
# localization and keyboard layout
packages+="console-common locales "
# needed for rpi-update bootstrap
packages+="git-core wget curl ca-certificates binutils "
# packages needed for networking
packages+="net-tools netbase ifupdown net-tools isc-dhcp-client "
packages+="wireless-tools wpasupplicant inetutils-ping "
# python and pyserial for serial to tcp redirect
packages+="python python-serial python-pip python-dev build-essential "
# needed for openocd
packages+="libusb-1.0-0 "
############################## Image Handling ##################################
# make sure buildenv directory exists
mkdir -p $buildenv
# create image
echo " # creating image file '$(basename $image)' with size ${img_size}M."
create_image $image $img_size || (echo "FAILED"; exit 1)
# make sure rootfs path exists
mkdir -p $rootfs
# mount image file to rootfs (mounts / and /boot)
mount_image $image $rootfs
# make sure /dev and /proc exist
mkdir -p $rootfs/dev
mkdir -p $rootfs/proc
mount -o bind /dev $rootfs/dev
mount -o bind /proc $rootfs/proc
############################### Bootstrapping ##################################
# First and second stage bootstrapping of Debian system
source ${distributions_confd}/debootstrap.sh
############################### Debian Setup ###################################
# set repository
echo "deb $deb_mirror $deb_release $deb_repos
" > $rootfs/etc/apt/sources.list
# create fstab
echo "proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 0
/dev/mmcblk0p2 / ext4 defaults 0 0
" > $rootfs/etc/fstab
# configure hostname
echo "$hostname" > $rootfs/etc/hostname
# configure hostname in /etc/hosts
# @TODO
# TODO: this should be moved to config file
# kernel commandline for boot
echo "Setting kernel boot command-line to " ${kernel_boot_command}
echo ${kernel_boot_command} > ${rootfs}/boot/cmdline.txt
############################## conf.d scripts ##################################
export LANG=C
export LC_CTYPE=C
export LC_MESSAGES=C
export LC_ALL=C
# Run scripts from board directory
for script in ${boards_confd}/${board}.d/*.sh
do
if [ ! -x "${script}" ]; then
continue
fi
echo "Running script '${script}' in directory '${rootfs}'"
${script} "${rootfs}"
done
echo "Finished scripts from ${board_confd}"
# Run scripts from distribution directory
for script in ${distributions_confd}/${distribution}.d/*.sh
do
if [ ! -x "${script}" ]; then
continue
fi
echo "Running script '${script}' in directory '${rootfs}'"
${script} "${rootfs}"
done
echo "Finished scripts from ${distributions_confd}"
# Run scripts from applications directory specified by array ${applications}
for script in "${applications[@]}";
do
if [ ! -x "${applications_confd}/${script}.sh" ]; then
continue
fi
echo "Running script '${applications_confd}/${script}.sh' in directory '${rootfs}'"
${applications_confd}/${script}.sh "${rootfs}"
done
echo "Finished scripts from ${applications_confd}"
# board specific configuration scripts
# if [ -n "$board_confd" ] && [ -d "$confd/$board_confd" ]
# then
# for script in $confd/$board_confd/*.sh
# do
# if [ ! -x "${script}" ]; then
# continue
# fi
#
# echo "Running script '${script}' in directory '${rootfs}'"
# ${script} "${rootfs}"
# done
#fi
################################ Cleanup #######################################
# safety first
sync; sleep 1
trap : 0
set +e
if mount | grep ${rootfs}/dev > /dev/null; then umount -l $rootfs/dev; fi
if mount | grep ${rootfs}/proc > /dev/null; then umount -l $rootfs/proc; fi
# this does all the heavy lifting
unmount_image $image
# change ownership of buildenv to top folder
owner="$(namei -o ${buildenv}/.. | tail -n 1 | cut -d ' ' -f 3)"
group="$(namei -o ${buildenv}/.. | tail -n 1 | cut -d ' ' -f 4)"
chown -R $owner:$group $buildenv
echo "*******************************************************************************"
echo "*** Created Image: $(basename $image)"
echo "*** * * * * Done * * * *"
echo "*******************************************************************************"