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