From 3d49850096228f5923d2ff9370313d405cac1cb5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 7 Feb 2025 18:22:13 +0100 Subject: [PATCH] import-generator: give import services better names 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 | 37 +++++++++++++++++++------- test/units/TEST-13-NSPAWN.importctl.sh | 2 +- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/import/import-generator.c b/src/import/import-generator.c index d63ed23a7b9..3e052e54ec1 100644 --- a/src/import/import-generator.c +++ b/src/import/import-generator.c @@ -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; } diff --git a/test/units/TEST-13-NSPAWN.importctl.sh b/test/units/TEST-13-NSPAWN.importctl.sh index ac4a70e9b02..e4931a9bfda 100755 --- a/test/units/TEST-13-NSPAWN.importctl.sh +++ b/test/units/TEST-13-NSPAWN.importctl.sh @@ -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 -- 2.47.3