From 7ea8e040d05f4d602e3bc81a9c257ed0104a5360 Mon Sep 17 00:00:00 2001 From: Ricardo Pardini Date: Fri, 14 Apr 2023 22:33:20 +0200 Subject: [PATCH] cli: `flash`: introduce `flash` CLI command; introduce hook `post_build_image_write` --- lib/functions/cli/cli-flash.sh | 45 ++++++++++++++++++++++++++ lib/functions/cli/commands.sh | 1 + lib/functions/image/rootfs-to-image.sh | 23 +++++++++++-- lib/library-functions.sh | 10 ++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 lib/functions/cli/cli-flash.sh diff --git a/lib/functions/cli/cli-flash.sh b/lib/functions/cli/cli-flash.sh new file mode 100644 index 000000000..79af87f8d --- /dev/null +++ b/lib/functions/cli/cli-flash.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com +# +# This file is a part of the Armbian Build Framework +# https://github.com/armbian/build/ + +function cli_flash_pre_run() { + display_alert "cli_distccd_pre_run" "func cli_distccd_run :: ${ARMBIAN_COMMAND}" "warn" + + # "gimme root on a Linux machine" + cli_standard_relaunch_docker_or_sudo +} + +function cli_flash_run() { + if [[ "x${BOARD}x" != "xx" ]]; then + use_board="yes" prep_conf_main_minimal_ni < /dev/null # no stdin for this, so it bombs if tries to be interactive. + else + use_board="no" prep_conf_main_minimal_ni < /dev/null # no stdin for this, so it bombs if tries to be interactive. + fi + + # the full build. It has its own logging sections. + do_with_default_build cli_flash +} + +function cli_flash() { + declare image_file="${IMAGE:-""}" + # If not set, find the latest .img file in ${SRC}/output/images/ + if [[ -z "${image_file}" ]]; then + # shellcheck disable=SC2012 + image_file="$(ls -1t "${SRC}/output/images"/*"${BOARD^}_${RELEASE}_${BRANCH}"*.img | head -1)" + display_alert "cli_flash" "No image file specified. Using latest built image file found: ${image_file}" "info" + fi + if [[ ! -f "${image_file}" ]]; then + exit_with_error "No image file to flash." + fi + declare image_file_basename + image_file_basename="$(basename "${image_file}")" + display_alert "cli_flash" "Flashing image file: ${image_file_basename}" "info" + countdown_and_continue_if_not_aborted 3 + + write_image_to_device_and_run_hooks "${image_file}" +} diff --git a/lib/functions/cli/commands.sh b/lib/functions/cli/commands.sh index 278993c9b..630c9fa95 100644 --- a/lib/functions/cli/commands.sh +++ b/lib/functions/cli/commands.sh @@ -30,6 +30,7 @@ function armbian_register_commands() { ["build"]="standard_build" # implemented in cli_standard_build_pre_run and cli_standard_build_run ["distccd"]="distccd" # implemented in cli_distccd_pre_run and cli_distccd_run + ["flash"]="flash" # implemented in cli_flash_pre_run and cli_flash_run # external tooling, made easy. ["oras-upload"]="oras" # implemented in cli_oras_pre_run and cli_oras_run; up/down/info are the same, see vars below diff --git a/lib/functions/image/rootfs-to-image.sh b/lib/functions/image/rootfs-to-image.sh index d719c8f78..825bece68 100644 --- a/lib/functions/image/rootfs-to-image.sh +++ b/lib/functions/image/rootfs-to-image.sh @@ -137,8 +137,8 @@ function create_image_from_sdcard_rootfs() { if [[ -f "${DESTIMG}/${version}.img" ]]; then display_alert "Done building" "${version}.img" "info" fingerprint_image "${DESTIMG}/${version}.img.txt" "${version}" - # write image to SD card - write_image_to_device "${DESTIMG}/${version}.img" "${CARD_DEVICE}" + + write_image_to_device_and_run_hooks "${DESTIMG}/${version}.img" fi declare compression_type # set by image_compress_and_checksum @@ -153,6 +153,25 @@ function create_image_from_sdcard_rootfs() { return 0 } +function write_image_to_device_and_run_hooks() { + if [[ ! -f "${1}" ]]; then + exit_with_error "Image file not found '${1}'" + fi + declare built_image_file="${1}" + + # write image to SD card + write_image_to_device "${built_image_file}" "${CARD_DEVICE}" + + # Hook: post_build_image_write + call_extension_method "post_build_image_write" <<- 'POST_BUILD_IMAGE_WRITE' + *custom post build hook* + Called after the final .img file is ready, and possibly written to an SD card. + The full path to the image is available in `${built_image_file}`. + POST_BUILD_IMAGE_WRITE + + unset built_image_file +} + function move_images_to_final_destination() { # validate that source_dir and destination_dir exist [[ ! -d "${source_dir}" ]] && return 1 diff --git a/lib/library-functions.sh b/lib/library-functions.sh index d13e2fe63..67ae9bb39 100644 --- a/lib/library-functions.sh +++ b/lib/library-functions.sh @@ -199,6 +199,15 @@ set -o errexit ## set -e : exit the script if any statement returns a non-true # shellcheck source=lib/functions/cli/cli-docker.sh source "${SRC}"/lib/functions/cli/cli-docker.sh +# no errors tolerated. invoked before each sourced file to make sure. +#set -o pipefail # trace ERR through pipes - will be enabled "soon" +#set -o nounset ## set -u : exit the script if you try to use an uninitialised variable - one day will be enabled +set -o errtrace # trace ERR through - enabled +set -o errexit ## set -e : exit the script if any statement returns a non-true return value - enabled +### lib/functions/cli/cli-flash.sh +# shellcheck source=lib/functions/cli/cli-flash.sh +source "${SRC}"/lib/functions/cli/cli-flash.sh + # no errors tolerated. invoked before each sourced file to make sure. #set -o pipefail # trace ERR through pipes - will be enabled "soon" #set -o nounset ## set -u : exit the script if you try to use an uninitialised variable - one day will be enabled @@ -1144,6 +1153,7 @@ set -o errexit ## set -e : exit the script if any statement returns a non-true # shellcheck source=lib/functions/rootfs/trap-rootfs.sh source "${SRC}"/lib/functions/rootfs/trap-rootfs.sh + # no errors tolerated. one last time for the win! #set -o pipefail # trace ERR through pipes - will be enabled "soon" #set -o nounset ## set -u : exit the script if you try to use an uninitialised variable - one day will be enabled