mkinitramfs: Simplify pipefail implementation

Echoing shell code that sets the variables `ec1` and `ec2` when
evaluated is way too complicated for implementing pipefail (which is not
available in dash). Just write the exit code to FD 3 instead.

Signed-off-by: Benjamin Drung <benjamin.drung@canonical.com>
This commit is contained in:
Benjamin Drung 2024-05-19 16:36:31 +02:00
parent a345082d86
commit e11494312f

View File

@ -466,7 +466,6 @@ fi
[ "${verbose}" = y ] && echo "Building cpio ${outfile} initramfs"
(
# preserve permissions if root builds the image, see #633582
[ "$(id -ru)" != 0 ] && cpio_owner_root="-R 0:0"
@ -480,34 +479,20 @@ if [ -n "${SOURCE_DATE_EPOCH}" ]; then
cpio_reproducible="--reproducible"
fi
# work around lack of "set -o pipefail" for the following pipe:
# cd "${DESTDIR}" && find . | LC_ALL=C sort | cpio --quiet $cpio_owner_root $cpio_reproducible -o -H newc >>"${outfile}" || exit 1
ec1=1
ec2=1
exec 3>&1
eval "$(
# http://cfaj.freeshell.org/shell/cus-faq-2.html
exec 4>&1 >&3 3>&-
# work around lack of "set -o pipefail" for pipes.
# Write exit code to FD 3 in case of a failure to pass it through the pipes.
{
cd "${DESTDIR}"
{
find . 4>&-; echo "ec1=$?;" >&4
find . || { echo "E: mkinitramfs failure find $?" >&2; echo 1 >&3; exit; }
} | {
LC_ALL=C sort
LC_ALL=C sort || { echo "E: mkinitramfs failure sort $?" >&2; echo 1 >&3; exit; }
} | {
# shellcheck disable=SC2086
cpio --quiet $cpio_owner_root $cpio_reproducible -o -H newc 4>&- >"${__TMPMAINCPIO}"
echo "ec2=$?;" >&4
cpio --quiet $cpio_owner_root $cpio_reproducible -o -H newc >"${__TMPMAINCPIO}" ||
{ echo "E: mkinitramfs failure cpio $?" >&2; echo 1 >&3; exit; }
}
)"
if [ "$ec1" -ne 0 ]; then
echo "E: mkinitramfs failure find $ec1 cpio $ec2" >&2
exit "$ec1"
fi
if [ "$ec2" -ne 0 ]; then
echo "E: mkinitramfs failure cpio $ec2" >&2
exit "$ec2"
fi
) || exit 1
} 3>&1 | { read -r exit_code; exit "${exit_code:-0}"; } || exit $?
{
if [ -s "${__TMPEARLYCPIO}" ]; then