From: Luca Boccassi Date: Thu, 28 Mar 2024 14:16:39 +0000 (+0000) Subject: portable: fix 'portablectl list' to show the actual state for extensions X-Git-Tag: v256-rc1~294^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=373a1e47b2878a278c130712ab2910139ccc18a2;p=thirdparty%2Fsystemd.git portable: fix 'portablectl list' to show the actual state for extensions When listing images they are inspected one by one, so in case of a portable with extensions they always resulted as not found. Allow a partial match when listing, so that we can find the appropriate unit that an image belongs to, and list the correct state as attached. --- diff --git a/src/portable/portable.c b/src/portable/portable.c index a544a013421..60dc98c5baf 100644 --- a/src/portable/portable.c +++ b/src/portable/portable.c @@ -1664,7 +1664,7 @@ int portable_attach( return 0; } -static bool marker_matches_images(const char *marker, const char *name_or_path, char **extension_image_paths) { +static bool marker_matches_images(const char *marker, const char *name_or_path, char **extension_image_paths, bool match_all) { _cleanup_strv_free_ char **root_and_extensions = NULL; int r; @@ -1675,7 +1675,9 @@ static bool marker_matches_images(const char *marker, const char *name_or_path, * list of images/paths. We enforce strict 1:1 matching, so that we are sure * we are detaching exactly what was attached. * For each image, starting with the root, we look for a token in the marker, - * and return a negative answer on any non-matching combination. */ + * and return a negative answer on any non-matching combination. + * If a partial match is allowed, then return immediately once it is found, otherwise + * ensure that everything matches. */ root_and_extensions = strv_new(name_or_path); if (!root_and_extensions) @@ -1704,11 +1706,14 @@ static bool marker_matches_images(const char *marker, const char *name_or_path, if (r < 0) return log_debug_errno(r, "Failed to extract image name from %s, ignoring: %m", *image_name_or_path); - if (!streq(base_image, base_image_name_or_path)) - return false; + if (!streq(base_image, base_image_name_or_path)) { + if (match_all) + return false; + } else if (!match_all) + return true; } - return true; + return match_all; } static int test_chroot_dropin( @@ -1763,7 +1768,9 @@ static int test_chroot_dropin( if (!name_or_path) r = true; else - r = marker_matches_images(marker, name_or_path, extension_image_paths); + /* When detaching we want to match exactly on all images, but when inspecting we only need + * to get the state of one component */ + r = marker_matches_images(marker, name_or_path, extension_image_paths, ret_marker != NULL); if (ret_marker) *ret_marker = TAKE_PTR(marker); diff --git a/test/units/testsuite-29.sh b/test/units/testsuite-29.sh index d529286c6ce..7af0ed45786 100755 --- a/test/units/testsuite-29.sh +++ b/test/units/testsuite-29.sh @@ -241,6 +241,12 @@ status="$(portablectl is-attached --extension app0 minimal_0)" status="$(portablectl is-attached --extension app1 minimal_0)" [[ "${status}" == "attached-runtime" ]] +# Ensure 'portablectl list' shows the correct status for both images +portablectl list +portablectl list | grep -F "minimal_0" | grep -q -F "attached-runtime" +portablectl list | grep -F "app0" | grep -q -F "attached-runtime" +portablectl list | grep -F "app1" | grep -q -F "attached-runtime" + portablectl detach --runtime --extension /usr/share/app0.raw /usr/share/minimal_0.raw app status="$(portablectl is-attached --extension app1 minimal_0)"