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