]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/machine-image.c
machine-image: use structured initialization
[thirdparty/systemd.git] / src / shared / machine-image.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
cd61c3bf 2
a8fbdf54 3#include <errno.h>
ebd93cb6 4#include <fcntl.h>
e08f94ac
LP
5#include <linux/fs.h>
6#include <linux/loop.h>
a8fbdf54
TA
7#include <stdio.h>
8#include <stdlib.h>
e306723e 9#include <sys/file.h>
204f52e3 10#include <sys/ioctl.h>
a8fbdf54
TA
11#include <sys/stat.h>
12#include <unistd.h>
546dbec5 13
b5efdb8a 14#include "alloc-util.h"
cd61c3bf 15#include "btrfs-util.h"
c8b3094d 16#include "chattr-util.h"
ebd93cb6 17#include "copy.h"
a0956174 18#include "dirent-util.h"
c7664c07 19#include "dissect-image.h"
686d13b9 20#include "env-file.h"
b6e953f2 21#include "env-util.h"
3ffd4af2 22#include "fd-util.h"
f4f15635 23#include "fs-util.h"
a8fbdf54 24#include "hashmap.h"
e2054217 25#include "hostname-setup.h"
c7664c07 26#include "id128-util.h"
a8fbdf54
TA
27#include "lockfile-util.h"
28#include "log.h"
c7664c07 29#include "loop-util.h"
3ffd4af2 30#include "machine-image.h"
546dbec5 31#include "macro.h"
30535c16 32#include "mkdir.h"
d8b4d14d 33#include "nulstr-util.h"
d58ad743 34#include "os-util.h"
8e0b6570 35#include "path-util.h"
c6878637 36#include "rm-rf.h"
8b43440b 37#include "string-table.h"
07630cea 38#include "string-util.h"
8e0b6570 39#include "strv.h"
a8fbdf54 40#include "time-util.h"
8e0b6570 41#include "utf8.h"
89a5a90c 42#include "xattr-util.h"
cd61c3bf 43
5ef46e5f 44static const char* const image_search_path[_IMAGE_CLASS_MAX] = {
ace9ab19
LP
45 [IMAGE_MACHINE] = "/etc/machines\0" /* only place symlinks here */
46 "/run/machines\0" /* and here too */
47 "/var/lib/machines\0" /* the main place for images */
48 "/var/lib/container\0" /* legacy */
5ef46e5f
LP
49 "/usr/local/lib/machines\0"
50 "/usr/lib/machines\0",
51
ace9ab19
LP
52 [IMAGE_PORTABLE] = "/etc/portables\0" /* only place symlinks here */
53 "/run/portables\0" /* and here too */
54 "/var/lib/portables\0" /* the main place for images */
5ef46e5f
LP
55 "/usr/local/lib/portables\0"
56 "/usr/lib/portables\0",
57};
c2ce6a3d 58
8301aa0b
YW
59static Image *image_free(Image *i) {
60 assert(i);
9614bb06 61
cd61c3bf
LP
62 free(i->name);
63 free(i->path);
c7664c07
LP
64
65 free(i->hostname);
66 strv_free(i->machine_info);
67 strv_free(i->os_release);
68
6b430fdb 69 return mfree(i);
cd61c3bf
LP
70}
71
8301aa0b 72DEFINE_TRIVIAL_REF_UNREF_FUNC(Image, image, image_free);
b07ec5a1
YW
73DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(image_hash_ops, char, string_hash_func, string_compare_func,
74 Image, image_unref);
9614bb06 75
8e0b6570
LP
76static char **image_settings_path(Image *image) {
77 _cleanup_strv_free_ char **l = NULL;
8e0b6570
LP
78 const char *fn, *s;
79 unsigned i = 0;
80
81 assert(image);
82
83 l = new0(char*, 4);
84 if (!l)
85 return NULL;
86
87 fn = strjoina(image->name, ".nspawn");
88
b910cc72
LP
89 FOREACH_STRING(s, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
90 l[i] = path_join(s, fn);
8e0b6570
LP
91 if (!l[i])
92 return NULL;
93
94 i++;
95 }
96
97 l[i] = file_in_same_dir(image->path, fn);
98 if (!l[i])
99 return NULL;
100
ae2a15bc 101 return TAKE_PTR(l);
8e0b6570
LP
102}
103
bafbac4e
LP
104static char *image_roothash_path(Image *image) {
105 const char *fn;
106
107 assert(image);
108
109 fn = strjoina(image->name, ".roothash");
110
111 return file_in_same_dir(image->path, fn);
112}
113
c2ce6a3d 114static int image_new(
cd61c3bf 115 ImageType t,
5fc7f358 116 const char *pretty,
cd61c3bf 117 const char *path,
5fc7f358 118 const char *filename,
cd61c3bf 119 bool read_only,
10f9c755 120 usec_t crtime,
cd61c3bf 121 usec_t mtime,
c2ce6a3d 122 Image **ret) {
cd61c3bf
LP
123
124 _cleanup_(image_unrefp) Image *i = NULL;
cd61c3bf 125
cd61c3bf
LP
126 assert(t >= 0);
127 assert(t < _IMAGE_TYPE_MAX);
5fc7f358
LP
128 assert(pretty);
129 assert(filename);
c2ce6a3d 130 assert(ret);
cd61c3bf 131
c2108701 132 i = new(Image, 1);
cd61c3bf
LP
133 if (!i)
134 return -ENOMEM;
135
c2108701
LP
136 *i = (Image) {
137 .n_ref = 1,
138 .type = t,
139 .read_only = read_only,
140 .crtime = crtime,
141 .mtime = mtime,
142 .usage = UINT64_MAX,
143 .usage_exclusive = UINT64_MAX,
144 .limit = UINT64_MAX,
145 .limit_exclusive = UINT64_MAX,
146 };
cd61c3bf 147
5fc7f358 148 i->name = strdup(pretty);
cd61c3bf
LP
149 if (!i->name)
150 return -ENOMEM;
151
657ee2d8 152 i->path = path_join(path, filename);
5fc7f358
LP
153 if (!i->path)
154 return -ENOMEM;
155
858d36c1 156 path_simplify(i->path, false);
cd61c3bf 157
1cc6c93a 158 *ret = TAKE_PTR(i);
c2ce6a3d 159
cd61c3bf
LP
160 return 0;
161}
162
4756c94e
LP
163static int extract_pretty(const char *path, const char *suffix, char **ret) {
164 _cleanup_free_ char *name = NULL;
165 const char *p;
166 size_t n;
167
168 assert(path);
169 assert(ret);
170
171 p = last_path_component(path);
172 n = strcspn(p, "/");
173
174 name = strndup(p, n);
175 if (!name)
176 return -ENOMEM;
177
178 if (suffix) {
179 char *e;
180
181 e = endswith(name, suffix);
182 if (!e)
183 return -EINVAL;
184
185 *e = 0;
186 }
187
188 if (!image_name_is_valid(name))
189 return -EINVAL;
190
191 *ret = TAKE_PTR(name);
192 return 0;
193}
194
5fc7f358
LP
195static int image_make(
196 const char *pretty,
197 int dfd,
198 const char *path,
199 const char *filename,
3a6ce860 200 const struct stat *st,
5fc7f358
LP
201 Image **ret) {
202
2570578d 203 _cleanup_free_ char *pretty_buffer = NULL, *parent = NULL;
3a6ce860 204 struct stat stbuf;
5fc7f358 205 bool read_only;
cd61c3bf
LP
206 int r;
207
3a6ce860 208 assert(dfd >= 0 || dfd == AT_FDCWD);
bcb846f3 209 assert(path || dfd == AT_FDCWD);
5fc7f358 210 assert(filename);
cd61c3bf 211
eb38edce 212 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
3a6ce860
LP
213 * devices into /var/lib/machines/, and treat them normally.
214 *
215 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
216 * recognize. */
cd61c3bf 217
3a6ce860
LP
218 if (!st) {
219 if (fstatat(dfd, filename, &stbuf, 0) < 0)
220 return -errno;
221
222 st = &stbuf;
223 }
cd61c3bf 224
2570578d
LP
225 if (!path) {
226 if (dfd == AT_FDCWD)
227 (void) safe_getcwd(&parent);
228 else
229 (void) fd_get_path(dfd, &parent);
230 }
bcb846f3 231
5fc7f358
LP
232 read_only =
233 (path && path_startswith(path, "/usr")) ||
08ff5529 234 (faccessat(dfd, filename, W_OK, AT_EACCESS) < 0 && errno == EROFS);
86e339c8 235
3a6ce860 236 if (S_ISDIR(st->st_mode)) {
01b72568
LP
237 _cleanup_close_ int fd = -1;
238 unsigned file_attr = 0;
cd61c3bf 239
c2ce6a3d 240 if (!ret)
3a6ce860 241 return 0;
cd61c3bf 242
4756c94e
LP
243 if (!pretty) {
244 r = extract_pretty(filename, NULL, &pretty_buffer);
245 if (r < 0)
246 return r;
247
248 pretty = pretty_buffer;
249 }
5fc7f358 250
01b72568
LP
251 fd = openat(dfd, filename, O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
252 if (fd < 0)
253 return -errno;
254
c2ce6a3d 255 /* btrfs subvolumes have inode 256 */
3a6ce860 256 if (st->st_ino == 256) {
cd61c3bf 257
21222ea5
LP
258 r = btrfs_is_filesystem(fd);
259 if (r < 0)
260 return r;
261 if (r) {
10f9c755 262 BtrfsSubvolInfo info;
cd61c3bf 263
c2ce6a3d 264 /* It's a btrfs subvolume */
cd61c3bf 265
5bcd08db 266 r = btrfs_subvol_get_info_fd(fd, 0, &info);
10f9c755
LP
267 if (r < 0)
268 return r;
c2ce6a3d
LP
269
270 r = image_new(IMAGE_SUBVOLUME,
5fc7f358 271 pretty,
c2ce6a3d 272 path,
5fc7f358
LP
273 filename,
274 info.read_only || read_only,
10f9c755 275 info.otime,
c2ce6a3d 276 0,
c2ce6a3d
LP
277 ret);
278 if (r < 0)
279 return r;
280
5bcd08db
LP
281 if (btrfs_quota_scan_ongoing(fd) == 0) {
282 BtrfsQuotaInfo quota;
b6b18498 283
5bcd08db
LP
284 r = btrfs_subvol_get_subtree_quota_fd(fd, 0, &quota);
285 if (r >= 0) {
286 (*ret)->usage = quota.referenced;
287 (*ret)->usage_exclusive = quota.exclusive;
288
289 (*ret)->limit = quota.referenced_max;
290 (*ret)->limit_exclusive = quota.exclusive_max;
291 }
b6b18498
LP
292 }
293
3a6ce860 294 return 0;
cd61c3bf 295 }
c2ce6a3d 296 }
cd61c3bf 297
01b72568
LP
298 /* If the IMMUTABLE bit is set, we consider the
299 * directory read-only. Since the ioctl is not
300 * supported everywhere we ignore failures. */
301 (void) read_attr_fd(fd, &file_attr);
cd61c3bf 302
01b72568 303 /* It's just a normal directory. */
c2ce6a3d 304 r = image_new(IMAGE_DIRECTORY,
5fc7f358 305 pretty,
c2ce6a3d 306 path,
5fc7f358 307 filename,
01b72568 308 read_only || (file_attr & FS_IMMUTABLE_FL),
c2ce6a3d
LP
309 0,
310 0,
311 ret);
312 if (r < 0)
313 return r;
cd61c3bf 314
3a6ce860 315 return 0;
cd61c3bf 316
3a6ce860 317 } else if (S_ISREG(st->st_mode) && endswith(filename, ".raw")) {
10f9c755 318 usec_t crtime = 0;
cd61c3bf 319
aceac2f0 320 /* It's a RAW disk image */
cd61c3bf 321
c2ce6a3d 322 if (!ret)
3a6ce860 323 return 0;
cd61c3bf 324
4756c94e 325 (void) fd_getcrtime_at(dfd, filename, &crtime, 0);
10f9c755 326
4756c94e
LP
327 if (!pretty) {
328 r = extract_pretty(filename, ".raw", &pretty_buffer);
329 if (r < 0)
330 return r;
331
332 pretty = pretty_buffer;
333 }
10f9c755 334
aceac2f0 335 r = image_new(IMAGE_RAW,
5fc7f358 336 pretty,
c2ce6a3d 337 path,
5fc7f358 338 filename,
3a6ce860 339 !(st->st_mode & 0222) || read_only,
10f9c755 340 crtime,
3a6ce860 341 timespec_load(&st->st_mtim),
c2ce6a3d
LP
342 ret);
343 if (r < 0)
344 return r;
cd61c3bf 345
3a6ce860
LP
346 (*ret)->usage = (*ret)->usage_exclusive = st->st_blocks * 512;
347 (*ret)->limit = (*ret)->limit_exclusive = st->st_size;
b6b18498 348
3a6ce860 349 return 0;
eb38edce 350
3a6ce860 351 } else if (S_ISBLK(st->st_mode)) {
eb38edce
LP
352 _cleanup_close_ int block_fd = -1;
353 uint64_t size = UINT64_MAX;
354
355 /* A block device */
356
357 if (!ret)
3a6ce860 358 return 0;
eb38edce 359
4756c94e
LP
360 if (!pretty) {
361 r = extract_pretty(filename, NULL, &pretty_buffer);
362 if (r < 0)
363 return r;
364
365 pretty = pretty_buffer;
366 }
eb38edce
LP
367
368 block_fd = openat(dfd, filename, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
369 if (block_fd < 0)
2570578d 370 log_debug_errno(errno, "Failed to open block device %s/%s, ignoring: %m", path ?: strnull(parent), filename);
eb38edce 371 else {
3a6ce860
LP
372 /* Refresh stat data after opening the node */
373 if (fstat(block_fd, &stbuf) < 0)
eb38edce 374 return -errno;
3a6ce860
LP
375 st = &stbuf;
376
377 if (!S_ISBLK(st->st_mode)) /* Verify that what we opened is actually what we think it is */
eb38edce
LP
378 return -ENOTTY;
379
380 if (!read_only) {
381 int state = 0;
382
383 if (ioctl(block_fd, BLKROGET, &state) < 0)
2570578d 384 log_debug_errno(errno, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path ?: strnull(parent), filename);
eb38edce
LP
385 else if (state)
386 read_only = true;
387 }
388
389 if (ioctl(block_fd, BLKGETSIZE64, &size) < 0)
2570578d 390 log_debug_errno(errno, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path ?: strnull(parent), filename);
eb38edce
LP
391
392 block_fd = safe_close(block_fd);
393 }
394
395 r = image_new(IMAGE_BLOCK,
396 pretty,
397 path,
398 filename,
3a6ce860 399 !(st->st_mode & 0222) || read_only,
eb38edce
LP
400 0,
401 0,
402 ret);
403 if (r < 0)
404 return r;
405
ed0cb346 406 if (!IN_SET(size, 0, UINT64_MAX))
eb38edce
LP
407 (*ret)->usage = (*ret)->usage_exclusive = (*ret)->limit = (*ret)->limit_exclusive = size;
408
3a6ce860 409 return 0;
c2ce6a3d 410 }
cd61c3bf 411
3a6ce860 412 return -EMEDIUMTYPE;
c2ce6a3d 413}
cd61c3bf 414
5ef46e5f 415int image_find(ImageClass class, const char *name, Image **ret) {
c2ce6a3d
LP
416 const char *path;
417 int r;
cd61c3bf 418
5ef46e5f
LP
419 assert(class >= 0);
420 assert(class < _IMAGE_CLASS_MAX);
c2ce6a3d 421 assert(name);
cd61c3bf 422
c2ce6a3d
LP
423 /* There are no images with invalid names */
424 if (!image_name_is_valid(name))
3a6ce860 425 return -ENOENT;
cd61c3bf 426
5ef46e5f 427 NULSTR_FOREACH(path, image_search_path[class]) {
c2ce6a3d 428 _cleanup_closedir_ DIR *d = NULL;
3a6ce860 429 struct stat st;
cd61c3bf 430
c2ce6a3d
LP
431 d = opendir(path);
432 if (!d) {
433 if (errno == ENOENT)
434 continue;
cd61c3bf 435
c2ce6a3d
LP
436 return -errno;
437 }
cd61c3bf 438
3a6ce860
LP
439 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
440 * symlink block devices into the search path */
441 if (fstatat(dirfd(d), name, &st, 0) < 0) {
aceac2f0 442 _cleanup_free_ char *raw = NULL;
5fc7f358 443
3a6ce860
LP
444 if (errno != ENOENT)
445 return -errno;
446
b910cc72 447 raw = strjoin(name, ".raw");
aceac2f0 448 if (!raw)
5fc7f358
LP
449 return -ENOMEM;
450
3a6ce860
LP
451 if (fstatat(dirfd(d), raw, &st, 0) < 0) {
452
453 if (errno == ENOENT)
454 continue;
455
456 return -errno;
457 }
458
459 if (!S_ISREG(st.st_mode))
5fc7f358 460 continue;
3a6ce860
LP
461
462 r = image_make(name, dirfd(d), path, raw, &st, ret);
463
464 } else {
465 if (!S_ISDIR(st.st_mode) && !S_ISBLK(st.st_mode))
466 continue;
467
468 r = image_make(name, dirfd(d), path, name, &st, ret);
5fc7f358 469 }
3a6ce860
LP
470 if (IN_SET(r, -ENOENT, -EMEDIUMTYPE))
471 continue;
c2ce6a3d
LP
472 if (r < 0)
473 return r;
cd61c3bf 474
cf604fd4
LP
475 if (ret)
476 (*ret)->discoverable = true;
477
c2ce6a3d
LP
478 return 1;
479 }
480
cf604fd4
LP
481 if (class == IMAGE_MACHINE && streq(name, ".host")) {
482 r = image_make(".host", AT_FDCWD, NULL, "/", NULL, ret);
483 if (r < 0)
484 return r;
485
486 if (ret)
487 (*ret)->discoverable = true;
488
489 return r;
490 }
5fc7f358 491
3a6ce860 492 return -ENOENT;
c2ce6a3d
LP
493};
494
2ddf182b 495int image_from_path(const char *path, Image **ret) {
cf604fd4
LP
496
497 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
498 * the image is in the image search path. And if it is we don't know if the path we used is actually not
3fe91079 499 * overridden by another, different image earlier in the search path */
cf604fd4 500
2ddf182b
LP
501 if (path_equal(path, "/"))
502 return image_make(".host", AT_FDCWD, NULL, "/", NULL, ret);
503
504 return image_make(NULL, AT_FDCWD, NULL, path, NULL, ret);
505}
506
507int image_find_harder(ImageClass class, const char *name_or_path, Image **ret) {
508 if (image_name_is_valid(name_or_path))
509 return image_find(class, name_or_path, ret);
510
511 return image_from_path(name_or_path, ret);
512}
513
5ef46e5f 514int image_discover(ImageClass class, Hashmap *h) {
c2ce6a3d
LP
515 const char *path;
516 int r;
517
5ef46e5f
LP
518 assert(class >= 0);
519 assert(class < _IMAGE_CLASS_MAX);
c2ce6a3d
LP
520 assert(h);
521
5ef46e5f 522 NULSTR_FOREACH(path, image_search_path[class]) {
c2ce6a3d
LP
523 _cleanup_closedir_ DIR *d = NULL;
524 struct dirent *de;
525
526 d = opendir(path);
527 if (!d) {
528 if (errno == ENOENT)
a67a4c8c 529 continue;
c2ce6a3d
LP
530
531 return -errno;
532 }
533
534 FOREACH_DIRENT_ALL(de, d, return -errno) {
535 _cleanup_(image_unrefp) Image *image = NULL;
1bf36bf9 536 _cleanup_free_ char *truncated = NULL;
3a6ce860
LP
537 const char *pretty;
538 struct stat st;
c2ce6a3d 539
1bf36bf9 540 if (dot_or_dot_dot(de->d_name))
c2ce6a3d
LP
541 continue;
542
3a6ce860
LP
543 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
544 * to symlink block devices into the search path */
545 if (fstatat(dirfd(d), de->d_name, &st, 0) < 0) {
546 if (errno == ENOENT)
547 continue;
548
549 return -errno;
550 }
551
552 if (S_ISREG(st.st_mode)) {
553 const char *e;
554
555 e = endswith(de->d_name, ".raw");
556 if (!e)
557 continue;
558
1bf36bf9
LP
559 truncated = strndup(de->d_name, e - de->d_name);
560 if (!truncated)
561 return -ENOMEM;
562
563 pretty = truncated;
3a6ce860 564 } else if (S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode))
1bf36bf9 565 pretty = de->d_name;
3a6ce860
LP
566 else
567 continue;
1bf36bf9
LP
568
569 if (!image_name_is_valid(pretty))
570 continue;
571
572 if (hashmap_contains(h, pretty))
c2ce6a3d
LP
573 continue;
574
3a6ce860
LP
575 r = image_make(pretty, dirfd(d), path, de->d_name, &st, &image);
576 if (IN_SET(r, -ENOENT, -EMEDIUMTYPE))
c2ce6a3d
LP
577 continue;
578 if (r < 0)
579 return r;
580
cf604fd4
LP
581 image->discoverable = true;
582
c2ce6a3d
LP
583 r = hashmap_put(h, image->name, image);
584 if (r < 0)
585 return r;
586
587 image = NULL;
cd61c3bf
LP
588 }
589 }
590
5ef46e5f 591 if (class == IMAGE_MACHINE && !hashmap_contains(h, ".host")) {
5fc7f358
LP
592 _cleanup_(image_unrefp) Image *image = NULL;
593
3a6ce860 594 r = image_make(".host", AT_FDCWD, NULL, "/", NULL, &image);
5fc7f358
LP
595 if (r < 0)
596 return r;
597
cf604fd4
LP
598 image->discoverable = true;
599
5fc7f358
LP
600 r = hashmap_put(h, image->name, image);
601 if (r < 0)
602 return r;
603
604 image = NULL;
5fc7f358
LP
605 }
606
cd61c3bf
LP
607 return 0;
608}
609
08682124 610int image_remove(Image *i) {
8e766630 611 _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
8e0b6570 612 _cleanup_strv_free_ char **settings = NULL;
bafbac4e 613 _cleanup_free_ char *roothash = NULL;
8e0b6570 614 char **j;
30535c16
LP
615 int r;
616
08682124
LP
617 assert(i);
618
d94c2b06 619 if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i))
08682124
LP
620 return -EROFS;
621
8e0b6570
LP
622 settings = image_settings_path(i);
623 if (!settings)
624 return -ENOMEM;
625
bafbac4e
LP
626 roothash = image_roothash_path(i);
627 if (!roothash)
628 return -ENOMEM;
629
30535c16
LP
630 /* Make sure we don't interfere with a running nspawn */
631 r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
632 if (r < 0)
633 return r;
634
ebd93cb6
LP
635 switch (i->type) {
636
637 case IMAGE_SUBVOLUME:
9fb0b9c7
LP
638
639 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
640 * big guns */
641 if (unlink(i->path) < 0) {
642 r = btrfs_subvol_remove(i->path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
643 if (r < 0)
644 return r;
645 }
646
8e0b6570 647 break;
ebd93cb6
LP
648
649 case IMAGE_DIRECTORY:
01b72568 650 /* Allow deletion of read-only directories */
db9a4254 651 (void) chattr_path(i->path, 0, FS_IMMUTABLE_FL, NULL);
8e0b6570
LP
652 r = rm_rf(i->path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
653 if (r < 0)
654 return r;
655
656 break;
01b72568 657
eb38edce
LP
658 case IMAGE_BLOCK:
659
660 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
661 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
662 * the thing (it's most likely a symlink after all). */
663
664 if (path_startswith(i->path, "/dev"))
665 break;
666
4831981d 667 _fallthrough_;
aceac2f0 668 case IMAGE_RAW:
41d1ed05
LP
669 if (unlink(i->path) < 0)
670 return -errno;
8e0b6570 671 break;
ebd93cb6
LP
672
673 default:
15411c0c 674 return -EOPNOTSUPP;
ebd93cb6 675 }
8e0b6570
LP
676
677 STRV_FOREACH(j, settings) {
678 if (unlink(*j) < 0 && errno != ENOENT)
679 log_debug_errno(errno, "Failed to unlink %s, ignoring: %m", *j);
680 }
681
bafbac4e
LP
682 if (unlink(roothash) < 0 && errno != ENOENT)
683 log_debug_errno(errno, "Failed to unlink %s, ignoring: %m", roothash);
684
8e0b6570
LP
685 return 0;
686}
687
bafbac4e 688static int rename_auxiliary_file(const char *path, const char *new_name, const char *suffix) {
8e0b6570
LP
689 _cleanup_free_ char *rs = NULL;
690 const char *fn;
691
bafbac4e 692 fn = strjoina(new_name, suffix);
8e0b6570
LP
693
694 rs = file_in_same_dir(path, fn);
695 if (!rs)
696 return -ENOMEM;
697
698 return rename_noreplace(AT_FDCWD, path, AT_FDCWD, rs);
ebd93cb6
LP
699}
700
701int image_rename(Image *i, const char *new_name) {
8e766630 702 _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT, name_lock = LOCK_FILE_INIT;
bafbac4e 703 _cleanup_free_ char *new_path = NULL, *nn = NULL, *roothash = NULL;
8e0b6570 704 _cleanup_strv_free_ char **settings = NULL;
01b72568 705 unsigned file_attr = 0;
8e0b6570 706 char **j;
ebd93cb6
LP
707 int r;
708
709 assert(i);
710
711 if (!image_name_is_valid(new_name))
712 return -EINVAL;
713
d94c2b06 714 if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i))
ebd93cb6
LP
715 return -EROFS;
716
8e0b6570
LP
717 settings = image_settings_path(i);
718 if (!settings)
719 return -ENOMEM;
720
bafbac4e
LP
721 roothash = image_roothash_path(i);
722 if (!roothash)
723 return -ENOMEM;
724
30535c16
LP
725 /* Make sure we don't interfere with a running nspawn */
726 r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
727 if (r < 0)
728 return r;
729
730 /* Make sure nobody takes the new name, between the time we
731 * checked it is currently unused in all search paths, and the
f8e2f4d6 732 * time we take possession of it */
30535c16
LP
733 r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock);
734 if (r < 0)
735 return r;
736
5ef46e5f 737 r = image_find(IMAGE_MACHINE, new_name, NULL);
3a6ce860 738 if (r >= 0)
ebd93cb6 739 return -EEXIST;
3a6ce860
LP
740 if (r != -ENOENT)
741 return r;
ebd93cb6
LP
742
743 switch (i->type) {
744
ebd93cb6 745 case IMAGE_DIRECTORY:
01b72568
LP
746 /* Turn of the immutable bit while we rename the image, so that we can rename it */
747 (void) read_attr_path(i->path, &file_attr);
748
749 if (file_attr & FS_IMMUTABLE_FL)
db9a4254 750 (void) chattr_path(i->path, 0, FS_IMMUTABLE_FL, NULL);
01b72568 751
4831981d 752 _fallthrough_;
01b72568 753 case IMAGE_SUBVOLUME:
ebd93cb6
LP
754 new_path = file_in_same_dir(i->path, new_name);
755 break;
756
eb38edce
LP
757 case IMAGE_BLOCK:
758
759 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
760 if (path_startswith(i->path, "/dev"))
761 return -EROFS;
762
763 new_path = file_in_same_dir(i->path, new_name);
764 break;
765
aceac2f0 766 case IMAGE_RAW: {
ebd93cb6
LP
767 const char *fn;
768
63c372cb 769 fn = strjoina(new_name, ".raw");
ebd93cb6
LP
770 new_path = file_in_same_dir(i->path, fn);
771 break;
772 }
773
774 default:
15411c0c 775 return -EOPNOTSUPP;
ebd93cb6
LP
776 }
777
778 if (!new_path)
779 return -ENOMEM;
780
781 nn = strdup(new_name);
782 if (!nn)
783 return -ENOMEM;
784
f85ef957
AC
785 r = rename_noreplace(AT_FDCWD, i->path, AT_FDCWD, new_path);
786 if (r < 0)
787 return r;
ebd93cb6 788
01b72568
LP
789 /* Restore the immutable bit, if it was set before */
790 if (file_attr & FS_IMMUTABLE_FL)
db9a4254 791 (void) chattr_path(new_path, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL, NULL);
01b72568 792
f9ecfd3b
DL
793 free_and_replace(i->path, new_path);
794 free_and_replace(i->name, nn);
ebd93cb6 795
8e0b6570 796 STRV_FOREACH(j, settings) {
bafbac4e 797 r = rename_auxiliary_file(*j, new_name, ".nspawn");
8e0b6570
LP
798 if (r < 0 && r != -ENOENT)
799 log_debug_errno(r, "Failed to rename settings file %s, ignoring: %m", *j);
800 }
801
bafbac4e
LP
802 r = rename_auxiliary_file(roothash, new_name, ".roothash");
803 if (r < 0 && r != -ENOENT)
804 log_debug_errno(r, "Failed to rename roothash file %s, ignoring: %m", roothash);
805
ebd93cb6
LP
806 return 0;
807}
808
bafbac4e 809static int clone_auxiliary_file(const char *path, const char *new_name, const char *suffix) {
8e0b6570
LP
810 _cleanup_free_ char *rs = NULL;
811 const char *fn;
812
bafbac4e 813 fn = strjoina(new_name, suffix);
8e0b6570
LP
814
815 rs = file_in_same_dir(path, fn);
816 if (!rs)
817 return -ENOMEM;
818
8a016c74 819 return copy_file_atomic(path, rs, 0664, 0, 0, COPY_REFLINK);
8e0b6570
LP
820}
821
ebd93cb6 822int image_clone(Image *i, const char *new_name, bool read_only) {
8e766630 823 _cleanup_(release_lock_file) LockFile name_lock = LOCK_FILE_INIT;
8e0b6570 824 _cleanup_strv_free_ char **settings = NULL;
bafbac4e 825 _cleanup_free_ char *roothash = NULL;
ebd93cb6 826 const char *new_path;
8e0b6570 827 char **j;
ebd93cb6
LP
828 int r;
829
830 assert(i);
831
832 if (!image_name_is_valid(new_name))
833 return -EINVAL;
834
8e0b6570
LP
835 settings = image_settings_path(i);
836 if (!settings)
837 return -ENOMEM;
838
bafbac4e
LP
839 roothash = image_roothash_path(i);
840 if (!roothash)
841 return -ENOMEM;
842
30535c16
LP
843 /* Make sure nobody takes the new name, between the time we
844 * checked it is currently unused in all search paths, and the
f8e2f4d6 845 * time we take possession of it */
30535c16
LP
846 r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock);
847 if (r < 0)
848 return r;
849
5ef46e5f 850 r = image_find(IMAGE_MACHINE, new_name, NULL);
3a6ce860 851 if (r >= 0)
ebd93cb6 852 return -EEXIST;
3a6ce860
LP
853 if (r != -ENOENT)
854 return r;
ebd93cb6
LP
855
856 switch (i->type) {
857
858 case IMAGE_SUBVOLUME:
859 case IMAGE_DIRECTORY:
9a50e3ca 860 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
13e785f7 861 * directory. */
9a50e3ca 862
63c372cb 863 new_path = strjoina("/var/lib/machines/", new_name);
ebd93cb6 864
17cbb288
LP
865 r = btrfs_subvol_snapshot(i->path, new_path,
866 (read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) |
867 BTRFS_SNAPSHOT_FALLBACK_COPY |
868 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY |
869 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE |
870 BTRFS_SNAPSHOT_RECURSIVE |
871 BTRFS_SNAPSHOT_QUOTA);
872 if (r >= 0)
9a50e3ca 873 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
8120ee28 874 (void) btrfs_subvol_auto_qgroup(new_path, 0, true);
5bcd08db 875
ebd93cb6
LP
876 break;
877
aceac2f0 878 case IMAGE_RAW:
63c372cb 879 new_path = strjoina("/var/lib/machines/", new_name, ".raw");
ebd93cb6 880
8a016c74 881 r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, FS_NOCOW_FL, FS_NOCOW_FL, COPY_REFLINK|COPY_CRTIME);
ebd93cb6
LP
882 break;
883
eb38edce 884 case IMAGE_BLOCK:
ebd93cb6 885 default:
15411c0c 886 return -EOPNOTSUPP;
ebd93cb6
LP
887 }
888
889 if (r < 0)
890 return r;
891
8e0b6570 892 STRV_FOREACH(j, settings) {
bafbac4e 893 r = clone_auxiliary_file(*j, new_name, ".nspawn");
8e0b6570
LP
894 if (r < 0 && r != -ENOENT)
895 log_debug_errno(r, "Failed to clone settings %s, ignoring: %m", *j);
896 }
897
bafbac4e
LP
898 r = clone_auxiliary_file(roothash, new_name, ".roothash");
899 if (r < 0 && r != -ENOENT)
900 log_debug_errno(r, "Failed to clone root hash file %s, ignoring: %m", roothash);
901
ebd93cb6
LP
902 return 0;
903}
904
905int image_read_only(Image *i, bool b) {
8e766630 906 _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
ebd93cb6 907 int r;
c7664c07 908
ebd93cb6
LP
909 assert(i);
910
d94c2b06 911 if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i))
ebd93cb6
LP
912 return -EROFS;
913
30535c16
LP
914 /* Make sure we don't interfere with a running nspawn */
915 r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
916 if (r < 0)
917 return r;
918
ebd93cb6
LP
919 switch (i->type) {
920
921 case IMAGE_SUBVOLUME:
5bcd08db
LP
922
923 /* Note that we set the flag only on the top-level
924 * subvolume of the image. */
925
ebd93cb6
LP
926 r = btrfs_subvol_set_read_only(i->path, b);
927 if (r < 0)
928 return r;
01b72568
LP
929
930 break;
931
932 case IMAGE_DIRECTORY:
933 /* For simple directory trees we cannot use the access
934 mode of the top-level directory, since it has an
935 effect on the container itself. However, we can
936 use the "immutable" flag, to at least make the
937 top-level directory read-only. It's not as good as
938 a read-only subvolume, but at least something, and
13e785f7 939 we can read the value back. */
01b72568 940
db9a4254 941 r = chattr_path(i->path, b ? FS_IMMUTABLE_FL : 0, FS_IMMUTABLE_FL, NULL);
01b72568
LP
942 if (r < 0)
943 return r;
944
ebd93cb6
LP
945 break;
946
aceac2f0 947 case IMAGE_RAW: {
ebd93cb6
LP
948 struct stat st;
949
950 if (stat(i->path, &st) < 0)
951 return -errno;
952
953 if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0)
954 return -errno;
f2068bcc
LP
955
956 /* If the images is now read-only, it's a good time to
957 * defrag it, given that no write patterns will
958 * fragment it again. */
959 if (b)
960 (void) btrfs_defrag(i->path);
ebd93cb6
LP
961 break;
962 }
963
eb38edce
LP
964 case IMAGE_BLOCK: {
965 _cleanup_close_ int fd = -1;
966 struct stat st;
967 int state = b;
968
969 fd = open(i->path, O_CLOEXEC|O_RDONLY|O_NONBLOCK|O_NOCTTY);
970 if (fd < 0)
971 return -errno;
972
973 if (fstat(fd, &st) < 0)
974 return -errno;
975 if (!S_ISBLK(st.st_mode))
976 return -ENOTTY;
977
978 if (ioctl(fd, BLKROSET, &state) < 0)
979 return -errno;
980
981 break;
982 }
983
ebd93cb6 984 default:
15411c0c 985 return -EOPNOTSUPP;
ebd93cb6
LP
986 }
987
988 return 0;
08682124
LP
989}
990
30535c16
LP
991int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local) {
992 _cleanup_free_ char *p = NULL;
993 LockFile t = LOCK_FILE_INIT;
994 struct stat st;
f25bed67 995 bool exclusive;
30535c16
LP
996 int r;
997
998 assert(path);
999 assert(global);
1000 assert(local);
1001
f25bed67
LP
1002 /* Locks an image path. This actually creates two locks: one "local" one, next to the image path
1003 * itself, which might be shared via NFS. And another "global" one, in /run, that uses the
1004 * device/inode number. This has the benefit that we can even lock a tree that is a mount point,
1005 * correctly. */
30535c16 1006
30535c16
LP
1007 if (!path_is_absolute(path))
1008 return -EINVAL;
1009
f25bed67
LP
1010 switch (operation & (LOCK_SH|LOCK_EX)) {
1011 case LOCK_SH:
1012 exclusive = false;
1013 break;
1014 case LOCK_EX:
1015 exclusive = true;
1016 break;
1017 default:
1018 return -EINVAL;
1019 }
1020
b6e953f2
LP
1021 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1022 *local = *global = (LockFile) LOCK_FILE_INIT;
1023 return 0;
1024 }
1025
f25bed67
LP
1026 /* Prohibit taking exclusive locks on the host image. We can't allow this, since we ourselves are
1027 * running off it after all, and we don't want any images to manipulate the host image. We make an
1028 * exception for shared locks however: we allow those (and make them NOPs since there's no point in
1029 * taking them if there can't be exclusive locks). Strictly speaking these are questionable as well,
1030 * since it means changes made to the host might propagate to the container as they happen (and a
1031 * shared lock kinda suggests that no changes happen at all while it is in place), but it's too
1032 * useful not to allow read-only containers off the host root, hence let's support this, and trust
1033 * the user to do the right thing with this. */
1034 if (path_equal(path, "/")) {
1035 if (exclusive)
1036 return -EBUSY;
1037
1038 *local = *global = (LockFile) LOCK_FILE_INIT;
1039 return 0;
1040 }
b6e953f2 1041
30535c16 1042 if (stat(path, &st) >= 0) {
eb38edce
LP
1043 if (S_ISBLK(st.st_mode))
1044 r = asprintf(&p, "/run/systemd/nspawn/locks/block-%u:%u", major(st.st_rdev), minor(st.st_rdev));
1045 else if (S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))
1046 r = asprintf(&p, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino);
1047 else
1048 return -ENOTTY;
1049
1050 if (r < 0)
30535c16
LP
1051 return -ENOMEM;
1052 }
1053
f25bed67
LP
1054 /* For block devices we don't need the "local" lock, as the major/minor lock above should be
1055 * sufficient, since block devices are host local anyway. */
1056 if (!path_startswith(path, "/dev/")) {
eb38edce 1057 r = make_lock_file_for(path, operation, &t);
8be17c9b 1058 if (r < 0) {
f25bed67 1059 if (!exclusive && r == -EROFS)
771b7ead 1060 log_debug_errno(r, "Failed to create shared lock for '%s', ignoring: %m", path);
8be17c9b
LT
1061 else
1062 return r;
1063 }
eb38edce 1064 }
30535c16
LP
1065
1066 if (p) {
6e5dcce4 1067 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
30535c16
LP
1068
1069 r = make_lock_file(p, operation, global);
1070 if (r < 0) {
1071 release_lock_file(&t);
1072 return r;
1073 }
546dbec5
LP
1074 } else
1075 *global = (LockFile) LOCK_FILE_INIT;
30535c16
LP
1076
1077 *local = t;
1078 return 0;
1079}
1080
cb81cd80 1081int image_set_limit(Image *i, uint64_t referenced_max) {
d6ce17c7
LP
1082 assert(i);
1083
d94c2b06 1084 if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i))
d6ce17c7
LP
1085 return -EROFS;
1086
1087 if (i->type != IMAGE_SUBVOLUME)
15411c0c 1088 return -EOPNOTSUPP;
d6ce17c7 1089
5bcd08db
LP
1090 /* We set the quota both for the subvolume as well as for the
1091 * subtree. The latter is mostly for historical reasons, since
1092 * we didn't use to have a concept of subtree quota, and hence
1093 * only modified the subvolume quota. */
1094
1095 (void) btrfs_qgroup_set_limit(i->path, 0, referenced_max);
1096 (void) btrfs_subvol_auto_qgroup(i->path, 0, true);
1097 return btrfs_subvol_set_subtree_quota_limit(i->path, 0, referenced_max);
d6ce17c7
LP
1098}
1099
c7664c07 1100int image_read_metadata(Image *i) {
8e766630 1101 _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
c7664c07
LP
1102 int r;
1103
1104 assert(i);
1105
1106 r = image_path_lock(i->path, LOCK_SH|LOCK_NB, &global_lock, &local_lock);
1107 if (r < 0)
1108 return r;
1109
1110 switch (i->type) {
1111
1112 case IMAGE_SUBVOLUME:
1113 case IMAGE_DIRECTORY: {
1114 _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL;
1115 sd_id128_t machine_id = SD_ID128_NULL;
1116 _cleanup_free_ char *hostname = NULL;
1117 _cleanup_free_ char *path = NULL;
1118
a5648b80 1119 r = chase_symlinks("/etc/hostname", i->path, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &path, NULL);
c7664c07
LP
1120 if (r < 0 && r != -ENOENT)
1121 log_debug_errno(r, "Failed to chase /etc/hostname in image %s: %m", i->name);
1122 else if (r >= 0) {
1123 r = read_etc_hostname(path, &hostname);
1124 if (r < 0)
1125 log_debug_errno(errno, "Failed to read /etc/hostname of image %s: %m", i->name);
1126 }
1127
1128 path = mfree(path);
1129
a5648b80 1130 r = chase_symlinks("/etc/machine-id", i->path, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &path, NULL);
c7664c07
LP
1131 if (r < 0 && r != -ENOENT)
1132 log_debug_errno(r, "Failed to chase /etc/machine-id in image %s: %m", i->name);
1133 else if (r >= 0) {
1134 _cleanup_close_ int fd = -1;
1135
1136 fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
1137 if (fd < 0)
1138 log_debug_errno(errno, "Failed to open %s: %m", path);
1139 else {
1140 r = id128_read_fd(fd, ID128_PLAIN, &machine_id);
1141 if (r < 0)
1142 log_debug_errno(r, "Image %s contains invalid machine ID.", i->name);
1143 }
1144 }
1145
1146 path = mfree(path);
1147
a5648b80 1148 r = chase_symlinks("/etc/machine-info", i->path, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &path, NULL);
c7664c07
LP
1149 if (r < 0 && r != -ENOENT)
1150 log_debug_errno(r, "Failed to chase /etc/machine-info in image %s: %m", i->name);
1151 else if (r >= 0) {
aa8fbc74 1152 r = load_env_file_pairs(NULL, path, &machine_info);
c7664c07
LP
1153 if (r < 0)
1154 log_debug_errno(r, "Failed to parse machine-info data of %s: %m", i->name);
1155 }
1156
d58ad743
LP
1157 r = load_os_release_pairs(i->path, &os_release);
1158 if (r < 0)
1159 log_debug_errno(r, "Failed to read os-release in image, ignoring: %m");
c7664c07
LP
1160
1161 free_and_replace(i->hostname, hostname);
1162 i->machine_id = machine_id;
1163 strv_free_and_replace(i->machine_info, machine_info);
1164 strv_free_and_replace(i->os_release, os_release);
1165
1166 break;
1167 }
1168
1169 case IMAGE_RAW:
1170 case IMAGE_BLOCK: {
1171 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
1172 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
1173
e08f94ac 1174 r = loop_device_make_by_path(i->path, O_RDONLY, LO_FLAGS_PARTSCAN, &d);
c7664c07
LP
1175 if (r < 0)
1176 return r;
1177
89e62e0b 1178 r = dissect_image(d->fd, NULL, NULL, DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_RELAX_VAR_CHECK, &m);
c7664c07
LP
1179 if (r < 0)
1180 return r;
1181
1182 r = dissected_image_acquire_metadata(m);
1183 if (r < 0)
1184 return r;
1185
1186 free_and_replace(i->hostname, m->hostname);
1187 i->machine_id = m->machine_id;
1188 strv_free_and_replace(i->machine_info, m->machine_info);
1189 strv_free_and_replace(i->os_release, m->os_release);
1190
1191 break;
1192 }
1193
1194 default:
1195 return -EOPNOTSUPP;
1196 }
1197
1198 i->metadata_valid = true;
1199
1200 return 0;
1201}
1202
30535c16 1203int image_name_lock(const char *name, int operation, LockFile *ret) {
30535c16
LP
1204 assert(name);
1205 assert(ret);
1206
1207 /* Locks an image name, regardless of the precise path used. */
1208
1209 if (!image_name_is_valid(name))
1210 return -EINVAL;
1211
b6e953f2
LP
1212 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1213 *ret = (LockFile) LOCK_FILE_INIT;
1214 return 0;
1215 }
1216
30535c16
LP
1217 if (streq(name, ".host"))
1218 return -EBUSY;
1219
6e5dcce4
ZJS
1220 const char *p = strjoina("/run/systemd/nspawn/locks/name-", name);
1221 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
30535c16
LP
1222 return make_lock_file(p, operation, ret);
1223}
1224
1225bool image_name_is_valid(const char *s) {
1226 if (!filename_is_valid(s))
1227 return false;
1228
1229 if (string_has_cc(s, NULL))
1230 return false;
1231
1232 if (!utf8_is_valid(s))
1233 return false;
1234
1235 /* Temporary files for atomically creating new files */
1236 if (startswith(s, ".#"))
1237 return false;
1238
1239 return true;
1240}
1241
ace9ab19
LP
1242bool image_in_search_path(ImageClass class, const char *image) {
1243 const char *path;
1244
1245 assert(image);
1246
1247 NULSTR_FOREACH(path, image_search_path[class]) {
1248 const char *p;
1249 size_t k;
1250
1251 p = path_startswith(image, path);
1252 if (!p)
1253 continue;
1254
1255 /* Make sure there's a filename following */
1256 k = strcspn(p, "/");
1257 if (k == 0)
1258 continue;
1259
1260 p += k;
1261
1262 /* Accept trailing slashes */
1263 if (p[strspn(p, "/")] == 0)
1264 return true;
1265
1266 }
1267
1268 return false;
1269}
1270
cd61c3bf
LP
1271static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
1272 [IMAGE_DIRECTORY] = "directory",
1273 [IMAGE_SUBVOLUME] = "subvolume",
aceac2f0 1274 [IMAGE_RAW] = "raw",
eb38edce 1275 [IMAGE_BLOCK] = "block",
cd61c3bf
LP
1276};
1277
1278DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);