-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.sh
executable file
·344 lines (273 loc) · 8.16 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/bash
# v1:
# Based on work by Klaus M Pfeiffer at http://blog.kmp.or.at/2012/05/build-your-own-raspberry-pi-image/
# you need to do: "sudo apt-get install binfmt-support qemu qemu-user-static debootstrap kpartx lvm2 dosfstools"
# run with "sudo bootstrap.sh /dev/sd[x]"
# v2:
# Based on work by Alexandre Bulte at https://gist.github.com/abulte/3917357
# v3:
# Jun 2013
# Yeri Tiete (http://yeri.be)
# > Made sure it bootstrapped again correctly.
# and some other changes
# Repo can be found on Github: http://yeri.be/11v
# More updates followed in July, August
# v3.1:
# Nov 2013
# Use of additional files, fixed some other issues.
# Support for 2Gb SD cards and resizefs at firstboot.
echo "Please be sure to be in the same directory as bootstrap.sh and more.sh."
echo "Else this will not work."
echo ""
echo "=> Use like: sudo bootstrap.sh /dev/sd[x]."
echo ""
# variables... Might want to change some stuff here.
# set your local domain name
DOMAIN=botnet.corp.flatturtle.com
# Keymap to use (this seems to provide a decent list: http://nic.phys.ethz.ch/projects/dphys3/planning/debconf.final)
# note: this doesn't really seem to work (check further down in the script and alter /etc/default/keyboard part)
KEYMAP="console-data/keymap/azerty/belgian/standard/keymap"
# size of the fat partition (p1). 128Mb is plenty:
bootsize="128M"
# set rootsize to 1,6Gb. This way, we'll be able to flash 2Gb and up SD cards.
# We'll run resize2fs later to make sure it uses the entire disk.
#rootsize="1024M" => irrelevant in new script, will use max size
# some other vars you should not really touch
buildenv="/root/raspbian/bootstrap"
rootfs="${buildenv}/rootfs"
bootfs="${rootfs}/boot"
workingpath=`pwd`
deb_local_mirror="http://mirrordirector.raspbian.org/raspbian"
deb_release="jessie"
device=$1
mydate=`date +%Y%m%d`
image=""
# configure which packages to install. Edit packages.sh to add more or less packages.
source ./packages.sh
#
####### START OF MESSY CODE
#
echo "=> starting partitioning."
if [ $EUID -ne 0 ]; then
echo "ERROR: This tool must be run as root"
exit 1
fi
if ! [ -b $device ]; then
echo "ERROR: Device $device is not a block device"
exit 1
fi
if [ "$device" == "" ]; then
echo "WARNING: No block device given, creating image instead."
mkdir -p $buildenv
image="${buildenv}/rpi_basic_${deb_release}_${mydate}.img"
dd if=/dev/zero of=$image bs=1MB count=1000
device=`losetup -f --show $image`
echo "Image $image Created and mounted as $device"
else
dd if=/dev/zero of=$device bs=512 count=1
fi
fdisk $device << EOF
n
p
1
+$bootsize
t
c
n
p
2
w
EOF
if [ "$image" != "" ]; then
# this fails
#losetup -d $device
losetup -D
device=`kpartx -va $image | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1`
echo "--- kpartx device ${device}"
device="/dev/mapper/${device}"
bootp=${device}p1
rootp=${device}p2
echo "--- rootp ${rootp}"
echo "--- bootp ${bootp}"
else
if ! [ -b ${device}1 ]; then
bootp=${device}p1
rootp=${device}p2
if ! [ -b ${bootp} ]; then
echo "ERROR: Can't find boot partition, neither as ${device}1, nor as ${device}p1. Exiting."
exit 1
fi
else
bootp=${device}1
rootp=${device}2
fi
fi
echo "=> creating filesystems."
mkfs.vfat $bootp
mkfs.ext4 $rootp
mkdir -p $rootfs
mount $rootp $rootfs
cd $rootfs
echo "=> bootstrapping:"
echo " --- debootstrap --no-check-gpg --foreign --arch=armhf --variant=minbase ${deb_release} ${rootfs} ${deb_local_mirror}"
debootstrap --no-check-gpg --foreign --arch=armhf --variant=minbase $deb_release $rootfs $deb_local_mirror
echo "=> debootstrapping finished."
cp /usr/bin/qemu-arm-static usr/bin/
echo "=> executing second-stage (debootstrap)."
LANG=C chroot $rootfs /debootstrap/debootstrap --second-stage
mount $bootp $bootfs
# prevent LOCALE errors
export LANGUAGE=C
export LANG=C
export LC_ALL=C
# This should match what has been written above. Couldn't use variables in my test; they got cleared for some reason.
echo "deb $deb_local_mirror $deb_release main contrib non-free rpi" > etc/apt/sources.list
echo "deb-src $deb_local_mirror $deb_release main contrib non-free rpi" >> etc/apt/sources.list
# get the raspbian key, or you'll get untrusted package errors
wget http://archive.raspbian.org/raspbian.public.key -O ./raspbian.key
echo "dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait" > boot/cmdline.txt
# make fstab file
echo "# /etc/fstab: static file system information.
# <file system> <dir> <type> <options> <dump> <pass>
# System mounts
proc /proc proc defaults 0 0
# Data mounts
/dev/mmcblk0p1 /boot vfat defaults 0 0
/dev/mmcblk0p2 / ext4 noatime,errors=remount-ro 0 1
# Minimize writes
tmpfs /var/log tmpfs defaults,noatime 0 0
tmpfs /var/cache/apt tmpfs defaults,noatime 0 0
tmpfs /var/lib/apt/lists tmpfs defaults,noatime 0 0
" > etc/fstab
# give it a name
echo "bootstrappi" > etc/hostname
# create network file
echo "auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
" > etc/network/interfaces
echo "vchiq
snd_bcm2835
" >> etc/modules
echo "console-common console-data/keymap/policy select Select keymap from full list
console-common console-data/keymap/full select $KEYMAP
" > debconf.set
# This should be changed probably.
echo "# KEYBOARD CONFIGURATION FILE
# Consult the keyboard(5) manual page.
XKBMODEL=\"pc105\"
XKBLAYOUT=\"be\"
XKBVARIANT=\"\"
XKBOPTIONS=\"\"
BACKSPACE=\"guess\"
" > etc/default/keyboard
echo "#!/bin/bash
debconf-set-selections /debconf.set
rm -f /debconf.set
apt-key add raspbian.key
rm -f raspbian.key
apt-get update
apt-get -y install $PACKAGES
wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update
chmod +x /usr/bin/rpi-update
touch /boot/start.elf
mkdir -p /lib/modules
rpi-update
# remove backups and other crap
rm -rf /boot.bak
rm -rf /lib/modules.bak
# set root psw to raspberry
echo root:raspberry | chpasswd
# The other keymap thing is not working. This forces be keymap...
rm -f /etc/udev/rules.d/70-persistent-net.rules
rm -f third-stage
sync
" > third-stage
chmod +x third-stage
echo "=> executing third-stage."
LANG=C chroot $rootfs /third-stage
# firstboot will repair all the broken stuff when booting the first time.
echo "#!/bin/sh -e
# Run local parts
run-parts /etc/rc.local.d
exit 0" > etc/rc.local
chmod +x etc/rc.local
mkdir etc/rc.local.d/
echo "#!/bin/sh
# Initialize the system on the first boot
if test -f /firstboot.sh
then
. /firstboot.sh
rm /firstboot.sh
exit 0
fi
if test -f /secondboot.sh
then
. /secondboot.sh
rm /secondboot.sh
exit 0
fi
exit 0" > etc/rc.local.d/firstboot
chmod +x etc/rc.local.d/firstboot
echo "#!/bin/sh
# fix disk size
fdisk /dev/mmcblk0 << EOF
d
2
n
p
2
w
EOF
# this wont work: have to run it manually with root privileges afterwards.
resize2fs /dev/root
# Generate a hostname
HOSTID=\$(ip addr show dev eth0 | grep ether | awk '{print \$2}' | awk 'BEGIN {FS=\":\"}; {print \$4\$5\$6}')
HOSTNAME=rpi-\$HOSTID
echo \$HOSTNAME > /etc/hostname
echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
echo "127.0.1.1 \$HOSTNAME.$DOMAIN \$HOSTNAME" >> /etc/hosts
invoke-rc.d hostname.sh start
# Configure all remaining packages
dpkg --configure -a
dpkg-reconfigure openssh-server
# Set the time
ntpdate europe.pool.ntp.org
# reboot to update partition table
reboot
" > firstboot.sh
chmod +x firstboot.sh
echo "#!/bin/sh
resize2fs /dev/root
" > secondboot.sh
chmod +x secondboot.sh
# execute "more.sh" if it exists
if [ -f $workingpath/more.sh ]; then
echo "=> executing more.sh."
source $workingpath/more.sh
fi
echo "#!/bin/bash
apt-get clean
rm -f cleanup
rm etc/ssh/*key
rm etc/ssh/*.pub
rm -rf tmp/*
rm -rf /var/log/*
sync
" > cleanup
chmod +x cleanup
echo "=> executing cleanup."
LANG=C chroot $rootfs /cleanup
cd
umount $bootp
umount $rootp
if [ "$image" != "" ]; then
kpartx -d $image
echo "=> created Image: $image."
fi
echo "=> finished."
echo "There is a big chance umounting will fail."
echo "Please manually unmount after killing active processes under qemu (ps x | grep qemu); probably cron and rsyslog."
echo ""
echo "Done... It's `date +%H:%m` and a beautiful day. Enjoy it."