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