2015-12-30 14:31:33 +03:00
# Copyright (c) 2015 Igor Pecovnik, igor.pecovnik@gma**.com
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
2017-08-01 12:51:10 +03:00
# This file is a part of the Armbian build script
# https://github.com/armbian/build/
2015-12-30 14:31:33 +03:00
# Functions:
# debootstrap_ng
# create_rootfs_cache
# prepare_partitions
2018-07-31 16:02:26 -05:00
# update_initramfs
2015-12-30 14:31:33 +03:00
# create_image
2017-09-07 12:22:50 +03:00
# debootstrap_ng
2015-12-30 14:31:33 +03:00
#
debootstrap_ng( )
{
2021-02-24 18:19:09 +01:00
display_alert "Starting rootfs and image building process for" " ${ BRANCH } ${ BOARD } ${ RELEASE } ${ DESKTOP_APPGROUPS_SELECTED } ${ DESKTOP_ENVIRONMENT } ${ BUILD_MINIMAL } " "info"
2015-12-30 14:31:33 +03:00
2016-03-14 21:54:03 +03:00
[ [ $ROOTFS_TYPE != ext4 ] ] && display_alert " Assuming $BOARD $BRANCH kernel supports $ROOTFS_TYPE " "" "wrn"
2016-03-05 15:54:44 +03:00
2015-12-30 14:31:33 +03:00
# trap to unmount stuff in case of error/manual interruption
trap unmount_on_exit INT TERM EXIT
# stage: clean and create directories
2020-06-25 20:19:37 +02:00
rm -rf $SDCARD $MOUNT
mkdir -p $SDCARD $MOUNT $DEST /images $SRC /cache/rootfs
2015-12-30 14:31:33 +03:00
# stage: verify tmpfs configuration and mount
# default maximum size for tmpfs mount is 1/2 of available RAM
2017-08-18 16:59:06 +03:00
# CLI needs ~1.2GiB+ (Xenial CLI), Desktop - ~2.8GiB+ (Xenial Desktop w/o HW acceleration)
2015-12-30 14:31:33 +03:00
# calculate and set tmpfs mount to use 2/3 of available RAM
2016-03-08 21:22:35 +03:00
local phymem = $(( $( awk '/MemTotal/ {print $2}' /proc/meminfo) / 1024 * 2 / 3 )) # MiB
2017-08-18 16:59:06 +03:00
if [ [ $BUILD_DESKTOP = = yes ] ] ; then local tmpfs_max_size = 3500; else local tmpfs_max_size = 1500; fi # MiB
2016-05-08 16:10:11 +03:00
if [ [ $FORCE_USE_RAMDISK = = no ] ] ; then local use_tmpfs = no
2015-12-30 14:31:33 +03:00
elif [ [ $FORCE_USE_RAMDISK = = yes || $phymem -gt $tmpfs_max_size ] ] ; then
local use_tmpfs = yes
fi
2017-08-18 16:59:06 +03:00
[ [ -n $FORCE_TMPFS_SIZE ] ] && phymem = $FORCE_TMPFS_SIZE
2015-12-30 14:31:33 +03:00
2020-06-25 20:19:37 +02:00
[ [ $use_tmpfs = = yes ] ] && mount -t tmpfs -o size = ${ phymem } M tmpfs $SDCARD
2015-12-30 14:31:33 +03:00
# stage: prepare basic rootfs: unpack cache or create from scratch
create_rootfs_cache
# stage: install kernel and u-boot packages
2016-03-09 12:18:40 +03:00
# install distribution and board specific applications
2016-06-15 19:44:14 +03:00
2016-03-09 12:18:40 +03:00
install_distribution_specific
2016-06-24 17:30:39 +03:00
install_common
2015-12-30 14:31:33 +03:00
2017-01-12 15:35:48 +03:00
# install locally built packages
2021-03-02 18:04:56 +01:00
[ [ $EXTERNAL_NEW = = compile ] ] && chroot_installpackages_local
2018-05-24 21:01:02 +02:00
2017-01-12 15:35:48 +03:00
# install from apt.armbian.com
2021-03-02 18:04:56 +01:00
[ [ $EXTERNAL_NEW = = prebuilt ] ] && chroot_installpackages "yes"
2016-07-11 21:20:34 +03:00
2015-12-30 14:31:33 +03:00
# stage: user customization script
# NOTE: installing too many packages may fill tmpfs mount
2016-03-19 17:26:15 +03:00
customize_image
2015-12-30 14:31:33 +03:00
2019-04-19 23:25:55 +02:00
# create list of installed packages for debug purposes
2019-08-16 19:21:12 +02:00
chroot $SDCARD /bin/bash -c "dpkg --get-selections" | grep -v deinstall | awk '{print $1}' | cut -f1 -d':' > $DEST /debug/installed-packages-${ RELEASE } $( [ [ ${ BUILD_MINIMAL } = = yes ] ] && echo "-minimal" ) $( [ [ ${ BUILD_DESKTOP } = = yes ] ] && echo "-desktop" ) .list 2>& 1
2019-04-19 23:25:55 +02:00
2017-02-21 15:40:17 +03:00
# clean up / prepare for making the image
2020-06-25 20:19:37 +02:00
umount_chroot " $SDCARD "
2017-02-21 15:40:17 +03:00
post_debootstrap_tweaks
2016-03-14 18:29:47 +03:00
2016-03-05 15:54:44 +03:00
if [ [ $ROOTFS_TYPE = = fel ] ] ; then
2017-07-12 19:13:38 +03:00
FEL_ROOTFS = $SDCARD /
2016-03-05 15:54:44 +03:00
display_alert "Starting FEL boot" " $BOARD " "info"
2020-06-25 20:19:37 +02:00
source $SRC /lib/fel-load.sh
2016-03-05 15:54:44 +03:00
else
prepare_partitions
create_image
fi
2015-12-30 14:31:33 +03:00
# stage: unmount tmpfs
2020-06-25 20:19:37 +02:00
umount $SDCARD 2>& 1
2019-09-01 22:13:13 +02:00
if [ [ $use_tmpfs = yes ] ] ; then
2020-06-25 20:19:37 +02:00
while grep -qs " $SDCARD " /proc/mounts
2019-09-01 22:13:13 +02:00
do
2020-06-25 20:19:37 +02:00
umount $SDCARD
2019-09-01 22:13:13 +02:00
sleep 5
done
fi
2020-06-25 20:19:37 +02:00
rm -rf $SDCARD
2016-03-07 19:34:56 +03:00
2015-12-30 14:31:33 +03:00
# remove exit trap
trap - INT TERM EXIT
} #############################################################################
# create_rootfs_cache
#
# unpacks cached rootfs for $RELEASE or creates one
#
create_rootfs_cache( )
{
2020-04-05 14:05:10 +02:00
if [ [ " $ROOT_FS_CREATE_ONLY " = = "force" ] ] ; then
local cycles = 1
else
local cycles = 2
fi
2020-02-09 10:25:41 +01:00
# seek last cache, proceed to previous otherwise build it
2020-04-05 14:05:10 +02:00
for ( ( n = 0; n<${ cycles } ; n++) ) ; do
2020-02-09 10:25:41 +01:00
2020-06-25 20:19:37 +02:00
local packages_hash = $( get_package_list_hash " $(( $ROOTFSCACHE_VERSION - $n )) " )
2020-10-16 15:25:15 +02:00
[ [ -z ${ FORCED_MONTH_OFFSET } ] ] && FORCED_MONTH_OFFSET = ${ n }
local packages_hash = $( get_package_list_hash " $( date -d " $D + ${ FORCED_MONTH_OFFSET } month " +" %Y-%m-module $ROOTFSCACHE_VERSION " | sed 's/^0*//' ) " )
2021-02-24 18:19:09 +01:00
local cache_type = "cli"
[ [ ${ BUILD_DESKTOP } = = yes ] ] && local cache_type = "xfce-desktop"
[ [ -n ${ DESKTOP_ENVIRONMENT } ] ] && local cache_type = " ${ DESKTOP_ENVIRONMENT } "
[ [ ${ BUILD_MINIMAL } = = yes ] ] && local cache_type = "minimal"
2020-06-25 20:19:37 +02:00
local cache_name = ${ RELEASE } -${ cache_type } -${ ARCH } .$packages_hash .tar.lz4
local cache_fname = ${ SRC } /cache/rootfs/${ cache_name }
local display_name = ${ RELEASE } -${ cache_type } -${ ARCH } .${ packages_hash : 0 : 3 } ...${ packages_hash : 29 } .tar.lz4
2020-02-09 10:25:41 +01:00
display_alert "Checking for local cache" " $display_name " "info"
if [ [ ! -f $cache_fname && " $ROOT_FS_CREATE_ONLY " != "force" ] ] ; then
display_alert "searching on servers"
download_and_verify "_rootfs" " $cache_name "
fi
2021-02-26 18:27:21 +01:00
if [ [ -f $cache_fname && -f $cache_fname .aria2 && $USE_TORRENT = "no" && -z " $ROOT_FS_CREATE_ONLY " ] ] ; then
rm ${ cache_fname } *
download_and_verify "_rootfs" " $cache_name "
fi
if [ [ -f $cache_fname .aria2 && -z " $ROOT_FS_CREATE_ONLY " ] ] ; then
display_alert "resuming"
download_and_verify "_rootfs" " $cache_name "
fi
2020-02-09 10:25:41 +01:00
if [ [ -f $cache_fname ] ] ; then
break
else
display_alert "not found: try to use previous cache"
fi
done
2019-06-02 19:49:59 +02:00
2021-02-26 18:27:21 +01:00
if [ [ -f $cache_fname && ! -f $cache_fname .aria2 && " $ROOT_FS_CREATE_ONLY " != "force" ] ] ; then
2020-06-25 20:19:37 +02:00
local date_diff = $(( ( $( date +%s) - $( stat -c %Y $cache_fname ) ) / 86400 ))
2016-04-18 19:20:15 +03:00
display_alert " Extracting $display_name " " $date_diff days old " "info"
2020-06-25 20:19:37 +02:00
pv -p -b -r -c -N " [ .... ] $display_name " " $cache_fname " | lz4 -dc | tar xp --xattrs -C $SDCARD /
2019-06-02 19:49:59 +02:00
[ [ $? -ne 0 ] ] && rm $cache_fname && exit_with_error " Cache $cache_fname is corrupted and was deleted. Restart. "
2020-06-25 20:19:37 +02:00
rm $SDCARD /etc/resolv.conf
echo " nameserver $NAMESERVER " >> $SDCARD /etc/resolv.conf
2019-09-01 06:11:40 +08:00
create_sources_list " $RELEASE " " $SDCARD / "
2015-12-30 14:31:33 +03:00
else
2019-06-02 19:49:59 +02:00
display_alert "... remote not found" " Creating new rootfs cache for $RELEASE " "info"
2015-12-30 14:31:33 +03:00
# stage: debootstrap base system
2016-05-04 17:16:39 +03:00
if [ [ $NO_APT_CACHER != yes ] ] ; then
2020-04-25 21:25:05 +02:00
# apt-cacher-ng apt-get proxy parameter
2016-05-28 15:10:29 +03:00
local apt_extra = " -o Acquire::http::Proxy=\"http:// ${ APT_PROXY_ADDR :- localhost : 3142 } \" "
local apt_mirror = " http:// ${ APT_PROXY_ADDR :- localhost : 3142 } / $APT_MIRROR "
2015-12-30 14:31:33 +03:00
else
2016-05-27 13:14:16 +03:00
local apt_mirror = " http:// $APT_MIRROR "
2015-12-30 14:31:33 +03:00
fi
2016-05-04 17:16:39 +03:00
2017-01-12 15:31:53 +03:00
# fancy progress bars
[ [ -z $OUTPUT_DIALOG ] ] && local apt_extra_progress = "--show-progress -o DPKG::Progress-Fancy=1"
2015-12-30 14:31:33 +03:00
2019-08-16 19:21:12 +02:00
2015-12-30 14:31:33 +03:00
display_alert "Installing base system" "Stage 1/2" "info"
2019-08-16 19:21:12 +02:00
eval ' debootstrap --variant= minbase --include= ${ DEBOOTSTRAP_LIST // /, } ${ PACKAGE_LIST_EXCLUDE : + --exclude= ${ PACKAGE_LIST_EXCLUDE // /, } } \
2021-03-02 18:04:56 +01:00
--arch= $ARCH --components= ${ DEBOOTSTRAP_COMPONENTS } $DEBOOTSTRAP_OPTION --foreign $RELEASE $SDCARD / $apt_mirror ' \
2015-12-30 14:31:33 +03:00
${ PROGRESS_LOG_TO_FILE : + ' | tee -a $DEST/debug/debootstrap.log' } \
2016-03-07 18:29:48 +01:00
${ OUTPUT_DIALOG : + ' | dialog --backtitle "$backtitle" --progressbox "Debootstrap (stage 1/2)..." $TTY_Y $TTY_X' } \
2015-12-30 14:31:33 +03:00
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
2021-02-24 18:19:09 +01:00
[ [ ${ PIPESTATUS [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 "
2016-03-10 16:24:28 +03:00
2020-06-25 20:19:37 +02:00
cp /usr/bin/$QEMU_BINARY $SDCARD /usr/bin/
2016-05-26 18:42:14 +03:00
2020-06-25 20:19:37 +02:00
mkdir -p $SDCARD /usr/share/keyrings/
cp /usr/share/keyrings/*-archive-keyring.gpg $SDCARD /usr/share/keyrings/
2015-12-30 14:31:33 +03:00
display_alert "Installing base system" "Stage 2/2" "info"
2017-07-12 19:13:38 +03:00
eval 'chroot $SDCARD /bin/bash -c "/debootstrap/debootstrap --second-stage"' \
2015-12-30 14:31:33 +03:00
${ PROGRESS_LOG_TO_FILE : + ' | tee -a $DEST/debug/debootstrap.log' } \
2016-03-07 18:29:48 +01:00
${ OUTPUT_DIALOG : + ' | dialog --backtitle "$backtitle" --progressbox "Debootstrap (stage 2/2)..." $TTY_Y $TTY_X' } \
2015-12-30 14:31:33 +03:00
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
2021-02-24 18:19:09 +01:00
[ [ ${ PIPESTATUS [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 "
2016-03-10 16:24:28 +03:00
2020-06-25 20:19:37 +02:00
mount_chroot " $SDCARD "
2015-12-30 14:31:33 +03:00
2016-05-01 17:49:29 +03:00
# policy-rc.d script prevents starting or reloading services during image creation
2020-06-25 20:19:37 +02:00
printf '#!/bin/sh\nexit 101' > $SDCARD /usr/sbin/policy-rc.d
chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/initctl"
chroot $SDCARD /bin/bash -c "dpkg-divert --quiet --local --rename --add /sbin/start-stop-daemon"
printf '#!/bin/sh\necho "Warning: Fake start-stop-daemon called, doing nothing"' > $SDCARD /sbin/start-stop-daemon
printf '#!/bin/sh\necho "Warning: Fake initctl called, doing nothing"' > $SDCARD /sbin/initctl
chmod 755 $SDCARD /usr/sbin/policy-rc.d
chmod 755 $SDCARD /sbin/initctl
chmod 755 $SDCARD /sbin/start-stop-daemon
2015-12-30 14:31:33 +03:00
# stage: configure language and locales
display_alert "Configuring locales" " $DEST_LANG " "info"
2020-06-25 20:19:37 +02:00
[ [ -f $SDCARD /etc/locale.gen ] ] && sed -i " s/^# $DEST_LANG / $DEST_LANG / " $SDCARD /etc/locale.gen
2020-06-20 22:23:57 +03:00
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "locale-gen $DEST_LANG"' ${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "update-locale LANG=$DEST_LANG LANGUAGE=$DEST_LANG LC_MESSAGES=$DEST_LANG"' \
2015-12-30 14:31:33 +03:00
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
2016-05-08 16:10:11 +03:00
2020-06-25 20:19:37 +02:00
if [ [ -f $SDCARD /etc/default/console-setup ] ] ; then
2016-05-08 16:10:11 +03:00
sed -e 's/CHARMAP=.*/CHARMAP="UTF-8"/' -e 's/FONTSIZE=.*/FONTSIZE="8x16"/' \
2020-06-25 20:19:37 +02:00
-e 's/CODESET=.*/CODESET="guess"/' -i $SDCARD /etc/default/console-setup
2020-12-11 00:30:25 +01:00
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "setupcon --save --force"'
2016-05-08 16:10:11 +03:00
fi
2015-12-30 14:31:33 +03:00
2020-04-25 21:25:05 +02:00
# stage: create apt-get sources list
2017-07-12 19:13:38 +03:00
create_sources_list " $RELEASE " " $SDCARD / "
2015-12-30 14:31:33 +03:00
2016-10-07 17:56:32 +02:00
# add armhf arhitecture to arm64
2017-07-12 19:13:38 +03:00
[ [ $ARCH = = arm64 ] ] && eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "dpkg --add-architecture armhf"'
2016-10-11 20:01:03 +03:00
2018-06-05 18:53:45 +03:00
# this should fix resolvconf installation failure in some cases
2020-06-25 20:19:37 +02:00
chroot $SDCARD /bin/bash -c 'echo "resolvconf resolvconf/linkify-resolvconf boolean false" | debconf-set-selections'
2018-06-05 18:53:45 +03:00
2015-12-30 14:31:33 +03:00
# stage: update packages list
display_alert "Updating package list" " $RELEASE " "info"
2020-09-22 20:04:10 +03:00
eval 'LC_ALL=C LANG=C chroot $SDCARD /bin/bash -c "apt-get -q -y $apt_extra update"' \
2015-12-30 14:31:33 +03:00
${ PROGRESS_LOG_TO_FILE : + ' | tee -a $DEST/debug/debootstrap.log' } \
2016-03-07 18:29:48 +01:00
${ OUTPUT_DIALOG : + ' | dialog --backtitle "$backtitle" --progressbox "Updating package lists..." $TTY_Y $TTY_X' } \
2015-12-30 14:31:33 +03:00
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
2020-12-11 00:28:48 +01:00
[ [ ${ PIPESTATUS [0] } -ne 0 ] ] && display_alert "Updating package lists" "failed" "wrn"
2016-05-04 17:16:39 +03:00
2016-03-22 17:15:10 +03:00
# stage: upgrade base packages from xxx-updates and xxx-backports repository branches
2016-05-01 14:24:40 +03:00
display_alert "Upgrading base packages" "Armbian" "info"
2020-09-22 20:04:10 +03:00
eval ' LC_ALL = C LANG = C chroot $SDCARD /bin/bash -c " DEBIAN_FRONTEND=noninteractive apt-get -y -q \
2016-03-22 17:15:10 +03:00
$apt_extra $apt_extra_progress upgrade" ' \
${ PROGRESS_LOG_TO_FILE : + ' | tee -a $DEST/debug/debootstrap.log' } \
${ OUTPUT_DIALOG : + ' | dialog --backtitle "$backtitle" --progressbox "Upgrading base packages..." $TTY_Y $TTY_X' } \
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
2021-02-24 18:19:09 +01:00
# Myy: Dividing the desktop packages installation steps into multiple
# ones. We first install the "ADDITIONAL_PACKAGES" in order to get
# access to software-common-properties installation.
# THEN we add the APT sources and install the Desktop packages.
# TODO : Find a way to add APT sources WITHOUT software-common-properties
2020-12-11 00:28:48 +01:00
[ [ ${ PIPESTATUS [0] } -ne 0 ] ] && display_alert "Upgrading base packages" "failed" "wrn"
2016-05-04 17:16:39 +03:00
2015-12-30 14:31:33 +03:00
# stage: install additional packages
2021-02-24 18:19:09 +01:00
display_alert "Installing the main packages for" "Armbian" "info"
2020-09-22 20:04:10 +03:00
eval ' LC_ALL = C LANG = C chroot $SDCARD /bin/bash -c " DEBIAN_FRONTEND=noninteractive apt-get -y -q \
2021-02-24 18:19:09 +01:00
$apt_extra $apt_extra_progress --no-install-recommends install $PACKAGE_MAIN_LIST " ' \
2015-12-30 14:31:33 +03:00
${ PROGRESS_LOG_TO_FILE : + ' | tee -a $DEST/debug/debootstrap.log' } \
2021-02-24 18:19:09 +01:00
${ OUTPUT_DIALOG : + ' | dialog --backtitle "$backtitle" --progressbox "Installing Armbian main packages..." $TTY_Y $TTY_X' } \
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
[ [ ${ PIPESTATUS [0] } -ne 0 ] ] && exit_with_error " Installation of Armbian main packages for ${ BRANCH } ${ BOARD } ${ RELEASE } ${ DESKTOP_APPGROUPS_SELECTED } ${ DESKTOP_ENVIRONMENT } ${ BUILD_MINIMAL } failed "
if [ [ $BUILD_DESKTOP = = "yes" ] ] ; then
# FIXME Myy : Are we keeping this only for Desktop users,
# or should we extend this to CLI users too ?
# There might be some clunky boards that require Debian packages from
# specific repos...
display_alert "Adding apt sources for Desktop packages"
add_desktop_package_sources
local apt_desktop_install_flags = ""
if [ [ ! -z ${ DESKTOP_APT_FLAGS_SELECTED +x } ] ] ; then
for flag in ${ DESKTOP_APT_FLAGS_SELECTED } ; do
apt_desktop_install_flags += " --install- ${ flag } "
done
else
# Myy : Using the previous default option, if the variable isn't defined
# And ONLY if it's not defined !
apt_desktop_install_flags += " --no-install-recommends"
fi
display_alert "Installing the desktop packages for" "Armbian" "info"
eval ' LC_ALL = C LANG = C chroot $SDCARD /bin/bash -c " DEBIAN_FRONTEND=noninteractive apt-get -y -q \
$apt_extra $apt_extra_progress install ${ apt_desktop_install_flags } $PACKAGE_LIST_DESKTOP " ' \
${ PROGRESS_LOG_TO_FILE : + ' | tee -a $DEST/debug/debootstrap.log' } \
${ OUTPUT_DIALOG : + ' | dialog --backtitle "$backtitle" --progressbox "Installing Armbian desktop packages..." $TTY_Y $TTY_X' } \
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
[ [ ${ PIPESTATUS [0] } -ne 0 ] ] && exit_with_error " Installation of Armbian desktop packages for ${ BRANCH } ${ BOARD } ${ RELEASE } ${ DESKTOP_APPGROUPS_SELECTED } ${ DESKTOP_ENVIRONMENT } ${ BUILD_MINIMAL } failed "
fi
# Remove packages from packages.uninstall
display_alert "Uninstall packages" " $PACKAGE_LIST_UNINSTALL " "info"
eval ' LC_ALL = C LANG = C chroot $SDCARD /bin/bash -c " DEBIAN_FRONTEND=noninteractive apt-get -y -qq \
$apt_extra $apt_extra_progress purge $PACKAGE_LIST_UNINSTALL " ' \
${ PROGRESS_LOG_TO_FILE : + ' >> $DEST/debug/debootstrap.log' } \
${ OUTPUT_DIALOG : + ' | dialog --backtitle "$backtitle" --progressbox "Removing packages.uninstall packages..." $TTY_Y $TTY_X' } \
2015-12-30 14:31:33 +03:00
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
2016-03-10 16:24:28 +03:00
[ [ ${ PIPESTATUS [0] } -ne 0 ] ] && exit_with_error "Installation of Armbian packages failed"
2021-02-20 00:44:26 +01:00
# stage: purge residual packages
display_alert "Purging residual packages for" "Armbian" "info"
PURGINGPACKAGES = $( chroot $SDCARD /bin/bash -c "dpkg -l | grep \"^rc\" | awk '{print \$2}' | tr \"\n\" \" \"" )
eval ' LC_ALL = C LANG = C chroot $SDCARD /bin/bash -c " DEBIAN_FRONTEND=noninteractive apt-get -y -q \
$apt_extra $apt_extra_progress remove --purge $PURGINGPACKAGES " ' \
${ PROGRESS_LOG_TO_FILE : + ' | tee -a $DEST/debug/debootstrap.log' } \
${ OUTPUT_DIALOG : + ' | dialog --backtitle "$backtitle" --progressbox "Purging residual Armbian packages..." $TTY_Y $TTY_X' } \
${ OUTPUT_VERYSILENT : + ' >/dev/null 2>/dev/null' }
[ [ ${ PIPESTATUS [0] } -ne 0 ] ] && exit_with_error "Purging of residual Armbian packages failed"
2019-08-16 19:21:12 +02:00
# stage: remove downloaded packages
2020-09-22 20:04:10 +03:00
chroot $SDCARD /bin/bash -c "apt-get clean"
2019-08-16 19:21:12 +02:00
2015-12-30 14:31:33 +03:00
# DEBUG: print free space
2020-11-11 19:25:51 +01:00
local freespace = $( LC_ALL = C df -h)
echo $freespace >> $DEST /debug/debootstrap.log
2020-11-14 19:29:39 +01:00
display_alert "Free SD cache" " $( echo -e " $freespace " | grep $SDCARD | awk '{print $5}' ) " "info"
display_alert "Mount point" " $( echo -e " $freespace " | grep $MOUNT | head -1 | awk '{print $5}' ) " "info"
2015-12-30 14:31:33 +03:00
2019-06-12 21:20:03 +02:00
# create list of installed packages for debug purposes
2020-06-25 20:19:37 +02:00
chroot $SDCARD /bin/bash -c "dpkg --get-selections" | grep -v deinstall | awk '{print $1}' | cut -f1 -d':' > ${ cache_fname } .list 2>& 1
2019-06-12 21:20:03 +02:00
2019-09-01 22:13:13 +02:00
# creating xapian index that synaptic runs faster
if [ [ $BUILD_DESKTOP = = yes ] ] ; then
display_alert "Recreating Synaptic search index" "Please wait" "info"
2021-02-24 18:19:09 +01:00
chroot $SDCARD /bin/bash -c "[[ -f /usr/sbin/update-apt-xapian-index ]] && /usr/sbin/update-apt-xapian-index -u"
2019-09-01 22:13:13 +02:00
fi
2017-02-21 15:40:17 +03:00
# this is needed for the build process later since resolvconf generated file in /run is not saved
2020-06-25 20:19:37 +02:00
rm $SDCARD /etc/resolv.conf
echo " nameserver $NAMESERVER " >> $SDCARD /etc/resolv.conf
2017-02-21 15:40:17 +03:00
2015-12-30 14:31:33 +03:00
# stage: make rootfs cache archive
display_alert "Ending debootstrap process and preparing cache" " $RELEASE " "info"
sync
# the only reason to unmount here is compression progress display
# based on rootfs size calculation
2020-06-25 20:19:37 +02:00
umount_chroot " $SDCARD "
2015-12-30 14:31:33 +03:00
2020-06-25 20:19:37 +02:00
tar cp --xattrs --directory= $SDCARD / --exclude= './dev/*' --exclude= './proc/*' --exclude= './run/*' --exclude= './tmp/*' \
2021-02-24 18:19:09 +01:00
--exclude= './sys/*' . | pv -p -b -r -s $( du -sb $SDCARD / | cut -f1) -N " $display_name " | lz4 -5 -c > $cache_fname
2018-07-01 19:47:35 +02:00
# sign rootfs cache archive that it can be used for web cache once. Internal purposes
if [ [ -n $GPG_PASS ] ] ; then
2020-06-25 20:19:37 +02:00
echo $GPG_PASS | gpg --passphrase-fd 0 --armor --detach-sign --pinentry-mode loopback --batch --yes $cache_fname
2018-07-01 19:47:35 +02:00
fi
2015-12-30 14:31:33 +03:00
fi
2018-05-24 21:01:02 +02:00
2021-02-22 22:35:49 +01:00
# used for internal purposes. Faster rootfs cache rebuilding
if [ [ -n " $ROOT_FS_CREATE_ONLY " ] ] ; then
[ [ $use_tmpfs = yes ] ] && umount $SDCARD
rm -rf $SDCARD
# remove exit trap
trap - INT TERM EXIT
exit
fi
2020-06-25 20:19:37 +02:00
mount_chroot " $SDCARD "
2015-12-30 14:31:33 +03:00
} #############################################################################
# prepare_partitions
#
# creates image file, partitions and fs
# and mounts it to local dir
# FS-dependent stuff (boot and root fs partition types) happens here
#
prepare_partitions( )
{
display_alert "Preparing image file for rootfs" " $BOARD $RELEASE " "info"
# possible partition combinations
2017-04-23 16:56:19 +03:00
# /boot: none, ext4, ext2, fat (BOOTFS_TYPE)
# root: ext4, btrfs, f2fs, nfs (ROOTFS_TYPE)
2016-03-05 15:54:44 +03:00
# declare makes local variables by default if used inside a function
2015-12-30 14:31:33 +03:00
# NOTE: mountopts string should always start with comma if not empty
# array copying in old bash versions is tricky, so having filesystems as arrays
# with attributes as keys is not a good idea
declare -A parttype mkopts mkfs mountopts
parttype[ ext4] = ext4
2017-04-23 16:56:19 +03:00
parttype[ ext2] = ext2
2015-12-30 14:31:33 +03:00
parttype[ fat] = fat16
parttype[ f2fs] = ext4 # not a copy-paste error
2016-03-05 15:54:44 +03:00
parttype[ btrfs] = btrfs
2020-11-01 09:24:08 +02:00
parttype[ xfs] = xfs
2016-03-05 15:54:44 +03:00
# parttype[nfs] is empty
2016-05-28 13:44:57 +03:00
2017-06-05 18:30:04 +03:00
# metadata_csum and 64bit may need to be disabled explicitly when migrating to newer supported host OS releases
2021-03-06 21:10:29 +01:00
# add -N number of inodes to keep mount from running out
2021-03-15 00:41:47 +00:00
# create bigger number for desktop builds
if [ [ $BUILD_DESKTOP = = yes ] ] ; then local node_number = 4096; else local node_number = 1024; fi
2021-02-24 18:19:09 +01:00
if [ [ $( lsb_release -sc) = ~ bionic| buster| bullseye| cosmic| groovy| focal| hirsute| sid ] ] ; then
2021-03-15 00:41:47 +00:00
mkopts[ ext4] = " -q -m 2 -O ^64bit,^metadata_csum -N $(( 128 * ${ node_number } )) "
2018-06-03 18:28:39 +03:00
elif [ [ $( lsb_release -sc) = = xenial ] ] ; then
2021-03-15 00:41:47 +00:00
mkopts[ ext4] = " -q -m 2 -N $(( 128 * ${ node_number } )) "
2018-06-03 18:28:39 +03:00
fi
2016-04-08 13:58:57 +02:00
mkopts[ fat] = '-n BOOT'
2017-04-23 16:56:19 +03:00
mkopts[ ext2] = '-q'
2016-03-05 15:54:44 +03:00
# mkopts[f2fs] is empty
various minor fixes (#1993)
* fix permisson denied error happening in line 79
`mktemp` creates files with `-rw-------` (on Ubuntu bionic at least), which causes `line 79` to throw a permission denied error if the file isn't world readable.
* fixes umount error caused by single quotes
* force metadata duplication for btrfs root
Without this change metadata duplication is automatically turned off for `mkfs.btrfs` when the **build system** has a SSD drive.
However, since Armbian image is most likely flashed to a SD card afterwards, the drive type of the build host system should not be the deciding factor.
Metadata duplication is a good idea (better reliability) to have on a SD card.
From `man mkfs.btrfs`:
```
-m|--metadata <profile>
Specify the profile for the metadata block groups. Valid values are raid0, raid1, raid5, raid6, raid10, single or dup, (case does not matter).
A single device filesystem will default to DUP, unless a SSD is detected. Then it will default to single.
```
2020-05-28 08:52:54 +00:00
mkopts[ btrfs] = '-m dup'
2020-11-01 09:24:08 +02:00
# mkopts[xfs] is empty
2016-03-05 15:54:44 +03:00
# mkopts[nfs] is empty
2015-12-30 14:31:33 +03:00
mkfs[ ext4] = ext4
2017-04-23 16:56:19 +03:00
mkfs[ ext2] = ext2
2015-12-30 14:31:33 +03:00
mkfs[ fat] = vfat
mkfs[ f2fs] = f2fs
2016-03-05 15:54:44 +03:00
mkfs[ btrfs] = btrfs
2020-11-01 09:24:08 +02:00
mkfs[ xfs] = xfs
2016-03-05 15:54:44 +03:00
# mkfs[nfs] is empty
2015-12-30 14:31:33 +03:00
2016-02-04 19:47:40 +03:00
mountopts[ ext4] = ',commit=600,errors=remount-ro'
2017-04-23 16:56:19 +03:00
# mountopts[ext2] is empty
2016-03-05 15:54:44 +03:00
# mountopts[fat] is empty
# mountopts[f2fs] is empty
2020-08-31 17:31:15 +03:00
mountopts[ btrfs] = ',commit=600'
2020-11-01 09:24:08 +02:00
# mountopts[xfs] is empty
2016-03-05 15:54:44 +03:00
# mountopts[nfs] is empty
2015-12-30 14:31:33 +03:00
2019-08-16 21:44:01 -07:00
# default BOOTSIZE to use if not specified
2020-12-10 20:17:13 +00:00
DEFAULT_BOOTSIZE = 256 # MiB
2019-08-16 21:44:01 -07:00
2017-04-23 16:56:19 +03:00
# stage: determine partition configuration
if [ [ -n $BOOTFS_TYPE ] ] ; then
# 2 partition setup with forced /boot type
local bootfs = $BOOTFS_TYPE
local bootpart = 1
local rootpart = 2
2019-08-16 21:44:01 -07:00
[ [ -z $BOOTSIZE || $BOOTSIZE -le 8 ] ] && BOOTSIZE = ${ DEFAULT_BOOTSIZE }
2017-04-23 16:56:19 +03:00
elif [ [ $ROOTFS_TYPE != ext4 && $ROOTFS_TYPE != nfs ] ] ; then
# 2 partition setup for non-ext4 local root
local bootfs = ext4
local bootpart = 1
local rootpart = 2
2019-08-16 21:44:01 -07:00
[ [ -z $BOOTSIZE || $BOOTSIZE -le 8 ] ] && BOOTSIZE = ${ DEFAULT_BOOTSIZE }
2017-04-23 16:56:19 +03:00
elif [ [ $ROOTFS_TYPE = = nfs ] ] ; then
# single partition ext4 /boot, no root
local bootfs = ext4
local bootpart = 1
2019-08-16 21:44:01 -07:00
[ [ -z $BOOTSIZE || $BOOTSIZE -le 8 ] ] && BOOTSIZE = ${ DEFAULT_BOOTSIZE } # For cleanup processing only
2018-07-31 16:02:26 -05:00
elif [ [ $CRYPTROOT_ENABLE = = yes ] ] ; then
# 2 partition setup for encrypted /root and non-encrypted /boot
local bootfs = ext4
local bootpart = 1
local rootpart = 2
2019-08-16 21:44:01 -07:00
[ [ -z $BOOTSIZE || $BOOTSIZE -le 8 ] ] && BOOTSIZE = ${ DEFAULT_BOOTSIZE }
2017-04-23 16:56:19 +03:00
else
# single partition ext4 root
local rootpart = 1
BOOTSIZE = 0
fi
2015-12-30 14:31:33 +03:00
# stage: calculate rootfs size
2020-06-25 20:19:37 +02:00
local rootfs_size = $( du -sm $SDCARD / | cut -f1) # MiB
2015-12-30 14:31:33 +03:00
display_alert "Current rootfs size" " $rootfs_size MiB " "info"
if [ [ -n $FIXED_IMAGE_SIZE && $FIXED_IMAGE_SIZE = ~ ^[ 0-9] +$ ] ] ; then
display_alert "Using user-defined image size" " $FIXED_IMAGE_SIZE MiB " "info"
local sdsize = $FIXED_IMAGE_SIZE
# basic sanity check
2016-03-05 15:54:44 +03:00
if [ [ $ROOTFS_TYPE != nfs && $sdsize -lt $rootfs_size ] ] ; then
2016-03-06 13:01:05 +03:00
exit_with_error "User defined image size is too small" " $sdsize <= $rootfs_size "
2015-12-30 14:31:33 +03:00
fi
else
local imagesize = $(( $rootfs_size + $OFFSET + $BOOTSIZE )) # MiB
2017-08-28 09:19:57 -07:00
case $ROOTFS_TYPE in
btrfs)
# Used for server images, currently no swap functionality, so disk space
2020-08-28 19:44:37 +03:00
if [ [ $BTRFS_COMPRESSION = = none ] ] ; then
local sdsize = $( bc -l <<< " scale=0; (( $imagesize * 1.25) / 4 + 1) * 4 " )
else
# requirements are rather low since rootfs gets filled with compress-force=zlib
local sdsize = $( bc -l <<< " scale=0; (( $imagesize * 0.8) / 4 + 1) * 4 " )
fi
2017-08-28 09:19:57 -07:00
; ;
*)
2018-06-17 11:52:15 +00:00
# Hardcoded overhead +25% is needed for desktop images,
# for CLI it could be lower. Align the size up to 4MiB
2018-03-01 22:44:39 +01:00
if [ [ $BUILD_DESKTOP = = yes ] ] ; then
2020-06-25 20:19:37 +02:00
local sdsize = $( bc -l <<< " scale=0; ((( $imagesize * 1.30) / 1 + 0) / 4 + 1) * 4 " )
2018-03-01 22:44:39 +01:00
else
2020-06-25 20:19:37 +02:00
local sdsize = $( bc -l <<< " scale=0; ((( $imagesize * 1.25) / 1 + 0) / 4 + 1) * 4 " )
2018-03-01 22:44:39 +01:00
fi
2017-08-28 09:19:57 -07:00
; ;
esac
2015-12-30 14:31:33 +03:00
fi
# stage: create blank image
display_alert "Creating blank image for rootfs" " $sdsize MiB " "info"
2020-11-22 08:54:41 +01:00
if [ [ $FAST_CREATE_IMAGE = = yes ] ] ; then
truncate --size= ${ sdsize } M ${ SDCARD } .raw # sometimes results in fs corruption, revert to previous know to work solution
sync
else
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
fi
2015-12-30 14:31:33 +03:00
# stage: calculate boot partition size
2016-10-15 00:34:27 +03:00
local bootstart = $(( $OFFSET * 2048 ))
local rootstart = $(( $bootstart + ( $BOOTSIZE * 2048 )) )
local bootend = $(( $rootstart - 1 ))
2015-12-30 14:31:33 +03:00
# stage: create partition table
2016-03-05 15:54:44 +03:00
display_alert "Creating partitions" " ${ bootfs : +/boot : $bootfs } root: $ROOTFS_TYPE " "info"
2020-06-25 20:19:37 +02:00
parted -s ${ SDCARD } .raw -- mklabel ${ IMAGE_PARTITION_TABLE }
2016-03-05 15:54:44 +03:00
if [ [ $ROOTFS_TYPE = = nfs ] ] ; then
2016-10-15 00:34:27 +03:00
# single /boot partition
2020-06-25 20:19:37 +02:00
parted -s ${ SDCARD } .raw -- mkpart primary ${ parttype [ $bootfs ] } ${ bootstart } s 100%
2016-03-05 15:54:44 +03:00
elif [ [ $BOOTSIZE = = 0 ] ] ; then
2016-10-15 00:34:27 +03:00
# single root partition
2020-06-25 20:19:37 +02:00
parted -s ${ SDCARD } .raw -- mkpart primary ${ parttype [ $ROOTFS_TYPE ] } ${ rootstart } s 100%
2015-12-30 14:31:33 +03:00
else
2016-10-15 00:34:27 +03:00
# /boot partition + root partition
2020-06-25 20:19:37 +02:00
parted -s ${ SDCARD } .raw -- mkpart primary ${ parttype [ $bootfs ] } ${ bootstart } s ${ bootend } s
parted -s ${ SDCARD } .raw -- mkpart primary ${ parttype [ $ROOTFS_TYPE ] } ${ rootstart } s 100%
2015-12-30 14:31:33 +03:00
fi
# stage: mount image
2016-12-14 15:17:13 +03:00
# lock access to loop devices
exec { FD} >/var/lock/armbian-debootstrap-losetup
2020-06-25 20:19:37 +02:00
flock -x $FD
2016-12-14 15:17:13 +03:00
2015-12-30 14:31:33 +03:00
LOOP = $( losetup -f)
2016-05-01 17:49:29 +03:00
[ [ -z $LOOP ] ] && exit_with_error "Unable to find free loop device"
2016-01-10 18:55:02 +03:00
2020-06-25 20:19:37 +02:00
check_loop_device " $LOOP "
2016-10-15 00:34:27 +03:00
2020-06-25 20:19:37 +02:00
losetup $LOOP ${ SDCARD } .raw
2016-12-14 15:17:13 +03:00
# loop device was grabbed here, unlock
2020-06-25 20:19:37 +02:00
flock -u $FD
2016-12-14 15:17:13 +03:00
2020-06-25 20:19:37 +02:00
partprobe $LOOP
2015-12-30 14:31:33 +03:00
2016-10-15 00:34:27 +03:00
# stage: create fs, mount partitions, create fstab
2020-06-25 20:19:37 +02:00
rm -f $SDCARD /etc/fstab
2016-10-15 00:34:27 +03:00
if [ [ -n $rootpart ] ] ; then
2018-05-24 21:01:02 +02:00
local rootdevice = " ${ LOOP } p ${ rootpart } "
2018-08-31 22:17:35 +02:00
2018-07-31 16:02:26 -05:00
if [ [ $CRYPTROOT_ENABLE = = yes ] ] ; then
display_alert "Encrypting root partition with LUKS..." " cryptsetup luksFormat $rootdevice " ""
2020-06-25 20:19:37 +02:00
echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksFormat $CRYPTROOT_PARAMETERS $rootdevice -
echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksOpen $rootdevice $ROOT_MAPPER -
2018-07-31 16:02:26 -05:00
display_alert "Root partition encryption complete." "" "ext"
# TODO: pass /dev/mapper to Docker
rootdevice = /dev/mapper/$ROOT_MAPPER # used by `mkfs` and `mount` commands
fi
2018-08-31 22:17:35 +02:00
2018-05-24 21:01:02 +02:00
check_loop_device " $rootdevice "
2018-07-31 16:02:26 -05:00
display_alert "Creating rootfs" " $ROOTFS_TYPE on $rootdevice "
2020-11-14 19:29:39 +01:00
mkfs.${ mkfs [ $ROOTFS_TYPE ] } ${ mkopts [ $ROOTFS_TYPE ] } $rootdevice >> " ${ DEST } " /debug/install.log 2>& 1
2018-05-24 21:01:02 +02:00
[ [ $ROOTFS_TYPE = = ext4 ] ] && tune2fs -o journal_data_writeback $rootdevice > /dev/null
2020-08-28 19:44:37 +03:00
if [ [ $ROOTFS_TYPE = = btrfs && $BTRFS_COMPRESSION != none ] ] ; then
local fscreateopt = " -o compress-force= ${ BTRFS_COMPRESSION } "
fi
2020-06-25 20:19:37 +02:00
mount ${ fscreateopt } $rootdevice $MOUNT /
2018-07-31 16:02:26 -05:00
# create fstab (and crypttab) entry
if [ [ $CRYPTROOT_ENABLE = = yes ] ] ; then
# map the LUKS container partition via its UUID to be the 'cryptroot' device
2020-06-25 20:19:37 +02:00
echo " $ROOT_MAPPER UUID= $( blkid -s UUID -o value ${ LOOP } p${ rootpart } ) none luks " >> $SDCARD /etc/crypttab
2018-07-31 16:02:26 -05:00
local rootfs = $rootdevice # used in fstab
else
2020-06-25 20:19:37 +02:00
local rootfs = " UUID= $( blkid -s UUID -o value $rootdevice ) "
2018-07-31 16:02:26 -05:00
fi
2021-02-22 21:49:04 +01:00
echo " $rootfs / ${ mkfs [ $ROOTFS_TYPE ] } defaults,noatime ${ mountopts [ $ROOTFS_TYPE ] } 0 1 " >> $SDCARD /etc/fstab
2016-10-11 20:01:03 +03:00
fi
2016-10-15 00:34:27 +03:00
if [ [ -n $bootpart ] ] ; then
2020-11-14 19:29:39 +01:00
display_alert "Creating /boot" " $bootfs on ${ LOOP } p ${ bootpart } "
2017-07-27 17:33:32 +03:00
check_loop_device " ${ LOOP } p ${ bootpart } "
2020-11-14 19:29:39 +01:00
mkfs.${ mkfs [ $bootfs ] } ${ mkopts [ $bootfs ] } ${ LOOP } p${ bootpart } >> " ${ DEST } " /debug/install.log 2>& 1
2020-06-25 20:19:37 +02:00
mkdir -p $MOUNT /boot/
mount ${ LOOP } p${ bootpart } $MOUNT /boot/
echo " UUID= $( blkid -s UUID -o value ${ LOOP } p${ bootpart } ) /boot ${ mkfs [ $bootfs ] } defaults ${ mountopts [ $bootfs ] } 0 2 " >> $SDCARD /etc/fstab
2016-10-15 00:34:27 +03:00
fi
2020-06-25 20:19:37 +02:00
[ [ $ROOTFS_TYPE = = nfs ] ] && echo "/dev/nfs / nfs defaults 0 0" >> $SDCARD /etc/fstab
echo "tmpfs /tmp tmpfs defaults,nosuid 0 0" >> $SDCARD /etc/fstab
2016-03-05 15:54:44 +03:00
2016-10-15 00:34:27 +03:00
# stage: adjust boot script or boot environment
2017-07-12 19:13:38 +03:00
if [ [ -f $SDCARD /boot/armbianEnv.txt ] ] ; then
2018-07-31 16:02:26 -05:00
if [ [ $CRYPTROOT_ENABLE = = yes ] ] ; then
2020-06-25 20:19:37 +02:00
echo " rootdev= $rootdevice cryptdevice=UUID= $( blkid -s UUID -o value ${ LOOP } p${ rootpart } ) : $ROOT_MAPPER " >> $SDCARD /boot/armbianEnv.txt
2018-07-31 16:02:26 -05:00
else
2020-06-25 20:19:37 +02:00
echo " rootdev= $rootfs " >> $SDCARD /boot/armbianEnv.txt
2018-07-31 16:02:26 -05:00
fi
2020-06-25 20:19:37 +02:00
echo " rootfstype= $ROOTFS_TYPE " >> $SDCARD /boot/armbianEnv.txt
2016-10-15 00:34:27 +03:00
elif [ [ $rootpart != 1 ] ] ; then
local bootscript_dst = ${ BOOTSCRIPT ##* : }
2020-06-25 20:19:37 +02:00
sed -i 's/mmcblk0p1/mmcblk0p2/' $SDCARD /boot/$bootscript_dst
2016-10-15 00:34:27 +03:00
sed -i -e " s/rootfstype=ext4/rootfstype= $ROOTFS_TYPE / " \
2020-06-25 20:19:37 +02:00
-e " s/rootfstype \"ext4\"/rootfstype \" $ROOTFS_TYPE \"/ " $SDCARD /boot/$bootscript_dst
2016-03-05 15:54:44 +03:00
fi
2016-10-11 20:01:03 +03:00
2017-05-18 19:41:37 +02:00
# if we have boot.ini = remove armbianEnv.txt and add UUID there if enabled
2017-07-12 19:13:38 +03:00
if [ [ -f $SDCARD /boot/boot.ini ] ] ; then
2020-06-25 20:19:37 +02:00
sed -i -e " s/rootfstype \"ext4\"/rootfstype \" $ROOTFS_TYPE \"/ " $SDCARD /boot/boot.ini
2018-07-31 16:02:26 -05:00
if [ [ $CRYPTROOT_ENABLE = = yes ] ] ; then
local rootpart = " UUID= $( blkid -s UUID -o value ${ LOOP } p${ rootpart } ) "
2020-06-25 20:19:37 +02:00
sed -i 's/^setenv rootdev .*/setenv rootdev "\/dev\/mapper\/' $ROOT_MAPPER ' cryptdevice=' $rootpart ':' $ROOT_MAPPER '"/' $SDCARD /boot/boot.ini
2018-07-31 16:02:26 -05:00
else
2020-06-25 20:19:37 +02:00
sed -i 's/^setenv rootdev .*/setenv rootdev "' $rootfs '"/' $SDCARD /boot/boot.ini
2018-07-31 16:02:26 -05:00
fi
2020-10-13 21:34:45 +03:00
if [ [ $LINUXFAMILY != meson64 ] ] ; then
[ [ -f $SDCARD /boot/armbianEnv.txt ] ] && rm $SDCARD /boot/armbianEnv.txt
fi
2017-05-18 19:41:37 +02:00
fi
2018-11-15 19:03:57 +01:00
# if we have a headless device, set console to DEFAULT_CONSOLE
if [ [ -n $DEFAULT_CONSOLE && -f $SDCARD /boot/armbianEnv.txt ] ] ; then
2020-06-25 20:19:37 +02:00
if grep -lq "^console=" $SDCARD /boot/armbianEnv.txt; then
sed -i " s/console=.*/console= $DEFAULT_CONSOLE / " $SDCARD /boot/armbianEnv.txt
2018-11-15 19:03:57 +01:00
else
2020-06-25 20:19:37 +02:00
echo " console= $DEFAULT_CONSOLE " >> $SDCARD /boot/armbianEnv.txt
2021-03-18 18:57:45 +03:00
fi
2018-11-15 14:43:32 +01:00
fi
2016-10-14 22:04:32 +03:00
# recompile .cmd to .scr if boot.cmd exists
2017-07-12 19:13:38 +03:00
[ [ -f $SDCARD /boot/boot.cmd ] ] && \
2020-06-25 20:19:37 +02:00
mkimage -C none -A arm -T script -d $SDCARD /boot/boot.cmd $SDCARD /boot/boot.scr > /dev/null 2>& 1
2016-01-04 18:20:53 +03:00
2021-03-18 18:57:45 +03:00
# create extlinux config
if [ [ -f $SDCARD /boot/extlinux/extlinux.conf ] ] ; then
echo " APPEND root= $rootfs $SRC_CMDLINE $MAIN_CMDLINE " >> $SDCARD /boot/extlinux/extlinux.conf
[ [ -f $SDCARD /boot/armbianEnv.txt ] ] && rm $SDCARD /boot/armbianEnv.txt
fi
2016-01-04 18:20:53 +03:00
} #############################################################################
2015-12-30 14:31:33 +03:00
2018-07-31 16:02:26 -05:00
# update_initramfs
#
# this should be invoked as late as possible for any modifications by
2018-08-31 22:17:35 +02:00
# customize_image (userpatches) and prepare_partitions to be reflected in the
2018-07-31 16:02:26 -05:00
# final initramfs
#
# especially, this needs to be invoked after /etc/crypttab has been created
# for cryptroot-unlock to work:
# https://serverfault.com/questions/907254/cryproot-unlock-with-dropbear-timeout-while-waiting-for-askpass
#
2020-04-20 08:39:29 +02:00
# since Debian buster, it has to be called within create_image() on the $MOUNT
2020-01-06 22:31:54 +00:00
# path instead of $SDCARD (which can be a tmpfs and breaks cryptsetup-initramfs).
# see: https://github.com/armbian/build/issues/1584
#
update_initramfs( )
{
local chroot_target = $1
2018-07-31 16:02:26 -05:00
update_initramfs_cmd = " update-initramfs -uv -k ${ VER } - ${ LINUXFAMILY } "
display_alert "Updating initramfs..." " $update_initramfs_cmd " ""
2020-06-25 20:19:37 +02:00
cp /usr/bin/$QEMU_BINARY $chroot_target /usr/bin/
mount_chroot " $chroot_target / "
2018-08-31 22:17:35 +02:00
2020-06-25 20:19:37 +02:00
chroot $chroot_target /bin/bash -c " $update_initramfs_cmd " >> $DEST /debug/install.log 2>& 1
2020-11-14 19:29:39 +01:00
display_alert "Updated initramfs." " for details see: $DEST /debug/install.log " "info"
2018-08-31 22:17:35 +02:00
2020-11-06 10:52:53 +01:00
display_alert "Re-enabling" "initramfs-tools hook for kernel"
2020-11-15 15:45:22 +01:00
chroot $chroot_target /bin/bash -c "chmod -v +x /etc/kernel/postinst.d/initramfs-tools" >> " ${ DEST } " /debug/install.log 2>& 1
2020-11-06 10:52:53 +01:00
2020-06-25 20:19:37 +02:00
umount_chroot " $chroot_target / "
rm $chroot_target /usr/bin/$QEMU_BINARY
2018-07-31 16:02:26 -05:00
} #############################################################################
2015-12-30 14:31:33 +03:00
# create_image
#
# finishes creation of image from cached rootfs
#
create_image( )
{
2016-03-05 15:54:44 +03:00
# stage: create file name
2021-02-24 18:19:09 +01:00
local version = " Armbian_ ${ REVISION } _ ${ BOARD ^ } _ ${ RELEASE } _ ${ BRANCH } _ ${ VER /- $LINUXFAMILY / } ${ DESKTOP_ENVIRONMENT : +_ $DESKTOP_ENVIRONMENT } "
2016-08-30 19:25:32 +03:00
[ [ $BUILD_DESKTOP = = yes ] ] && version = ${ version } _desktop
2019-08-16 19:21:12 +02:00
[ [ $BUILD_MINIMAL = = yes ] ] && version = ${ version } _minimal
2016-08-30 19:25:32 +03:00
[ [ $ROOTFS_TYPE = = nfs ] ] && version = ${ version } _nfsboot
2016-03-05 15:54:44 +03:00
if [ [ $ROOTFS_TYPE != nfs ] ] ; then
2020-11-11 19:25:51 +01:00
display_alert "Copying files to" "/"
2016-06-05 13:12:47 +03:00
rsync -aHWXh --exclude= "/boot/*" --exclude= "/dev/*" --exclude= "/proc/*" --exclude= "/run/*" --exclude= "/tmp/*" \
2020-11-11 19:25:51 +01:00
--exclude= "/sys/*" --info= progress2,stats1 $SDCARD / $MOUNT / >> " ${ DEST } " /debug/install.log 2>& 1
2016-03-05 15:54:44 +03:00
else
display_alert "Creating rootfs archive" "rootfs.tgz" "info"
2020-06-25 20:19:37 +02:00
tar cp --xattrs --directory= $SDCARD / --exclude= './boot/*' --exclude= './dev/*' --exclude= './proc/*' --exclude= './run/*' --exclude= './tmp/*' \
--exclude= './sys/*' . | pv -p -b -r -s $( du -sb $SDCARD / | cut -f1) -N "rootfs.tgz" | gzip -c > $DEST /images/${ version } -rootfs.tgz
2016-03-05 15:54:44 +03:00
fi
2015-12-30 14:31:33 +03:00
# stage: rsync /boot
2020-11-11 19:25:51 +01:00
display_alert "Copying files to" "/boot"
2020-06-25 20:19:37 +02:00
if [ [ $( findmnt --target $MOUNT /boot -o FSTYPE -n) = = vfat ] ] ; then
2016-03-05 15:54:44 +03:00
# fat32
2020-11-11 19:25:51 +01:00
rsync -rLtWh --info= progress2,stats1 $SDCARD /boot $MOUNT >> " ${ DEST } " /debug/install.log 2>& 1
2016-03-05 15:54:44 +03:00
else
# ext4
2020-11-11 19:25:51 +01:00
rsync -aHWXh --info= progress2,stats1 $SDCARD /boot $MOUNT >> " ${ DEST } " /debug/install.log 2>& 1
2016-01-09 21:31:33 +03:00
fi
2015-12-30 14:31:33 +03:00
2020-01-06 22:31:54 +00:00
# stage: create final initramfs
2020-06-25 20:19:37 +02:00
update_initramfs $MOUNT
2020-01-06 22:31:54 +00:00
2015-12-30 14:31:33 +03:00
# DEBUG: print free space
2020-11-11 19:25:51 +01:00
local freespace = $( LC_ALL = C df -h)
echo $freespace >> $DEST /debug/debootstrap.log
2020-11-14 19:29:39 +01:00
display_alert "Free SD cache" " $( echo -e " $freespace " | grep $SDCARD | awk '{print $5}' ) " "info"
display_alert "Mount point" " $( echo -e " $freespace " | grep $MOUNT | head -1 | awk '{print $5}' ) " "info"
2015-12-30 14:31:33 +03:00
# stage: write u-boot
2020-06-25 20:19:37 +02:00
write_uboot $LOOP
2015-12-30 14:31:33 +03:00
2018-09-05 12:40:43 -07:00
# fix wrong / permissions
2020-06-25 20:19:37 +02:00
chmod 755 $MOUNT
2018-09-05 12:40:43 -07:00
2015-12-30 14:31:33 +03:00
# unmount /boot first, rootfs second, image file last
2016-05-26 18:42:14 +03:00
sync
2020-06-25 20:19:37 +02:00
[ [ $BOOTSIZE != 0 ] ] && umount -l $MOUNT /boot
[ [ $ROOTFS_TYPE != nfs ] ] && umount -l $MOUNT
[ [ $CRYPTROOT_ENABLE = = yes ] ] && cryptsetup luksClose $ROOT_MAPPER
2018-08-31 22:17:35 +02:00
2019-09-01 22:13:13 +02:00
# to make sure its unmounted
while grep -Eq '(${MOUNT}|${DESTIMG})' /proc/mounts
do
display_alert "Unmounting" " ${ MOUNT } " "info"
sleep 5
done
2020-06-25 20:19:37 +02:00
losetup -d $LOOP
rm -rf --one-file-system $DESTIMG $MOUNT
2019-09-01 22:13:13 +02:00
2020-06-25 20:19:37 +02:00
mkdir -p $DESTIMG
mv ${ SDCARD } .raw $DESTIMG /${ version } .img
2017-04-27 08:57:52 +02:00
2020-12-23 18:19:30 +01:00
FINALDEST = $DEST /images
if [ [ $BUILD_ALL = = yes ] ] ; then
if [ [ " $BETA " = = yes ] ] ; then
FINALDEST = $DEST /images/" ${ BOARD } " /nightly
else
FINALDEST = $DEST /images/" ${ BOARD } " /archive
fi
2020-12-24 11:51:53 +01:00
install -d -o nobody -g nogroup -m 775 ${ FINALDEST }
2020-12-23 18:19:30 +01:00
fi
2020-08-18 22:31:07 +02:00
if [ [ -z $SEND_TO_SERVER ] ] ; then
2020-05-28 15:52:44 +02:00
2020-04-20 08:39:29 +02:00
if [ [ $COMPRESS_OUTPUTIMAGE = = "" || $COMPRESS_OUTPUTIMAGE = = no ] ] ; then
COMPRESS_OUTPUTIMAGE = "sha,gpg,img"
elif [ [ $COMPRESS_OUTPUTIMAGE = = yes ] ] ; then
2019-07-06 22:35:43 +02:00
COMPRESS_OUTPUTIMAGE = "sha,gpg,7z"
fi
2020-05-28 15:52:44 +02:00
if [ [ $COMPRESS_OUTPUTIMAGE = = *gz* ] ] ; then
2020-12-23 18:19:30 +01:00
display_alert "Compressing" " ${ FINALDEST } / ${ version } .img.gz " "info"
pigz -3 < $DESTIMG /${ version } .img > ${ FINALDEST } /${ version } .img.gz
2020-05-28 15:52:44 +02:00
compression_type = ".gz"
fi
if [ [ $COMPRESS_OUTPUTIMAGE = = *xz* ] ] ; then
2020-12-23 18:19:30 +01:00
display_alert "Compressing" " ${ FINALDEST } / ${ version } .img.xz " "info"
2021-01-24 21:30:25 +01:00
# 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
2021-03-15 00:41:47 +00:00
[ [ ${ BUILD_ALL } = = yes && $( free | grep Mem | awk '{print $4/$2 * 100.0}' | awk '{print int($1)}' ) -lt 5 ] ] && while [ [ $( ps -uax | grep "pixz" | wc -l) -gt 4 ] ] ; do echo -en "#" ; sleep 20; done
pixz -8 -p 12 < $DESTIMG /${ version } .img > ${ FINALDEST } /${ version } .img.xz
2020-05-28 15:52:44 +02:00
compression_type = ".xz"
fi
if [ [ $COMPRESS_OUTPUTIMAGE = = *img* || $COMPRESS_OUTPUTIMAGE = = *7z* ] ] ; then
2020-12-23 18:19:30 +01:00
mv $DESTIMG /${ version } .img ${ FINALDEST } /${ version } .img || exit 1
2020-05-28 15:52:44 +02:00
compression_type = ""
fi
if [ [ $COMPRESS_OUTPUTIMAGE = = *sha* ] ] ; then
2020-12-23 18:19:30 +01:00
cd ${ FINALDEST }
2020-05-28 15:52:44 +02:00
display_alert "SHA256 calculating" " ${ version } .img ${ compression_type } " "info"
2020-06-25 20:19:37 +02:00
sha256sum -b ${ version } .img${ compression_type } > ${ version } .img${ compression_type } .sha
2019-07-06 22:35:43 +02:00
fi
if [ [ $COMPRESS_OUTPUTIMAGE = = *gpg* ] ] ; then
2020-12-23 18:19:30 +01:00
cd ${ FINALDEST }
2019-07-06 22:35:43 +02:00
if [ [ -n $GPG_PASS ] ] ; then
2020-05-28 15:52:44 +02:00
display_alert "GPG signing" " ${ version } .img ${ compression_type } " "info"
2020-12-23 18:19:30 +01:00
echo $GPG_PASS | gpg --passphrase-fd 0 --armor --detach-sign --pinentry-mode loopback --batch --yes ${ FINALDEST } /${ version } .img${ compression_type } || exit 1
2019-07-06 22:35:43 +02:00
else
display_alert "GPG signing skipped - no GPG_PASS" " ${ version } .img " "wrn"
fi
fi
2020-12-23 18:19:30 +01:00
fingerprint_image " ${ FINALDEST } / ${ version } .img ${ compression_type } .txt " " ${ version } "
2020-05-28 15:52:44 +02:00
2019-07-06 22:35:43 +02:00
if [ [ $COMPRESS_OUTPUTIMAGE = = *7z* ] ] ; then
2020-12-23 18:19:30 +01:00
display_alert "Compressing" " ${ FINALDEST } / ${ version } .7z " "info"
2020-04-12 22:28:37 +02:00
7za a -t7z -bd -m0= lzma2 -mx= 3 -mfb= 64 -md= 32m -ms= on \
2020-12-23 18:19:30 +01:00
${ FINALDEST } /${ version } .7z ${ version } .key ${ version } .img* >/dev/null 2>& 1
find ${ FINALDEST } / -type \
2020-05-28 15:52:44 +02:00
f \( -name " ${ version } .img " -o -name " ${ version } .img.asc " -o -name " ${ version } .img.txt " -o -name " ${ version } .img.sha " \) -print0 \
2020-04-12 22:28:37 +02:00
| xargs -0 rm >/dev/null 2>& 1
2020-04-04 18:29:42 +02:00
fi
2020-05-28 15:52:44 +02:00
2020-06-25 20:19:37 +02:00
rm -rf $DESTIMG
2017-01-15 23:10:47 +03:00
fi
2020-12-23 18:19:30 +01:00
display_alert "Done building" " ${ FINALDEST } / ${ version } .img " "info"
2020-08-18 22:31:07 +02:00
2017-06-11 16:28:24 +03:00
# call custom post build hook
2020-12-23 18:19:30 +01:00
[ [ $( type -t post_build_image) = = function ] ] && post_build_image " ${ FINALDEST } / ${ version } .img "
2017-06-11 16:28:24 +03:00
2018-08-03 19:35:19 +02:00
# write image to SD card
2020-12-23 18:19:30 +01:00
if [ [ $( lsblk " $CARD_DEVICE " 2>/dev/null) && -f ${ FINALDEST } /${ version } .img ] ] ; then
2019-09-01 00:12:36 +02:00
# make sha256sum if it does not exists. we need it for comparisson
2020-12-23 18:19:30 +01:00
if [ [ -f " ${ FINALDEST } / ${ version } " .img.sha ] ] ; then
local ifsha = $( cat ${ FINALDEST } /${ version } .img.sha | awk '{print $1}' )
2019-09-01 00:12:36 +02:00
else
2020-12-23 18:19:30 +01:00
local ifsha = $( sha256sum -b " ${ FINALDEST } / ${ version } " .img | awk '{print $1}' )
2019-09-01 00:12:36 +02:00
fi
display_alert "Writing image" " $CARD_DEVICE ${ readsha } " "info"
# write to SD card
2020-12-23 18:19:30 +01:00
pv -p -b -r -c -N "[ .... ] dd" ${ FINALDEST } /${ version } .img | dd of = $CARD_DEVICE bs = 1M iflag = fullblock oflag = direct status = none
2019-09-01 00:12:36 +02:00
# read and compare
display_alert "Verifying. Please wait!"
2020-12-23 18:19:30 +01:00
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}' )
2020-06-25 20:19:37 +02:00
if [ [ $ifsha = = $ofsha ] ] ; then
2019-09-01 00:12:36 +02:00
display_alert "Writing verified" " ${ version } .img " "info"
2019-07-06 22:35:43 +02:00
else
2018-08-03 19:35:19 +02:00
display_alert "Writing failed" " ${ version } .img " "err"
fi
2020-06-25 20:19:37 +02:00
elif [ [ ` systemd-detect-virt` = = 'docker' && -n $CARD_DEVICE ] ] ; then
2019-09-01 00:12:36 +02:00
# display warning when we want to write sd card under Docker
display_alert " Can't write to $CARD_DEVICE " "Enable docker privileged mode in config-docker.conf" "wrn"
2018-08-03 19:35:19 +02:00
fi
2016-03-09 12:18:40 +03:00
} #############################################################################