machine-image: introduce two different classes of images

This distuingishes two different classes of images, one for the purpose
of npsawn-like containers, i.e. "machines", and one for portable
services.

This distinction is mostly about search paths. We look for machine
images in /var/lib/machines and for portable images in
/var/lib/portables.
This commit is contained in:
Lennart Poettering 2018-04-05 15:39:43 +02:00
parent 3e36211be0
commit 5ef46e5f65
8 changed files with 52 additions and 36 deletions

View file

@ -69,7 +69,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
int r, fd;
if (machine_name_is_valid(argv[1])) {
r = image_find(argv[1], &image);
r = image_find(IMAGE_MACHINE, argv[1], &image);
if (r < 0)
return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
if (r == 0) {
@ -148,7 +148,7 @@ static int export_raw(int argc, char *argv[], void *userdata) {
int r, fd;
if (machine_name_is_valid(argv[1])) {
r = image_find(argv[1], &image);
r = image_find(IMAGE_MACHINE, argv[1], &image);
if (r < 0)
return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
if (r == 0) {

View file

@ -75,7 +75,7 @@ static int import_tar(int argc, char *argv[], void *userdata) {
}
if (!arg_force) {
r = image_find(local, NULL);
r = image_find(IMAGE_MACHINE, local, NULL);
if (r < 0)
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
else if (r > 0) {
@ -170,7 +170,7 @@ static int import_raw(int argc, char *argv[], void *userdata) {
}
if (!arg_force) {
r = image_find(local, NULL);
r = image_find(IMAGE_MACHINE, local, NULL);
if (r < 0)
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
else if (r > 0) {

View file

@ -83,7 +83,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
}
if (!arg_force) {
r = image_find(local, NULL);
r = image_find(IMAGE_MACHINE, local, NULL);
if (r < 0)
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
else if (r > 0) {
@ -169,7 +169,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
}
if (!arg_force) {
r = image_find(local, NULL);
r = image_find(IMAGE_MACHINE, local, NULL);
if (r < 0)
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
else if (r > 0) {

View file

@ -435,7 +435,7 @@ int image_object_find(sd_bus *bus, const char *path, const char *interface, void
if (r < 0)
return r;
r = image_find(e, &image);
r = image_find(IMAGE_MACHINE, e, &image);
if (r <= 0)
return r;
@ -478,7 +478,7 @@ int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char **
if (!images)
return -ENOMEM;
r = image_discover(images);
r = image_discover(IMAGE_MACHINE, images);
if (r < 0)
return r;

View file

@ -145,7 +145,7 @@ static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_erro
if (r < 0)
return r;
r = image_find(name, NULL);
r = image_find(IMAGE_MACHINE, name, NULL);
if (r == 0)
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
if (r < 0)
@ -545,7 +545,7 @@ static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_er
if (!images)
return -ENOMEM;
r = image_discover(images);
r = image_discover(IMAGE_MACHINE, images);
if (r < 0)
return r;
@ -738,7 +738,7 @@ static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_e
if (!image_name_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
r = image_find(name, &i);
r = image_find(IMAGE_MACHINE, name, &i);
if (r < 0)
return r;
if (r == 0)
@ -762,7 +762,7 @@ static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_e
if (!image_name_is_valid(old_name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
r = image_find(old_name, &i);
r = image_find(IMAGE_MACHINE, old_name, &i);
if (r < 0)
return r;
if (r == 0)
@ -786,7 +786,7 @@ static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_er
if (!image_name_is_valid(old_name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
r = image_find(old_name, &i);
r = image_find(IMAGE_MACHINE, old_name, &i);
if (r < 0)
return r;
if (r == 0)
@ -810,7 +810,7 @@ static int method_mark_image_read_only(sd_bus_message *message, void *userdata,
if (!image_name_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
r = image_find(name, &i);
r = image_find(IMAGE_MACHINE, name, &i);
if (r < 0)
return r;
if (r == 0)
@ -834,7 +834,7 @@ static int method_get_image_hostname(sd_bus_message *message, void *userdata, sd
if (!image_name_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
r = image_find(name, &i);
r = image_find(IMAGE_MACHINE, name, &i);
if (r < 0)
return r;
if (r == 0)
@ -858,7 +858,7 @@ static int method_get_image_machine_id(sd_bus_message *message, void *userdata,
if (!image_name_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
r = image_find(name, &i);
r = image_find(IMAGE_MACHINE, name, &i);
if (r < 0)
return r;
if (r == 0)
@ -882,7 +882,7 @@ static int method_get_image_machine_info(sd_bus_message *message, void *userdata
if (!image_name_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
r = image_find(name, &i);
r = image_find(IMAGE_MACHINE, name, &i);
if (r < 0)
return r;
if (r == 0)
@ -906,7 +906,7 @@ static int method_get_image_os_release(sd_bus_message *message, void *userdata,
if (!image_name_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
r = image_find(name, &i);
r = image_find(IMAGE_MACHINE, name, &i);
if (r < 0)
return r;
if (r == 0)
@ -1070,7 +1070,7 @@ static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_err
goto child_fail;
}
r = image_discover(images);
r = image_discover(IMAGE_MACHINE, images);
if (r < 0)
goto child_fail;
@ -1211,7 +1211,7 @@ static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bu
if (!image_name_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
r = image_find(name, &i);
r = image_find(IMAGE_MACHINE, name, &i);
if (r < 0)
return r;
if (r == 0)

View file

@ -2399,7 +2399,7 @@ static int determine_names(void) {
if (arg_machine) {
_cleanup_(image_unrefp) Image *i = NULL;
r = image_find(arg_machine, &i);
r = image_find(IMAGE_MACHINE, arg_machine, &i);
if (r < 0)
return log_error_errno(r, "Failed to find image for machine '%s': %m", arg_machine);
if (r == 0) {

View file

@ -46,11 +46,16 @@
#include "util.h"
#include "xattr-util.h"
static const char image_search_path[] =
"/var/lib/machines\0"
"/var/lib/container\0" /* legacy */
"/usr/local/lib/machines\0"
"/usr/lib/machines\0";
static const char* const image_search_path[_IMAGE_CLASS_MAX] = {
[IMAGE_MACHINE] = "/var/lib/machines\0"
"/var/lib/container\0" /* legacy */
"/usr/local/lib/machines\0"
"/usr/lib/machines\0",
[IMAGE_PORTABLE] = "/var/lib/portables\0"
"/usr/local/lib/portables\0"
"/usr/lib/portables\0",
};
Image *image_unref(Image *i) {
if (!i)
@ -336,17 +341,19 @@ static int image_make(
return 0;
}
int image_find(const char *name, Image **ret) {
int image_find(ImageClass class, const char *name, Image **ret) {
const char *path;
int r;
assert(class >= 0);
assert(class < _IMAGE_CLASS_MAX);
assert(name);
/* There are no images with invalid names */
if (!image_name_is_valid(name))
return 0;
NULSTR_FOREACH(path, image_search_path) {
NULSTR_FOREACH(path, image_search_path[class]) {
_cleanup_closedir_ DIR *d = NULL;
d = opendir(path);
@ -375,19 +382,21 @@ int image_find(const char *name, Image **ret) {
return 1;
}
if (streq(name, ".host"))
if (class == IMAGE_MACHINE && streq(name, ".host"))
return image_make(".host", AT_FDCWD, NULL, "/", ret);
return 0;
};
int image_discover(Hashmap *h) {
int image_discover(ImageClass class, Hashmap *h) {
const char *path;
int r;
assert(class >= 0);
assert(class < _IMAGE_CLASS_MAX);
assert(h);
NULSTR_FOREACH(path, image_search_path) {
NULSTR_FOREACH(path, image_search_path[class]) {
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
@ -422,7 +431,7 @@ int image_discover(Hashmap *h) {
}
}
if (!hashmap_contains(h, ".host")) {
if (class == IMAGE_MACHINE && !hashmap_contains(h, ".host")) {
_cleanup_(image_unrefp) Image *image = NULL;
r = image_make(".host", AT_FDCWD, NULL, "/", &image);
@ -567,7 +576,7 @@ int image_rename(Image *i, const char *new_name) {
if (r < 0)
return r;
r = image_find(new_name, NULL);
r = image_find(IMAGE_MACHINE, new_name, NULL);
if (r < 0)
return r;
if (r > 0)
@ -680,7 +689,7 @@ int image_clone(Image *i, const char *new_name, bool read_only) {
if (r < 0)
return r;
r = image_find(new_name, NULL);
r = image_find(IMAGE_MACHINE, new_name, NULL);
if (r < 0)
return r;
if (r > 0)

View file

@ -17,6 +17,13 @@
#include "string-util.h"
#include "time-util.h"
typedef enum ImageClass {
IMAGE_MACHINE,
IMAGE_PORTABLE,
_IMAGE_CLASS_MAX,
_IMAGE_CLASS_INVALID = -1
} ImageClass;
typedef enum ImageType {
IMAGE_DIRECTORY,
IMAGE_SUBVOLUME,
@ -58,8 +65,8 @@ static inline Hashmap* image_hashmap_free(Hashmap *map) {
DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free);
int image_find(const char *name, Image **ret);
int image_discover(Hashmap *map);
int image_find(ImageClass class, const char *name, Image **ret);
int image_discover(ImageClass class, Hashmap *map);
int image_remove(Image *i);
int image_rename(Image *i, const char *new_name);