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