cli: flash: introduce flash CLI command; introduce hook post_build_image_write

This commit is contained in:
Ricardo Pardini
2023-04-14 22:33:20 +02:00
parent d59271f903
commit 7ea8e040d0
4 changed files with 77 additions and 2 deletions

View File

@@ -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}"
}

View File

@@ -30,6 +30,7 @@ function armbian_register_commands() {
["build"]="standard_build" # implemented in cli_standard_build_pre_run and cli_standard_build_run ["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 ["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. # 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 ["oras-upload"]="oras" # implemented in cli_oras_pre_run and cli_oras_run; up/down/info are the same, see vars below

View File

@@ -137,8 +137,8 @@ function create_image_from_sdcard_rootfs() {
if [[ -f "${DESTIMG}/${version}.img" ]]; then if [[ -f "${DESTIMG}/${version}.img" ]]; then
display_alert "Done building" "${version}.img" "info" display_alert "Done building" "${version}.img" "info"
fingerprint_image "${DESTIMG}/${version}.img.txt" "${version}" 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 fi
declare compression_type # set by image_compress_and_checksum declare compression_type # set by image_compress_and_checksum
@@ -153,6 +153,25 @@ function create_image_from_sdcard_rootfs() {
return 0 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() { function move_images_to_final_destination() {
# validate that source_dir and destination_dir exist # validate that source_dir and destination_dir exist
[[ ! -d "${source_dir}" ]] && return 1 [[ ! -d "${source_dir}" ]] && return 1

View File

@@ -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 # shellcheck source=lib/functions/cli/cli-docker.sh
source "${SRC}"/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. # no errors tolerated. invoked before each sourced file to make sure.
#set -o pipefail # trace ERR through pipes - will be enabled "soon" #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 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 # shellcheck source=lib/functions/rootfs/trap-rootfs.sh
source "${SRC}"/lib/functions/rootfs/trap-rootfs.sh source "${SRC}"/lib/functions/rootfs/trap-rootfs.sh
# no errors tolerated. one last time for the win! # no errors tolerated. one last time for the win!
#set -o pipefail # trace ERR through pipes - will be enabled "soon" #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 nounset ## set -u : exit the script if you try to use an uninitialised variable - one day will be enabled