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