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.
|
|
|
|
#
|
|
|
|
# This file is a part of tool chain https://github.com/igorpecovnik/lib
|
|
|
|
#
|
|
|
|
|
|
|
|
# Functions:
|
|
|
|
# debootstrap_ng
|
|
|
|
# create_rootfs_cache
|
|
|
|
# prepare_partitions
|
|
|
|
# create_image
|
2016-03-09 12:18:40 +03:00
|
|
|
# mount_chroot
|
|
|
|
# umount_chroot
|
2015-12-30 14:31:33 +03:00
|
|
|
# unmount_on_exit
|
|
|
|
|
|
|
|
# custom_debootstrap_ng
|
|
|
|
#
|
|
|
|
# main debootstrap function
|
|
|
|
#
|
|
|
|
debootstrap_ng()
|
|
|
|
{
|
|
|
|
display_alert "Starting build process for" "$BOARD $RELEASE" "info"
|
|
|
|
|
2016-03-05 15:54:44 +03:00
|
|
|
# default rootfs type is ext4
|
2016-03-09 12:18:40 +03:00
|
|
|
[[ -z $ROOTFS_TYPE ]] && ROOTFS_TYPE=ext4
|
2016-03-05 15:54:44 +03:00
|
|
|
|
2016-03-09 12:18:40 +03:00
|
|
|
[[ "ext4 f2fs btrfs nfs fel" != *"$ROOTFS_TYPE"* ]] && exit_with_error "Unknown rootfs type" "$ROOTFS_TYPE"
|
2016-03-05 15:54:44 +03:00
|
|
|
|
|
|
|
# Fixed image size is in 1M dd blocks (MiB)
|
|
|
|
# to get size of block device /dev/sdX execute as root:
|
|
|
|
# echo $(( $(blockdev --getsize64 /dev/sdX) / 1024 / 1024 ))
|
2016-03-09 12:18:40 +03:00
|
|
|
[[ "btrfs f2fs" == *"$ROOTFS_TYPE"* && -z $FIXED_IMAGE_SIZE ]] && exit_with_error "please define FIXED_IMAGE_SIZE"
|
2016-03-05 15:54:44 +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
|
|
|
|
|
|
|
# small SD card with kernel, boot scritpt and .dtb/.bin files
|
2016-03-09 12:18:40 +03:00
|
|
|
[[ $ROOTFS_TYPE == nfs ]] && FIXED_IMAGE_SIZE=64
|
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
|
|
|
|
rm -rf $DEST/cache/sdcard $DEST/cache/mount
|
2016-03-05 15:54:44 +03:00
|
|
|
mkdir -p $DEST/cache/sdcard $DEST/cache/mount $DEST/images
|
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
|
2016-03-08 21:22:35 +03:00
|
|
|
# CLI needs ~1.2GiB+ (Xenial CLI), Desktop - ~2.2GiB+ (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
|
|
|
|
if [[ $BUILD_DESKTOP == yes ]]; then local tmpfs_max_size=2500; else local tmpfs_max_size=1500; fi # MiB
|
2015-12-30 14:31:33 +03:00
|
|
|
if [[ $FORCE_USE_RAMDISK == no ]]; then
|
|
|
|
local use_tmpfs=no
|
|
|
|
elif [[ $FORCE_USE_RAMDISK == yes || $phymem -gt $tmpfs_max_size ]]; then
|
|
|
|
local use_tmpfs=yes
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $use_tmpfs == yes ]]; then
|
|
|
|
mount -t tmpfs -o size=${tmpfs_max_size}M tmpfs $DEST/cache/sdcard
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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
|
|
|
|
install_distribution_specific
|
2015-12-30 14:31:33 +03:00
|
|
|
install_kernel
|
|
|
|
install_board_specific
|
|
|
|
|
|
|
|
# cleanup for install_kernel and install_board_specific
|
|
|
|
umount $DEST/cache/sdcard/tmp
|
|
|
|
|
|
|
|
# install desktop files
|
|
|
|
if [[ $BUILD_DESKTOP == yes ]]; then
|
|
|
|
install_desktop
|
|
|
|
fi
|
|
|
|
|
|
|
|
# install additional applications
|
|
|
|
if [[ $EXTERNAL == yes ]]; then
|
|
|
|
install_external_applications
|
|
|
|
fi
|
|
|
|
|
|
|
|
# stage: user customization script
|
|
|
|
# NOTE: installing too many packages may fill tmpfs mount
|
|
|
|
cp $SRC/userpatches/customize-image.sh $DEST/cache/sdcard/tmp/customize-image.sh
|
|
|
|
chmod +x $DEST/cache/sdcard/tmp/customize-image.sh
|
|
|
|
display_alert "Calling image customization script" "customize-image.sh" "info"
|
|
|
|
chroot $DEST/cache/sdcard /bin/bash -c "/tmp/customize-image.sh $RELEASE $FAMILY $BOARD $BUILD_DESKTOP"
|
|
|
|
|
|
|
|
# stage: cleanup
|
|
|
|
rm -f $DEST/cache/sdcard/usr/sbin/policy-rc.d
|
|
|
|
rm -f $DEST/cache/sdcard/usr/bin/qemu-arm-static
|
2016-01-02 13:28:51 +03:00
|
|
|
if [[ -x $DEST/cache/sdcard/sbin/initctl.REAL ]]; then
|
|
|
|
mv -f $DEST/cache/sdcard/sbin/initctl.REAL $DEST/cache/sdcard/sbin/initctl
|
|
|
|
fi
|
|
|
|
if [[ -x $DEST/cache/sdcard/sbin/start-stop-daemon.REAL ]]; then
|
|
|
|
mv -f $DEST/cache/sdcard/sbin/start-stop-daemon.REAL $DEST/cache/sdcard/sbin/start-stop-daemon
|
|
|
|
fi
|
2015-12-30 14:31:33 +03:00
|
|
|
|
2016-03-09 12:18:40 +03:00
|
|
|
umount_chroot
|
2015-12-30 14:31:33 +03:00
|
|
|
|
2016-03-14 18:29:47 +03:00
|
|
|
if [[ $ROOTFS_TYPE == fel || $ROOTFS_TYPE == nfs ]]; then
|
|
|
|
# kill /etc/network/interfaces on target to prevent conflicts between kernel
|
|
|
|
# and userspace network config (mainly on Xenial)
|
|
|
|
rm -f $DEST/cache/sdcard/etc/network/interfaces
|
2016-03-16 19:53:45 +03:00
|
|
|
printf "auto lo\niface lo inet loopback\n\niface eth0 inet manual" > $DEST/cache/sdcard/etc/network/interfaces
|
2016-03-14 18:29:47 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $ROOTFS_TYPE != ext4 ]]; then
|
|
|
|
# to prevent creating swap file on NFS (needs specific kernel options)
|
|
|
|
# and f2fs/btrfs (not recommended or needs specific kernel options)
|
|
|
|
touch $FEL_ROOTFS/var/swap
|
|
|
|
fi
|
|
|
|
|
2016-03-05 15:54:44 +03:00
|
|
|
if [[ $ROOTFS_TYPE == fel ]]; then
|
|
|
|
FEL_ROOTFS=$DEST/cache/sdcard/
|
|
|
|
display_alert "Starting FEL boot" "$BOARD" "info"
|
|
|
|
source $SRC/lib/fel-load.sh
|
|
|
|
else
|
|
|
|
prepare_partitions
|
|
|
|
create_image
|
|
|
|
fi
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
# stage: unmount tmpfs
|
|
|
|
if [[ $use_tmpfs = yes ]]; then
|
|
|
|
umount $DEST/cache/sdcard
|
|
|
|
fi
|
|
|
|
|
2016-03-07 19:34:56 +03:00
|
|
|
rm -rf $DEST/cache/sdcard
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
[[ $BUILD_DESKTOP == yes ]] && local variant_desktop=yes
|
2016-03-04 00:00:37 +03:00
|
|
|
local packages_hash=$(get_package_list_hash $PACKAGE_LIST)
|
2016-02-26 19:25:49 +03:00
|
|
|
local cache_fname="$DEST/cache/rootfs/$RELEASE${variant_desktop:+_desktop}-ng.$packages_hash.tgz"
|
2016-03-05 15:54:44 +03:00
|
|
|
local display_name=$RELEASE${variant_desktop:+_desktop}-ng.${packages_hash:0:3}...${packages_hash:29}.tgz
|
2015-12-30 14:31:33 +03:00
|
|
|
if [[ -f $cache_fname ]]; then
|
|
|
|
local filemtime=$(stat -c %Y $cache_fname)
|
|
|
|
local currtime=$(date +%s)
|
|
|
|
local diff=$(( (currtime - filemtime) / 86400 ))
|
2016-03-05 15:54:44 +03:00
|
|
|
display_alert "Extracting $display_name" "$diff days old" "info"
|
|
|
|
pv -p -b -r -c -N "$display_name" "$cache_fname" | pigz -dc | tar xp -C $DEST/cache/sdcard/
|
2015-12-30 14:31:33 +03:00
|
|
|
else
|
|
|
|
display_alert "Creating new rootfs for" "$RELEASE" "info"
|
|
|
|
|
|
|
|
# stage: debootstrap base system
|
|
|
|
# apt-cacher-ng mirror configurarion
|
2016-03-10 16:24:28 +03:00
|
|
|
[[ -n $APT_PROXY_ADDR ]] && display_alert "Using custom apt-cacher-ng address" "$APT_PROXY_ADDR" "info"
|
2016-03-07 19:34:56 +03:00
|
|
|
if [[ $RELEASE == trusty || $RELEASE == xenial ]]; then
|
2016-03-10 16:24:28 +03:00
|
|
|
local apt_mirror="http://${APT_PROXY_ADDR:-localhost:3142}/ports.ubuntu.com/"
|
2015-12-30 14:31:33 +03:00
|
|
|
else
|
2016-03-10 16:24:28 +03:00
|
|
|
local apt_mirror="http://${APT_PROXY_ADDR:-localhost:3142}/httpredir.debian.org/debian"
|
2015-12-30 14:31:33 +03:00
|
|
|
fi
|
|
|
|
# apt-cacher-ng apt-get proxy parameter
|
2016-03-10 16:24:28 +03:00
|
|
|
local apt_extra='-o Acquire::http::Proxy="http://${APT_PROXY_ADDR:-localhost:3142}"'
|
2016-03-10 17:14:34 +03:00
|
|
|
# fancy progress bars (except for Wheezy target)
|
|
|
|
[[ -z $OUTPUT_DIALOG && $RELEASE != wheezy ]] && local apt_extra_progress="--show-progress -o DPKG::Progress-Fancy=1"
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
display_alert "Installing base system" "Stage 1/2" "info"
|
|
|
|
eval 'debootstrap --include=debconf-utils,locales --arch=armhf --foreign $RELEASE $DEST/cache/sdcard/ $apt_mirror' \
|
|
|
|
${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'}
|
|
|
|
|
2016-03-10 16:24:28 +03:00
|
|
|
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Debootstrap base system first stage failed"
|
|
|
|
|
2015-12-30 14:31:33 +03:00
|
|
|
cp /usr/bin/qemu-arm-static $DEST/cache/sdcard/usr/bin/
|
|
|
|
# NOTE: not needed?
|
|
|
|
mkdir -p $DEST/cache/sdcard/usr/share/keyrings/
|
|
|
|
cp /usr/share/keyrings/debian-archive-keyring.gpg $DEST/cache/sdcard/usr/share/keyrings/
|
|
|
|
|
|
|
|
display_alert "Installing base system" "Stage 2/2" "info"
|
|
|
|
eval 'chroot $DEST/cache/sdcard /bin/bash -c "/debootstrap/debootstrap --second-stage"' \
|
|
|
|
${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'}
|
|
|
|
|
2016-03-10 16:24:28 +03:00
|
|
|
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit_with_error "Debootstrap base system second stage failed"
|
|
|
|
|
2016-03-09 12:18:40 +03:00
|
|
|
mount_chroot
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
# policy-rc.d script prevents starting or reloading services
|
|
|
|
# from dpkg pre- and post-install scripts during image creation
|
|
|
|
|
|
|
|
cat <<EOF > $DEST/cache/sdcard/usr/sbin/policy-rc.d
|
|
|
|
#!/bin/sh
|
|
|
|
exit 101
|
|
|
|
EOF
|
2016-01-02 13:28:51 +03:00
|
|
|
chmod 755 $DEST/cache/sdcard/usr/sbin/policy-rc.d
|
|
|
|
|
|
|
|
# ported from debootstrap and multistrap for upstart support
|
|
|
|
if [[ -x $DEST/cache/sdcard/sbin/initctl ]]; then
|
|
|
|
mv $DEST/cache/sdcard/sbin/start-stop-daemon $DEST/cache/sdcard/sbin/start-stop-daemon.REAL
|
|
|
|
cat <<EOF > $DEST/cache/sdcard/sbin/start-stop-daemon
|
|
|
|
#!/bin/sh
|
|
|
|
echo "Warning: Fake start-stop-daemon called, doing nothing"
|
|
|
|
EOF
|
|
|
|
chmod 755 $DEST/cache/sdcard/sbin/start-stop-daemon
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -x $DEST/cache/sdcard/sbin/initctl ]]; then
|
|
|
|
mv $DEST/cache/sdcard/sbin/initctl $DEST/cache/sdcard/sbin/initctl.REAL
|
|
|
|
cat <<EOF > $DEST/cache/sdcard/sbin/initctl
|
|
|
|
#!/bin/sh
|
|
|
|
echo "Warning: Fake initctl called, doing nothing"
|
|
|
|
EOF
|
|
|
|
chmod 755 $DEST/cache/sdcard/sbin/initctl
|
|
|
|
fi
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
# stage: configure language and locales
|
|
|
|
display_alert "Configuring locales" "$DEST_LANG" "info"
|
|
|
|
|
|
|
|
if [ -f $DEST/cache/sdcard/etc/locale.gen ]; then sed -i "s/^# $DEST_LANG/$DEST_LANG/" $DEST/cache/sdcard/etc/locale.gen; fi
|
|
|
|
eval 'LC_ALL=C LANG=C chroot $DEST/cache/sdcard /bin/bash -c "locale-gen $DEST_LANG"' ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
|
|
|
|
eval 'LC_ALL=C LANG=C chroot $DEST/cache/sdcard /bin/bash -c "update-locale LANG=$DEST_LANG LANGUAGE=$DEST_LANG LC_MESSAGES=POSIX"' \
|
|
|
|
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
|
|
|
|
LC_ALL=C LANG=C chroot $DEST/cache/sdcard /bin/bash -c "export CHARMAP=$CONSOLE_CHAR FONTFACE=8x16"
|
|
|
|
|
|
|
|
# stage: copy proper apt sources list
|
|
|
|
cp $SRC/lib/config/sources.list.$RELEASE $DEST/cache/sdcard/etc/apt/sources.list
|
|
|
|
|
|
|
|
# stage: add armbian repository and install key
|
|
|
|
echo "deb http://apt.armbian.com $RELEASE main" > $DEST/cache/sdcard/etc/apt/sources.list.d/armbian.list
|
|
|
|
cp $SRC/lib/bin/armbian.key $DEST/cache/sdcard
|
|
|
|
eval 'chroot $DEST/cache/sdcard /bin/bash -c "cat armbian.key | apt-key add -"' \
|
|
|
|
${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
|
|
|
|
rm $DEST/cache/sdcard/armbian.key
|
|
|
|
|
|
|
|
# stage: update packages list
|
|
|
|
display_alert "Updating package list" "$RELEASE" "info"
|
|
|
|
eval 'LC_ALL=C LANG=C chroot $DEST/cache/sdcard /bin/bash -c "apt-get -y $apt_extra update"' \
|
|
|
|
${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'}
|
|
|
|
|
|
|
|
# stage: install additional packages
|
|
|
|
display_alert "Installing packages for" "Armbian" "info"
|
2016-03-10 17:14:34 +03:00
|
|
|
eval 'LC_ALL=C LANG=C chroot $DEST/cache/sdcard /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get -y -q \
|
|
|
|
$apt_extra $apt_extra_progress --no-install-recommends install $PACKAGE_LIST"' \
|
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 "Installing Armbian system..." $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"
|
|
|
|
|
2015-12-30 14:31:33 +03:00
|
|
|
# DEBUG: print free space
|
2016-03-10 16:24:28 +03:00
|
|
|
echo
|
|
|
|
echo "Free space:"
|
2015-12-30 14:31:33 +03:00
|
|
|
df -h | grep "$DEST/cache/" | tee -a $DEST/debug/debootstrap.log
|
|
|
|
|
|
|
|
# stage: remove downloaded packages
|
|
|
|
chroot $DEST/cache/sdcard /bin/bash -c "apt-get clean"
|
|
|
|
|
|
|
|
# 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
|
2016-03-09 12:18:40 +03:00
|
|
|
umount_chroot
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
tar cp --directory=$DEST/cache/sdcard/ --exclude='./dev/*' --exclude='./proc/*' --exclude='./run/*' --exclude='./tmp/*' \
|
|
|
|
--exclude='./sys/*' . | \
|
2016-03-05 15:54:44 +03:00
|
|
|
pv -p -b -r -s $(du -sb $DEST/cache/sdcard/ | cut -f1) -N "$display_name" | pigz > $cache_fname
|
2015-12-30 14:31:33 +03:00
|
|
|
fi
|
2016-03-09 12:18:40 +03:00
|
|
|
mount_chroot
|
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
|
2016-03-05 15:54:44 +03:00
|
|
|
# ext4 root only (BOOTSIZE == 0 && ROOTFS_TYPE == ext4)
|
|
|
|
# ext4 boot + non-ext4 local root (BOOTSIZE == 0; ROOTFS_TYPE != ext4 or nfs)
|
|
|
|
# fat32 boot + ext4 root (BOOTSIZE > 0 && ROOTFS_TYPE == ext4)
|
|
|
|
# fat32 boot + non-ext4 local root (BOOTSIZE > 0; ROOTFS_TYPE != ext4 or nfs)
|
|
|
|
# ext4 boot + NFS root (BOOTSIZE == 0; ROOTFS_TYPE == nfs)
|
|
|
|
# fat32 boot + NFS root (BOOTSIZE > 0; ROOTFS_TYPE == nfs)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
parttype[fat]=fat16
|
|
|
|
parttype[f2fs]=ext4 # not a copy-paste error
|
2016-03-05 15:54:44 +03:00
|
|
|
parttype[btrfs]=btrfs
|
|
|
|
# parttype[nfs] is empty
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
mkopts[ext4]='-q' # "-O journal_data_writeback" can be set here
|
|
|
|
mkopts[fat]='-n boot'
|
2016-03-05 15:54:44 +03:00
|
|
|
# mkopts[f2fs] is empty
|
|
|
|
# mkopts[btrfs] is empty
|
|
|
|
# mkopts[nfs] is empty
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
mkfs[ext4]=ext4
|
|
|
|
mkfs[fat]=vfat
|
|
|
|
mkfs[f2fs]=f2fs
|
2016-03-05 15:54:44 +03:00
|
|
|
mkfs[btrfs]=btrfs
|
|
|
|
# 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'
|
2016-03-05 15:54:44 +03:00
|
|
|
# mountopts[fat] is empty
|
|
|
|
# mountopts[f2fs] is empty
|
|
|
|
# mountopts[btrfs] is empty
|
|
|
|
# mountopts[nfs] is empty
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
# stage: calculate rootfs size
|
|
|
|
local rootfs_size=$(du -sm $DEST/cache/sdcard/ | cut -f1) # MiB
|
|
|
|
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
|
|
|
|
# Hardcoded overhead +30% for ext4 leaves ~10% free on root partition
|
|
|
|
# extra 128 MiB for emergency swap file
|
|
|
|
local sdsize=$(bc -l <<< "scale=0; ($imagesize * 1.3) / 1 + 128")
|
|
|
|
fi
|
|
|
|
|
|
|
|
# stage: create blank image
|
|
|
|
display_alert "Creating blank image for rootfs" "$sdsize MiB" "info"
|
|
|
|
dd if=/dev/zero bs=1M status=none count=$sdsize | pv -p -b -r -s $(( $sdsize * 1024 * 1024 )) | dd status=none of=$DEST/cache/tmprootfs.raw
|
|
|
|
|
|
|
|
# stage: determine partition configuration
|
|
|
|
# boot
|
2016-03-05 15:54:44 +03:00
|
|
|
if [[ $ROOTFS_TYPE != ext4 && $BOOTSIZE == 0 ]]; then
|
2015-12-30 14:31:33 +03:00
|
|
|
local bootfs=ext4
|
2016-02-04 19:47:40 +03:00
|
|
|
BOOTSIZE=32 # MiB
|
2015-12-30 14:31:33 +03:00
|
|
|
elif [[ $BOOTSIZE != 0 ]]; then
|
|
|
|
local bootfs=fat
|
2016-02-04 19:47:40 +03:00
|
|
|
BOOTSIZE=64 # MiB, fix for rsync duplicating zImage
|
2015-12-30 14:31:33 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# stage: calculate boot partition size
|
|
|
|
BOOTSTART=$(($OFFSET * 2048))
|
|
|
|
ROOTSTART=$(($BOOTSTART + ($BOOTSIZE * 2048)))
|
|
|
|
BOOTEND=$(($ROOTSTART - 1))
|
|
|
|
|
|
|
|
# stage: create partition table
|
2016-03-05 15:54:44 +03:00
|
|
|
display_alert "Creating partitions" "${bootfs:+/boot: $bootfs }root: $ROOTFS_TYPE" "info"
|
2015-12-30 14:31:33 +03:00
|
|
|
parted -s $DEST/cache/tmprootfs.raw -- mklabel msdos
|
2016-03-05 15:54:44 +03:00
|
|
|
if [[ $ROOTFS_TYPE == nfs ]]; then
|
|
|
|
parted -s $DEST/cache/tmprootfs.raw -- mkpart primary ${parttype[$bootfs]} ${BOOTSTART}s -1s
|
|
|
|
elif [[ $BOOTSIZE == 0 ]]; then
|
|
|
|
parted -s $DEST/cache/tmprootfs.raw -- mkpart primary ${parttype[$ROOTFS_TYPE]} ${ROOTSTART}s -1s
|
2015-12-30 14:31:33 +03:00
|
|
|
else
|
|
|
|
parted -s $DEST/cache/tmprootfs.raw -- mkpart primary ${parttype[$bootfs]} ${BOOTSTART}s ${BOOTEND}s
|
2016-03-05 15:54:44 +03:00
|
|
|
parted -s $DEST/cache/tmprootfs.raw -- mkpart primary ${parttype[$ROOTFS_TYPE]} ${ROOTSTART}s -1s
|
2015-12-30 14:31:33 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# stage: mount image
|
|
|
|
LOOP=$(losetup -f)
|
|
|
|
if [[ -z $LOOP ]]; then
|
|
|
|
# NOTE: very unlikely with this debootstrap process
|
2016-03-05 21:26:01 +03:00
|
|
|
exit_with_error "Unable to find free loop device"
|
2015-12-30 14:31:33 +03:00
|
|
|
fi
|
2016-01-10 18:55:02 +03:00
|
|
|
|
|
|
|
# NOTE: losetup -P option is not available in Trusty
|
|
|
|
losetup $LOOP $DEST/cache/tmprootfs.raw
|
|
|
|
partprobe $LOOP
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
# stage: create fs
|
|
|
|
if [[ $BOOTSIZE == 0 ]]; then
|
2016-03-05 15:54:44 +03:00
|
|
|
eval mkfs.${mkfs[$ROOTFS_TYPE]} ${mkopts[$ROOTFS_TYPE]} ${LOOP}p1 ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
|
2015-12-30 14:31:33 +03:00
|
|
|
else
|
2016-03-05 15:54:44 +03:00
|
|
|
if [[ $ROOTFS_TYPE != nfs ]]; then
|
|
|
|
eval mkfs.${mkfs[$ROOTFS_TYPE]} ${mkopts[$ROOTFS_TYPE]} ${LOOP}p2 ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
|
|
|
|
fi
|
2015-12-30 14:31:33 +03:00
|
|
|
eval mkfs.${mkfs[$bootfs]} ${mkopts[$bootfs]} ${LOOP}p1 ${OUTPUT_VERYSILENT:+' >/dev/null 2>/dev/null'}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# stage: mount partitions and create proper fstab
|
|
|
|
rm -f $DEST/cache/sdcard/etc/fstab
|
|
|
|
if [[ $BOOTSIZE == 0 ]]; then
|
|
|
|
mount ${LOOP}p1 $DEST/cache/mount/
|
2016-03-05 15:54:44 +03:00
|
|
|
echo "/dev/mmcblk0p1 / ${parttype[$ROOTFS_TYPE]} defaults,noatime,nodiratime${mountopts[$ROOTFS_TYPE]} 0 1" >> $DEST/cache/sdcard/etc/fstab
|
2015-12-30 14:31:33 +03:00
|
|
|
else
|
2016-03-05 15:54:44 +03:00
|
|
|
if [[ $ROOTFS_TYPE != nfs ]]; then
|
|
|
|
mount ${LOOP}p2 $DEST/cache/mount/
|
|
|
|
echo "/dev/mmcblk0p2 / ${parttype[$ROOTFS_TYPE]} defaults,noatime,nodiratime${mountopts[$ROOTFS_TYPE]} 0 1" >> $DEST/cache/sdcard/etc/fstab
|
|
|
|
fi
|
2015-12-30 14:31:33 +03:00
|
|
|
# create /boot on rootfs after it is mounted
|
|
|
|
mkdir -p $DEST/cache/mount/boot/
|
|
|
|
mount ${LOOP}p1 $DEST/cache/mount/boot/
|
2016-03-05 15:54:44 +03:00
|
|
|
echo "/dev/mmcblk0p1 /boot ${parttype[$bootfs]} defaults${mountopts[$bootfs]} 0 2" >> $DEST/cache/sdcard/etc/fstab
|
2015-12-30 14:31:33 +03:00
|
|
|
fi
|
2016-03-05 15:54:44 +03:00
|
|
|
echo "tmpfs /tmp tmpfs defaults,rw,nosuid 0 0" >> $DEST/cache/sdcard/etc/fstab
|
|
|
|
|
|
|
|
# stage: create boot script
|
|
|
|
if [[ $ROOTFS_TYPE == nfs ]]; then
|
|
|
|
# copy script provided by user if exists
|
|
|
|
if [[ -f $SRC/userpatches/nfs-boot.cmd ]]; then
|
|
|
|
display_alert "Using custom NFS boot script" "userpatches/nfs-boot.cmd" "info"
|
|
|
|
cp $SRC/userpatches/nfs-boot.cmd $DEST/cache/sdcard/boot/boot.cmd
|
|
|
|
else
|
|
|
|
cp $SRC/lib/scripts/nfs-boot.cmd.template $DEST/cache/sdcard/boot/boot.cmd
|
|
|
|
fi
|
|
|
|
elif [[ $BOOTSIZE != 0 ]]; then
|
|
|
|
sed -i 's/mmcblk0p1/mmcblk0p2/' $DEST/cache/sdcard/boot/boot.cmd
|
|
|
|
sed -i "s/rootfstype=ext4/rootfstype=$ROOTFS_TYPE/" $DEST/cache/sdcard/boot/boot.cmd
|
|
|
|
fi
|
|
|
|
mkimage -C none -A arm -T script -d $DEST/cache/sdcard/boot/boot.cmd $DEST/cache/sdcard/boot/boot.scr > /dev/null 2>&1
|
2016-01-04 18:20:53 +03: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
|
|
|
|
VER=${VER/-$LINUXFAMILY/}
|
|
|
|
VERSION=$VERSION" "$VER
|
|
|
|
VERSION=${VERSION// /_}
|
|
|
|
VERSION=${VERSION//$BRANCH/}
|
|
|
|
VERSION=${VERSION//__/_}
|
|
|
|
[[ $BUILD_DESKTOP == yes ]] && VERSION=${VERSION}_desktop
|
|
|
|
[[ $ROOTFS_TYPE == nfs ]] && VERSION=${VERSION}_nfsboot
|
|
|
|
|
|
|
|
if [[ $ROOTFS_TYPE != nfs ]]; then
|
|
|
|
display_alert "Copying files to image" "tmprootfs.raw" "info"
|
|
|
|
eval 'rsync -aHWh --exclude="/boot/*" --exclude="/dev/*" --exclude="/proc/*" --exclude="/run/*" --exclude="/tmp/*" \
|
|
|
|
--exclude="/sys/*" --info=progress2,stats1 $DEST/cache/sdcard/ $DEST/cache/mount/'
|
|
|
|
else
|
|
|
|
display_alert "Creating rootfs archive" "rootfs.tgz" "info"
|
|
|
|
tar cp --directory=$DEST/cache/sdcard/ --exclude='./boot/*' --exclude='./dev/*' --exclude='./proc/*' --exclude='./run/*' --exclude='./tmp/*' \
|
|
|
|
--exclude='./sys/*' . | \
|
|
|
|
pv -p -b -r -s $(du -sb $DEST/cache/sdcard/ | cut -f1) -N "rootfs.tgz" | pigz > $DEST/images/$VERSION-rootfs.tgz
|
|
|
|
fi
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
# stage: rsync /boot
|
2016-03-05 15:54:44 +03:00
|
|
|
display_alert "Copying files to /boot partition" "tmprootfs.raw" "info"
|
|
|
|
if [[ $(findmnt --target $DEST/cache/mount/boot -o FSTYPE -n) == vfat ]]; then
|
|
|
|
# fat32
|
2016-03-08 16:55:14 +03:00
|
|
|
rsync -rLtWh --info=progress2,stats1 $DEST/cache/sdcard/boot $DEST/cache/mount
|
2016-03-05 15:54:44 +03:00
|
|
|
else
|
|
|
|
# ext4
|
2016-03-08 16:55:14 +03:00
|
|
|
rsync -aHWh --info=progress2,stats1 $DEST/cache/sdcard/boot $DEST/cache/mount
|
2016-01-09 21:31:33 +03:00
|
|
|
fi
|
2015-12-30 14:31:33 +03:00
|
|
|
|
|
|
|
# DEBUG: print free space
|
2016-03-10 16:24:28 +03:00
|
|
|
echo
|
|
|
|
echo "Free space:"
|
2015-12-30 14:31:33 +03:00
|
|
|
df -h | grep "$DEST/cache/" | tee -a $DEST/debug/debootstrap.log
|
|
|
|
|
|
|
|
# stage: write u-boot
|
|
|
|
write_uboot $LOOP
|
|
|
|
|
2016-02-04 19:47:40 +03:00
|
|
|
# stage: copy armbian.txt TODO: Copy only if creating zip file?
|
|
|
|
cp $DEST/cache/sdcard/etc/armbian.txt $DEST/cache/
|
|
|
|
|
2015-12-30 14:31:33 +03:00
|
|
|
# unmount /boot first, rootfs second, image file last
|
|
|
|
if [[ $BOOTSIZE != 0 ]]; then umount -l $DEST/cache/mount/boot; fi
|
2016-03-09 12:18:40 +03:00
|
|
|
if [[ $ROOTFS_TYPE != nfs ]]; then umount -l $DEST/cache/mount; fi
|
2015-12-30 14:31:33 +03:00
|
|
|
losetup -d $LOOP
|
|
|
|
|
|
|
|
mv $DEST/cache/tmprootfs.raw $DEST/cache/$VERSION.raw
|
|
|
|
cd $DEST/cache/
|
|
|
|
|
|
|
|
# stage: compressing or copying image file
|
2016-01-24 16:09:36 +01:00
|
|
|
if [[ -n $FIXED_IMAGE_SIZE || $COMPRESS_OUTPUTIMAGE == no ]]; then
|
2015-12-30 14:31:33 +03:00
|
|
|
display_alert "Copying image file" "$VERSION.raw" "info"
|
|
|
|
mv -f $DEST/cache/$VERSION.raw $DEST/images/$VERSION.raw
|
|
|
|
display_alert "Done building" "$DEST/images/$VERSION.raw" "info"
|
|
|
|
else
|
|
|
|
display_alert "Signing and compressing" "$VERSION.zip" "info"
|
|
|
|
# stage: sign with PGP
|
2016-03-05 15:54:44 +03:00
|
|
|
if [[ -n $GPG_PASS ]]; then
|
2015-12-30 14:31:33 +03:00
|
|
|
echo $GPG_PASS | gpg --passphrase-fd 0 --armor --detach-sign --batch --yes $VERSION.raw
|
|
|
|
echo $GPG_PASS | gpg --passphrase-fd 0 --armor --detach-sign --batch --yes armbian.txt
|
|
|
|
fi
|
|
|
|
zip -FSq $DEST/images/$VERSION.zip $VERSION.raw* armbian.txt
|
|
|
|
rm -f $VERSION.raw *.asc armbian.txt
|
|
|
|
display_alert "Done building" "$DEST/images/$VERSION.zip" "info"
|
|
|
|
fi
|
2016-03-09 12:18:40 +03:00
|
|
|
} #############################################################################
|
2015-12-30 14:31:33 +03:00
|
|
|
|
2016-03-09 12:18:40 +03:00
|
|
|
# mount_chroot
|
|
|
|
#
|
|
|
|
# helper to reduce code duplication
|
|
|
|
#
|
|
|
|
mount_chroot()
|
|
|
|
{
|
|
|
|
mount -t proc chproc $DEST/cache/sdcard/proc
|
|
|
|
mount -t sysfs chsys $DEST/cache/sdcard/sys
|
|
|
|
mount -t devtmpfs chdev $DEST/cache/sdcard/dev || mount --bind /dev $DEST/cache/sdcard/dev
|
|
|
|
mount -t devpts chpts $DEST/cache/sdcard/dev/pts
|
|
|
|
} #############################################################################
|
|
|
|
|
|
|
|
# umount_chroot
|
|
|
|
#
|
|
|
|
# helper to reduce code duplication
|
|
|
|
#
|
|
|
|
umount_chroot()
|
|
|
|
{
|
2016-03-10 16:24:28 +03:00
|
|
|
umount -l $DEST/cache/sdcard/dev/pts >/dev/null 2>&1
|
|
|
|
umount -l $DEST/cache/sdcard/dev >/dev/null 2>&1
|
|
|
|
umount -l $DEST/cache/sdcard/proc >/dev/null 2>&1
|
|
|
|
umount -l $DEST/cache/sdcard/sys >/dev/null 2>&1
|
2015-12-30 14:31:33 +03:00
|
|
|
} #############################################################################
|
|
|
|
|
|
|
|
# unmount_on_exit
|
|
|
|
#
|
|
|
|
unmount_on_exit()
|
|
|
|
{
|
2016-03-09 12:18:40 +03:00
|
|
|
umount_chroot
|
|
|
|
umount -l $DEST/cache/sdcard >/dev/null 2>&1
|
2016-03-05 15:54:44 +03:00
|
|
|
umount -l $DEST/cache/mount/boot >/dev/null 2>&1
|
2016-03-09 12:18:40 +03:00
|
|
|
umount -l $DEST/cache/mount >/dev/null 2>&1
|
2016-03-05 15:54:44 +03:00
|
|
|
losetup -d $LOOP >/dev/null 2>&1
|
2016-03-07 19:34:56 +03:00
|
|
|
rm -rf $DEST/cache/sdcard
|
2016-03-05 21:26:01 +03:00
|
|
|
exit_with_error "debootstrap-ng was interrupted"
|
|
|
|
} #############################################################################
|