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