Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong
GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/scripts/debootstrap.sh
14996 views
1
#!/bin/bash
2
#
3
# Copyright (c) 2013-2021 Igor Pecovnik, igor.pecovnik@gma**.com
4
#
5
# This file is licensed under the terms of the GNU General Public
6
# License version 2. This program is licensed "as is" without any
7
# warranty of any kind, whether express or implied.
8
9
10
# Functions:
11
12
# debootstrap_ng
13
# create_rootfs_cache
14
# prepare_partitions
15
# update_initramfs
16
# create_image
17
18
19
20
21
# debootstrap_ng
22
#
23
debootstrap_ng()
24
{
25
display_alert "Starting rootfs and image building process for" "${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED:-null} ${DESKTOP_ENVIRONMENT:-null} ${BUILD_MINIMAL}" "info"
26
27
[[ $ROOTFS_TYPE != ext4 ]] && display_alert "Assuming $BOARD $BRANCH kernel supports $ROOTFS_TYPE" "" "wrn"
28
29
# trap to unmount stuff in case of error/manual interruption
30
trap unmount_on_exit INT TERM EXIT
31
32
# stage: clean and create directories
33
rm -rf $SDCARD $MOUNT
34
mkdir -p $SDCARD $MOUNT $DEST/images $EXTER/cache/rootfs
35
36
# stage: verify tmpfs configuration and mount
37
# CLI needs ~1.5GiB, desktop - ~3.5GiB
38
# calculate and set tmpfs mount to use 9/10 of available RAM+SWAP
39
local phymem=$(( (($(awk '/MemTotal/ {print $2}' /proc/meminfo) + $(awk '/SwapTotal/ {print $2}' /proc/meminfo))) / 1024 * 9 / 10 )) # MiB
40
if [[ $BUILD_DESKTOP == yes ]]; then local tmpfs_max_size=3500; else local tmpfs_max_size=1500; fi # MiB
41
if [[ $FORCE_USE_RAMDISK == no ]]; then local use_tmpfs=no
42
elif [[ $FORCE_USE_RAMDISK == yes || $phymem -gt $tmpfs_max_size ]]; then
43
local use_tmpfs=yes
44
fi
45
[[ -n $FORCE_TMPFS_SIZE ]] && phymem=$FORCE_TMPFS_SIZE
46
47
[[ $use_tmpfs == yes ]] && mount -t tmpfs -o size=${phymem}M tmpfs $SDCARD
48
49
# stage: prepare basic rootfs: unpack cache or create from scratch
50
create_rootfs_cache
51
52
call_extension_method "pre_install_distribution_specific" "config_pre_install_distribution_specific" << 'PRE_INSTALL_DISTRIBUTION_SPECIFIC'
53
*give config a chance to act before install_distribution_specific*
54
Called after `create_rootfs_cache` (_prepare basic rootfs: unpack cache or create from scratch_) but before `install_distribution_specific` (_install distribution and board specific applications_).
55
PRE_INSTALL_DISTRIBUTION_SPECIFIC
56
57
# stage: install kernel and u-boot packages
58
# install distribution and board specific applications
59
60
if [[ ${RELEASE} == "raspi" ]]; then
61
install_opi_specific
62
else
63
install_distribution_specific
64
install_common
65
66
# install locally built packages or install pre-built packages from orangepi
67
[[ $EXTERNAL_NEW == compile || $EXTERNAL_NEW == prebuilt ]] && chroot_installpackages_local
68
69
#[[ $EXTERNAL_NEW == prebuilt ]] && chroot_installpackages "yes"
70
71
# stage: user customization script
72
# NOTE: installing too many packages may fill tmpfs mount
73
customize_image
74
75
# remove packages that are no longer needed. Since we have intrudoced uninstall feature, we might want to clean things that are no longer needed
76
display_alert "No longer needed packages" "purge" "info"
77
chroot $SDCARD /bin/bash -c "apt-get autoremove -y" >/dev/null 2>&1
78
79
# create list of installed packages for debug purposes
80
chroot $SDCARD /bin/bash -c "dpkg --get-selections" | grep -v deinstall | awk '{print $1}' | cut -f1 -d':' > $DEST/${LOG_SUBPATH}/installed-packages-${RELEASE}$([[ ${BUILD_MINIMAL} == yes ]] && echo "-minimal")$([[ ${BUILD_DESKTOP} == yes ]] && echo "-desktop").list 2>&1
81
82
fi
83
84
# clean up / prepare for making the image
85
umount_chroot "$SDCARD"
86
post_debootstrap_tweaks
87
88
if [[ $ROOTFS_TYPE == fel ]]; then
89
FEL_ROOTFS=$SDCARD/
90
display_alert "Starting FEL boot" "$BOARD" "info"
91
source $SRC/scripts/fel-load.sh
92
else
93
if [[ $BOARDFAMILY == "cix" ]]; then
94
create_cix_rootfs
95
create_cix_image
96
else
97
prepare_partitions
98
create_image
99
fi
100
fi
101
102
# stage: unmount tmpfs
103
umount $SDCARD 2>&1
104
if [[ $use_tmpfs = yes ]]; then
105
while grep -qs "$SDCARD" /proc/mounts
106
do
107
umount $SDCARD
108
sleep 5
109
done
110
fi
111
rm -rf $SDCARD
112
113
# remove exit trap
114
trap - INT TERM EXIT
115
} #############################################################################
116
117
bootstrap(){
118
local BOOTSTRAP_CMD=debootstrap
119
local BOOTSTRAP_ARGS=()
120
121
export CAPSH_ARG="--drop=cap_setfcap"
122
export http_proxy=${APT_PROXY}
123
124
BOOTSTRAP_ARGS+=(--arch arm64)
125
BOOTSTRAP_ARGS+=(--include gnupg)
126
#BOOTSTRAP_ARGS+=(--components "main,contrib,non-free")
127
BOOTSTRAP_ARGS+=(--components "main")
128
BOOTSTRAP_ARGS+=(--exclude=info)
129
BOOTSTRAP_ARGS+=(--include=ca-certificates)
130
BOOTSTRAP_ARGS+=("$@")
131
printf -v BOOTSTRAP_STR '%q ' "${BOOTSTRAP_ARGS[@]}"
132
133
${BOOTSTRAP_CMD} $BOOTSTRAP_STR || true
134
}
135
export -f bootstrap
136
137
# create_rootfs_cache
138
#
139
# unpacks cached rootfs for $RELEASE or creates one
140
#
141
create_rootfs_cache()
142
{
143
144
local packages_hash=$(get_package_list_hash "$ROOTFSCACHE_VERSION")
145
local cache_type="cli"
146
[[ ${BUILD_DESKTOP} == yes ]] && local cache_type="xfce-desktop"
147
[[ -n ${DESKTOP_ENVIRONMENT} ]] && local cache_type="${DESKTOP_ENVIRONMENT}"
148
[[ ${BUILD_MINIMAL} == yes ]] && local cache_type="minimal"
149
local cache_name=${RELEASE}-${cache_type}-${ARCH}.$packages_hash.tar.lz4
150
local cache_fname=${EXTER}/cache/rootfs/${cache_name}
151
local display_name=${RELEASE}-${cache_type}-${ARCH}.${packages_hash:0:3}...${packages_hash:29}.tar.lz4
152
153
if [[ -f $cache_fname && "$ROOT_FS_CREATE_ONLY" != "force" ]]; then
154
local date_diff=$(( ($(date +%s) - $(stat -c %Y $cache_fname)) / 86400 ))
155
display_alert "Extracting $display_name" "$date_diff days old" "info"
156
pv -p -b -r -c -N "[ .... ] $display_name" "$cache_fname" | lz4 -dc | tar xp --xattrs -C $SDCARD/
157
[[ $? -ne 0 ]] && rm $cache_fname && exit_with_error "Cache $cache_fname is corrupted and was deleted. Restart."
158
rm $SDCARD/etc/resolv.conf
159
echo "nameserver $NAMESERVER" >> $SDCARD/etc/resolv.conf
160
create_sources_list "$RELEASE" "$SDCARD/"
161
elif [[ $RELEASE == "raspi" ]]; then
162
display_alert "local not found" "Creating new rootfs cache for $RELEASE" "info"
163
164
cd $SDCARD # this will prevent error sh: 0: getcwd() failed
165
166
bootstrap bullseye "$SDCARD" "https://mirrors.ustc.edu.cn/debian/"
167
168
mount_chroot "$SDCARD"
169
170
display_alert "Diverting" "initctl/start-stop-daemon" "info"
171
# policy-rc.d script prevents starting or reloading services during image creation
172
printf '#!/bin/sh\nexit 101' > $SDCARD/usr/sbin/policy-rc.d
173
LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/initctl" &> /dev/null
174
LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/start-stop-daemon" &> /dev/null
175
printf '#!/bin/sh\necho "Warning: Fake start-stop-daemon called, doing nothing"' > $SDCARD/sbin/start-stop-daemon
176
printf '#!/bin/sh\necho "Warning: Fake initctl called, doing nothing"' > $SDCARD/sbin/initctl
177
chmod 755 $SDCARD/usr/sbin/policy-rc.d
178
chmod 755 $SDCARD/sbin/initctl
179
chmod 755 $SDCARD/sbin/start-stop-daemon
180
181
install_raspi_specific
182
183
umount_chroot "$SDCARD"
184
185
tar cp --xattrs --directory=$SDCARD/ --exclude='./dev/*' --exclude='./proc/*' --exclude='./run/*' --exclude='./tmp/*' \
186
--exclude='./sys/*' . | pv -p -b -r -s $(du -sb $SDCARD/ | cut -f1) -N "$display_name" | lz4 -5 -c > $cache_fname
187
else
188
display_alert "local not found" "Creating new rootfs cache for $RELEASE" "info"
189
190
# stage: debootstrap base system
191
if [[ $NO_APT_CACHER != yes ]]; then
192
# apt-cacher-ng apt-get proxy parameter
193
local apt_extra="-o Acquire::http::Proxy=\"http://${APT_PROXY_ADDR:-localhost:3142}\""
194
local apt_mirror="http://${APT_PROXY_ADDR:-localhost:3142}/$APT_MIRROR"
195
else
196
local apt_mirror="http://$APT_MIRROR"
197
fi
198
199
# fancy progress bars
200
[[ -z $OUTPUT_DIALOG ]] && local apt_extra_progress="--show-progress -o DPKG::Progress-Fancy=1"
201
202
# Ok so for eval+PIPESTATUS.
203
# Try this on your bash shell:
204
# ONEVAR="testing" eval 'bash -c "echo value once $ONEVAR && false && echo value twice $ONEVAR"' '| grep value' '| grep value' ; echo ${PIPESTATUS[*]}
205
# Notice how PIPESTATUS has only one element. and it is always true, although we failed explicitly with false in the middle of the bash.
206
# That is because eval itself is considered a single command, no matter how many pipes you put in there, you'll get a single value, the return code of the LAST pipe.
207
# Lets export the value of the pipe inside eval so we know outside what happened:
208
# ONEVAR="testing" eval 'bash -e -c "echo value once $ONEVAR && false && echo value twice $ONEVAR"' '| grep value' '| grep value' ';EVALPIPE=(${PIPESTATUS[@]})' ; echo ${EVALPIPE[*]}
209
210
local release_version=${RELEASE}
211
212
if [[ ${RELEASE} == "sid" ]]; then
213
release_version=unstable
214
apt_mirror="https://snapshot.debian.org/archive/debian-ports/20221225T084846Z"
215
DEBOOTSTRAP_OPTION="--no-check-gpg --no-merged-usr"
216
PACKAGE_LIST_EXCLUDE="usr-is-merged"
217
fi
218
219
display_alert "Installing base system" "Stage 1/2" "info"
220
cd $SDCARD # this will prevent error sh: 0: getcwd() failed
221
222
eval 'debootstrap --variant=minbase --include=${DEBOOTSTRAP_LIST// /,} ${PACKAGE_LIST_EXCLUDE:+ --exclude=${PACKAGE_LIST_EXCLUDE// /,}} \
223
--no-check-gpg --arch=$ARCH --components=${DEBOOTSTRAP_COMPONENTS} $DEBOOTSTRAP_OPTION --foreign ${release_version} $SDCARD/ ${apt_mirror}' \
224
${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
225
${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Debootstrap (stage 1/2)..." $TTY_Y $TTY_X'} \
226
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
227
228
[[ ${EVALPIPE[0]} -ne 0 || ! -f $SDCARD/debootstrap/debootstrap ]] && exit_with_error "Debootstrap base system for ${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED} ${DESKTOP_ENVIRONMENT} ${BUILD_MINIMAL} first stage failed"
229
230
cp /usr/bin/$QEMU_BINARY $SDCARD/usr/bin/
231
232
mkdir -p $SDCARD/usr/share/keyrings/
233
cp /usr/share/keyrings/*-archive-keyring.gpg $SDCARD/usr/share/keyrings/
234
235
display_alert "Installing base system" "Stage 2/2" "info"
236
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "/debootstrap/debootstrap --second-stage"' \
237
${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
238
${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Debootstrap (stage 2/2)..." $TTY_Y $TTY_X'} \
239
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
240
241
[[ ${EVALPIPE[0]} -ne 0 || ! -f $SDCARD/bin/bash ]] && exit_with_error "Debootstrap base system for ${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED} ${DESKTOP_ENVIRONMENT} ${BUILD_MINIMAL} second stage failed"
242
243
mount_chroot "$SDCARD"
244
245
if [[ ${RELEASE} == "sid" ]]; then
246
mkdir -p $SDCARD/etc/apt/apt.conf.d/
247
echo "Acquire::Check-Valid-Until no;" > $SDCARD/etc/apt/apt.conf.d/99-no-check-valid-until
248
wget -qnc -P ${EXTER}/cache/debs/ https://snapshot.debian.org/archive/debian-ports/20220616T194833Z/pool-riscv64/main/i/icu/libicu71_71.1-3_riscv64.deb
249
cp -v ${EXTER}/cache/debs/libicu71_71.1-3_riscv64.deb $SDCARD/
250
LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg -i /libicu71_71.1-3_riscv64.deb" &> /dev/null
251
fi
252
253
display_alert "Diverting" "initctl/start-stop-daemon" "info"
254
# policy-rc.d script prevents starting or reloading services during image creation
255
printf '#!/bin/sh\nexit 101' > $SDCARD/usr/sbin/policy-rc.d
256
LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/initctl" &> /dev/null
257
LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/start-stop-daemon" &> /dev/null
258
printf '#!/bin/sh\necho "Warning: Fake start-stop-daemon called, doing nothing"' > $SDCARD/sbin/start-stop-daemon
259
printf '#!/bin/sh\necho "Warning: Fake initctl called, doing nothing"' > $SDCARD/sbin/initctl
260
chmod 755 $SDCARD/usr/sbin/policy-rc.d
261
chmod 755 $SDCARD/sbin/initctl
262
chmod 755 $SDCARD/sbin/start-stop-daemon
263
264
# stage: configure language and locales
265
display_alert "Configuring locales" "$DEST_LANG" "info"
266
267
[[ -f $SDCARD/etc/locale.gen ]] && sed -i "s/^# $DEST_LANG/$DEST_LANG/" $SDCARD/etc/locale.gen
268
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "locale-gen $DEST_LANG"' ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
269
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "update-locale LANG=$DEST_LANG LANGUAGE=$DEST_LANG LC_MESSAGES=$DEST_LANG"' \
270
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
271
272
if [[ -f $SDCARD/etc/default/console-setup ]]; then
273
sed -e 's/CHARMAP=.*/CHARMAP="UTF-8"/' -e 's/FONTSIZE=.*/FONTSIZE="8x16"/' \
274
-e 's/CODESET=.*/CODESET="guess"/' -i $SDCARD/etc/default/console-setup
275
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "setupcon --save --force"'
276
fi
277
278
# stage: create apt-get sources list
279
create_sources_list "$RELEASE" "$SDCARD/"
280
281
# add armhf arhitecture to arm64, unless configured not to do so.
282
if [[ "a${ARMHF_ARCH}" != "askip" ]]; then
283
[[ $ARCH == arm64 ]] && eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg --add-architecture armhf"'
284
fi
285
286
# this should fix resolvconf installation failure in some cases
287
chroot $SDCARD /bin/bash -c 'echo "resolvconf resolvconf/linkify-resolvconf boolean false" | debconf-set-selections'
288
289
# stage: update packages list
290
display_alert "Updating package list" "$RELEASE" "info"
291
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "apt-get -q -y $apt_extra update"' \
292
${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
293
${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Updating package lists..." $TTY_Y $TTY_X'} \
294
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
295
296
[[ ${EVALPIPE[0]} -ne 0 ]] && display_alert "Updating package lists" "failed" "wrn"
297
298
# stage: upgrade base packages from xxx-updates and xxx-backports repository branches
299
display_alert "Upgrading base packages" "Orange Pi" "info"
300
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
301
$apt_extra $apt_extra_progress upgrade"' \
302
${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
303
${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Upgrading base packages..." $TTY_Y $TTY_X'} \
304
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
305
306
# Myy: Dividing the desktop packages installation steps into multiple
307
# ones. We first install the "ADDITIONAL_PACKAGES" in order to get
308
# access to software-common-properties installation.
309
# THEN we add the APT sources and install the Desktop packages.
310
# TODO : Find a way to add APT sources WITHOUT software-common-properties
311
312
[[ ${EVALPIPE[0]} -ne 0 ]] && display_alert "Upgrading base packages" "failed" "wrn"
313
314
# stage: install additional packages
315
display_alert "Installing the main packages for" "Orange Pi" "info"
316
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
317
$apt_extra $apt_extra_progress --no-install-recommends install $PACKAGE_MAIN_LIST"' \
318
${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
319
${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Installing Orange Pi main packages..." $TTY_Y $TTY_X'} \
320
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
321
322
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Installation of Orange Pi main packages for ${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED} ${DESKTOP_ENVIRONMENT} ${BUILD_MINIMAL} failed"
323
324
if [[ $BUILD_DESKTOP == "yes" ]]; then
325
# FIXME Myy : Are we keeping this only for Desktop users,
326
# or should we extend this to CLI users too ?
327
# There might be some clunky boards that require Debian packages from
328
# specific repos...
329
display_alert "Adding apt sources for Desktop packages"
330
add_desktop_package_sources
331
332
local apt_desktop_install_flags=""
333
if [[ ! -z ${DESKTOP_APT_FLAGS_SELECTED+x} ]]; then
334
for flag in ${DESKTOP_APT_FLAGS_SELECTED}; do
335
apt_desktop_install_flags+=" --install-${flag}"
336
done
337
else
338
# Myy : Using the previous default option, if the variable isn't defined
339
# And ONLY if it's not defined !
340
apt_desktop_install_flags+=" --no-install-recommends"
341
fi
342
343
display_alert "Installing the desktop packages for" "Orange Pi" "info"
344
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
345
$apt_extra $apt_extra_progress install ${apt_desktop_install_flags} $PACKAGE_LIST_DESKTOP"' \
346
${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
347
${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Installing Orange Pi desktop packages..." $TTY_Y $TTY_X'} \
348
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
349
350
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Installation of Orange Pi desktop packages for ${BRANCH} ${BOARD} ${RELEASE} ${DESKTOP_APPGROUPS_SELECTED} ${DESKTOP_ENVIRONMENT} ${BUILD_MINIMAL} failed"
351
fi
352
353
install_docker
354
[[ ${BOARDFAMILY} == "starfive2" ]] && jh7110_install_libs
355
[[ ${BOARDFAMILY} == "cix" ]] && install_cix_debs
356
357
# Remove packages from packages.uninstall
358
359
display_alert "Uninstall packages" "$PACKAGE_LIST_UNINSTALL" "info"
360
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -qq \
361
$apt_extra $apt_extra_progress purge $PACKAGE_LIST_UNINSTALL"' \
362
${PROGRESS_LOG_TO_FILE:+' >> $DEST/${LOG_SUBPATH}/debootstrap.log'} \
363
${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Removing packages.uninstall packages..." $TTY_Y $TTY_X'} \
364
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
365
366
[[ ${EVALPIPE[0]} -ne 0 ]] && exit_with_error "Installation of Orange Pi packages failed"
367
368
# stage: purge residual packages
369
display_alert "Purging residual packages for" "Orange Pi" "info"
370
PURGINGPACKAGES=$(chroot $SDCARD /bin/bash -c "dpkg -l | grep \"^rc\" | awk '{print \$2}' | tr \"\n\" \" \"")
371
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -e -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
372
$apt_extra $apt_extra_progress remove --purge $PURGINGPACKAGES"' \
373
${PROGRESS_LOG_TO_FILE:+' | tee -a $DEST/${LOG_SUBPATH}/debootstrap.log'} \
374
${OUTPUT_DIALOG:+' | dialog --backtitle "$backtitle" --progressbox "Purging residual Orange Pi packages..." $TTY_Y $TTY_X'} \
375
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'} ';EVALPIPE=(${PIPESTATUS[@]})'
376
377
[[ ${EVALPIPE[0]} -ne 0 ]] && exit_with_error "Purging of residual Orange Pi packages failed"
378
379
# stage: remove downloaded packages
380
chroot $SDCARD /bin/bash -c "apt-get -y autoremove; apt-get clean"
381
382
# DEBUG: print free space
383
local freespace=$(LC_ALL=C df -h)
384
echo $freespace >> $DEST/${LOG_SUBPATH}/debootstrap.log
385
display_alert "Free SD cache" "$(echo -e "$freespace" | grep $SDCARD | awk '{print $5}')" "info"
386
display_alert "Mount point" "$(echo -e "$freespace" | grep $MOUNT | head -1 | awk '{print $5}')" "info"
387
388
# create list of installed packages for debug purposes
389
chroot $SDCARD /bin/bash -c "dpkg --get-selections" | grep -v deinstall | awk '{print $1}' | cut -f1 -d':' > ${cache_fname}.list 2>&1
390
391
# creating xapian index that synaptic runs faster
392
if [[ $BUILD_DESKTOP == yes ]]; then
393
display_alert "Recreating Synaptic search index" "Please wait" "info"
394
chroot $SDCARD /bin/bash -c "[[ -f /usr/sbin/update-apt-xapian-index ]] && /usr/sbin/update-apt-xapian-index -u"
395
fi
396
397
# this is needed for the build process later since resolvconf generated file in /run is not saved
398
rm $SDCARD/etc/resolv.conf
399
echo "nameserver $NAMESERVER" >> $SDCARD/etc/resolv.conf
400
401
# stage: make rootfs cache archive
402
display_alert "Ending debootstrap process and preparing cache" "$RELEASE" "info"
403
sync
404
# the only reason to unmount here is compression progress display
405
# based on rootfs size calculation
406
umount_chroot "$SDCARD"
407
408
tar cp --xattrs --directory=$SDCARD/ --exclude='./dev/*' --exclude='./proc/*' --exclude='./run/*' --exclude='./tmp/*' \
409
--exclude='./sys/*' --exclude='./home/*' --exclude='./root/*' . | pv -p -b -r -s $(du -sb $SDCARD/ | cut -f1) -N "$display_name" | lz4 -5 -c > $cache_fname
410
411
# sign rootfs cache archive that it can be used for web cache once. Internal purposes
412
if [[ -n "${GPG_PASS}" && "${SUDO_USER}" ]]; then
413
[[ -n ${SUDO_USER} ]] && sudo chown -R ${SUDO_USER}:${SUDO_USER} "${DEST}"/images/
414
echo "${GPG_PASS}" | sudo -H -u ${SUDO_USER} bash -c "gpg --passphrase-fd 0 --armor --detach-sign --pinentry-mode loopback --batch --yes ${cache_fname}" || exit 1
415
fi
416
417
# needed for backend to keep current only
418
touch $cache_fname.current
419
420
fi
421
422
# used for internal purposes. Faster rootfs cache rebuilding
423
if [[ -n "$ROOT_FS_CREATE_ONLY" ]]; then
424
umount --lazy "$SDCARD"
425
rm -rf $SDCARD
426
427
display_alert "Rootfs build done" "@host" "info"
428
display_alert "Target directory" "${EXTER}/cache/rootfs" "info"
429
display_alert "File name" "${cache_name}" "info"
430
431
# remove exit trap
432
trap - INT TERM EXIT
433
exit
434
fi
435
436
mount_chroot "$SDCARD"
437
} #############################################################################
438
439
# prepare_partitions
440
#
441
# creates image file, partitions and fs
442
# and mounts it to local dir
443
# FS-dependent stuff (boot and root fs partition types) happens here
444
#
445
prepare_partitions() {
446
display_alert "Preparing image file for rootfs" "$BOARD $RELEASE" "info"
447
448
# possible partition combinations
449
# /boot: none, ext4, ext2, fat (BOOTFS_TYPE)
450
# root: ext4, btrfs, f2fs, nfs (ROOTFS_TYPE)
451
452
# declare makes local variables by default if used inside a function
453
# NOTE: mountopts string should always start with comma if not empty
454
455
# array copying in old bash versions is tricky, so having filesystems as arrays
456
# with attributes as keys is not a good idea
457
declare -A parttype mkopts mkopts_label mkfs mountopts
458
459
parttype[ext4]=ext4
460
parttype[ext2]=ext2
461
parttype[fat]=fat16
462
parttype[f2fs]=ext4 # not a copy-paste error
463
parttype[btrfs]=btrfs
464
parttype[xfs]=xfs
465
# parttype[nfs] is empty
466
467
# metadata_csum and 64bit may need to be disabled explicitly when migrating to newer supported host OS releases
468
if [[ $HOSTRELEASE =~ buster|bullseye|bookworm|bionic|focal|jammy|noble|kinetic|sid ]]; then
469
mkopts[ext4]="-q -m 2 -O ^64bit,^metadata_csum"
470
fi
471
# mkopts[fat] is empty
472
mkopts[ext2]='-q'
473
# mkopts[f2fs] is empty
474
mkopts[btrfs]='-m dup'
475
# mkopts[xfs] is empty
476
# mkopts[nfs] is empty
477
478
mkopts_label[ext4]='-L '
479
mkopts_label[ext2]='-L '
480
mkopts_label[fat]='-n '
481
mkopts_label[f2fs]='-l '
482
mkopts_label[btrfs]='-L '
483
mkopts_label[xfs]='-L '
484
# mkopts_label[nfs] is empty
485
486
mkfs[ext4]=ext4
487
mkfs[ext2]=ext2
488
mkfs[fat]=vfat
489
mkfs[f2fs]=f2fs
490
mkfs[btrfs]=btrfs
491
mkfs[xfs]=xfs
492
# mkfs[nfs] is empty
493
494
mountopts[ext4]=',commit=600,errors=remount-ro'
495
# mountopts[ext2] is empty
496
# mountopts[fat] is empty
497
# mountopts[f2fs] is empty
498
mountopts[btrfs]=',commit=600'
499
# mountopts[xfs] is empty
500
# mountopts[nfs] is empty
501
502
# default BOOTSIZE to use if not specified
503
DEFAULT_BOOTSIZE=1024 # MiB
504
# size of UEFI partition. 0 for no UEFI. Don't mix UEFISIZE>0 and BOOTSIZE>0
505
UEFISIZE=${UEFISIZE:-0}
506
BIOSSIZE=${BIOSSIZE:-0}
507
UEFI_MOUNT_POINT=${UEFI_MOUNT_POINT:-/boot/efi}
508
UEFI_FS_LABEL="${UEFI_FS_LABEL:-opi_efi}"
509
ROOT_FS_LABEL="${ROOT_FS_LABEL:-opi_root}"
510
BOOT_FS_LABEL="${BOOT_FS_LABEL:-opi_boot}"
511
512
call_extension_method "pre_prepare_partitions" "prepare_partitions_custom" << 'PRE_PREPARE_PARTITIONS'
513
*allow custom options for mkfs*
514
Good time to change stuff like mkfs opts, types etc.
515
PRE_PREPARE_PARTITIONS
516
517
# stage: determine partition configuration
518
local next=1
519
# Check if we need UEFI partition
520
if [[ $UEFISIZE -gt 0 ]]; then
521
# Check if we need BIOS partition
522
[[ $BIOSSIZE -gt 0 ]] && local biospart=$((next++))
523
local uefipart=$((next++))
524
fi
525
# Check if we need boot partition
526
if [[ -n $BOOTFS_TYPE || $ROOTFS_TYPE != ext4 || $CRYPTROOT_ENABLE == yes ]]; then
527
local bootpart=$((next++))
528
local bootfs=${BOOTFS_TYPE:-ext4}
529
[[ -z $BOOTSIZE || $BOOTSIZE -le 8 ]] && BOOTSIZE=${DEFAULT_BOOTSIZE}
530
else
531
BOOTSIZE=0
532
fi
533
# Check if we need root partition
534
[[ $ROOTFS_TYPE != nfs ]] &&
535
local rootpart=$((next++))
536
537
# stage: calculate rootfs size
538
export rootfs_size=$(du -sm $SDCARD/ | cut -f1) # MiB
539
display_alert "Current rootfs size" "$rootfs_size MiB" "info"
540
541
call_extension_method "prepare_image_size" "config_prepare_image_size" << 'PREPARE_IMAGE_SIZE'
542
*allow dynamically determining the size based on the $rootfs_size*
543
Called after `${rootfs_size}` is known, but before `${FIXED_IMAGE_SIZE}` is taken into account.
544
A good spot to determine `FIXED_IMAGE_SIZE` based on `rootfs_size`.
545
UEFISIZE can be set to 0 for no UEFI partition, or to a size in MiB to include one.
546
Last chance to set `USE_HOOK_FOR_PARTITION`=yes and then implement create_partition_table hook_point.
547
PREPARE_IMAGE_SIZE
548
549
if [[ -n $FIXED_IMAGE_SIZE && $FIXED_IMAGE_SIZE =~ ^[0-9]+$ ]]; then
550
display_alert "Using user-defined image size" "$FIXED_IMAGE_SIZE MiB" "info"
551
local sdsize=$FIXED_IMAGE_SIZE
552
# basic sanity check
553
if [[ $ROOTFS_TYPE != nfs && $sdsize -lt $rootfs_size ]]; then
554
exit_with_error "User defined image size is too small" "$sdsize <= $rootfs_size"
555
fi
556
else
557
local imagesize=$(($rootfs_size + $OFFSET + $BOOTSIZE + $UEFISIZE + $EXTRA_ROOTFS_MIB_SIZE)) # MiB
558
# Hardcoded overhead +25% is needed for desktop images,
559
# for CLI it could be lower. Align the size up to 4MiB
560
if [[ $BUILD_DESKTOP == yes ]]; then
561
local sdsize=$(bc -l <<< "scale=0; ((($imagesize * 1.35) / 1 + 0) / 4 + 1) * 4")
562
else
563
local sdsize=$(bc -l <<< "scale=0; ((($imagesize * 1.30) / 1 + 0) / 4 + 1) * 4")
564
fi
565
fi
566
567
# stage: create blank image
568
display_alert "Creating blank image for rootfs" "$sdsize MiB" "info"
569
if [[ $FAST_CREATE_IMAGE == yes ]]; then
570
truncate --size=${sdsize}M ${SDCARD}.raw # sometimes results in fs corruption, revert to previous know to work solution
571
sync
572
else
573
dd if=/dev/zero bs=1M status=none count=$sdsize | pv -p -b -r -s $(($sdsize * 1024 * 1024)) -N "[ .... ] dd" | dd status=none of=${SDCARD}.raw
574
fi
575
576
# stage: create partition table
577
display_alert "Creating partitions" "${bootfs:+/boot: $bootfs }root: $ROOTFS_TYPE" "info"
578
if [[ "${USE_HOOK_FOR_PARTITION}" == "yes" ]]; then
579
{
580
[[ "$IMAGE_PARTITION_TABLE" == "msdos" ]] &&
581
echo "label: dos" ||
582
echo "label: $IMAGE_PARTITION_TABLE"
583
} | sfdisk ${SDCARD}.raw >> "${DEST}/${LOG_SUBPATH}/install.log" 2>&1 ||
584
exit_with_error "Create partition table fail. Please check" "${DEST}/${LOG_SUBPATH}/install.log"
585
586
call_extension_method "create_partition_table" <<- 'CREATE_PARTITION_TABLE'
587
*only called when USE_HOOK_FOR_PARTITION=yes to create the complete partition table*
588
Finally, we can get our own partition table. You have to partition ${SDCARD}.raw
589
yourself. Good luck.
590
CREATE_PARTITION_TABLE
591
else
592
{
593
[[ "$IMAGE_PARTITION_TABLE" == "msdos" ]] &&
594
echo "label: dos" ||
595
echo "label: $IMAGE_PARTITION_TABLE"
596
597
local next=$OFFSET
598
if [[ -n "$biospart" ]]; then
599
# gpt: BIOS boot
600
local type="21686148-6449-6E6F-744E-656564454649"
601
echo "$biospart : name=\"bios\", start=${next}MiB, size=${BIOSSIZE}MiB, type=${type}"
602
local next=$(($next + $BIOSSIZE))
603
fi
604
if [[ -n "$uefipart" ]]; then
605
# dos: EFI (FAT-12/16/32)
606
# gpt: EFI System
607
[[ "$IMAGE_PARTITION_TABLE" != "gpt" ]] &&
608
local type="ef" ||
609
local type="C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
610
echo "$uefipart : name=\"efi\", start=${next}MiB, size=${UEFISIZE}MiB, type=${type}"
611
local next=$(($next + $UEFISIZE))
612
fi
613
if [[ -n "$bootpart" ]]; then
614
# Linux extended boot
615
[[ "$IMAGE_PARTITION_TABLE" != "gpt" ]] &&
616
local type="ea" ||
617
local type="BC13C2FF-59E6-4262-A352-B275FD6F7172"
618
if [[ -n "$rootpart" ]]; then
619
echo "$bootpart : name=\"bootfs\", start=${next}MiB, size=${BOOTSIZE}MiB, type=${type}"
620
local next=$(($next + $BOOTSIZE))
621
else
622
# no `size` argument mean "as much as possible"
623
echo "$bootpart : name=\"bootfs\", start=${next}MiB, type=${type}"
624
fi
625
fi
626
if [[ -n "$rootpart" ]]; then
627
# dos: Linux
628
# gpt: Linux filesystem
629
[[ "$IMAGE_PARTITION_TABLE" != "gpt" ]] &&
630
local type="83" ||
631
local type="0FC63DAF-8483-4772-8E79-3D69D8477DE4"
632
# no `size` argument mean "as much as possible"
633
echo "$rootpart : name=\"rootfs\", start=${next}MiB, type=${type}"
634
fi
635
} | sfdisk ${SDCARD}.raw >> "${DEST}/${LOG_SUBPATH}/install.log" 2>&1 ||
636
exit_with_error "Partition fail. Please check" "${DEST}/${LOG_SUBPATH}/install.log"
637
fi
638
639
call_extension_method "post_create_partitions" <<- 'POST_CREATE_PARTITIONS'
640
*called after all partitions are created, but not yet formatted*
641
POST_CREATE_PARTITIONS
642
643
# stage: mount image
644
# lock access to loop devices
645
exec {FD}> /var/lock/orangepi-debootstrap-losetup
646
flock -x $FD
647
648
LOOP=$(losetup -f)
649
[[ -z $LOOP ]] && exit_with_error "Unable to find free loop device"
650
651
check_loop_device "$LOOP"
652
653
losetup -P $LOOP ${SDCARD}.raw
654
655
# loop device was grabbed here, unlock
656
flock -u $FD
657
658
# stage: create fs, mount partitions, create fstab
659
rm -f $SDCARD/etc/fstab
660
if [[ -n $rootpart ]]; then
661
local rootdevice="${LOOP}p${rootpart}"
662
663
if [[ $CRYPTROOT_ENABLE == yes ]]; then
664
display_alert "Encrypting root partition with LUKS..." "cryptsetup luksFormat $rootdevice" ""
665
echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksFormat $CRYPTROOT_PARAMETERS $rootdevice -
666
echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksOpen $rootdevice $ROOT_MAPPER -
667
display_alert "Root partition encryption complete." "" "ext"
668
# TODO: pass /dev/mapper to Docker
669
rootdevice=/dev/mapper/$ROOT_MAPPER # used by `mkfs` and `mount` commands
670
fi
671
672
check_loop_device "$rootdevice"
673
display_alert "Creating rootfs" "$ROOTFS_TYPE on $rootdevice"
674
mkfs.${mkfs[$ROOTFS_TYPE]} ${mkopts[$ROOTFS_TYPE]} ${mkopts_label[$ROOTFS_TYPE]:+${mkopts_label[$ROOTFS_TYPE]}"$ROOT_FS_LABEL"} $rootdevice >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
675
[[ $ROOTFS_TYPE == ext4 ]] && tune2fs -o journal_data_writeback $rootdevice > /dev/null
676
if [[ $ROOTFS_TYPE == btrfs && $BTRFS_COMPRESSION != none ]]; then
677
local fscreateopt="-o compress-force=${BTRFS_COMPRESSION}"
678
fi
679
mount ${fscreateopt} $rootdevice $MOUNT/
680
# create fstab (and crypttab) entry
681
if [[ $CRYPTROOT_ENABLE == yes ]]; then
682
# map the LUKS container partition via its UUID to be the 'cryptroot' device
683
echo "$ROOT_MAPPER UUID=$(blkid -s UUID -o value ${LOOP}p${rootpart}) none luks" >> $SDCARD/etc/crypttab
684
local rootfs=$rootdevice # used in fstab
685
else
686
local rootfs="UUID=$(blkid -s UUID -o value $rootdevice)"
687
fi
688
echo "$rootfs / ${mkfs[$ROOTFS_TYPE]} defaults,noatime${mountopts[$ROOTFS_TYPE]} 0 1" >> $SDCARD/etc/fstab
689
else
690
# update_initramfs will fail if /lib/modules/ doesn't exist
691
mount --bind --make-private $SDCARD $MOUNT/
692
echo "/dev/nfs / nfs defaults 0 0" >> $SDCARD/etc/fstab
693
fi
694
if [[ -n $bootpart ]]; then
695
display_alert "Creating /boot" "$bootfs on ${LOOP}p${bootpart}"
696
check_loop_device "${LOOP}p${bootpart}"
697
mkfs.${mkfs[$bootfs]} ${mkopts[$bootfs]} ${mkopts_label[$bootfs]:+${mkopts_label[$bootfs]}"$BOOT_FS_LABEL"} ${LOOP}p${bootpart} >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
698
mkdir -p $MOUNT/boot/
699
mount ${LOOP}p${bootpart} $MOUNT/boot/
700
echo "UUID=$(blkid -s UUID -o value ${LOOP}p${bootpart}) /boot ${mkfs[$bootfs]} defaults${mountopts[$bootfs]} 0 2" >> $SDCARD/etc/fstab
701
fi
702
if [[ -n $uefipart ]]; then
703
display_alert "Creating EFI partition" "FAT32 ${UEFI_MOUNT_POINT} on ${LOOP}p${uefipart} label ${UEFI_FS_LABEL}"
704
check_loop_device "${LOOP}p${uefipart}"
705
mkfs.fat -F32 -n "${UEFI_FS_LABEL}" ${LOOP}p${uefipart} >> "${DEST}"/debug/install.log 2>&1
706
mkdir -p "${MOUNT}${UEFI_MOUNT_POINT}"
707
mount ${LOOP}p${uefipart} "${MOUNT}${UEFI_MOUNT_POINT}"
708
echo "UUID=$(blkid -s UUID -o value ${LOOP}p${uefipart}) ${UEFI_MOUNT_POINT} vfat defaults 0 2" >> $SDCARD/etc/fstab
709
fi
710
echo "tmpfs /tmp tmpfs defaults,nosuid 0 0" >> $SDCARD/etc/fstab
711
712
call_extension_method "format_partitions" <<- 'FORMAT_PARTITIONS'
713
*if you created your own partitions, this would be a good time to format them*
714
The loop device is mounted, so ${LOOP}p1 is it's first partition etc.
715
FORMAT_PARTITIONS
716
717
# stage: adjust boot script or boot environment
718
if [[ -f $SDCARD/boot/orangepiEnv.txt ]]; then
719
if [[ $CRYPTROOT_ENABLE == yes ]]; then
720
echo "rootdev=$rootdevice cryptdevice=UUID=$(blkid -s UUID -o value ${LOOP}p${rootpart}):$ROOT_MAPPER" >> $SDCARD/boot/orangepiEnv.txt
721
else
722
echo "rootdev=$rootfs" >> $SDCARD/boot/orangepiEnv.txt
723
fi
724
echo "rootfstype=$ROOTFS_TYPE" >> $SDCARD/boot/orangepiEnv.txt
725
elif [[ $rootpart != 1 ]] && [[ $SRC_EXTLINUX != yes ]]; then
726
local bootscript_dst=${BOOTSCRIPT##*:}
727
sed -i 's/mmcblk0p1/mmcblk0p2/' $SDCARD/boot/$bootscript_dst
728
sed -i -e "s/rootfstype=ext4/rootfstype=$ROOTFS_TYPE/" \
729
-e "s/rootfstype \"ext4\"/rootfstype \"$ROOTFS_TYPE\"/" $SDCARD/boot/$bootscript_dst
730
fi
731
732
# if we have boot.ini = remove orangepiEnv.txt and add UUID there if enabled
733
if [[ -f $SDCARD/boot/boot.ini ]]; then
734
sed -i -e "s/rootfstype \"ext4\"/rootfstype \"$ROOTFS_TYPE\"/" $SDCARD/boot/boot.ini
735
if [[ $CRYPTROOT_ENABLE == yes ]]; then
736
local rootpart="UUID=$(blkid -s UUID -o value ${LOOP}p${rootpart})"
737
sed -i 's/^setenv rootdev .*/setenv rootdev "\/dev\/mapper\/'$ROOT_MAPPER' cryptdevice='$rootpart':'$ROOT_MAPPER'"/' $SDCARD/boot/boot.ini
738
else
739
sed -i 's/^setenv rootdev .*/setenv rootdev "'$rootfs'"/' $SDCARD/boot/boot.ini
740
fi
741
if [[ $LINUXFAMILY != meson64 ]]; then
742
[[ -f $SDCARD/boot/orangepiEnv.txt ]] && rm $SDCARD/boot/orangepiEnv.txt
743
fi
744
fi
745
746
# if we have a headless device, set console to DEFAULT_CONSOLE
747
if [[ -n $DEFAULT_CONSOLE && -f $SDCARD/boot/orangepiEnv.txt ]]; then
748
if grep -lq "^console=" $SDCARD/boot/orangepiEnv.txt; then
749
sed -i "s/^console=.*/console=$DEFAULT_CONSOLE/" $SDCARD/boot/orangepiEnv.txt
750
else
751
echo "console=$DEFAULT_CONSOLE" >> $SDCARD/boot/orangepiEnv.txt
752
fi
753
fi
754
755
# recompile .cmd to .scr if boot.cmd exists
756
757
if [[ -f $SDCARD/boot/boot.cmd ]]; then
758
if [ -z $BOOTSCRIPT_OUTPUT ]; then BOOTSCRIPT_OUTPUT=boot.scr; fi
759
mkimage -C none -A arm -T script -d $SDCARD/boot/boot.cmd $SDCARD/boot/$BOOTSCRIPT_OUTPUT > /dev/null 2>&1
760
fi
761
762
# create extlinux config
763
if [[ -f $SDCARD/boot/extlinux/extlinux.conf ]]; then
764
echo " append root=$rootfs $SRC_CMDLINE $MAIN_CMDLINE" >> $SDCARD/boot/extlinux/extlinux.conf
765
[[ -f $SDCARD/boot/orangepiEnv.txt ]] && rm $SDCARD/boot/orangepiEnv.txt
766
fi
767
768
}
769
770
# update_initramfs
771
#
772
# this should be invoked as late as possible for any modifications by
773
# customize_image (userpatches) and prepare_partitions to be reflected in the
774
# final initramfs
775
#
776
# especially, this needs to be invoked after /etc/crypttab has been created
777
# for cryptroot-unlock to work:
778
# https://serverfault.com/questions/907254/cryproot-unlock-with-dropbear-timeout-while-waiting-for-askpass
779
#
780
# since Debian buster, it has to be called within create_image() on the $MOUNT
781
# path instead of $SDCARD (which can be a tmpfs and breaks cryptsetup-initramfs).
782
#
783
update_initramfs()
784
{
785
local chroot_target=$1
786
local target_dir=$(
787
find ${chroot_target}/lib/modules/ -maxdepth 1 -type d -name "*${VER}*"
788
)
789
if [ "$target_dir" != "" ]; then
790
update_initramfs_cmd="update-initramfs -uv -k $(basename $target_dir)"
791
else
792
exit_with_error "No kernel installed for the version" "${VER}"
793
fi
794
display_alert "Updating initramfs..." "$update_initramfs_cmd" ""
795
cp /usr/bin/$QEMU_BINARY $chroot_target/usr/bin/
796
mount_chroot "$chroot_target/"
797
798
chroot $chroot_target /bin/bash -c "$update_initramfs_cmd" >> $DEST/${LOG_SUBPATH}/install.log 2>&1 || {
799
display_alert "Updating initramfs FAILED, see:" "$DEST/${LOG_SUBPATH}/install.log" "err"
800
exit 23
801
}
802
display_alert "Updated initramfs." "for details see: $DEST/${LOG_SUBPATH}/install.log" "info"
803
804
display_alert "Re-enabling" "initramfs-tools hook for kernel"
805
chroot $chroot_target /bin/bash -c "chmod -v +x /etc/kernel/postinst.d/initramfs-tools" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
806
807
umount_chroot "$chroot_target/"
808
rm $chroot_target/usr/bin/$QEMU_BINARY
809
810
} #############################################################################
811
812
# create_image
813
#
814
# finishes creation of image from cached rootfs
815
#
816
create_image()
817
{
818
# stage: create file name
819
if [[ $SELECTED_CONFIGURATION == "cli_standard" ]]; then
820
IMAGE_TYPE=server
821
elif [[ $SELECTED_CONFIGURATION == "cli_minimal" ]]; then
822
IMAGE_TYPE=minimal
823
else
824
IMAGE_TYPE=desktop
825
fi
826
827
local version="${BOARD^}_${REVISION}_${DISTRIBUTION,}_${RELEASE}_${IMAGE_TYPE}"${DESKTOP_ENVIRONMENT:+_$DESKTOP_ENVIRONMENT}"_linux$(grab_version "$LINUXSOURCEDIR")"
828
829
if [[ ${RELEASE} == "raspi" ]]; then
830
local version="${BOARD^}_${REVISION}_raspios_bullseye_${IMAGE_TYPE}"${DESKTOP_ENVIRONMENT:+_$DESKTOP_ENVIRONMENT}"_linux$(grab_version "$LINUXSOURCEDIR")"
831
fi
832
833
[[ $ROOTFS_TYPE == nfs ]] && version=${version}_nfsboot
834
835
destimg=$DEST/images/${version}
836
rm -rf $destimg
837
mkdir -p $destimg
838
839
if [[ $ROOTFS_TYPE != nfs ]]; then
840
display_alert "Copying files to" "/"
841
echo -e "\nCopying files to [/]" >>"${DEST}"/${LOG_SUBPATH}/install.log
842
rsync -aHWXh \
843
--exclude="/boot/*" \
844
--exclude="/dev/*" \
845
--exclude="/proc/*" \
846
--exclude="/run/*" \
847
--exclude="/tmp/*" \
848
--exclude="/sys/*" \
849
--info=progress0,stats1 $SDCARD/ $MOUNT/ >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
850
else
851
display_alert "Creating rootfs archive" "rootfs.tgz" "info"
852
tar cp --xattrs --directory=$SDCARD/ --exclude='./boot/*' --exclude='./dev/*' --exclude='./proc/*' --exclude='./run/*' --exclude='./tmp/*' \
853
--exclude='./sys/*' . | pv -p -b -r -s $(du -sb $SDCARD/ | cut -f1) -N "rootfs.tgz" | gzip -c > $destimg/${version}-rootfs.tgz
854
fi
855
856
# stage: rsync /boot
857
display_alert "Copying files to" "/boot"
858
echo -e "\nCopying files to [/boot]" >>"${DEST}"/${LOG_SUBPATH}/install.log
859
if [[ $(findmnt --target $MOUNT/boot -o FSTYPE -n) == vfat ]]; then
860
# fat32
861
rsync -rLtWh \
862
--info=progress0,stats1 \
863
--log-file="${DEST}"/${LOG_SUBPATH}/install.log $SDCARD/boot $MOUNT >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
864
else
865
# ext4
866
rsync -aHWXh \
867
--info=progress0,stats1 \
868
--log-file="${DEST}"/${LOG_SUBPATH}/install.log $SDCARD/boot $MOUNT >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
869
fi
870
871
call_extension_method "pre_update_initramfs" "config_pre_update_initramfs" << 'PRE_UPDATE_INITRAMFS'
872
*allow config to hack into the initramfs create process*
873
Called after rsync has synced both `/root` and `/root` on the target, but before calling `update_initramfs`.
874
PRE_UPDATE_INITRAMFS
875
876
# stage: create final initramfs
877
[[ -n $KERNELSOURCE ]] && {
878
update_initramfs $MOUNT
879
}
880
881
# DEBUG: print free space
882
local freespace=$(LC_ALL=C df -h)
883
echo $freespace >> $DEST/${LOG_SUBPATH}/debootstrap.log
884
display_alert "Free SD cache" "$(echo -e "$freespace" | grep $SDCARD | awk '{print $5}')" "info"
885
display_alert "Mount point" "$(echo -e "$freespace" | grep $MOUNT | head -1 | awk '{print $5}')" "info"
886
887
# stage: write u-boot
888
write_uboot $LOOP
889
890
# fix wrong / permissions
891
chmod 755 $MOUNT
892
893
call_extension_method "pre_umount_final_image" "config_pre_umount_final_image" << 'PRE_UMOUNT_FINAL_IMAGE'
894
*allow config to hack into the image before the unmount*
895
Called before unmounting both `/root` and `/boot`.
896
PRE_UMOUNT_FINAL_IMAGE
897
898
# unmount /boot/efi first, then /boot, rootfs third, image file last
899
sync
900
[[ $UEFISIZE != 0 ]] && umount -l "${MOUNT}${UEFI_MOUNT_POINT}"
901
[[ $BOOTSIZE != 0 ]] && umount -l $MOUNT/boot
902
[[ $ROOTFS_TYPE != nfs ]] && umount -l $MOUNT
903
[[ $CRYPTROOT_ENABLE == yes ]] && cryptsetup luksClose $ROOT_MAPPER
904
905
call_extension_method "post_umount_final_image" "config_post_umount_final_image" << 'POST_UMOUNT_FINAL_IMAGE'
906
*allow config to hack into the image after the unmount*
907
Called after unmounting both `/root` and `/boot`.
908
POST_UMOUNT_FINAL_IMAGE
909
910
# to make sure its unmounted
911
while grep -Eq '(${MOUNT}|${DESTIMG})' /proc/mounts
912
do
913
display_alert "Wait for unmount" "${MOUNT}" "info"
914
sleep 5
915
done
916
917
losetup -d $LOOP
918
rm -rf --one-file-system $DESTIMG $MOUNT
919
920
mkdir -p $DESTIMG
921
mv ${SDCARD}.raw $DESTIMG/${version}.img
922
923
FINALDEST=${destimg}
924
925
# custom post_build_image_modify hook to run before fingerprinting and compression
926
[[ $(type -t post_build_image_modify) == function ]] && display_alert "Custom Hook Detected" "post_build_image_modify" "info" && post_build_image_modify "${DESTIMG}/${version}.img"
927
928
if [[ $BUILD_ALL != yes ]]; then
929
930
if [[ $COMPRESS_OUTPUTIMAGE == "" || $COMPRESS_OUTPUTIMAGE == no ]]; then
931
COMPRESS_OUTPUTIMAGE="sha,gpg,img"
932
elif [[ $COMPRESS_OUTPUTIMAGE == yes ]]; then
933
COMPRESS_OUTPUTIMAGE="sha,gpg,7z"
934
fi
935
936
if [[ $COMPRESS_OUTPUTIMAGE == *gz* ]]; then
937
display_alert "Compressing" "${DESTIMG}/${version}.img.gz" "info"
938
pigz -3 < $DESTIMG/${version}.img > $DESTIMG/${version}.img.gz
939
compression_type=".gz"
940
fi
941
942
if [[ $COMPRESS_OUTPUTIMAGE == *xz* ]]; then
943
display_alert "Compressing" "${DESTIMG}/${version}.img.xz" "info"
944
# compressing consumes a lot of memory we don't have. Waiting for previous packing job to finish helps to run a lot more builds in parallel
945
available_cpu=$(grep -c 'processor' /proc/cpuinfo)
946
[[ ${BUILD_ALL} == yes ]] && available_cpu=$(( $available_cpu * 30 / 100 )) # lets use 20% of resources in case of build-all
947
[[ ${available_cpu} -gt 8 ]] && available_cpu=8 # using more cpu cores for compressing is pointless
948
available_mem=$(LC_ALL=c free | grep Mem | awk '{print $4/$2 * 100.0}' | awk '{print int($1)}') # in percentage
949
# build optimisations when memory drops below 5%
950
if [[ ${BUILD_ALL} == yes && ( ${available_mem} -lt 15 || $(ps -uax | grep "pixz" | wc -l) -gt 4 )]]; then
951
while [[ $(ps -uax | grep "pixz" | wc -l) -gt 2 ]]
952
do echo -en "#"
953
sleep 20
954
done
955
fi
956
pixz -7 -p ${available_cpu} -f $(expr ${available_cpu} + 2) < $DESTIMG/${version}.img > ${DESTIMG}/${version}.img.xz
957
compression_type=".xz"
958
fi
959
960
if [[ $COMPRESS_OUTPUTIMAGE == *img* || $COMPRESS_OUTPUTIMAGE == *7z* ]]; then
961
# mv $DESTIMG/${version}.img ${FINALDEST}/${version}.img || exit 1
962
compression_type=""
963
fi
964
965
if [[ $COMPRESS_OUTPUTIMAGE == *sha* ]]; then
966
cd ${DESTIMG}
967
display_alert "SHA256 calculating" "${version}.img${compression_type}" "info"
968
sha256sum -b ${version}.img${compression_type} > ${version}.img${compression_type}.sha
969
fi
970
971
if [[ $COMPRESS_OUTPUTIMAGE == *gpg* ]]; then
972
cd ${DESTIMG}
973
if [[ -n $GPG_PASS ]]; then
974
display_alert "GPG signing" "${version}.img${compression_type}" "info"
975
[[ -n ${SUDO_USER} ]] && sudo chown -R ${SUDO_USER}:${SUDO_USER} "${DESTIMG}"/
976
echo "${GPG_PASS}" | sudo -H -u ${SUDO_USER} bash -c "gpg --passphrase-fd 0 --armor --detach-sign --pinentry-mode loopback --batch --yes ${DESTIMG}/${version}.img${compression_type}" || exit 1
977
#else
978
# display_alert "GPG signing skipped - no GPG_PASS" "${version}.img" "wrn"
979
fi
980
fi
981
982
#fingerprint_image "${DESTIMG}/${version}.img${compression_type}.txt" "${version}"
983
984
if [[ $COMPRESS_OUTPUTIMAGE == *7z* ]]; then
985
display_alert "Compressing" "${DESTIMG}/${version}.7z" "info"
986
7za a -t7z -bd -m0=lzma2 -mx=3 -mfb=64 -md=32m -ms=on \
987
${DESTIMG}/${version}.7z ${version}.key ${version}.img* >/dev/null 2>&1
988
find ${DESTIMG}/ -type \
989
f \( -name "${version}.img" -o -name "${version}.img.asc" -o -name "${version}.img.txt" -o -name "${version}.img.sha" \) -print0 \
990
>/dev/null 2>&1
991
fi
992
993
fi
994
#display_alert "Done building" "${DESTIMG}/${version}.img" "info"
995
display_alert "Done building" "${FINALDEST}/${version}.img" "info"
996
997
# call custom post build hook
998
[[ $(type -t post_build_image) == function ]] && post_build_image "${DESTIMG}/${version}.img"
999
1000
# move artefacts from temporally directory to its final destination
1001
[[ -n $compression_type ]] && rm $DESTIMG/${version}.img
1002
mv $DESTIMG/${version}* ${FINALDEST}
1003
rm -rf $DESTIMG
1004
1005
# write image to SD card
1006
if [[ $(lsblk "$CARD_DEVICE" 2>/dev/null) && -f ${FINALDEST}/${version}.img ]]; then
1007
1008
# make sha256sum if it does not exists. we need it for comparisson
1009
if [[ -f "${FINALDEST}/${version}".img.sha ]]; then
1010
local ifsha=$(cat ${FINALDEST}/${version}.img.sha | awk '{print $1}')
1011
else
1012
local ifsha=$(sha256sum -b "${FINALDEST}/${version}".img | awk '{print $1}')
1013
fi
1014
1015
display_alert "Writing image" "$CARD_DEVICE ${readsha}" "info"
1016
1017
# write to SD card
1018
pv -p -b -r -c -N "[ .... ] dd" ${FINALDEST}/${version}.img | dd of=$CARD_DEVICE bs=1M iflag=fullblock oflag=direct status=none
1019
1020
call_extension_method "post_write_sdcard" <<- 'POST_BUILD_IMAGE'
1021
*run after writing img to sdcard*
1022
After the image is written to `$CARD_DEVICE`, but before verifying it.
1023
You can still set SKIP_VERIFY=yes to skip verification.
1024
POST_BUILD_IMAGE
1025
1026
if [[ "${SKIP_VERIFY}" != "yes" ]]; then
1027
# read and compare
1028
display_alert "Verifying. Please wait!"
1029
local ofsha=$(dd if=$CARD_DEVICE count=$(du -b ${FINALDEST}/${version}.img | cut -f1) status=none iflag=count_bytes oflag=direct | sha256sum | awk '{print $1}')
1030
if [[ $ifsha == $ofsha ]]; then
1031
display_alert "Writing verified" "${version}.img" "info"
1032
else
1033
display_alert "Writing failed" "${version}.img" "err"
1034
fi
1035
fi
1036
elif [[ `systemd-detect-virt` == 'docker' && -n $CARD_DEVICE ]]; then
1037
# display warning when we want to write sd card under Docker
1038
display_alert "Can't write to $CARD_DEVICE" "Enable docker privileged mode in config-docker.conf" "wrn"
1039
fi
1040
1041
} #############################################################################
1042
1043