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