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