Re: [PATCH RFC] gen_init_cpio: Do fsync() only on regular files

From: David Disseldorp

Date: Mon Oct 06 2025 - 21:17:38 EST


Thanks for reporting this regression, Dmitry...

On Tue, 07 Oct 2025 00:55:03 +0100, Dmitry Safonov via B4 Relay wrote:

> From: Dmitry Safonov <dima@xxxxxxxxxx>
>
> Here at Arista gen_init_cpio is used in testing in order to create
> an initramfs for specific tests. Most notably, there is a test that does
> essentially a fork-bomb in kdump/panic kernel, replacing build-time
> generated init script: instead of doing makedumpfile, it does call
> shell tests.
>
> In comparison to usr/Makefile, which creates an intermediate .cpio file,
> the Makefile that generates initrd for tests is a one-liner:
> > file lib/kdump ${src_dir}/oom-crashkernel 0644 0 0 | usr/gen_init_cpio /dev/stdin | xz -z -c -e -Ccrc32 > ${target_dir}/oom-crashkernel.initrd
>
> As fsync() on a pipe fd returns -EINVAL, that stopped working.
> Check that outfd is a regular file descriptor before calling fsync().
>
> Sending this as RFC as these are local tests, rather than upstream ones,
> unfortunately. Yet, the fix is trivial and increases correctness of
> gen_init_cpio (and maybe saves some time for another person debugging
> it). A workaround to use temporary cpio file is also trivial, so not
> insisting on merging.

The code change looks fine, but the commit message is a bit verbose IMO.
Please drop the first and last paragraphs. The reproducer could also be
simplified to e.g.
echo | usr/gen_init_cpio /dev/stdin > /dev/null

With that:
Reviewed-by: David Disseldorp <ddiss@xxxxxxx>

> Fixes: ae18b94099b0 ("gen_init_cpio: support -o <output_file> parameter")
> Cc: David Disseldorp <ddiss@xxxxxxx>
> Cc: Nicolas Schier <nsc@xxxxxxxxxx>
> Signed-off-by: Dmitry Safonov <dima@xxxxxxxxxx>
> ---
> usr/gen_init_cpio.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c
> index 75e9561ba31392e12536e76a918245a8ea07f9b8..845e2d92f0e56b02ba5fc12ecd243ce99c53f552 100644
> --- a/usr/gen_init_cpio.c
> +++ b/usr/gen_init_cpio.c
> @@ -6,6 +6,7 @@
> #include <stdbool.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> +#include <sys/socket.h>
> #include <string.h>
> #include <unistd.h>
> #include <time.h>
> @@ -112,6 +113,9 @@ static int cpio_trailer(void)
> push_pad(padlen(offset, 512)) < 0)
> return -1;
>
> + if (!isfdtype(outfd, S_IFREG))
> + return 0;
> +
> return fsync(outfd);
> }

Another option would be to catch the EINVAL error, e.g.

--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -112,7 +112,10 @@ static int cpio_trailer(void)
push_pad(padlen(offset, 512)) < 0)
return -1;

- return fsync(outfd);
+ if (fsync(outfd) < 0 && errno != EINVAL)
+ return -1;
+
+ return 0;
}

It may be a little portable than isfdtype(), but I don't feel strongly
either way.

Thanks, David