1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <linux/loop.h>
7 #include <linux/magic.h>
11 #include <sys/ioctl.h>
15 #include "alloc-util.h"
16 #include "btrfs-util.h"
18 #include "chattr-util.h"
20 #include "dirent-util.h"
21 #include "discover-image.h"
22 #include "dissect-image.h"
25 #include "extension-util.h"
29 #include "hostname-setup.h"
30 #include "id128-util.h"
31 #include "lock-util.h"
33 #include "loop-util.h"
36 #include "nulstr-util.h"
38 #include "path-util.h"
40 #include "stat-util.h"
41 #include "string-table.h"
42 #include "string-util.h"
44 #include "time-util.h"
46 #include "xattr-util.h"
48 static const char* const image_search_path
[_IMAGE_CLASS_MAX
] = {
49 [IMAGE_MACHINE
] = "/etc/machines\0" /* only place symlinks here */
50 "/run/machines\0" /* and here too */
51 "/var/lib/machines\0" /* the main place for images */
52 "/var/lib/container\0" /* legacy */
53 "/usr/local/lib/machines\0"
54 "/usr/lib/machines\0",
56 [IMAGE_PORTABLE
] = "/etc/portables\0" /* only place symlinks here */
57 "/run/portables\0" /* and here too */
58 "/var/lib/portables\0" /* the main place for images */
59 "/usr/local/lib/portables\0"
60 "/usr/lib/portables\0",
62 /* Note that we don't allow storing extensions under /usr/, unlike with other image types. That's
63 * because extension images are supposed to extend /usr/, so you get into recursive races, especially
64 * with directory-based extensions, as the kernel's OverlayFS explicitly checks for this and errors
65 * out with -ELOOP if it finds that a lowerdir= is a child of another lowerdir=. */
66 [IMAGE_EXTENSION
] = "/etc/extensions\0" /* only place symlinks here */
67 "/run/extensions\0" /* and here too */
68 "/var/lib/extensions\0", /* the main place for images */
71 static Image
*image_free(Image
*i
) {
78 strv_free(i
->machine_info
);
79 strv_free(i
->os_release
);
80 strv_free(i
->extension_release
);
85 DEFINE_TRIVIAL_REF_UNREF_FUNC(Image
, image
, image_free
);
86 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(image_hash_ops
, char, string_hash_func
, string_compare_func
,
89 static char **image_settings_path(Image
*image
) {
90 _cleanup_strv_free_
char **l
= NULL
;
91 _cleanup_free_
char *fn
= NULL
;
101 fn
= strjoin(image
->name
, ".nspawn");
105 FOREACH_STRING(s
, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
106 l
[i
] = path_join(s
, fn
);
113 r
= file_in_same_dir(image
->path
, fn
, l
+ i
);
117 log_debug_errno(r
, "Failed to generate .nspawn settings path from image path, ignoring: %m");
124 static int image_roothash_path(Image
*image
, char **ret
) {
125 _cleanup_free_
char *fn
= NULL
;
129 fn
= strjoin(image
->name
, ".roothash");
133 return file_in_same_dir(image
->path
, fn
, ret
);
136 static int image_new(
141 const char *filename
,
147 _cleanup_(image_unrefp
) Image
*i
= NULL
;
150 assert(t
< _IMAGE_TYPE_MAX
);
163 .read_only
= read_only
,
167 .usage_exclusive
= UINT64_MAX
,
169 .limit_exclusive
= UINT64_MAX
,
172 i
->name
= strdup(pretty
);
176 i
->path
= path_join(path
, filename
);
180 path_simplify(i
->path
);
187 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
188 _cleanup_free_
char *name
= NULL
;
194 p
= last_path_component(path
);
196 name
= strdupcspn(p
, "/");
203 e
= endswith(name
, suffix
);
210 if (!image_name_is_valid(name
))
213 *ret
= TAKE_PTR(name
);
217 static int image_make(
222 const char *filename
,
223 const struct stat
*st
,
226 _cleanup_free_
char *pretty_buffer
= NULL
, *parent
= NULL
;
231 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
232 assert(path
|| dfd
== AT_FDCWD
);
235 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
236 * devices into /var/lib/machines/, and treat them normally.
238 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
242 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
250 (void) safe_getcwd(&parent
);
252 (void) fd_get_path(dfd
, &parent
);
256 (path
&& path_startswith(path
, "/usr")) ||
257 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
259 if (S_ISDIR(st
->st_mode
)) {
260 _cleanup_close_
int fd
= -EBADF
;
261 unsigned file_attr
= 0;
268 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
272 pretty
= pretty_buffer
;
275 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
279 if (btrfs_might_be_subvol(st
)) {
281 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
285 BtrfsSubvolInfo info
;
287 /* It's a btrfs subvolume */
289 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
293 r
= image_new(IMAGE_SUBVOLUME
,
298 info
.read_only
|| read_only
,
305 if (btrfs_quota_scan_ongoing(fd
) == 0) {
306 BtrfsQuotaInfo quota
;
308 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
310 (*ret
)->usage
= quota
.referenced
;
311 (*ret
)->usage_exclusive
= quota
.exclusive
;
313 (*ret
)->limit
= quota
.referenced_max
;
314 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
322 /* Get directory creation time (not available everywhere, but that's OK */
323 (void) fd_getcrtime(fd
, &crtime
);
325 /* If the IMMUTABLE bit is set, we consider the directory read-only. Since the ioctl is not
326 * supported everywhere we ignore failures. */
327 (void) read_attr_fd(fd
, &file_attr
);
329 /* It's just a normal directory. */
330 r
= image_new(IMAGE_DIRECTORY
,
335 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
337 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 */
344 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
347 /* It's a RAW disk image */
352 (void) fd_getcrtime_at(dfd
, filename
, AT_SYMLINK_FOLLOW
, &crtime
);
355 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
359 pretty
= pretty_buffer
;
362 r
= image_new(IMAGE_RAW
,
367 !(st
->st_mode
& 0222) || read_only
,
369 timespec_load(&st
->st_mtim
),
374 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
375 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
379 } else if (S_ISBLK(st
->st_mode
)) {
380 _cleanup_close_
int block_fd
= -EBADF
;
381 uint64_t size
= UINT64_MAX
;
389 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
393 pretty
= pretty_buffer
;
396 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
398 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
400 /* Refresh stat data after opening the node */
401 if (fstat(block_fd
, &stbuf
) < 0)
405 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
411 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
412 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
417 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
418 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
420 block_fd
= safe_close(block_fd
);
423 r
= image_new(IMAGE_BLOCK
,
428 !(st
->st_mode
& 0222) || read_only
,
435 if (!IN_SET(size
, 0, UINT64_MAX
))
436 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
444 int image_find(ImageClass
class,
452 assert(class < _IMAGE_CLASS_MAX
);
455 /* There are no images with invalid names */
456 if (!image_name_is_valid(name
))
459 NULSTR_FOREACH(path
, image_search_path
[class]) {
460 _cleanup_free_
char *resolved
= NULL
;
461 _cleanup_closedir_
DIR *d
= NULL
;
465 r
= chase_and_opendir(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, &d
);
471 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
472 * to symlink block devices into the search path. (For now, we disable that when operating
473 * relative to some root directory.) */
474 flags
= root
? AT_SYMLINK_NOFOLLOW
: 0;
475 if (fstatat(dirfd(d
), name
, &st
, flags
) < 0) {
476 _cleanup_free_
char *raw
= NULL
;
481 raw
= strjoin(name
, ".raw");
485 if (fstatat(dirfd(d
), raw
, &st
, flags
) < 0) {
492 if (!S_ISREG(st
.st_mode
))
495 r
= image_make(class, name
, dirfd(d
), resolved
, raw
, &st
, ret
);
498 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
501 r
= image_make(class, name
, dirfd(d
), resolved
, name
, &st
, ret
);
503 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
509 (*ret
)->discoverable
= true;
514 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
515 r
= image_make(class, ".host", AT_FDCWD
, NULL
, empty_to_root(root
), NULL
, ret
);
520 (*ret
)->discoverable
= true;
528 int image_from_path(const char *path
, Image
**ret
) {
530 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
531 * the image is in the image search path. And if it is we don't know if the path we used is actually not
532 * overridden by another, different image earlier in the search path */
534 if (path_equal(path
, "/"))
535 return image_make(IMAGE_MACHINE
, ".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
537 return image_make(_IMAGE_CLASS_INVALID
, NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
540 int image_find_harder(ImageClass
class, const char *name_or_path
, const char *root
, Image
**ret
) {
541 if (image_name_is_valid(name_or_path
))
542 return image_find(class, name_or_path
, root
, ret
);
544 return image_from_path(name_or_path
, ret
);
555 assert(class < _IMAGE_CLASS_MAX
);
558 NULSTR_FOREACH(path
, image_search_path
[class]) {
559 _cleanup_free_
char *resolved
= NULL
;
560 _cleanup_closedir_
DIR *d
= NULL
;
562 r
= chase_and_opendir(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, &d
);
568 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
569 _cleanup_(image_unrefp
) Image
*image
= NULL
;
570 _cleanup_free_
char *truncated
= NULL
;
575 if (dot_or_dot_dot(de
->d_name
))
578 /* As mentioned above, we follow symlinks on this fstatat(), because we want to
579 * permit people to symlink block devices into the search path. */
580 flags
= root
? AT_SYMLINK_NOFOLLOW
: 0;
581 if (fstatat(dirfd(d
), de
->d_name
, &st
, flags
) < 0) {
588 if (S_ISREG(st
.st_mode
)) {
591 e
= endswith(de
->d_name
, ".raw");
595 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
600 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
605 if (!image_name_is_valid(pretty
))
608 if (hashmap_contains(h
, pretty
))
611 r
= image_make(class, pretty
, dirfd(d
), resolved
, de
->d_name
, &st
, &image
);
612 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
617 image
->discoverable
= true;
619 r
= hashmap_put(h
, image
->name
, image
);
627 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
628 _cleanup_(image_unrefp
) Image
*image
= NULL
;
630 r
= image_make(IMAGE_MACHINE
, ".host", AT_FDCWD
, NULL
, empty_to_root("/"), NULL
, &image
);
634 image
->discoverable
= true;
636 r
= hashmap_put(h
, image
->name
, image
);
646 int image_remove(Image
*i
) {
647 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
648 _cleanup_strv_free_
char **settings
= NULL
;
649 _cleanup_free_
char *roothash
= NULL
;
654 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
657 settings
= image_settings_path(i
);
661 r
= image_roothash_path(i
, &roothash
);
665 /* Make sure we don't interfere with a running nspawn */
666 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
672 case IMAGE_SUBVOLUME
:
674 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
676 if (unlink(i
->path
) < 0) {
677 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
684 case IMAGE_DIRECTORY
:
685 /* Allow deletion of read-only directories */
686 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
687 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
695 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
696 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
697 * the thing (it's most likely a symlink after all). */
699 if (path_startswith(i
->path
, "/dev"))
704 if (unlink(i
->path
) < 0)
712 STRV_FOREACH(j
, settings
)
713 if (unlink(*j
) < 0 && errno
!= ENOENT
)
714 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
716 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
717 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
722 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
723 _cleanup_free_
char *fn
= NULL
, *rs
= NULL
;
726 fn
= strjoin(new_name
, suffix
);
730 r
= file_in_same_dir(path
, fn
, &rs
);
734 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
737 int image_rename(Image
*i
, const char *new_name
) {
738 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
739 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
740 _cleanup_strv_free_
char **settings
= NULL
;
741 unsigned file_attr
= 0;
746 if (!image_name_is_valid(new_name
))
749 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
752 settings
= image_settings_path(i
);
756 r
= image_roothash_path(i
, &roothash
);
760 /* Make sure we don't interfere with a running nspawn */
761 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
765 /* Make sure nobody takes the new name, between the time we
766 * checked it is currently unused in all search paths, and the
767 * time we take possession of it */
768 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
772 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
, NULL
);
780 case IMAGE_DIRECTORY
:
781 /* Turn of the immutable bit while we rename the image, so that we can rename it */
782 (void) read_attr_path(i
->path
, &file_attr
);
784 if (file_attr
& FS_IMMUTABLE_FL
)
785 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
788 case IMAGE_SUBVOLUME
:
789 r
= file_in_same_dir(i
->path
, new_name
, &new_path
);
794 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
795 if (path_startswith(i
->path
, "/dev"))
798 r
= file_in_same_dir(i
->path
, new_name
, &new_path
);
804 fn
= strjoina(new_name
, ".raw");
806 r
= file_in_same_dir(i
->path
, fn
, &new_path
);
816 nn
= strdup(new_name
);
820 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
824 /* Restore the immutable bit, if it was set before */
825 if (file_attr
& FS_IMMUTABLE_FL
)
826 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
828 free_and_replace(i
->path
, new_path
);
829 free_and_replace(i
->name
, nn
);
831 STRV_FOREACH(j
, settings
) {
832 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
833 if (r
< 0 && r
!= -ENOENT
)
834 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
837 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
838 if (r
< 0 && r
!= -ENOENT
)
839 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
844 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
845 _cleanup_free_
char *fn
= NULL
, *rs
= NULL
;
848 fn
= strjoin(new_name
, suffix
);
852 r
= file_in_same_dir(path
, fn
, &rs
);
856 return copy_file_atomic(path
, rs
, 0664, COPY_REFLINK
);
859 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
860 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
861 _cleanup_strv_free_
char **settings
= NULL
;
862 _cleanup_free_
char *roothash
= NULL
;
863 const char *new_path
;
868 if (!image_name_is_valid(new_name
))
871 settings
= image_settings_path(i
);
875 r
= image_roothash_path(i
, &roothash
);
879 /* Make sure nobody takes the new name, between the time we
880 * checked it is currently unused in all search paths, and the
881 * time we take possession of it */
882 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
886 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
, NULL
);
894 case IMAGE_SUBVOLUME
:
895 case IMAGE_DIRECTORY
:
896 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
899 new_path
= strjoina("/var/lib/machines/", new_name
);
901 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
902 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
903 BTRFS_SNAPSHOT_FALLBACK_COPY
|
904 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
905 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
906 BTRFS_SNAPSHOT_RECURSIVE
|
907 BTRFS_SNAPSHOT_QUOTA
);
909 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
910 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
915 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
917 r
= copy_file_atomic_full(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, FS_NOCOW_FL
,
918 COPY_REFLINK
|COPY_CRTIME
, NULL
, NULL
);
929 STRV_FOREACH(j
, settings
) {
930 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
931 if (r
< 0 && r
!= -ENOENT
)
932 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
935 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
936 if (r
< 0 && r
!= -ENOENT
)
937 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
942 int image_read_only(Image
*i
, bool b
) {
943 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
948 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
951 /* Make sure we don't interfere with a running nspawn */
952 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
958 case IMAGE_SUBVOLUME
:
960 /* Note that we set the flag only on the top-level
961 * subvolume of the image. */
963 r
= btrfs_subvol_set_read_only(i
->path
, b
);
969 case IMAGE_DIRECTORY
:
970 /* For simple directory trees we cannot use the access
971 mode of the top-level directory, since it has an
972 effect on the container itself. However, we can
973 use the "immutable" flag, to at least make the
974 top-level directory read-only. It's not as good as
975 a read-only subvolume, but at least something, and
976 we can read the value back. */
978 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
, NULL
);
987 if (stat(i
->path
, &st
) < 0)
990 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
993 /* If the images is now read-only, it's a good time to
994 * defrag it, given that no write patterns will
995 * fragment it again. */
997 (void) btrfs_defrag(i
->path
);
1002 _cleanup_close_
int fd
= -EBADF
;
1006 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
1010 if (fstat(fd
, &st
) < 0)
1012 if (!S_ISBLK(st
.st_mode
))
1015 if (ioctl(fd
, BLKROSET
, &state
) < 0)
1028 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
1029 _cleanup_free_
char *p
= NULL
;
1030 LockFile t
= LOCK_FILE_INIT
;
1039 /* Locks an image path. This actually creates two locks: one "local" one, next to the image path
1040 * itself, which might be shared via NFS. And another "global" one, in /run, that uses the
1041 * device/inode number. This has the benefit that we can even lock a tree that is a mount point,
1044 if (!path_is_absolute(path
))
1047 switch (operation
& (LOCK_SH
|LOCK_EX
)) {
1058 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1059 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1063 /* Prohibit taking exclusive locks on the host image. We can't allow this, since we ourselves are
1064 * running off it after all, and we don't want any images to manipulate the host image. We make an
1065 * exception for shared locks however: we allow those (and make them NOPs since there's no point in
1066 * taking them if there can't be exclusive locks). Strictly speaking these are questionable as well,
1067 * since it means changes made to the host might propagate to the container as they happen (and a
1068 * shared lock kinda suggests that no changes happen at all while it is in place), but it's too
1069 * useful not to allow read-only containers off the host root, hence let's support this, and trust
1070 * the user to do the right thing with this. */
1071 if (path_equal(path
, "/")) {
1075 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1079 if (stat(path
, &st
) >= 0) {
1080 if (S_ISBLK(st
.st_mode
))
1081 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1082 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1083 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1090 /* For block devices we don't need the "local" lock, as the major/minor lock above should be
1091 * sufficient, since block devices are host local anyway. */
1092 if (!path_startswith(path
, "/dev/")) {
1093 r
= make_lock_file_for(path
, operation
, &t
);
1095 if (!exclusive
&& r
== -EROFS
)
1096 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1103 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1105 r
= make_lock_file(p
, operation
, global
);
1107 release_lock_file(&t
);
1111 *global
= (LockFile
) LOCK_FILE_INIT
;
1117 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1120 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1123 if (i
->type
!= IMAGE_SUBVOLUME
)
1126 /* We set the quota both for the subvolume as well as for the
1127 * subtree. The latter is mostly for historical reasons, since
1128 * we didn't use to have a concept of subtree quota, and hence
1129 * only modified the subvolume quota. */
1131 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1132 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1133 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1136 int image_read_metadata(Image
*i
, const ImagePolicy
*image_policy
) {
1137 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1142 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1148 case IMAGE_SUBVOLUME
:
1149 case IMAGE_DIRECTORY
: {
1150 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
, **extension_release
= NULL
;
1151 sd_id128_t machine_id
= SD_ID128_NULL
;
1152 _cleanup_free_
char *hostname
= NULL
;
1153 _cleanup_free_
char *path
= NULL
;
1155 if (i
->class == IMAGE_EXTENSION
) {
1156 r
= extension_has_forbidden_content(i
->path
);
1160 return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM
),
1161 "Conflicting content found in image %s, refusing.",
1165 r
= chase("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1166 if (r
< 0 && r
!= -ENOENT
)
1167 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1169 r
= read_etc_hostname(path
, &hostname
);
1171 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1176 r
= id128_get_machine(i
->path
, &machine_id
);
1178 log_debug_errno(r
, "Failed to read machine ID in image %s, ignoring: %m", i
->name
);
1180 r
= chase("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1181 if (r
< 0 && r
!= -ENOENT
)
1182 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1184 r
= load_env_file_pairs(NULL
, path
, &machine_info
);
1186 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1189 r
= load_os_release_pairs(i
->path
, &os_release
);
1191 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1193 r
= load_extension_release_pairs(i
->path
, i
->name
, /* relax_extension_release_check= */ false, &extension_release
);
1195 log_debug_errno(r
, "Failed to read extension-release in image, ignoring: %m");
1197 free_and_replace(i
->hostname
, hostname
);
1198 i
->machine_id
= machine_id
;
1199 strv_free_and_replace(i
->machine_info
, machine_info
);
1200 strv_free_and_replace(i
->os_release
, os_release
);
1201 strv_free_and_replace(i
->extension_release
, extension_release
);
1208 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1209 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1211 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, /* sector_size= */ UINT32_MAX
, LO_FLAGS_PARTSCAN
, LOCK_SH
, &d
);
1215 r
= dissect_loop_device(
1218 /* mount_options= */ NULL
,
1220 DISSECT_IMAGE_GENERIC_ROOT
|
1221 DISSECT_IMAGE_REQUIRE_ROOT
|
1222 DISSECT_IMAGE_RELAX_VAR_CHECK
|
1223 DISSECT_IMAGE_READ_ONLY
|
1224 DISSECT_IMAGE_USR_NO_ROOT
|
1225 DISSECT_IMAGE_ADD_PARTITION_DEVICES
|
1226 DISSECT_IMAGE_PIN_PARTITION_DEVICES
,
1231 r
= dissected_image_acquire_metadata(m
,
1232 DISSECT_IMAGE_VALIDATE_OS
|
1233 DISSECT_IMAGE_VALIDATE_OS_EXT
);
1237 free_and_replace(i
->hostname
, m
->hostname
);
1238 i
->machine_id
= m
->machine_id
;
1239 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1240 strv_free_and_replace(i
->os_release
, m
->os_release
);
1241 strv_free_and_replace(i
->extension_release
, m
->extension_release
);
1250 i
->metadata_valid
= true;
1255 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1261 /* Locks an image name, regardless of the precise path used. */
1263 if (streq(name
, ".host"))
1266 if (!image_name_is_valid(name
))
1269 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1270 *ret
= (LockFile
) LOCK_FILE_INIT
;
1274 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1276 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1277 return make_lock_file(p
, operation
, ret
);
1280 bool image_in_search_path(
1283 const char *image
) {
1287 NULSTR_FOREACH(path
, image_search_path
[class]) {
1291 if (!empty_or_root(root
)) {
1292 q
= path_startswith(path
, root
);
1298 p
= path_startswith(q
, path
);
1302 /* Make sure there's a filename following */
1303 k
= strcspn(p
, "/");
1309 /* Accept trailing slashes */
1310 if (p
[strspn(p
, "/")] == 0)
1318 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1319 [IMAGE_DIRECTORY
] = "directory",
1320 [IMAGE_SUBVOLUME
] = "subvolume",
1321 [IMAGE_RAW
] = "raw",
1322 [IMAGE_BLOCK
] = "block",
1325 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);
1327 static const char* const image_class_table
[_IMAGE_CLASS_MAX
] = {
1328 [IMAGE_MACHINE
] = "machine",
1329 [IMAGE_PORTABLE
] = "portable",
1330 [IMAGE_EXTENSION
] = "extension",
1333 DEFINE_STRING_TABLE_LOOKUP(image_class
, ImageClass
);