]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dissect: optionally derive loop-ref from image filename
authorLennart Poettering <lennart@poettering.net>
Fri, 7 Feb 2025 16:55:08 +0000 (17:55 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 21 Feb 2025 08:57:02 +0000 (09:57 +0100)
man/systemd-dissect.xml
src/dissect/dissect.c

index 7c9369e387e36ea1fd712ba9f08c1a88db1e1f85..84f4a4ab10884a26c20fd01fc43c3d610031f081 100644 (file)
         <xi:include href="version-info.xml" xpointer="v254"/></listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><option>--loop-ref-auto</option></term>
+
+        <listitem><para>Similar to <option>--loop-ref=</option>, but automatically derive the reference
+        string from the specified backing filename, truncating it if necessary.</para>
+
+        <xi:include href="version-info.xml" xpointer="v258"/></listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><option>--mtree-hash=no</option></term>
 
index 956cac67084964dcfc57a34b2b93723712bb3639..cc538c4b92a11c3be72695558c04edaa36943dd3 100644 (file)
@@ -94,6 +94,7 @@ static bool arg_rmdir = false;
 static bool arg_in_memory = false;
 static char **arg_argv = NULL;
 static char *arg_loop_ref = NULL;
+static bool arg_loop_ref_auto = false;
 static ImagePolicy *arg_image_policy = NULL;
 static bool arg_mtree_hash = true;
 static bool arg_via_service = false;
@@ -156,6 +157,7 @@ static int help(void) {
                "     --json=pretty|short|off\n"
                "                          Generate JSON output\n"
                "     --loop-ref=NAME      Set reference string for loopback device\n"
+               "     --loop-ref-auto      Derive reference string from image file name\n"
                "     --mtree-hash=BOOL    Whether to include SHA256 hash in the mtree output\n"
                "     --user               Discover user images\n"
                "     --system             Discover system images\n"
@@ -280,6 +282,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_ATTACH,
                 ARG_DETACH,
                 ARG_LOOP_REF,
+                ARG_LOOP_REF_AUTO,
                 ARG_IMAGE_POLICY,
                 ARG_VALIDATE,
                 ARG_MTREE_HASH,
@@ -317,6 +320,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "json",          required_argument, NULL, ARG_JSON          },
                 { "discover",      no_argument,       NULL, ARG_DISCOVER      },
                 { "loop-ref",      required_argument, NULL, ARG_LOOP_REF      },
+                { "loop-ref-auto", no_argument,       NULL, ARG_LOOP_REF_AUTO },
                 { "image-policy",  required_argument, NULL, ARG_IMAGE_POLICY  },
                 { "validate",      no_argument,       NULL, ARG_VALIDATE      },
                 { "mtree-hash",    required_argument, NULL, ARG_MTREE_HASH    },
@@ -522,6 +526,7 @@ static int parse_argv(int argc, char *argv[]) {
                 case ARG_LOOP_REF:
                         if (isempty(optarg)) {
                                 arg_loop_ref = mfree(arg_loop_ref);
+                                arg_loop_ref_auto = false;
                                 break;
                         }
 
@@ -531,6 +536,13 @@ static int parse_argv(int argc, char *argv[]) {
                         r = free_and_strdup_warn(&arg_loop_ref, optarg);
                         if (r < 0)
                                 return r;
+
+                        arg_loop_ref_auto = false;
+                        break;
+
+                case ARG_LOOP_REF_AUTO:
+                        arg_loop_ref = mfree(arg_loop_ref);
+                        arg_loop_ref_auto = true;
                         break;
 
                 case ARG_IMAGE_POLICY:
@@ -2149,6 +2161,12 @@ static int run(int argc, char *argv[]) {
                                                                         * support */
         }
 
+        if (!arg_loop_ref && arg_loop_ref_auto) {
+                r = path_extract_filename(arg_image, &arg_loop_ref);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to extract file name from image path '%s': %m", arg_image);
+        }
+
         if (arg_action == ACTION_VALIDATE)
                 return action_validate();
 
@@ -2214,8 +2232,8 @@ static int run(int argc, char *argv[]) {
                         if (arg_in_memory)
                                 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--in-memory= not supported when operating via systemd-mountfsd.");
 
-                        if (arg_loop_ref)
-                                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--loop-ref= not supported when operating via systemd-mountfsd.");
+                        if (arg_loop_ref || arg_loop_ref_auto) /* yes, the 2nd check is strictly speaking redundant, given the normalization we did above, but let's be explicit here */
+                                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--loop-ref=/--loop-ref-auto not supported when operating via systemd-mountfsd.");
 
                         if (verity_settings_set(&arg_verity_settings))
                                 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Externally configured verity settings not supported when operating via systemd-mountfsd.");