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"
17 #include "chase-symlinks.h"
18 #include "chattr-util.h"
20 #include "dirent-util.h"
21 #include "discover-image.h"
22 #include "dissect-image.h"
28 #include "hostname-setup.h"
29 #include "id128-util.h"
30 #include "lockfile-util.h"
32 #include "loop-util.h"
35 #include "nulstr-util.h"
37 #include "path-util.h"
39 #include "stat-util.h"
40 #include "string-table.h"
41 #include "string-util.h"
43 #include "time-util.h"
45 #include "xattr-util.h"
47 static const char* const image_search_path
[_IMAGE_CLASS_MAX
] = {
48 [IMAGE_MACHINE
] = "/etc/machines\0" /* only place symlinks here */
49 "/run/machines\0" /* and here too */
50 "/var/lib/machines\0" /* the main place for images */
51 "/var/lib/container\0" /* legacy */
52 "/usr/local/lib/machines\0"
53 "/usr/lib/machines\0",
55 [IMAGE_PORTABLE
] = "/etc/portables\0" /* only place symlinks here */
56 "/run/portables\0" /* and here too */
57 "/var/lib/portables\0" /* the main place for images */
58 "/usr/local/lib/portables\0"
59 "/usr/lib/portables\0",
61 [IMAGE_EXTENSION
] = "/etc/extensions\0" /* only place symlinks here */
62 "/run/extensions\0" /* and here too */
63 "/var/lib/extensions\0" /* the main place for images */
64 "/usr/local/lib/extensions\0"
65 "/usr/lib/extensions\0",
68 static Image
*image_free(Image
*i
) {
75 strv_free(i
->machine_info
);
76 strv_free(i
->os_release
);
77 strv_free(i
->extension_release
);
82 DEFINE_TRIVIAL_REF_UNREF_FUNC(Image
, image
, image_free
);
83 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(image_hash_ops
, char, string_hash_func
, string_compare_func
,
86 static char **image_settings_path(Image
*image
) {
87 _cleanup_strv_free_
char **l
= NULL
;
97 fn
= strjoina(image
->name
, ".nspawn");
99 FOREACH_STRING(s
, "/etc/systemd/nspawn", "/run/systemd/nspawn") {
100 l
[i
] = path_join(s
, fn
);
107 l
[i
] = file_in_same_dir(image
->path
, fn
);
114 static char *image_roothash_path(Image
*image
) {
119 fn
= strjoina(image
->name
, ".roothash");
121 return file_in_same_dir(image
->path
, fn
);
124 static int image_new(
129 const char *filename
,
135 _cleanup_(image_unrefp
) Image
*i
= NULL
;
138 assert(t
< _IMAGE_TYPE_MAX
);
151 .read_only
= read_only
,
155 .usage_exclusive
= UINT64_MAX
,
157 .limit_exclusive
= UINT64_MAX
,
160 i
->name
= strdup(pretty
);
164 i
->path
= path_join(path
, filename
);
168 path_simplify(i
->path
);
175 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
176 _cleanup_free_
char *name
= NULL
;
182 p
= last_path_component(path
);
184 name
= strdupcspn(p
, "/");
191 e
= endswith(name
, suffix
);
198 if (!image_name_is_valid(name
))
201 *ret
= TAKE_PTR(name
);
205 static int image_make(
210 const char *filename
,
211 const struct stat
*st
,
214 _cleanup_free_
char *pretty_buffer
= NULL
, *parent
= NULL
;
219 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
220 assert(path
|| dfd
== AT_FDCWD
);
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.
226 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
230 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
238 (void) safe_getcwd(&parent
);
240 (void) fd_get_path(dfd
, &parent
);
244 (path
&& path_startswith(path
, "/usr")) ||
245 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
247 if (S_ISDIR(st
->st_mode
)) {
248 _cleanup_close_
int fd
= -EBADF
;
249 unsigned file_attr
= 0;
256 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
260 pretty
= pretty_buffer
;
263 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
267 if (btrfs_might_be_subvol(st
)) {
269 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
273 BtrfsSubvolInfo info
;
275 /* It's a btrfs subvolume */
277 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
281 r
= image_new(IMAGE_SUBVOLUME
,
286 info
.read_only
|| read_only
,
293 if (btrfs_quota_scan_ongoing(fd
) == 0) {
294 BtrfsQuotaInfo quota
;
296 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
298 (*ret
)->usage
= quota
.referenced
;
299 (*ret
)->usage_exclusive
= quota
.exclusive
;
301 (*ret
)->limit
= quota
.referenced_max
;
302 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
310 /* Get directory creation time (not available everywhere, but that's OK */
311 (void) fd_getcrtime(fd
, &crtime
);
313 /* If the IMMUTABLE bit is set, we consider the directory read-only. Since the ioctl is not
314 * supported everywhere we ignore failures. */
315 (void) read_attr_fd(fd
, &file_attr
);
317 /* It's just a normal directory. */
318 r
= image_new(IMAGE_DIRECTORY
,
323 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
325 0, /* we don't use mtime of stat() here, since it's not the time of last change of the tree, but only of the top-level dir */
332 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
335 /* It's a RAW disk image */
340 (void) fd_getcrtime_at(dfd
, filename
, AT_SYMLINK_FOLLOW
, &crtime
);
343 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
347 pretty
= pretty_buffer
;
350 r
= image_new(IMAGE_RAW
,
355 !(st
->st_mode
& 0222) || read_only
,
357 timespec_load(&st
->st_mtim
),
362 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
363 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
367 } else if (S_ISBLK(st
->st_mode
)) {
368 _cleanup_close_
int block_fd
= -EBADF
;
369 uint64_t size
= UINT64_MAX
;
377 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
381 pretty
= pretty_buffer
;
384 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
386 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
388 /* Refresh stat data after opening the node */
389 if (fstat(block_fd
, &stbuf
) < 0)
393 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
399 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
400 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
405 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
406 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
408 block_fd
= safe_close(block_fd
);
411 r
= image_new(IMAGE_BLOCK
,
416 !(st
->st_mode
& 0222) || read_only
,
423 if (!IN_SET(size
, 0, UINT64_MAX
))
424 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
432 int image_find(ImageClass
class,
440 assert(class < _IMAGE_CLASS_MAX
);
443 /* There are no images with invalid names */
444 if (!image_name_is_valid(name
))
447 NULSTR_FOREACH(path
, image_search_path
[class]) {
448 _cleanup_free_
char *resolved
= NULL
;
449 _cleanup_closedir_
DIR *d
= NULL
;
453 r
= chase_symlinks_and_opendir(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, &d
);
459 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
460 * to symlink block devices into the search path. (For now, we disable that when operating
461 * relative to some root directory.) */
462 flags
= root
? AT_SYMLINK_NOFOLLOW
: 0;
463 if (fstatat(dirfd(d
), name
, &st
, flags
) < 0) {
464 _cleanup_free_
char *raw
= NULL
;
469 raw
= strjoin(name
, ".raw");
473 if (fstatat(dirfd(d
), raw
, &st
, flags
) < 0) {
480 if (!S_ISREG(st
.st_mode
))
483 r
= image_make(class, name
, dirfd(d
), resolved
, raw
, &st
, ret
);
486 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
489 r
= image_make(class, name
, dirfd(d
), resolved
, name
, &st
, ret
);
491 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
497 (*ret
)->discoverable
= true;
502 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
503 r
= image_make(class, ".host", AT_FDCWD
, NULL
, empty_to_root(root
), NULL
, ret
);
508 (*ret
)->discoverable
= true;
516 int image_from_path(const char *path
, Image
**ret
) {
518 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
519 * the image is in the image search path. And if it is we don't know if the path we used is actually not
520 * overridden by another, different image earlier in the search path */
522 if (path_equal(path
, "/"))
523 return image_make(IMAGE_MACHINE
, ".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
525 return image_make(_IMAGE_CLASS_INVALID
, NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
528 int image_find_harder(ImageClass
class, const char *name_or_path
, const char *root
, Image
**ret
) {
529 if (image_name_is_valid(name_or_path
))
530 return image_find(class, name_or_path
, root
, ret
);
532 return image_from_path(name_or_path
, ret
);
543 assert(class < _IMAGE_CLASS_MAX
);
546 NULSTR_FOREACH(path
, image_search_path
[class]) {
547 _cleanup_free_
char *resolved
= NULL
;
548 _cleanup_closedir_
DIR *d
= NULL
;
550 r
= chase_symlinks_and_opendir(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, &d
);
556 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
557 _cleanup_(image_unrefp
) Image
*image
= NULL
;
558 _cleanup_free_
char *truncated
= NULL
;
563 if (dot_or_dot_dot(de
->d_name
))
566 /* As mentioned above, we follow symlinks on this fstatat(), because we want to
567 * permit people to symlink block devices into the search path. */
568 flags
= root
? AT_SYMLINK_NOFOLLOW
: 0;
569 if (fstatat(dirfd(d
), de
->d_name
, &st
, flags
) < 0) {
576 if (S_ISREG(st
.st_mode
)) {
579 e
= endswith(de
->d_name
, ".raw");
583 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
588 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
593 if (!image_name_is_valid(pretty
))
596 if (hashmap_contains(h
, pretty
))
599 r
= image_make(class, pretty
, dirfd(d
), resolved
, de
->d_name
, &st
, &image
);
600 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
605 image
->discoverable
= true;
607 r
= hashmap_put(h
, image
->name
, image
);
615 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
616 _cleanup_(image_unrefp
) Image
*image
= NULL
;
618 r
= image_make(IMAGE_MACHINE
, ".host", AT_FDCWD
, NULL
, empty_to_root("/"), NULL
, &image
);
622 image
->discoverable
= true;
624 r
= hashmap_put(h
, image
->name
, image
);
634 int image_remove(Image
*i
) {
635 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
636 _cleanup_strv_free_
char **settings
= NULL
;
637 _cleanup_free_
char *roothash
= NULL
;
642 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
645 settings
= image_settings_path(i
);
649 roothash
= image_roothash_path(i
);
653 /* Make sure we don't interfere with a running nspawn */
654 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
660 case IMAGE_SUBVOLUME
:
662 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
664 if (unlink(i
->path
) < 0) {
665 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
672 case IMAGE_DIRECTORY
:
673 /* Allow deletion of read-only directories */
674 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
675 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
683 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
684 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
685 * the thing (it's most likely a symlink after all). */
687 if (path_startswith(i
->path
, "/dev"))
692 if (unlink(i
->path
) < 0)
700 STRV_FOREACH(j
, settings
)
701 if (unlink(*j
) < 0 && errno
!= ENOENT
)
702 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
704 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
705 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
710 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
711 _cleanup_free_
char *rs
= NULL
;
714 fn
= strjoina(new_name
, suffix
);
716 rs
= file_in_same_dir(path
, fn
);
720 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
723 int image_rename(Image
*i
, const char *new_name
) {
724 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
725 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
726 _cleanup_strv_free_
char **settings
= NULL
;
727 unsigned file_attr
= 0;
732 if (!image_name_is_valid(new_name
))
735 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
738 settings
= image_settings_path(i
);
742 roothash
= image_roothash_path(i
);
746 /* Make sure we don't interfere with a running nspawn */
747 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
751 /* Make sure nobody takes the new name, between the time we
752 * checked it is currently unused in all search paths, and the
753 * time we take possession of it */
754 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
758 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
, NULL
);
766 case IMAGE_DIRECTORY
:
767 /* Turn of the immutable bit while we rename the image, so that we can rename it */
768 (void) read_attr_path(i
->path
, &file_attr
);
770 if (file_attr
& FS_IMMUTABLE_FL
)
771 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
774 case IMAGE_SUBVOLUME
:
775 new_path
= file_in_same_dir(i
->path
, new_name
);
780 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
781 if (path_startswith(i
->path
, "/dev"))
784 new_path
= file_in_same_dir(i
->path
, new_name
);
790 fn
= strjoina(new_name
, ".raw");
791 new_path
= file_in_same_dir(i
->path
, fn
);
802 nn
= strdup(new_name
);
806 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
810 /* Restore the immutable bit, if it was set before */
811 if (file_attr
& FS_IMMUTABLE_FL
)
812 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
814 free_and_replace(i
->path
, new_path
);
815 free_and_replace(i
->name
, nn
);
817 STRV_FOREACH(j
, settings
) {
818 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
819 if (r
< 0 && r
!= -ENOENT
)
820 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
823 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
824 if (r
< 0 && r
!= -ENOENT
)
825 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
830 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
831 _cleanup_free_
char *rs
= NULL
;
834 fn
= strjoina(new_name
, suffix
);
836 rs
= file_in_same_dir(path
, fn
);
840 return copy_file_atomic(path
, rs
, 0664, 0, 0, COPY_REFLINK
);
843 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
844 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
845 _cleanup_strv_free_
char **settings
= NULL
;
846 _cleanup_free_
char *roothash
= NULL
;
847 const char *new_path
;
852 if (!image_name_is_valid(new_name
))
855 settings
= image_settings_path(i
);
859 roothash
= image_roothash_path(i
);
863 /* Make sure nobody takes the new name, between the time we
864 * checked it is currently unused in all search paths, and the
865 * time we take possession of it */
866 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
870 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
, NULL
);
878 case IMAGE_SUBVOLUME
:
879 case IMAGE_DIRECTORY
:
880 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
883 new_path
= strjoina("/var/lib/machines/", new_name
);
885 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
886 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
887 BTRFS_SNAPSHOT_FALLBACK_COPY
|
888 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
889 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
890 BTRFS_SNAPSHOT_RECURSIVE
|
891 BTRFS_SNAPSHOT_QUOTA
);
893 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
894 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
899 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
901 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, FS_NOCOW_FL
, COPY_REFLINK
|COPY_CRTIME
);
912 STRV_FOREACH(j
, settings
) {
913 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
914 if (r
< 0 && r
!= -ENOENT
)
915 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
918 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
919 if (r
< 0 && r
!= -ENOENT
)
920 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
925 int image_read_only(Image
*i
, bool b
) {
926 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
931 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
934 /* Make sure we don't interfere with a running nspawn */
935 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
941 case IMAGE_SUBVOLUME
:
943 /* Note that we set the flag only on the top-level
944 * subvolume of the image. */
946 r
= btrfs_subvol_set_read_only(i
->path
, b
);
952 case IMAGE_DIRECTORY
:
953 /* For simple directory trees we cannot use the access
954 mode of the top-level directory, since it has an
955 effect on the container itself. However, we can
956 use the "immutable" flag, to at least make the
957 top-level directory read-only. It's not as good as
958 a read-only subvolume, but at least something, and
959 we can read the value back. */
961 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
, NULL
);
970 if (stat(i
->path
, &st
) < 0)
973 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
976 /* If the images is now read-only, it's a good time to
977 * defrag it, given that no write patterns will
978 * fragment it again. */
980 (void) btrfs_defrag(i
->path
);
985 _cleanup_close_
int fd
= -EBADF
;
989 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
993 if (fstat(fd
, &st
) < 0)
995 if (!S_ISBLK(st
.st_mode
))
998 if (ioctl(fd
, BLKROSET
, &state
) < 0)
1011 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
1012 _cleanup_free_
char *p
= NULL
;
1013 LockFile t
= LOCK_FILE_INIT
;
1022 /* Locks an image path. This actually creates two locks: one "local" one, next to the image path
1023 * itself, which might be shared via NFS. And another "global" one, in /run, that uses the
1024 * device/inode number. This has the benefit that we can even lock a tree that is a mount point,
1027 if (!path_is_absolute(path
))
1030 switch (operation
& (LOCK_SH
|LOCK_EX
)) {
1041 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1042 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1046 /* Prohibit taking exclusive locks on the host image. We can't allow this, since we ourselves are
1047 * running off it after all, and we don't want any images to manipulate the host image. We make an
1048 * exception for shared locks however: we allow those (and make them NOPs since there's no point in
1049 * taking them if there can't be exclusive locks). Strictly speaking these are questionable as well,
1050 * since it means changes made to the host might propagate to the container as they happen (and a
1051 * shared lock kinda suggests that no changes happen at all while it is in place), but it's too
1052 * useful not to allow read-only containers off the host root, hence let's support this, and trust
1053 * the user to do the right thing with this. */
1054 if (path_equal(path
, "/")) {
1058 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1062 if (stat(path
, &st
) >= 0) {
1063 if (S_ISBLK(st
.st_mode
))
1064 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1065 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1066 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1073 /* For block devices we don't need the "local" lock, as the major/minor lock above should be
1074 * sufficient, since block devices are host local anyway. */
1075 if (!path_startswith(path
, "/dev/")) {
1076 r
= make_lock_file_for(path
, operation
, &t
);
1078 if (!exclusive
&& r
== -EROFS
)
1079 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1086 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1088 r
= make_lock_file(p
, operation
, global
);
1090 release_lock_file(&t
);
1094 *global
= (LockFile
) LOCK_FILE_INIT
;
1100 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1103 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1106 if (i
->type
!= IMAGE_SUBVOLUME
)
1109 /* We set the quota both for the subvolume as well as for the
1110 * subtree. The latter is mostly for historical reasons, since
1111 * we didn't use to have a concept of subtree quota, and hence
1112 * only modified the subvolume quota. */
1114 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1115 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1116 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1119 int image_read_metadata(Image
*i
) {
1120 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1125 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1131 case IMAGE_SUBVOLUME
:
1132 case IMAGE_DIRECTORY
: {
1133 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
, **extension_release
= NULL
;
1134 sd_id128_t machine_id
= SD_ID128_NULL
;
1135 _cleanup_free_
char *hostname
= NULL
;
1136 _cleanup_free_
char *path
= NULL
;
1138 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1139 if (r
< 0 && r
!= -ENOENT
)
1140 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1142 r
= read_etc_hostname(path
, &hostname
);
1144 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1149 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1150 if (r
< 0 && r
!= -ENOENT
)
1151 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1153 _cleanup_close_
int fd
= -EBADF
;
1155 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1157 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1159 r
= id128_read_fd(fd
, ID128_FORMAT_PLAIN
, &machine_id
);
1161 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1167 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1168 if (r
< 0 && r
!= -ENOENT
)
1169 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1171 r
= load_env_file_pairs(NULL
, path
, &machine_info
);
1173 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1176 r
= load_os_release_pairs(i
->path
, &os_release
);
1178 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1180 r
= load_extension_release_pairs(i
->path
, i
->name
, /* relax_extension_release_check= */ false, &extension_release
);
1182 log_debug_errno(r
, "Failed to read extension-release in image, ignoring: %m");
1184 free_and_replace(i
->hostname
, hostname
);
1185 i
->machine_id
= machine_id
;
1186 strv_free_and_replace(i
->machine_info
, machine_info
);
1187 strv_free_and_replace(i
->os_release
, os_release
);
1188 strv_free_and_replace(i
->extension_release
, extension_release
);
1195 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1196 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1198 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, /* sector_size= */ UINT32_MAX
, LO_FLAGS_PARTSCAN
, LOCK_SH
, &d
);
1202 r
= dissect_loop_device(
1205 DISSECT_IMAGE_GENERIC_ROOT
|
1206 DISSECT_IMAGE_REQUIRE_ROOT
|
1207 DISSECT_IMAGE_RELAX_VAR_CHECK
|
1208 DISSECT_IMAGE_READ_ONLY
|
1209 DISSECT_IMAGE_USR_NO_ROOT
|
1210 DISSECT_IMAGE_ADD_PARTITION_DEVICES
|
1211 DISSECT_IMAGE_PIN_PARTITION_DEVICES
,
1216 r
= dissected_image_acquire_metadata(m
,
1217 DISSECT_IMAGE_VALIDATE_OS
|
1218 DISSECT_IMAGE_VALIDATE_OS_EXT
);
1222 free_and_replace(i
->hostname
, m
->hostname
);
1223 i
->machine_id
= m
->machine_id
;
1224 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1225 strv_free_and_replace(i
->os_release
, m
->os_release
);
1226 strv_free_and_replace(i
->extension_release
, m
->extension_release
);
1235 i
->metadata_valid
= true;
1240 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1246 /* Locks an image name, regardless of the precise path used. */
1248 if (streq(name
, ".host"))
1251 if (!image_name_is_valid(name
))
1254 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1255 *ret
= (LockFile
) LOCK_FILE_INIT
;
1259 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1261 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1262 return make_lock_file(p
, operation
, ret
);
1265 bool image_in_search_path(
1268 const char *image
) {
1272 NULSTR_FOREACH(path
, image_search_path
[class]) {
1276 if (!empty_or_root(root
)) {
1277 q
= path_startswith(path
, root
);
1283 p
= path_startswith(q
, path
);
1287 /* Make sure there's a filename following */
1288 k
= strcspn(p
, "/");
1294 /* Accept trailing slashes */
1295 if (p
[strspn(p
, "/")] == 0)
1303 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1304 [IMAGE_DIRECTORY
] = "directory",
1305 [IMAGE_SUBVOLUME
] = "subvolume",
1306 [IMAGE_RAW
] = "raw",
1307 [IMAGE_BLOCK
] = "block",
1310 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);
1312 static const char* const image_class_table
[_IMAGE_CLASS_MAX
] = {
1313 [IMAGE_MACHINE
] = "machine",
1314 [IMAGE_PORTABLE
] = "portable",
1315 [IMAGE_EXTENSION
] = "extension",
1318 DEFINE_STRING_TABLE_LOOKUP(image_class
, ImageClass
);