OSDN Git Service

mke2fs, libext2fs: make filesystem image reproducible
authorJin Qian <jinqian@google.com>
Mon, 28 Aug 2017 21:44:14 +0000 (14:44 -0700)
committerTao Bao <tbao@google.com>
Wed, 27 Sep 2017 21:48:53 +0000 (14:48 -0700)
Override fs->now with env E2FSPROGS_FAKE_TIME
Override hash seed with -E hash_seed=<uuid>

Bug: 64994964
Change-Id: If1af7e990b5bf2939a7d5f2a9986406e12c294e9
(cherry picked from commit 3164c7644fd096a1ed88cdbe888fca0702ef7415)

lib/ext2fs/initialize.c
lib/ext2fs/openfs.c
misc/mke2fs.c

index 99fedcd..dbe798b 100644 (file)
@@ -107,6 +107,7 @@ errcode_t ext2fs_initialize(const char *name, int flags,
        char            *buf = 0;
        char            c;
        double          reserved_ratio;
+       char            *time_env;
 
        if (!param || !ext2fs_blocks_count(param))
                return EXT2_ET_INVALID_ARGUMENT;
@@ -123,6 +124,11 @@ errcode_t ext2fs_initialize(const char *name, int flags,
 #ifdef WORDS_BIGENDIAN
        fs->flags |= EXT2_FLAG_SWAP_BYTES;
 #endif
+
+       time_env = getenv("E2FSPROGS_FAKE_TIME");
+       if (time_env)
+               fs->now = strtoul(time_env, NULL, 0);
+
        io_flags = IO_FLAG_RW;
        if (flags & EXT2_FLAG_EXCLUSIVE)
                io_flags |= IO_FLAG_EXCLUSIVE;
index ba39e01..94d0ef1 100644 (file)
@@ -126,6 +126,7 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
        struct ext2_group_desc *gdp;
        int             j;
 #endif
+       char            *time_env;
 
        EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
 
@@ -139,6 +140,11 @@ errcode_t ext2fs_open2(const char *name, const char *io_options,
        /* don't overwrite sb backups unless flag is explicitly cleared */
        fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
        fs->umask = 022;
+
+       time_env = getenv("E2FSPROGS_FAKE_TIME");
+       if (time_env)
+               fs->now = strtoul(time_env, NULL, 0);
+
        retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
        if (retval)
                goto cleanup;
index ed10cc4..9ba16b9 100644 (file)
@@ -105,6 +105,7 @@ static int  proceed_delay = -1;
 static blk64_t dev_size;
 
 static struct ext2_super_block fs_param;
+static __u32 zero_buf[4];
 static char *fs_uuid = NULL;
 static char *creator_os;
 static char *volume_label;
@@ -833,6 +834,19 @@ static void parse_extended_opts(struct ext2_super_block *param,
                                continue;
                        }
                        param->s_desc_size = desc_size;
+               } else if (strcmp(token, "hash_seed") == 0) {
+                       if (!arg) {
+                               r_usage++;
+                               badopt = token;
+                               continue;
+                       }
+                       if (uuid_parse(arg,
+                               (unsigned char *)param->s_hash_seed) != 0) {
+                               fprintf(stderr,
+                                       _("Invalid hash seed: %s\n"), arg);
+                               r_usage++;
+                               continue;
+                       }
                } else if (strcmp(token, "offset") == 0) {
                        if (!arg) {
                                r_usage++;
@@ -2944,7 +2958,13 @@ int main (int argc, char *argv[])
        free(hash_alg_str);
        fs->super->s_def_hash_version = (hash_alg >= 0) ? hash_alg :
                EXT2_HASH_HALF_MD4;
-       uuid_generate((unsigned char *) fs->super->s_hash_seed);
+
+       if (memcmp(fs_param.s_hash_seed, zero_buf,
+               sizeof(fs_param.s_hash_seed)) != 0) {
+               memcpy(fs->super->s_hash_seed, fs_param.s_hash_seed,
+                       sizeof(fs->super->s_hash_seed));
+       } else
+               uuid_generate((unsigned char *) fs->super->s_hash_seed);
 
        /*
         * Periodic checks can be enabled/disabled via config file.