]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
import-generator: give import services better names
authorLennart Poettering <lennart@poettering.net>
Fri, 7 Feb 2025 17:22:13 +0000 (18:22 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 21 Feb 2025 09:03:32 +0000 (10:03 +0100)
Previously, we'd name the import services numerically. Let's instead use
the local target file name, i.e. the object we are creating with these
services locally. That's useful so that we can robustely order against
these service instances, should we need to one day.

src/import/import-generator.c
test/units/TEST-13-NSPAWN.importctl.sh

index d63ed23a7b94aa0cab8141b2057e06e801628172..3e052e54ec1de1cef1153f37af73e5f002400450 100644 (file)
@@ -289,15 +289,39 @@ static int parse_credentials(void) {
         return 0;
 }
 
-static int transfer_generate(const Transfer *t, size_t c) {
+static char *transfer_get_local_path(const Transfer *t) {
+        assert(t);
+        assert(t->image_root);
+        assert(t->local);
+
+        switch (t->type) {
+        case IMPORT_RAW:
+                return strjoin(t->image_root, "/", t->local, ".raw");
+
+        case IMPORT_TAR:
+                return path_join(t->image_root, t->local);
+
+        default:
+                assert_not_reached();
+        }
+}
+
+static int transfer_generate(const Transfer *t) {
         int r;
 
         assert(t);
 
-        _cleanup_free_ char *service = NULL;
-        if (asprintf(&service, "import%zu.service", c) < 0)
+        _cleanup_free_ char *local_path = transfer_get_local_path(t);
+        if (!local_path)
                 return log_oom();
 
+        /* Give this unit a clear name derived from the file system object we are installed into the OS, so
+         * that other components can nicely have dependencies on this. */
+        _cleanup_free_ char *service = NULL;
+        r = unit_name_from_path_instance("systemd-import", local_path, ".service", &service);
+        if (r < 0)
+                return log_error_errno(r, "Failed to build import unit name from '%s': %m", local_path);
+
         _cleanup_fclose_ FILE *f = NULL;
         r = generator_open_unit_file(arg_dest, /* source = */ NULL, service, &f);
         if (r < 0)
@@ -337,10 +361,6 @@ static int transfer_generate(const Transfer *t, size_t c) {
         if (t->blockdev) {
                 assert(t->type == IMPORT_RAW);
 
-                _cleanup_free_ char *local_path = strjoin(t->image_root, "/", t->local, ".raw");
-                if (!local_path)
-                        return log_oom();
-
                 r = unit_name_from_path_instance("systemd-loop", local_path, ".service", &loop_service);
                 if (r < 0)
                         return log_error_errno(r, "Failed to build systemd-loop@.service instance name from path '%s': %m", local_path);
@@ -384,11 +404,10 @@ static int transfer_generate(const Transfer *t, size_t c) {
 }
 
 static int generate(void) {
-        size_t c = 0;
         int r = 0;
 
         FOREACH_ARRAY(i, arg_transfers, arg_n_transfers)
-                RET_GATHER(r, transfer_generate(i, c++));
+                RET_GATHER(r, transfer_generate(i));
 
         return r;
 }
index ac4a70e9b0256e632df57a30deb394ce714e7995..e4931a9bfda0aef130f3631a9cfe8678b1694b98 100755 (executable)
@@ -81,7 +81,7 @@ cat /proc/cmdline
 
 systemctl daemon-reload
 
-systemctl start import0.service
+systemctl start systemd-import@var-lib-confexts-importtest9.service
 cmp /var/tmp/importtest /var/lib/confexts/importtest9/importtest
 
 # Verify generic service calls, too