From ace9ab19ffa321a84ddce466e90b8f3ba97e4d7f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Apr 2018 11:08:54 +0200 Subject: [PATCH] machine-image: extend search path This adds directories in /etc and /run to the search paths for OS images. While it doesn't make much sense to actually place huge disk images there, it's good enough for symlinks to those. The main reason for supporting this is that this allows us to neatly symlink portable image files located outside of the search path into the search path when attaching them, so that attaching them also means they are discoverable properly for all commands. --- src/shared/machine-image.c | 39 +++++++++++++++++++++++++++++++++++--- src/shared/machine-image.h | 2 ++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c index 41e9513abc..d97b522bdb 100644 --- a/src/shared/machine-image.c +++ b/src/shared/machine-image.c @@ -47,12 +47,16 @@ #include "xattr-util.h" static const char* const image_search_path[_IMAGE_CLASS_MAX] = { - [IMAGE_MACHINE] = "/var/lib/machines\0" - "/var/lib/container\0" /* legacy */ + [IMAGE_MACHINE] = "/etc/machines\0" /* only place symlinks here */ + "/run/machines\0" /* and here too */ + "/var/lib/machines\0" /* the main place for images */ + "/var/lib/container\0" /* legacy */ "/usr/local/lib/machines\0" "/usr/lib/machines\0", - [IMAGE_PORTABLE] = "/var/lib/portables\0" + [IMAGE_PORTABLE] = "/etc/portables\0" /* only place symlinks here */ + "/run/portables\0" /* and here too */ + "/var/lib/portables\0" /* the main place for images */ "/usr/local/lib/portables\0" "/usr/lib/portables\0", }; @@ -1224,6 +1228,35 @@ bool image_name_is_valid(const char *s) { return true; } +bool image_in_search_path(ImageClass class, const char *image) { + const char *path; + + assert(image); + + NULSTR_FOREACH(path, image_search_path[class]) { + const char *p; + size_t k; + + p = path_startswith(image, path); + if (!p) + continue; + + /* Make sure there's a filename following */ + k = strcspn(p, "/"); + if (k == 0) + continue; + + p += k; + + /* Accept trailing slashes */ + if (p[strspn(p, "/")] == 0) + return true; + + } + + return false; +} + static const char* const image_type_table[_IMAGE_TYPE_MAX] = { [IMAGE_DIRECTORY] = "directory", [IMAGE_SUBVOLUME] = "subvolume", diff --git a/src/shared/machine-image.h b/src/shared/machine-image.h index 7e09e8002a..60868fac94 100644 --- a/src/shared/machine-image.h +++ b/src/shared/machine-image.h @@ -92,6 +92,8 @@ int image_set_limit(Image *i, uint64_t referenced_max); int image_read_metadata(Image *i); +bool image_in_search_path(ImageClass class, const char *image); + static inline bool IMAGE_IS_HIDDEN(const struct Image *i) { assert(i);