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