Skip to content

Commit f3b2306

Browse files
0x7f454c46masahir0y
authored andcommitted
gen_init_cpio: Apply mtime supplied by user to all file types
Currently gen_init_cpio -d <timestamp> is applied to symlinks, directories and special files. These files are created by gen_init_cpio from their description. Without <timestamp> option current time(NULL) is used. And regular files that go in initramfs are created before cpio generation, so their mtime(s) are preserved. This is usually not an issue as reproducible builds should rebuild everything in the distribution, including binaries, configs and whatever other regular files may find their way into kernel's initramfs. On the other hand, gen_initramfs.sh usage claims: > -d <date> Use date for all file mtime values Ar Arista initramfs files are managed with version control system that preserves mtime. Those are configs, boot parameters, init scripts, version files, platform-specific files, probably some others, too. While it's certainly possible to work this around by copying the file into temp directory and adjusting mtime prior to gen_init_cpio call, I don't see why it needs workarounds. The intended user of -d <date> option is the one that needs to create a reproducible build, see commit a8b8017 ("initramfs: Use KBUILD_BUILD_TIMESTAMP for generated entries"). If a user wants the build reproduction, they use -d <date>, which can be set on all types of files, without surprising exceptions and workarounds. Let's KISS here and just apply the time that user specified with -d option. Based-on-a-patch-by: Baptiste Covolato <[email protected]> Link: https://lore.kernel.org/lkml/[email protected]/ Signed-off-by: Dmitry Safonov <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 7beba04 commit f3b2306

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

usr/gen_init_cpio.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
static unsigned int offset;
2828
static unsigned int ino = 721;
2929
static time_t default_mtime;
30+
static bool do_file_mtime;
3031
static bool do_csum = false;
3132

3233
struct file_handler {
@@ -329,6 +330,7 @@ static int cpio_mkfile(const char *name, const char *location,
329330
int file;
330331
int retval;
331332
int rc = -1;
333+
time_t mtime;
332334
int namesize;
333335
unsigned int i;
334336
uint32_t csum = 0;
@@ -347,16 +349,21 @@ static int cpio_mkfile(const char *name, const char *location,
347349
goto error;
348350
}
349351

350-
if (buf.st_mtime > 0xffffffff) {
351-
fprintf(stderr, "%s: Timestamp exceeds maximum cpio timestamp, clipping.\n",
352-
location);
353-
buf.st_mtime = 0xffffffff;
354-
}
352+
if (do_file_mtime) {
353+
mtime = default_mtime;
354+
} else {
355+
mtime = buf.st_mtime;
356+
if (mtime > 0xffffffff) {
357+
fprintf(stderr, "%s: Timestamp exceeds maximum cpio timestamp, clipping.\n",
358+
location);
359+
mtime = 0xffffffff;
360+
}
355361

356-
if (buf.st_mtime < 0) {
357-
fprintf(stderr, "%s: Timestamp negative, clipping.\n",
358-
location);
359-
buf.st_mtime = 0;
362+
if (mtime < 0) {
363+
fprintf(stderr, "%s: Timestamp negative, clipping.\n",
364+
location);
365+
mtime = 0;
366+
}
360367
}
361368

362369
if (buf.st_size > 0xffffffff) {
@@ -387,7 +394,7 @@ static int cpio_mkfile(const char *name, const char *location,
387394
(long) uid, /* uid */
388395
(long) gid, /* gid */
389396
nlinks, /* nlink */
390-
(long) buf.st_mtime, /* mtime */
397+
(long) mtime, /* mtime */
391398
size, /* filesize */
392399
3, /* major */
393400
1, /* minor */
@@ -536,8 +543,9 @@ static void usage(const char *prog)
536543
"file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n"
537544
"\n"
538545
"<timestamp> is time in seconds since Epoch that will be used\n"
539-
"as mtime for symlinks, special files and directories. The default\n"
540-
"is to use the current time for these entries.\n"
546+
"as mtime for symlinks, directories, regular and special files.\n"
547+
"The default is to use the current time for all files, but\n"
548+
"preserve modification time for regular files.\n"
541549
"-c: calculate and store 32-bit checksums for file data.\n",
542550
prog);
543551
}
@@ -594,6 +602,7 @@ int main (int argc, char *argv[])
594602
usage(argv[0]);
595603
exit(1);
596604
}
605+
do_file_mtime = true;
597606
break;
598607
case 'c':
599608
do_csum = true;

0 commit comments

Comments
 (0)