orangepi-build-for-h618/scripts/image-helpers.sh

332 lines
9.2 KiB
Bash
Raw Permalink Normal View History

2020-10-30 01:50:39 +00:00
#!/bin/bash
#
2022-03-07 08:26:01 +00:00
# Copyright (c) 2013-2021 Igor Pecovnik, igor.pecovnik@gma**.com
2020-10-30 01:50:39 +00:00
#
# 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.
# Functions:
2022-03-07 08:26:01 +00:00
2020-10-30 01:50:39 +00:00
# mount_chroot
# umount_chroot
# unmount_on_exit
# check_loop_device
# install_external_applications
# write_uboot
2022-03-07 08:26:01 +00:00
# copy_all_packages_files_for
2020-10-30 01:50:39 +00:00
# customize_image
# install_deb_chroot
2022-03-07 08:26:01 +00:00
# run_on_sdcard
2020-10-30 01:50:39 +00:00
# mount_chroot <target>
#
# helper to reduce code duplication
#
mount_chroot()
{
2022-03-07 08:26:01 +00:00
2020-10-30 01:50:39 +00:00
local target=$1
mount -t proc chproc "${target}"/proc
mount -t sysfs chsys "${target}"/sys
mount -t devtmpfs chdev "${target}"/dev || mount --bind /dev "${target}"/dev
mount -t devpts chpts "${target}"/dev/pts
2022-03-07 08:26:01 +00:00
}
2020-10-30 01:50:39 +00:00
# umount_chroot <target>
#
# helper to reduce code duplication
#
umount_chroot()
{
2022-03-07 08:26:01 +00:00
2020-10-30 01:50:39 +00:00
local target=$1
display_alert "Unmounting" "$target" "info"
while grep -Eq "${target}.*(dev|proc|sys)" /proc/mounts
do
umount -l --recursive "${target}"/dev >/dev/null 2>&1
umount -l "${target}"/proc >/dev/null 2>&1
umount -l "${target}"/sys >/dev/null 2>&1
sleep 5
done
2022-03-07 08:26:01 +00:00
}
2020-10-30 01:50:39 +00:00
# unmount_on_exit
#
unmount_on_exit()
{
2022-03-07 08:26:01 +00:00
2020-10-30 01:50:39 +00:00
trap - INT TERM EXIT
2022-03-07 08:26:01 +00:00
local stacktrace="$(get_extension_hook_stracktrace "${BASH_SOURCE[*]}" "${BASH_LINENO[*]}")"
display_alert "unmount_on_exit() called!" "$stacktrace" "err"
if [[ "${ERROR_DEBUG_SHELL}" == "yes" ]]; then
ERROR_DEBUG_SHELL=no # dont do it twice
display_alert "MOUNT" "${MOUNT}" "err"
display_alert "SDCARD" "${SDCARD}" "err"
display_alert "ERROR_DEBUG_SHELL=yes, starting a shell." "ERROR_DEBUG_SHELL" "err"
bash < /dev/tty || true
fi
2020-10-30 01:50:39 +00:00
umount_chroot "${SDCARD}/"
umount -l "${SDCARD}"/tmp >/dev/null 2>&1
umount -l "${SDCARD}" >/dev/null 2>&1
umount -l "${MOUNT}"/boot >/dev/null 2>&1
umount -l "${MOUNT}" >/dev/null 2>&1
[[ $CRYPTROOT_ENABLE == yes ]] && cryptsetup luksClose "${ROOT_MAPPER}"
losetup -d "${LOOP}" >/dev/null 2>&1
rm -rf --one-file-system "${SDCARD}"
2022-03-07 08:26:01 +00:00
exit_with_error "debootstrap-ng was interrupted" || true # don't trigger again
}
2020-10-30 01:50:39 +00:00
# check_loop_device <device_node>
#
check_loop_device()
{
2022-03-07 08:26:01 +00:00
2020-10-30 01:50:39 +00:00
local device=$1
if [[ ! -b $device ]]; then
if [[ $CONTAINER_COMPAT == yes && -b /tmp/$device ]]; then
display_alert "Creating device node" "$device"
mknod -m0660 "${device}" b "0x$(stat -c '%t' "/tmp/$device")" "0x$(stat -c '%T' "/tmp/$device")"
else
exit_with_error "Device node $device does not exist"
fi
fi
2022-03-07 08:26:01 +00:00
}
2020-10-30 01:50:39 +00:00
# write_uboot <loopdev>
#
write_uboot()
{
2022-03-07 08:26:01 +00:00
local loop=$1 revision
2020-10-30 01:50:39 +00:00
display_alert "Writing U-boot bootloader" "$loop" "info"
2022-03-07 08:26:01 +00:00
TEMP_DIR=$(mktemp -d || exit 1)
chmod 700 ${TEMP_DIR}
revision=${REVISION}
if [[ -n $UPSTREM_VER ]]; then
revision=${UPSTREM_VER}
dpkg -x "${DEB_STORAGE}/u-boot/linux-u-boot-${BOARD}-${BRANCH}_${revision}_${ARCH}.deb" ${TEMP_DIR}/
else
dpkg -x "${DEB_STORAGE}/u-boot/${CHOSEN_UBOOT}_${revision}_${ARCH}.deb" ${TEMP_DIR}/
fi
# source platform install to read $DIR
source ${TEMP_DIR}/usr/lib/u-boot/platform_install.sh
write_uboot_platform "${TEMP_DIR}${DIR}" "$loop"
2020-10-30 01:50:39 +00:00
[[ $? -ne 0 ]] && exit_with_error "U-boot bootloader failed to install" "@host"
2022-03-07 08:26:01 +00:00
rm -rf ${TEMP_DIR}
}
# copy_all_packages_files_for <folder> to package
#
copy_all_packages_files_for()
{
local package_name="${1}"
for package_src_dir in ${PACKAGES_SEARCH_ROOT_ABSOLUTE_DIRS};
do
local package_dirpath="${package_src_dir}/${package_name}"
if [ -d "${package_dirpath}" ];
then
cp -r "${package_dirpath}/"* "${destination}/" 2> /dev/null
display_alert "Adding files from" "${package_dirpath}"
fi
done
}
2020-10-30 01:50:39 +00:00
customize_image()
{
2022-03-07 08:26:01 +00:00
2020-10-30 01:50:39 +00:00
# for users that need to prepare files at host
[[ -f $USERPATCHES_PATH/customize-image-host.sh ]] && source "$USERPATCHES_PATH"/customize-image-host.sh
2022-03-07 08:26:01 +00:00
call_extension_method "pre_customize_image" "image_tweaks_pre_customize" << 'PRE_CUSTOMIZE_IMAGE'
*run before customize-image.sh*
This hook is called after `customize-image-host.sh` is called, but before the overlay is mounted.
It thus can be used for the same purposes as `customize-image-host.sh`.
PRE_CUSTOMIZE_IMAGE
2020-10-30 01:50:39 +00:00
cp "$USERPATCHES_PATH"/customize-image.sh "${SDCARD}"/tmp/customize-image.sh
chmod +x "${SDCARD}"/tmp/customize-image.sh
mkdir -p "${SDCARD}"/tmp/overlay
# util-linux >= 2.27 required
mount -o bind,ro "$USERPATCHES_PATH"/overlay "${SDCARD}"/tmp/overlay
display_alert "Calling image customization script" "customize-image.sh" "info"
2022-03-07 08:26:01 +00:00
chroot "${SDCARD}" /bin/bash -c "/tmp/customize-image.sh $RELEASE $LINUXFAMILY $BOARD $BUILD_DESKTOP $ARCH"
2020-10-30 01:50:39 +00:00
CUSTOMIZE_IMAGE_RC=$?
umount -i "${SDCARD}"/tmp/overlay >/dev/null 2>&1
mountpoint -q "${SDCARD}"/tmp/overlay || rm -r "${SDCARD}"/tmp/overlay
if [[ $CUSTOMIZE_IMAGE_RC != 0 ]]; then
exit_with_error "customize-image.sh exited with error (rc: $CUSTOMIZE_IMAGE_RC)"
fi
2022-03-07 08:26:01 +00:00
call_extension_method "post_customize_image" "image_tweaks_post_customize" << 'POST_CUSTOMIZE_IMAGE'
*post customize-image.sh hook*
Run after the customize-image.sh script is run, and the overlay is unmounted.
POST_CUSTOMIZE_IMAGE
}
2020-10-30 01:50:39 +00:00
install_deb_chroot()
{
2022-03-07 08:26:01 +00:00
local package=$1
local variant=$2
local transfer=$3
local name
local desc
if [[ ${variant} != remote ]]; then
name="/root/"$(basename "${package}")
[[ ! -f "${SDCARD}${name}" ]] && cp "${package}" "${SDCARD}${name}"
desc=""
2020-10-30 01:50:39 +00:00
else
2022-03-07 08:26:01 +00:00
name=$1
desc=" from repository"
2020-10-30 01:50:39 +00:00
fi
2022-03-07 08:26:01 +00:00
display_alert "Installing${desc}" "${name/\/root\//}"
2020-10-30 01:50:39 +00:00
[[ $NO_APT_CACHER != yes ]] && local apt_extra="-o Acquire::http::Proxy=\"http://${APT_PROXY_ADDR:-localhost:3142}\" -o Acquire::http::Proxy::localhost=\"DIRECT\""
2022-03-07 08:26:01 +00:00
# when building in bulk from remote, lets make sure we have up2date index
[[ $BUILD_ALL == yes && ${variant} == remote ]] && chroot "${SDCARD}" /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get $apt_extra -yqq update"
chroot "${SDCARD}" /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get -yqq $apt_extra --no-install-recommends install $name" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
[[ $? -ne 0 ]] && exit_with_error "Installation of $name failed" "${BOARD} ${RELEASE} ${BUILD_DESKTOP} ${LINUXFAMILY}"
[[ ${variant} == remote && ${transfer} == yes ]] && rsync -rq "${SDCARD}"/var/cache/apt/archives/*.deb ${DEB_STORAGE}/
}
dpkg_install_deb_chroot()
{
local package=$1
local name
local desc
name="/root/"$(basename "${package}")
[[ ! -f "${SDCARD}${name}" ]] && cp "${package}" "${SDCARD}${name}"
display_alert "Installing${desc}" "${name/\/root\//}"
# when building in bulk from remote, lets make sure we have up2date index
chroot "${SDCARD}" /bin/bash -c "dpkg -i $name" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
2023-07-14 02:10:47 +00:00
chroot "${SDCARD}" /bin/bash -c "dpkg-deb -f $name Package | xargs apt-mark hold" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
2022-03-07 08:26:01 +00:00
[[ $? -ne 0 ]] && exit_with_error "Installation of $name failed" "${BOARD} ${RELEASE} ${BUILD_DESKTOP} ${LINUXFAMILY}"
}
2023-11-09 08:23:39 +00:00
dpkg_install_debs_chroot()
{
local deb_dir=$1
local unsatisfied_dependencies=()
local package_names=()
local package_dependencies=()
[ ! -d "$deb_dir" ] && return
2024-01-16 08:03:47 +00:00
deb_packages=($(find "${deb_dir}/" -mindepth 1 -maxdepth 1 -type f -name "*.deb"))
2023-11-09 08:23:39 +00:00
find_in_array() {
local target="$1"
local element=""
shift
for element in "$@"; do
[[ "$element" == "$target" ]] && return 0
done
return 1
}
for package in "${deb_packages[@]}"; do
package_names+=($(dpkg-deb -f "$package" Package))
dep_str=$(dpkg-deb -I "${package}" | grep 'Depends' | sed 's/.*: //' | sed 's/ //g' | sed 's/([^)]*)//g')
IFS=',' read -ra dep_array <<< "$dep_str"
if [[ ! ${#dep_array[@]} -eq 0 ]]; then
#dep_array[-1]="${dep_array[-1]} "
for element in "${dep_array[@]}"; do
if [[ $element == *"|"* ]]; then
#dep_array=("${dep_array[@]/$element}")
:
else
if ! find_in_array "$element" "${package_dependencies[@]}"; then
package_dependencies+=("${element}")
fi
fi
done
fi
done
for dependency in "${package_dependencies[@]}"; do
if ! chroot "${SDCARD}" /bin/bash -c "dpkg-query -W --showformat='\${Status}' ${dependency} \
| grep -q 'ok installed'" &>/dev/null; then
all=("${package_names[@]}" "${unsatisfied_dependencies[@]}")
if ! find_in_array "$dependency" "${all[@]}"; then
unsatisfied_dependencies+=("$dependency")
fi
fi
done
2022-03-07 08:26:01 +00:00
2023-11-09 08:23:39 +00:00
if [[ ! -z "${unsatisfied_dependencies[*]}" ]]; then
display_alert "Installing Dependencies" "${unsatisfied_dependencies[*]}"
2024-01-05 03:58:21 +00:00
chroot $SDCARD /bin/bash -c "apt-get -y -qq install ${unsatisfied_dependencies[*]}" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
2023-11-09 08:23:39 +00:00
fi
local names=""
for package in "${deb_packages[@]}"; do
name="/root/"$(basename "${package}")
names+=($name)
[[ ! -f "${SDCARD}${name}" ]] && cp "${package}" "${SDCARD}${name}"
done
if [[ ! -z "${names[*]}" ]]; then
display_alert "Installing" "$(basename $deb_dir)"
# when building in bulk from remote, lets make sure we have up2date index
chroot "${SDCARD}" /bin/bash -c "dpkg -i ${names[*]} " >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
[[ $? -ne 0 ]] && exit_with_error "Installation of $(basename $deb_dir) failed" "${BOARD} ${RELEASE} ${BUILD_DESKTOP} ${LINUXFAMILY}"
chroot "${SDCARD}" /bin/bash -c "apt-mark hold ${package_names[*]}" >> "${DEST}"/${LOG_SUBPATH}/install.log 2>&1
fi
}
2022-03-07 08:26:01 +00:00
run_on_sdcard()
{
# Lack of quotes allows for redirections and pipes easily.
chroot "${SDCARD}" /bin/bash -c "${@}" >> "${DEST}"/${LOG_SUBPATH}/install.log
2020-10-30 01:50:39 +00:00
}