armbian-build/lib/functions/image/write-device.sh
Igor Velkov ed371f5f5a
(#9400 P3a) Replace useless cat with input redirection (#9404)
* (#9400 P3a) trap-logging: replace useless cat with direct file arguments

- gzip < file → gzip -c file (direct argument, -c for stdout)
- cat file | ansi2txt → ansi2txt < file (ansi2txt has no file argument)
- Remove now-unnecessary shellcheck disable=SC2002 comments

* (#9400 P3a) logging: replace useless cat with direct file argument

- cat file | grep | sed → grep file | sed (grep accepts filename directly)
- Remove now-unnecessary shellcheck disable=SC2002 comment

* (#9400 P3a) initrd: replace useless cat with direct file argument

- cat file | md5sum → md5sum file (md5sum accepts filename directly)

* (#9400 P3a) write-device: replace useless cat with direct file argument

- cat file | awk → awk file (awk accepts filename directly)
- Remove now-unnecessary shellcheck disable=SC2002 comment

* (#9400 P3a) export-logs: replace useless cat with direct file argument

- cat file | sed → sed file (sed accepts filename directly)
- Remove now-unnecessary shellcheck disable=SC2002 comment

* (#9400 P3a) python-tools: replace cat subshell with $(<file) syntax

- $(cat file) → $(< file) (bash builtin, no subprocess)

* (#9400 P3a) artifact-armbian-base-files: replace useless cat with direct file argument

- cat file | grep → grep file (grep accepts filename directly)
2026-02-18 09:28:14 +01:00

55 lines
1.8 KiB
Bash

#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2013-2026 Igor Pecovnik, igor@armbian.com
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
# @TODO: make usable as a separate tool as well
function write_image_to_device() {
local image_file="${1}"
local device="${2}"
if [[ $(lsblk "${device}" 2> /dev/null) && -f "${image_file}" ]]; then
if [[ "${SKIP_VERIFY}" != "yes" ]]; then
# create sha256sum if it does not exist. we need it for comparison, later.
local if_sha=""
if [[ -f "${image_file}.img.sha" ]]; then
if_sha=$(awk '{print $1}' "${image_file}.sha")
else
if_sha=$(sha256sum -b "${image_file}" | awk '{print $1}')
fi
fi
display_alert "Writing image" "${device} ${if_sha}" "info"
# write to SD card
pv -p -b -r -c -N "$(logging_echo_prefix_for_pv "write_device") dd" "${image_file}" | dd "of=${device}" bs=1M iflag=fullblock oflag=direct status=none
call_extension_method "post_write_sdcard" <<- 'POST_WRITE_SDCARD'
*run after writing img to sdcard*
After the image is written to `${device}`, but before verifying it.
You can still set SKIP_VERIFY=yes to skip verification.
POST_WRITE_SDCARD
if [[ "${SKIP_VERIFY}" != "yes" ]]; then
# read and compare
display_alert "Verifying. Please wait!"
local of_sha=""
of_sha=$(dd "if=${device}" "count=$(du -b "${image_file}" | cut -f1)" status=none iflag=count_bytes oflag=direct | sha256sum | awk '{print $1}')
if [[ "$if_sha" == "$of_sha" ]]; then
display_alert "Writing verified" "${image_file}" "info"
else
display_alert "Writing failed" "${image_file}" "err"
fi
fi
elif armbian_is_running_in_container; then
if [[ -n ${device} ]]; then
# display warning when we want to write sd card under Docker
display_alert "Can't write to ${device}" "Under Docker" "wrn"
fi
fi
}