Skip to content

Commit ae18b94

Browse files
ddissnathanchance
authored andcommitted
gen_init_cpio: support -o <output_file> parameter
This is another preparatory change to allow for reflink-optimized cpio archives with file data written / cloned via copy_file_range(). The output file is truncated prior to write, so that it maps to usr/gen_initramfs.sh usage. It may make sense to offer an append option in future, for easier archive concatenation. Signed-off-by: David Disseldorp <ddiss@suse.de> Reviewed-by: Nicolas Schier <nsc@kernel.org> Link: https://lore.kernel.org/r/20250819032607.28727-3-ddiss@suse.de Signed-off-by: Nathan Chancellor <nathan@kernel.org>
1 parent 1400227 commit ae18b94

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

usr/gen_init_cpio.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// SPDX-License-Identifier: GPL-2.0
2+
#define _GNU_SOURCE
23
#include <stdio.h>
34
#include <stdlib.h>
45
#include <stdint.h>
@@ -110,7 +111,7 @@ static int cpio_trailer(void)
110111
push_pad(padlen(offset, 512)) < 0)
111112
return -1;
112113

113-
return 0;
114+
return fsync(outfd);
114115
}
115116

116117
static int cpio_mkslink(const char *name, const char *target,
@@ -532,7 +533,7 @@ static int cpio_mkfile_line(const char *line)
532533
static void usage(const char *prog)
533534
{
534535
fprintf(stderr, "Usage:\n"
535-
"\t%s [-t <timestamp>] [-c] <cpio_list>\n"
536+
"\t%s [-t <timestamp>] [-c] [-o <output_file>] <cpio_list>\n"
536537
"\n"
537538
"<cpio_list> is a file containing newline separated entries that\n"
538539
"describe the files to be included in the initramfs archive:\n"
@@ -569,7 +570,8 @@ static void usage(const char *prog)
569570
"as mtime for symlinks, directories, regular and special files.\n"
570571
"The default is to use the current time for all files, but\n"
571572
"preserve modification time for regular files.\n"
572-
"-c: calculate and store 32-bit checksums for file data.\n",
573+
"-c: calculate and store 32-bit checksums for file data.\n"
574+
"<output_file>: write cpio to this file instead of stdout\n",
573575
prog);
574576
}
575577

@@ -611,7 +613,7 @@ int main (int argc, char *argv[])
611613

612614
default_mtime = time(NULL);
613615
while (1) {
614-
int opt = getopt(argc, argv, "t:ch");
616+
int opt = getopt(argc, argv, "t:cho:");
615617
char *invalid;
616618

617619
if (opt == -1)
@@ -630,6 +632,16 @@ int main (int argc, char *argv[])
630632
case 'c':
631633
do_csum = true;
632634
break;
635+
case 'o':
636+
outfd = open(optarg,
637+
O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC,
638+
0600);
639+
if (outfd < 0) {
640+
fprintf(stderr, "failed to open %s\n", optarg);
641+
usage(argv[0]);
642+
exit(1);
643+
}
644+
break;
633645
case 'h':
634646
case '?':
635647
usage(argv[0]);

0 commit comments

Comments
 (0)