Skip to content

Commit 5ef46e5

Browse files
committed
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.
1 parent 3e36211 commit 5ef46e5

File tree

8 files changed

+52
-36
lines changed

8 files changed

+52
-36
lines changed

src/import/export.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static int export_tar(int argc, char *argv[], void *userdata) {
6969
int r, fd;
7070

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

150150
if (machine_name_is_valid(argv[1])) {
151-
r = image_find(argv[1], &image);
151+
r = image_find(IMAGE_MACHINE, argv[1], &image);
152152
if (r < 0)
153153
return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
154154
if (r == 0) {

src/import/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static int import_tar(int argc, char *argv[], void *userdata) {
7575
}
7676

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

172172
if (!arg_force) {
173-
r = image_find(local, NULL);
173+
r = image_find(IMAGE_MACHINE, local, NULL);
174174
if (r < 0)
175175
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
176176
else if (r > 0) {

src/import/pull.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
8383
}
8484

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

171171
if (!arg_force) {
172-
r = image_find(local, NULL);
172+
r = image_find(IMAGE_MACHINE, local, NULL);
173173
if (r < 0)
174174
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
175175
else if (r > 0) {

src/machine/image-dbus.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ int image_object_find(sd_bus *bus, const char *path, const char *interface, void
435435
if (r < 0)
436436
return r;
437437

438-
r = image_find(e, &image);
438+
r = image_find(IMAGE_MACHINE, e, &image);
439439
if (r <= 0)
440440
return r;
441441

@@ -478,7 +478,7 @@ int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char **
478478
if (!images)
479479
return -ENOMEM;
480480

481-
r = image_discover(images);
481+
r = image_discover(IMAGE_MACHINE, images);
482482
if (r < 0)
483483
return r;
484484

src/machine/machined-dbus.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_erro
145145
if (r < 0)
146146
return r;
147147

148-
r = image_find(name, NULL);
148+
r = image_find(IMAGE_MACHINE, name, NULL);
149149
if (r == 0)
150150
return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name);
151151
if (r < 0)
@@ -545,7 +545,7 @@ static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_er
545545
if (!images)
546546
return -ENOMEM;
547547

548-
r = image_discover(images);
548+
r = image_discover(IMAGE_MACHINE, images);
549549
if (r < 0)
550550
return r;
551551

@@ -738,7 +738,7 @@ static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_e
738738
if (!image_name_is_valid(name))
739739
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
740740

741-
r = image_find(name, &i);
741+
r = image_find(IMAGE_MACHINE, name, &i);
742742
if (r < 0)
743743
return r;
744744
if (r == 0)
@@ -762,7 +762,7 @@ static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_e
762762
if (!image_name_is_valid(old_name))
763763
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
764764

765-
r = image_find(old_name, &i);
765+
r = image_find(IMAGE_MACHINE, old_name, &i);
766766
if (r < 0)
767767
return r;
768768
if (r == 0)
@@ -786,7 +786,7 @@ static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_er
786786
if (!image_name_is_valid(old_name))
787787
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", old_name);
788788

789-
r = image_find(old_name, &i);
789+
r = image_find(IMAGE_MACHINE, old_name, &i);
790790
if (r < 0)
791791
return r;
792792
if (r == 0)
@@ -810,7 +810,7 @@ static int method_mark_image_read_only(sd_bus_message *message, void *userdata,
810810
if (!image_name_is_valid(name))
811811
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
812812

813-
r = image_find(name, &i);
813+
r = image_find(IMAGE_MACHINE, name, &i);
814814
if (r < 0)
815815
return r;
816816
if (r == 0)
@@ -834,7 +834,7 @@ static int method_get_image_hostname(sd_bus_message *message, void *userdata, sd
834834
if (!image_name_is_valid(name))
835835
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
836836

837-
r = image_find(name, &i);
837+
r = image_find(IMAGE_MACHINE, name, &i);
838838
if (r < 0)
839839
return r;
840840
if (r == 0)
@@ -858,7 +858,7 @@ static int method_get_image_machine_id(sd_bus_message *message, void *userdata,
858858
if (!image_name_is_valid(name))
859859
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
860860

861-
r = image_find(name, &i);
861+
r = image_find(IMAGE_MACHINE, name, &i);
862862
if (r < 0)
863863
return r;
864864
if (r == 0)
@@ -882,7 +882,7 @@ static int method_get_image_machine_info(sd_bus_message *message, void *userdata
882882
if (!image_name_is_valid(name))
883883
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
884884

885-
r = image_find(name, &i);
885+
r = image_find(IMAGE_MACHINE, name, &i);
886886
if (r < 0)
887887
return r;
888888
if (r == 0)
@@ -906,7 +906,7 @@ static int method_get_image_os_release(sd_bus_message *message, void *userdata,
906906
if (!image_name_is_valid(name))
907907
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
908908

909-
r = image_find(name, &i);
909+
r = image_find(IMAGE_MACHINE, name, &i);
910910
if (r < 0)
911911
return r;
912912
if (r == 0)
@@ -1070,7 +1070,7 @@ static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_err
10701070
goto child_fail;
10711071
}
10721072

1073-
r = image_discover(images);
1073+
r = image_discover(IMAGE_MACHINE, images);
10741074
if (r < 0)
10751075
goto child_fail;
10761076

@@ -1211,7 +1211,7 @@ static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bu
12111211
if (!image_name_is_valid(name))
12121212
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name);
12131213

1214-
r = image_find(name, &i);
1214+
r = image_find(IMAGE_MACHINE, name, &i);
12151215
if (r < 0)
12161216
return r;
12171217
if (r == 0)

src/nspawn/nspawn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2399,7 +2399,7 @@ static int determine_names(void) {
23992399
if (arg_machine) {
24002400
_cleanup_(image_unrefp) Image *i = NULL;
24012401

2402-
r = image_find(arg_machine, &i);
2402+
r = image_find(IMAGE_MACHINE, arg_machine, &i);
24032403
if (r < 0)
24042404
return log_error_errno(r, "Failed to find image for machine '%s': %m", arg_machine);
24052405
if (r == 0) {

src/shared/machine-image.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@
4646
#include "util.h"
4747
#include "xattr-util.h"
4848

49-
static const char image_search_path[] =
50-
"/var/lib/machines\0"
51-
"/var/lib/container\0" /* legacy */
52-
"/usr/local/lib/machines\0"
53-
"/usr/lib/machines\0";
49+
static const char* const image_search_path[_IMAGE_CLASS_MAX] = {
50+
[IMAGE_MACHINE] = "/var/lib/machines\0"
51+
"/var/lib/container\0" /* legacy */
52+
"/usr/local/lib/machines\0"
53+
"/usr/lib/machines\0",
54+
55+
[IMAGE_PORTABLE] = "/var/lib/portables\0"
56+
"/usr/local/lib/portables\0"
57+
"/usr/lib/portables\0",
58+
};
5459

5560
Image *image_unref(Image *i) {
5661
if (!i)
@@ -336,17 +341,19 @@ static int image_make(
336341
return 0;
337342
}
338343

339-
int image_find(const char *name, Image **ret) {
344+
int image_find(ImageClass class, const char *name, Image **ret) {
340345
const char *path;
341346
int r;
342347

348+
assert(class >= 0);
349+
assert(class < _IMAGE_CLASS_MAX);
343350
assert(name);
344351

345352
/* There are no images with invalid names */
346353
if (!image_name_is_valid(name))
347354
return 0;
348355

349-
NULSTR_FOREACH(path, image_search_path) {
356+
NULSTR_FOREACH(path, image_search_path[class]) {
350357
_cleanup_closedir_ DIR *d = NULL;
351358

352359
d = opendir(path);
@@ -375,19 +382,21 @@ int image_find(const char *name, Image **ret) {
375382
return 1;
376383
}
377384

378-
if (streq(name, ".host"))
385+
if (class == IMAGE_MACHINE && streq(name, ".host"))
379386
return image_make(".host", AT_FDCWD, NULL, "/", ret);
380387

381388
return 0;
382389
};
383390

384-
int image_discover(Hashmap *h) {
391+
int image_discover(ImageClass class, Hashmap *h) {
385392
const char *path;
386393
int r;
387394

395+
assert(class >= 0);
396+
assert(class < _IMAGE_CLASS_MAX);
388397
assert(h);
389398

390-
NULSTR_FOREACH(path, image_search_path) {
399+
NULSTR_FOREACH(path, image_search_path[class]) {
391400
_cleanup_closedir_ DIR *d = NULL;
392401
struct dirent *de;
393402

@@ -422,7 +431,7 @@ int image_discover(Hashmap *h) {
422431
}
423432
}
424433

425-
if (!hashmap_contains(h, ".host")) {
434+
if (class == IMAGE_MACHINE && !hashmap_contains(h, ".host")) {
426435
_cleanup_(image_unrefp) Image *image = NULL;
427436

428437
r = image_make(".host", AT_FDCWD, NULL, "/", &image);
@@ -567,7 +576,7 @@ int image_rename(Image *i, const char *new_name) {
567576
if (r < 0)
568577
return r;
569578

570-
r = image_find(new_name, NULL);
579+
r = image_find(IMAGE_MACHINE, new_name, NULL);
571580
if (r < 0)
572581
return r;
573582
if (r > 0)
@@ -680,7 +689,7 @@ int image_clone(Image *i, const char *new_name, bool read_only) {
680689
if (r < 0)
681690
return r;
682691

683-
r = image_find(new_name, NULL);
692+
r = image_find(IMAGE_MACHINE, new_name, NULL);
684693
if (r < 0)
685694
return r;
686695
if (r > 0)

src/shared/machine-image.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
#include "string-util.h"
1818
#include "time-util.h"
1919

20+
typedef enum ImageClass {
21+
IMAGE_MACHINE,
22+
IMAGE_PORTABLE,
23+
_IMAGE_CLASS_MAX,
24+
_IMAGE_CLASS_INVALID = -1
25+
} ImageClass;
26+
2027
typedef enum ImageType {
2128
IMAGE_DIRECTORY,
2229
IMAGE_SUBVOLUME,
@@ -58,8 +65,8 @@ static inline Hashmap* image_hashmap_free(Hashmap *map) {
5865
DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref);
5966
DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free);
6067

61-
int image_find(const char *name, Image **ret);
62-
int image_discover(Hashmap *map);
68+
int image_find(ImageClass class, const char *name, Image **ret);
69+
int image_discover(ImageClass class, Hashmap *map);
6370

6471
int image_remove(Image *i);
6572
int image_rename(Image *i, const char *new_name);

0 commit comments

Comments
 (0)