Skip to content

Commit 12d58b6

Browse files
committed
dissect: Add --mtree-hash= option
Let's make including hashes in the mtree output configurable to allow speeding up the --mtree command in cases where file hashes are not required.
1 parent 2292fa1 commit 12d58b6

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

man/systemd-dissect.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,14 @@
422422
<command>cfdisk /dev/loop/by-ref/quux</command>.</para></listitem>
423423
</varlistentry>
424424

425+
<varlistentry>
426+
<term><option>--mtree-hash=no</option></term>
427+
428+
<listitem><para>If combined with <option>--mtree</option>, turns off inclusion of file hashes in the
429+
mtree output. This makes the <option>--mtree</option> faster when operating on large images.
430+
</para></listitem>
431+
</varlistentry>
432+
425433
<xi:include href="standard-options.xml" xpointer="image-policy-open" />
426434
<xi:include href="standard-options.xml" xpointer="no-pager" />
427435
<xi:include href="standard-options.xml" xpointer="no-legend" />

src/dissect/dissect.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static bool arg_in_memory = false;
8686
static char **arg_argv = NULL;
8787
static char *arg_loop_ref = NULL;
8888
static ImagePolicy* arg_image_policy = NULL;
89+
static bool arg_mtree_hash = true;
8990

9091
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
9192
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
@@ -137,6 +138,7 @@ static int help(void) {
137138
" --json=pretty|short|off\n"
138139
" Generate JSON output\n"
139140
" --loop-ref=NAME Set reference string for loopback device\n"
141+
" --mtree-hash=BOOL Whether to include SHA256 hash in the mtree output\n"
140142
"\n%3$sCommands:%4$s\n"
141143
" -h --help Show this help\n"
142144
" --version Show package version\n"
@@ -257,6 +259,7 @@ static int parse_argv(int argc, char *argv[]) {
257259
ARG_LOOP_REF,
258260
ARG_IMAGE_POLICY,
259261
ARG_VALIDATE,
262+
ARG_MTREE_HASH,
260263
};
261264

262265
static const struct option options[] = {
@@ -288,6 +291,7 @@ static int parse_argv(int argc, char *argv[]) {
288291
{ "loop-ref", required_argument, NULL, ARG_LOOP_REF },
289292
{ "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
290293
{ "validate", no_argument, NULL, ARG_VALIDATE },
294+
{ "mtree-hash", required_argument, NULL, ARG_MTREE_HASH },
291295
{}
292296
};
293297

@@ -505,6 +509,12 @@ static int parse_argv(int argc, char *argv[]) {
505509
arg_action = ACTION_VALIDATE;
506510
break;
507511

512+
case ARG_MTREE_HASH:
513+
r = parse_boolean_argument("--mtree-hash=", optarg, &arg_mtree_hash);
514+
if (r < 0)
515+
return r;
516+
break;
517+
508518
case '?':
509519
return -EINVAL;
510520

@@ -1202,7 +1212,7 @@ static int mtree_print_item(
12021212
ansi_normal(),
12031213
(uint64_t) sx->stx_size);
12041214

1205-
if (inode_fd >= 0 && sx->stx_size > 0) {
1215+
if (arg_mtree_hash && inode_fd >= 0 && sx->stx_size > 0) {
12061216
uint8_t hash[SHA256_DIGEST_SIZE];
12071217

12081218
r = get_file_sha256(inode_fd, hash);

test/units/testsuite-50.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ systemd-dissect "${image}.raw" | grep -q -F "MARKER=1"
4343
systemd-dissect "${image}.raw" | grep -q -F -f <(sed 's/"//g' "$os_release")
4444

4545
systemd-dissect --list "${image}.raw" | grep -q '^etc/os-release$'
46-
systemd-dissect --mtree "${image}.raw" | grep -q "./usr/bin/cat type=file mode=0755 uid=0 gid=0"
46+
systemd-dissect --mtree "${image}.raw" --mtree-hash yes | grep -qe "^./usr/bin/cat type=file mode=0755 uid=0 gid=0 size=[0-9]* sha256sum=[a-z0-9]*$"
47+
systemd-dissect --mtree "${image}.raw" --mtree-hash no | grep -qe "^./usr/bin/cat type=file mode=0755 uid=0 gid=0 size=[0-9]*$"
4748

4849
read -r SHA256SUM1 _ < <(systemd-dissect --copy-from "${image}.raw" etc/os-release | sha256sum)
4950
test "$SHA256SUM1" != ""

0 commit comments

Comments
 (0)