forked from RevolutionPi/imagebakery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customize_image.sh
executable file
·387 lines (329 loc) · 12.7 KB
/
customize_image.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/bash
# customize raspbian image for revolution pi
usage () {
echo 'Usage: customize_image.sh [-m, --minimize | -h, --help] <source-image> [<target-image>]
-m, --minimize Install only software that is necessary for basic operation (eg. Pictory and other RevPi tools)
-f, --force Force in-place modification of the source image if not output image was specfied
-h, --help Print the usage page'
}
if [ ! -x "$(which curl)" ]; then
echo 1>&1 "Error: Command curl not found."
exit 1
fi
if [ ! -x "$(which fsck.vfat)" ]; then
echo 1>&1 "Error: Command fsck.vfat not found."
exit 1
fi
if [ ! -x "$(which lsof)" ]; then
echo 1>&1 "Error: Command lsof not found."
exit 1
fi
PARTED="$(which parted)"
if [ "x$PARTED" = "x" ] ; then
echo 1>&1 "Error: Command parted not found."
exit 1
fi
if [ ! -x "$PARTED" ] ; then
echo 1>&1 "Error: Command $PARTED is not executable."
exit 1
fi
set -ex
# pivot to new PID namespace
if [ $$ != 2 ] && [ -x /usr/bin/newpid ] ; then
exec /usr/bin/newpid "$0" "$@"
fi
# set MINIMG as 0: build the normal image by default
MINIMG=0
FORCE=0
# get the options
if ! MYOPTS=$(getopt -o mfh --long minimize,force,help -- "$@"); then
usage;
exit 1;
fi
eval set -- "$MYOPTS"
# extract options and their arguments into variables.
while true ; do
case "$1" in
-m|--minimize) MINIMG=1 ; shift ;;
-f|--force) FORCE=1 ; shift ;;
-h|--help) usage ; exit 0;;
*) shift; break ;;
esac
done
SOURCE_IMAGE=$1
OUTPUT_IMAGE=${2:-$1}
if [ "$SOURCE_IMAGE" == "$OUTPUT_IMAGE" ]; then
echo "WARNING: No name for the output image was specified. The source image will be modfied in-place."
if [ $FORCE -eq 0 ]; then
echo ""
echo -n "Do you want to continue? [Ny] "
read -r choice
if ! [[ "$choice" == "y" || "$choice" == "Y" ]]; then
exit
fi
fi
else
echo "Copying source image to ${OUTPUT_IMAGE}. This can take a while ..."
cp "$SOURCE_IMAGE" "$OUTPUT_IMAGE"
fi
if [ "$MINIMG" != "1" ]; then
echo "All additional applications will be built into the given image."
else
echo "Only a reduced application set will be built into the given image."
fi
IMAGEDIR=`mktemp -d -p /tmp img.XXXXXXXX`
BAKERYDIR=$(dirname "$0")
LOOPDEVICE=$(losetup -f)
CONFIGTXT="$IMAGEDIR/boot/config.txt"
cleanup_umount() {
if [ -e "$IMAGEDIR" ] ; then
lsof -t "$IMAGEDIR" | xargs --no-run-if-empty kill
fi
if [ -e "$IMAGEDIR/etc/resolv.conf" ] ; then
umount "$IMAGEDIR/etc/resolv.conf"
fi
if [ -e "$IMAGEDIR/usr/bin/qemu-arm-static" ] ; then
rm -f "$IMAGEDIR/usr/bin/qemu-arm-static"
fi
if mountpoint -q "$IMAGEDIR/tmp/debs-to-install" ; then
umount "$IMAGEDIR/tmp/debs-to-install"
fi
if [ -e "$IMAGEDIR/tmp/debs-to-install" ] ; then
rmdir "$IMAGEDIR/tmp/debs-to-install"
fi
if mountpoint -q "$IMAGEDIR/boot" ; then
umount "$IMAGEDIR/boot"
fi
if mountpoint -q "$IMAGEDIR" ; then
umount "$IMAGEDIR"
fi
if [ -d "$IMAGEDIR" ] ; then
rmdir "$IMAGEDIR"
fi
}
cleanup_losetup() {
if [ -e "$LOOPDEVICE"p1 ] ; then
delpart "$LOOPDEVICE" 1
fi
if [ -e "$LOOPDEVICE"p2 ] ; then
delpart "$LOOPDEVICE" 2
fi
if losetup "$LOOPDEVICE" 2>/dev/null ; then
losetup -d "$LOOPDEVICE"
fi
}
cleanup() {
cleanup_umount
cleanup_losetup
}
trap cleanup ERR SIGINT
# Block size is 512 bytes
blocksize=512
# Minimal eMMC size is 4 GB with an available disk size of 3909091328 bytes
disksize=3909091328
imgblocks=$(/sbin/blockdev --getsz "$OUTPUT_IMAGE")
[ "$imgblocks" = "" ] && echo 1>&1 "Error: Cannot get image size" && exit 1
imgsize=$((imgblocks * blocksize))
# The smallest size of CM3-eMMC is 4 GB , the available disksize for a system is 3909091328 bytes
# An image like raspios-lite just have 2 GB , so we need to resize rootfs first before
# we start processing the image. Otherwise build will fail with "no space left on device"
if [ "$imgsize" -lt 3900000000 ] ; then
bcount=$(((disksize-imgsize)/blocksize))
dd if=/dev/zero count=$bcount bs=$blocksize >> "$OUTPUT_IMAGE"
$PARTED "$OUTPUT_IMAGE" resizepart 2 "$((disksize-1))"B
losetup "$LOOPDEVICE" "$OUTPUT_IMAGE"
partprobe "$LOOPDEVICE"
resize2fs "$LOOPDEVICE"p2
e2fsck -p -f "$LOOPDEVICE"p2
sync
losetup -D
fi
# mount ext4 + FAT filesystems
losetup "$LOOPDEVICE" "$OUTPUT_IMAGE"
partprobe "$LOOPDEVICE"
mount "$LOOPDEVICE"p2 "$IMAGEDIR"
mount "$LOOPDEVICE"p1 "$IMAGEDIR/boot"
mount --bind /etc/resolv.conf "$IMAGEDIR/etc/resolv.conf"
# see https://wiki.debian.org/QemuUserEmulation
if [ -e /usr/bin/qemu-arm-static ] ; then
cp /usr/bin/qemu-arm-static "$IMAGEDIR/usr/bin"
fi
# copy templates
cp "$BAKERYDIR/templates/config.txt" "$IMAGEDIR/boot"
cp "$BAKERYDIR/templates/cmdline.txt" "$IMAGEDIR/boot"
cp "$BAKERYDIR/templates/revpi-aliases.sh" "$IMAGEDIR/etc/profile.d"
cp "$BAKERYDIR/templates/rsyslog.conf" "$IMAGEDIR/etc"
# limit disk space occupied by logs
ln -s ../cron.daily/logrotate "$IMAGEDIR/etc/cron.hourly"
sed -r -i -e 's/delaycompress/#delaycompress/' \
-e 's/sharedscripts/#sharedscripts/' \
"$IMAGEDIR/etc/logrotate.d/rsyslog"
sed -r -i -e 's/#compress/compress/' -e '2i \
\
# limit size of each log file\
maxsize 10M\
\
# compress harder\
compresscmd /usr/bin/nice\
compressoptions /usr/bin/xz\
compressext .xz\
uncompresscmd /usr/bin/unxz\
' "$IMAGEDIR"/etc/logrotate.conf
# bootstrap apt source, will be overwritten by revpi-repo package
cp "$BAKERYDIR/templates/revpi.gpg" "$IMAGEDIR/etc/apt/trusted.gpg.d"
cp "$BAKERYDIR/templates/revpi.list" "$IMAGEDIR/etc/apt/sources.list.d"
# Move ld.so.preload until installation is finished. Otherwise we get errors
# from ld.so:
# ERROR: ld.so: object '/usr/lib/arm-linux-gnueabihf/libarmmem-${PLATFORM}.so'
# from /etc/ld.so.preload cannot be preloaded (cannot open shared object file): ignored.
[[ -f "$IMAGEDIR/etc/ld.so.preload" ]] && mv "$IMAGEDIR/etc/ld.so.preload" "$IMAGEDIR/etc/ld.so.preload.bak"
# copy piTest source code
PICONTROLDIR=`mktemp -d -p /tmp piControl.XXXXXXXX`
git clone https://github.com/RevolutionPi/piControl $PICONTROLDIR
cp -pr $PICONTROLDIR/piTest "$IMAGEDIR/home/pi/demo"
cp -p $PICONTROLDIR/piControl.h "$IMAGEDIR/home/pi/demo"
sed -i -r -e 's%\.\./%%' "$IMAGEDIR/home/pi/demo/Makefile"
chown -R 1000:1000 "$IMAGEDIR/home/pi/demo"
chmod -R a+rX "$IMAGEDIR/home/pi/demo"
rm -r $PICONTROLDIR
# remove bookshelf if present
if [[ -d $IMAGEDIR/home/pi/Bookshelf ]]; then
rm -r $IMAGEDIR/home/pi/Bookshelf
fi
# customize settings
echo Europe/Berlin > "$IMAGEDIR/etc/timezone"
rm "$IMAGEDIR/etc/localtime"
echo RevPi > "$IMAGEDIR/etc/hostname"
sed -i -e 's/raspberrypi/RevPi/g' "$IMAGEDIR/etc/hosts"
if ! grep -qE '^i2c-dev$' "$IMAGEDIR/etc/modules" ; then
echo i2c-dev >> "$IMAGEDIR/etc/modules"
fi
echo piControl >> "$IMAGEDIR/etc/modules"
sed -i -r -e 's/^(XKBMODEL).*/\1="pc104"/' \
-e 's/^(XKBLAYOUT).*/\1="us"/' \
-e 's/^(XKBVARIANT).*/\1=""/' \
"$IMAGEDIR/etc/default/keyboard"
sed -i -r -e 's/^(LANG).*/\1="en_US.UTF-8"/' "$IMAGEDIR/etc/default/locale"
sed -i -r -e 's/^(# en_US.UTF-8 UTF-8)/en_US.UTF-8 UTF-8/' "$IMAGEDIR/etc/locale.gen"
sed -i -r -e 's/^(en_GB.UTF-8 UTF-8)/# en_GB.UTF-8 UTF-8/' "$IMAGEDIR/etc/locale.gen"
install -d -m 755 -o root -g root "$IMAGEDIR/etc/revpi"
basename "$OUTPUT_IMAGE" > "$IMAGEDIR/etc/revpi/image-release"
install -d -m 700 -o 1000 -g 1000 "$IMAGEDIR/home/pi/.ssh"
# activate settings
chroot "$IMAGEDIR" dpkg-reconfigure -fnoninteractive keyboard-configuration
chroot "$IMAGEDIR" dpkg-reconfigure -fnoninteractive tzdata
chroot "$IMAGEDIR" dpkg-reconfigure -fnoninteractive locales
# automatically bring up eth0 and eth1 again after a USB bus reset
sed -i -e '6i# allow-hotplug eth0\n# allow-hotplug eth1\n' "$IMAGEDIR/etc/network/interfaces"
cat >> "$IMAGEDIR/etc/dhcpcd.conf" <<-EOF
# Prioritize wlan0 routes over eth0 routes.
interface wlan0
metric 100
EOF
# harden network configuration
chroot "$IMAGEDIR" /usr/bin/patch /etc/sysctl.conf \
< "$BAKERYDIR/templates/sysctl.conf.patch"
# display IP address at login prompt
sed -i -e '1s/$/ \\4 \\6/' "$IMAGEDIR/etc/issue"
# free up disk space
dpkg --root "$IMAGEDIR" --purge `egrep -v '^#' "$BAKERYDIR/debs-to-remove"`
chroot "$IMAGEDIR" apt-get -y autoremove --purge
rm -rf "$IMAGEDIR/home/pi/MagPi"
# avoid installing unnecessary packages on this space-constrained machine
echo 'APT::Install-Recommends "false";' >> "$IMAGEDIR/etc/apt/apt.conf"
# download and install missing packages
chroot "$IMAGEDIR" apt-get update --allow-releaseinfo-change -y
chroot "$IMAGEDIR" apt-get -y install `egrep -v '^#' "$BAKERYDIR/min-debs-to-download"`
if [ "$MINIMG" != "1" ]; then
chroot "$IMAGEDIR" apt-get -y install `egrep -v '^#' "$BAKERYDIR/debs-to-download"`
fi
dpkg --root "$IMAGEDIR" --force-depends --purge rpd-wallpaper
chroot "$IMAGEDIR" apt-get -y install revpi-wallpaper
chroot "$IMAGEDIR" apt-get update
if [ "$MINIMG" != "1" ]; then
chroot "$IMAGEDIR" apt-get -y install teamviewer-revpi
fi
chroot "$IMAGEDIR" apt-mark hold raspi-copies-and-fills
chroot "$IMAGEDIR" apt-get -y upgrade
chroot "$IMAGEDIR" apt-mark unhold raspi-copies-and-fills
chroot "$IMAGEDIR" apt-get clean
if [ -e "$IMAGEDIR/etc/init.d/apache2" ] ; then
# annoyingly, the postinstall script starts apache2 on fresh installs
mount -t proc procfs "$IMAGEDIR/proc"
sed -r -i -e 's/pidof /pidof -x /' "$IMAGEDIR/etc/init.d/apache2"
chroot "$IMAGEDIR" /etc/init.d/apache2 stop
umount "$IMAGEDIR/proc"
# configure apache2
chroot "$IMAGEDIR" a2enmod ssl
sed -r -i -e 's/^(\tOptions .*Indexes.*)/#\1/' \
"$IMAGEDIR/etc/apache2/apache2.conf"
fi
if [ "$MINIMG" != "1" ]; then
# install nodejs and nodered with an install script and revpi-nodes from npm repository
NODEREDSCRIPT="/tmp/update-nodejs-and-nodered.sh"
/usr/bin/curl -sL \
https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered\
--output "$IMAGEDIR/$NODEREDSCRIPT"
chmod 755 "$IMAGEDIR/$NODEREDSCRIPT"
NODERED_VER="2.2.3"
chroot "$IMAGEDIR" /usr/bin/sudo -u pi $NODEREDSCRIPT --confirm-install --confirm-pi --no-init --nodered-version="$NODERED_VER"
rm "$IMAGEDIR/$NODEREDSCRIPT"
chroot "$IMAGEDIR" /usr/bin/sudo -u pi /usr/bin/npm install --prefix /home/pi/.node-red node-red-contrib-revpi-nodes
fi
# enable ssh daemon by default, disable swap, disable bluetooth on mini-uart
chroot "$IMAGEDIR" systemctl enable ssh
chroot "$IMAGEDIR" systemctl disable dphys-swapfile
chroot "$IMAGEDIR" systemctl disable hciuart
# disable 3rd party software
if [ "$MINIMG" != "1" ]; then
chroot "$IMAGEDIR" systemctl disable logiclab
chroot "$IMAGEDIR" systemctl disable nodered
fi
chroot "$IMAGEDIR" systemctl disable noderedrevpinodes-server
chroot "$IMAGEDIR" systemctl disable revpipyload
# boot to console by default, disable autologin
chroot "$IMAGEDIR" systemctl set-default multi-user.target
chroot "$IMAGEDIR" systemctl enable [email protected]
if [ -e "$IMAGEDIR/etc/lightdm/lightdm.conf" ] ; then
sed -r -i -e "s/^autologin-user=/#autologin-user=/" \
"$IMAGEDIR/etc/lightdm/lightdm.conf"
fi
# autologin.conf enables autologin in raspios and raspios-full
# but not in raspios-lite
if [ -e "$IMAGEDIR/etc/systemd/system/[email protected]/autologin.conf" ] ; then
rm -f "$IMAGEDIR/etc/systemd/system/[email protected]/autologin.conf"
fi
# peg cpu at 1200 MHz to maximize spi0 throughput and avoid jitter
chroot "$IMAGEDIR" /usr/bin/revpi-config enable perf-governor
# remove package lists, they will be outdated within days
rm "$IMAGEDIR/var/lib/apt/lists/"*Packages
# install local packages
if [ "$(/bin/ls "$BAKERYDIR/debs-to-install/"*.deb 2>/dev/null)" ] ; then
mkdir "$IMAGEDIR/tmp/debs-to-install"
mount --bind "$BAKERYDIR/debs-to-install" "$IMAGEDIR/tmp/debs-to-install"
chroot "$IMAGEDIR" sh -c "dpkg -i /tmp/debs-to-install/*.deb"
fi
# remove logs and ssh host keys
find "$IMAGEDIR/var/log" -type f -delete
find "$IMAGEDIR/etc/ssh" -name "ssh_host_*_key*" -delete
# restore ld.so.preload
[[ -f "$IMAGEDIR/etc/ld.so.preload.bak" ]] && mv "$IMAGEDIR/etc/ld.so.preload.bak" "$IMAGEDIR/etc/ld.so.preload"
# after package raspberrypi-kernel installed, install revpi-dt-blob.dtbo as default dt-blob
install -T "$IMAGEDIR/boot/overlays/revpi-dt-blob.dtbo" "$IMAGEDIR/boot/dt-blob.bin"
cleanup_umount
fsck.vfat -a "$LOOPDEVICE"p1
sleep 2
fsck.ext4 -f -p "$LOOPDEVICE"p2
sleep 2
# shrink image to speed up flashing
# HACK: Get minimum image size, add some blocks for extra space and shrink image to this size
# (will be removed when we have a first boot service)
SHRINKED_SIZE=$(resize2fs -P "$LOOPDEVICE"p2 | cut -f 2 -d :)
SHRINKED_SIZE=$((SHRINKED_SIZE + 20000))
resize2fs "$LOOPDEVICE"p2 "$SHRINKED_SIZE"
PARTSIZE=$(dumpe2fs -h "$LOOPDEVICE"p2 | egrep "^Block count:" | cut -d" " -f3-)
PARTSIZE=$((($PARTSIZE) * 8)) # ext4 uses 4k blocks, partitions use 512 bytes
PARTSTART=$(cat /sys/block/$(basename "$LOOPDEVICE")/$(basename "$LOOPDEVICE"p2)/start)
echo Yes | $PARTED ---pretend-input-tty "$LOOPDEVICE" resizepart 2 "$(($PARTSTART+$PARTSIZE-1))"s
cleanup_losetup
truncate -s $((512 * ($PARTSTART + $PARTSIZE))) "$OUTPUT_IMAGE"