From 12d58b6c743dc8c851d8fbb74096a0808e9f0e02 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Thu, 6 Jul 2023 11:24:22 +0200 Subject: [PATCH] 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. --- man/systemd-dissect.xml | 8 ++++++++ src/dissect/dissect.c | 12 +++++++++++- test/units/testsuite-50.sh | 3 ++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/man/systemd-dissect.xml b/man/systemd-dissect.xml index ce41faaf1a2..2b8620a5f0e 100644 --- a/man/systemd-dissect.xml +++ b/man/systemd-dissect.xml @@ -422,6 +422,14 @@ cfdisk /dev/loop/by-ref/quux. + + + + If combined with , turns off inclusion of file hashes in the + mtree output. This makes the faster when operating on large images. + + + diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index 9db8f862e8e..24e17bcad9e 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -86,6 +86,7 @@ static bool arg_in_memory = false; static char **arg_argv = NULL; static char *arg_loop_ref = NULL; static ImagePolicy* arg_image_policy = NULL; +static bool arg_mtree_hash = true; STATIC_DESTRUCTOR_REGISTER(arg_image, freep); STATIC_DESTRUCTOR_REGISTER(arg_root, freep); @@ -137,6 +138,7 @@ static int help(void) { " --json=pretty|short|off\n" " Generate JSON output\n" " --loop-ref=NAME Set reference string for loopback device\n" + " --mtree-hash=BOOL Whether to include SHA256 hash in the mtree output\n" "\n%3$sCommands:%4$s\n" " -h --help Show this help\n" " --version Show package version\n" @@ -257,6 +259,7 @@ static int parse_argv(int argc, char *argv[]) { ARG_LOOP_REF, ARG_IMAGE_POLICY, ARG_VALIDATE, + ARG_MTREE_HASH, }; static const struct option options[] = { @@ -288,6 +291,7 @@ static int parse_argv(int argc, char *argv[]) { { "loop-ref", required_argument, NULL, ARG_LOOP_REF }, { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY }, { "validate", no_argument, NULL, ARG_VALIDATE }, + { "mtree-hash", required_argument, NULL, ARG_MTREE_HASH }, {} }; @@ -505,6 +509,12 @@ static int parse_argv(int argc, char *argv[]) { arg_action = ACTION_VALIDATE; break; + case ARG_MTREE_HASH: + r = parse_boolean_argument("--mtree-hash=", optarg, &arg_mtree_hash); + if (r < 0) + return r; + break; + case '?': return -EINVAL; @@ -1202,7 +1212,7 @@ static int mtree_print_item( ansi_normal(), (uint64_t) sx->stx_size); - if (inode_fd >= 0 && sx->stx_size > 0) { + if (arg_mtree_hash && inode_fd >= 0 && sx->stx_size > 0) { uint8_t hash[SHA256_DIGEST_SIZE]; r = get_file_sha256(inode_fd, hash); diff --git a/test/units/testsuite-50.sh b/test/units/testsuite-50.sh index ab2c2959c05..52d11b9cb98 100755 --- a/test/units/testsuite-50.sh +++ b/test/units/testsuite-50.sh @@ -43,7 +43,8 @@ systemd-dissect "${image}.raw" | grep -q -F "MARKER=1" systemd-dissect "${image}.raw" | grep -q -F -f <(sed 's/"//g' "$os_release") systemd-dissect --list "${image}.raw" | grep -q '^etc/os-release$' -systemd-dissect --mtree "${image}.raw" | grep -q "./usr/bin/cat type=file mode=0755 uid=0 gid=0" +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]*$" +systemd-dissect --mtree "${image}.raw" --mtree-hash no | grep -qe "^./usr/bin/cat type=file mode=0755 uid=0 gid=0 size=[0-9]*$" read -r SHA256SUM1 _ < <(systemd-dissect --copy-from "${image}.raw" etc/os-release | sha256sum) test "$SHA256SUM1" != "" -- 2.47.3