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
;
183 p
= last_path_component(path
);
186 name
= strndup(p
, n
);
193 e
= endswith(name
, suffix
);
200 if (!image_name_is_valid(name
))
203 *ret
= TAKE_PTR(name
);
207 static int image_make(
212 const char *filename
,
213 const struct stat
*st
,
216 _cleanup_free_
char *pretty_buffer
= NULL
, *parent
= NULL
;
221 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
222 assert(path
|| dfd
== AT_FDCWD
);
225 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
226 * devices into /var/lib/machines/, and treat them normally.
228 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
232 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
240 (void) safe_getcwd(&parent
);
242 (void) fd_get_path(dfd
, &parent
);
246 (path
&& path_startswith(path
, "/usr")) ||
247 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
249 if (S_ISDIR(st
->st_mode
)) {
250 _cleanup_close_
int fd
= -EBADF
;
251 unsigned file_attr
= 0;
258 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
262 pretty
= pretty_buffer
;
265 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
269 if (btrfs_might_be_subvol(st
)) {
271 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
275 BtrfsSubvolInfo info
;
277 /* It's a btrfs subvolume */
279 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
283 r
= image_new(IMAGE_SUBVOLUME
,
288 info
.read_only
|| read_only
,
295 if (btrfs_quota_scan_ongoing(fd
) == 0) {
296 BtrfsQuotaInfo quota
;
298 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
300 (*ret
)->usage
= quota
.referenced
;
301 (*ret
)->usage_exclusive
= quota
.exclusive
;
303 (*ret
)->limit
= quota
.referenced_max
;
304 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
312 /* Get directory creation time (not available everywhere, but that's OK */
313 (void) fd_getcrtime(fd
, &crtime
);
315 /* If the IMMUTABLE bit is set, we consider the directory read-only. Since the ioctl is not
316 * supported everywhere we ignore failures. */
317 (void) read_attr_fd(fd
, &file_attr
);
319 /* It's just a normal directory. */
320 r
= image_new(IMAGE_DIRECTORY
,
325 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
327 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 */
334 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
337 /* It's a RAW disk image */
342 (void) fd_getcrtime_at(dfd
, filename
, AT_SYMLINK_FOLLOW
, &crtime
);
345 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
349 pretty
= pretty_buffer
;
352 r
= image_new(IMAGE_RAW
,
357 !(st
->st_mode
& 0222) || read_only
,
359 timespec_load(&st
->st_mtim
),
364 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
365 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
369 } else if (S_ISBLK(st
->st_mode
)) {
370 _cleanup_close_
int block_fd
= -EBADF
;
371 uint64_t size
= UINT64_MAX
;
379 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
383 pretty
= pretty_buffer
;
386 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
388 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
390 /* Refresh stat data after opening the node */
391 if (fstat(block_fd
, &stbuf
) < 0)
395 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
401 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
402 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
407 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
408 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
410 block_fd
= safe_close(block_fd
);
413 r
= image_new(IMAGE_BLOCK
,
418 !(st
->st_mode
& 0222) || read_only
,
425 if (!IN_SET(size
, 0, UINT64_MAX
))
426 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
434 int image_find(ImageClass
class,
442 assert(class < _IMAGE_CLASS_MAX
);
445 /* There are no images with invalid names */
446 if (!image_name_is_valid(name
))
449 NULSTR_FOREACH(path
, image_search_path
[class]) {
450 _cleanup_free_
char *resolved
= NULL
;
451 _cleanup_closedir_
DIR *d
= NULL
;
455 r
= chase_symlinks_and_opendir(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, &d
);
461 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
462 * to symlink block devices into the search path. (For now, we disable that when operating
463 * relative to some root directory.) */
464 flags
= root
? AT_SYMLINK_NOFOLLOW
: 0;
465 if (fstatat(dirfd(d
), name
, &st
, flags
) < 0) {
466 _cleanup_free_
char *raw
= NULL
;
471 raw
= strjoin(name
, ".raw");
475 if (fstatat(dirfd(d
), raw
, &st
, flags
) < 0) {
482 if (!S_ISREG(st
.st_mode
))
485 r
= image_make(class, name
, dirfd(d
), resolved
, raw
, &st
, ret
);
488 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
491 r
= image_make(class, name
, dirfd(d
), resolved
, name
, &st
, ret
);
493 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
499 (*ret
)->discoverable
= true;
504 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
505 r
= image_make(class, ".host", AT_FDCWD
, NULL
, empty_to_root(root
), NULL
, ret
);
510 (*ret
)->discoverable
= true;
518 int image_from_path(const char *path
, Image
**ret
) {
520 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
521 * the image is in the image search path. And if it is we don't know if the path we used is actually not
522 * overridden by another, different image earlier in the search path */
524 if (path_equal(path
, "/"))
525 return image_make(IMAGE_MACHINE
, ".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
527 return image_make(_IMAGE_CLASS_INVALID
, NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
530 int image_find_harder(ImageClass
class, const char *name_or_path
, const char *root
, Image
**ret
) {
531 if (image_name_is_valid(name_or_path
))
532 return image_find(class, name_or_path
, root
, ret
);
534 return image_from_path(name_or_path
, ret
);
545 assert(class < _IMAGE_CLASS_MAX
);
548 NULSTR_FOREACH(path
, image_search_path
[class]) {
549 _cleanup_free_
char *resolved
= NULL
;
550 _cleanup_closedir_
DIR *d
= NULL
;
552 r
= chase_symlinks_and_opendir(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, &d
);
558 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
559 _cleanup_(image_unrefp
) Image
*image
= NULL
;
560 _cleanup_free_
char *truncated
= NULL
;
565 if (dot_or_dot_dot(de
->d_name
))
568 /* As mentioned above, we follow symlinks on this fstatat(), because we want to
569 * permit people to symlink block devices into the search path. */
570 flags
= root
? AT_SYMLINK_NOFOLLOW
: 0;
571 if (fstatat(dirfd(d
), de
->d_name
, &st
, flags
) < 0) {
578 if (S_ISREG(st
.st_mode
)) {
581 e
= endswith(de
->d_name
, ".raw");
585 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
590 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
595 if (!image_name_is_valid(pretty
))
598 if (hashmap_contains(h
, pretty
))
601 r
= image_make(class, pretty
, dirfd(d
), resolved
, de
->d_name
, &st
, &image
);
602 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
607 image
->discoverable
= true;
609 r
= hashmap_put(h
, image
->name
, image
);
617 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
618 _cleanup_(image_unrefp
) Image
*image
= NULL
;
620 r
= image_make(IMAGE_MACHINE
, ".host", AT_FDCWD
, NULL
, empty_to_root("/"), NULL
, &image
);
624 image
->discoverable
= true;
626 r
= hashmap_put(h
, image
->name
, image
);
636 int image_remove(Image
*i
) {
637 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
638 _cleanup_strv_free_
char **settings
= NULL
;
639 _cleanup_free_
char *roothash
= NULL
;
644 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
647 settings
= image_settings_path(i
);
651 roothash
= image_roothash_path(i
);
655 /* Make sure we don't interfere with a running nspawn */
656 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
662 case IMAGE_SUBVOLUME
:
664 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
666 if (unlink(i
->path
) < 0) {
667 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
674 case IMAGE_DIRECTORY
:
675 /* Allow deletion of read-only directories */
676 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
677 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
685 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
686 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
687 * the thing (it's most likely a symlink after all). */
689 if (path_startswith(i
->path
, "/dev"))
694 if (unlink(i
->path
) < 0)
702 STRV_FOREACH(j
, settings
)
703 if (unlink(*j
) < 0 && errno
!= ENOENT
)
704 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
706 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
707 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
712 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
713 _cleanup_free_
char *rs
= NULL
;
716 fn
= strjoina(new_name
, suffix
);
718 rs
= file_in_same_dir(path
, fn
);
722 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
725 int image_rename(Image
*i
, const char *new_name
) {
726 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
727 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
728 _cleanup_strv_free_
char **settings
= NULL
;
729 unsigned file_attr
= 0;
734 if (!image_name_is_valid(new_name
))
737 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
740 settings
= image_settings_path(i
);
744 roothash
= image_roothash_path(i
);
748 /* Make sure we don't interfere with a running nspawn */
749 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
753 /* Make sure nobody takes the new name, between the time we
754 * checked it is currently unused in all search paths, and the
755 * time we take possession of it */
756 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
760 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
, NULL
);
768 case IMAGE_DIRECTORY
:
769 /* Turn of the immutable bit while we rename the image, so that we can rename it */
770 (void) read_attr_path(i
->path
, &file_attr
);
772 if (file_attr
& FS_IMMUTABLE_FL
)
773 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
776 case IMAGE_SUBVOLUME
:
777 new_path
= file_in_same_dir(i
->path
, new_name
);
782 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
783 if (path_startswith(i
->path
, "/dev"))
786 new_path
= file_in_same_dir(i
->path
, new_name
);
792 fn
= strjoina(new_name
, ".raw");
793 new_path
= file_in_same_dir(i
->path
, fn
);
804 nn
= strdup(new_name
);
808 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
812 /* Restore the immutable bit, if it was set before */
813 if (file_attr
& FS_IMMUTABLE_FL
)
814 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
816 free_and_replace(i
->path
, new_path
);
817 free_and_replace(i
->name
, nn
);
819 STRV_FOREACH(j
, settings
) {
820 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
821 if (r
< 0 && r
!= -ENOENT
)
822 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
825 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
826 if (r
< 0 && r
!= -ENOENT
)
827 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
832 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
833 _cleanup_free_
char *rs
= NULL
;
836 fn
= strjoina(new_name
, suffix
);
838 rs
= file_in_same_dir(path
, fn
);
842 return copy_file_atomic(path
, rs
, 0664, 0, 0, COPY_REFLINK
);
845 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
846 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
847 _cleanup_strv_free_
char **settings
= NULL
;
848 _cleanup_free_
char *roothash
= NULL
;
849 const char *new_path
;
854 if (!image_name_is_valid(new_name
))
857 settings
= image_settings_path(i
);
861 roothash
= image_roothash_path(i
);
865 /* Make sure nobody takes the new name, between the time we
866 * checked it is currently unused in all search paths, and the
867 * time we take possession of it */
868 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
872 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
, NULL
);
880 case IMAGE_SUBVOLUME
:
881 case IMAGE_DIRECTORY
:
882 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
885 new_path
= strjoina("/var/lib/machines/", new_name
);
887 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
888 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
889 BTRFS_SNAPSHOT_FALLBACK_COPY
|
890 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
891 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
892 BTRFS_SNAPSHOT_RECURSIVE
|
893 BTRFS_SNAPSHOT_QUOTA
);
895 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
896 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
901 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
903 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, FS_NOCOW_FL
, COPY_REFLINK
|COPY_CRTIME
);
914 STRV_FOREACH(j
, settings
) {
915 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
916 if (r
< 0 && r
!= -ENOENT
)
917 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
920 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
921 if (r
< 0 && r
!= -ENOENT
)
922 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
927 int image_read_only(Image
*i
, bool b
) {
928 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
933 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
936 /* Make sure we don't interfere with a running nspawn */
937 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
943 case IMAGE_SUBVOLUME
:
945 /* Note that we set the flag only on the top-level
946 * subvolume of the image. */
948 r
= btrfs_subvol_set_read_only(i
->path
, b
);
954 case IMAGE_DIRECTORY
:
955 /* For simple directory trees we cannot use the access
956 mode of the top-level directory, since it has an
957 effect on the container itself. However, we can
958 use the "immutable" flag, to at least make the
959 top-level directory read-only. It's not as good as
960 a read-only subvolume, but at least something, and
961 we can read the value back. */
963 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
, NULL
);
972 if (stat(i
->path
, &st
) < 0)
975 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
978 /* If the images is now read-only, it's a good time to
979 * defrag it, given that no write patterns will
980 * fragment it again. */
982 (void) btrfs_defrag(i
->path
);
987 _cleanup_close_
int fd
= -EBADF
;
991 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
995 if (fstat(fd
, &st
) < 0)
997 if (!S_ISBLK(st
.st_mode
))
1000 if (ioctl(fd
, BLKROSET
, &state
) < 0)
1013 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
1014 _cleanup_free_
char *p
= NULL
;
1015 LockFile t
= LOCK_FILE_INIT
;
1024 /* Locks an image path. This actually creates two locks: one "local" one, next to the image path
1025 * itself, which might be shared via NFS. And another "global" one, in /run, that uses the
1026 * device/inode number. This has the benefit that we can even lock a tree that is a mount point,
1029 if (!path_is_absolute(path
))
1032 switch (operation
& (LOCK_SH
|LOCK_EX
)) {
1043 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1044 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1048 /* Prohibit taking exclusive locks on the host image. We can't allow this, since we ourselves are
1049 * running off it after all, and we don't want any images to manipulate the host image. We make an
1050 * exception for shared locks however: we allow those (and make them NOPs since there's no point in
1051 * taking them if there can't be exclusive locks). Strictly speaking these are questionable as well,
1052 * since it means changes made to the host might propagate to the container as they happen (and a
1053 * shared lock kinda suggests that no changes happen at all while it is in place), but it's too
1054 * useful not to allow read-only containers off the host root, hence let's support this, and trust
1055 * the user to do the right thing with this. */
1056 if (path_equal(path
, "/")) {
1060 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1064 if (stat(path
, &st
) >= 0) {
1065 if (S_ISBLK(st
.st_mode
))
1066 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1067 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1068 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1075 /* For block devices we don't need the "local" lock, as the major/minor lock above should be
1076 * sufficient, since block devices are host local anyway. */
1077 if (!path_startswith(path
, "/dev/")) {
1078 r
= make_lock_file_for(path
, operation
, &t
);
1080 if (!exclusive
&& r
== -EROFS
)
1081 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1088 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1090 r
= make_lock_file(p
, operation
, global
);
1092 release_lock_file(&t
);
1096 *global
= (LockFile
) LOCK_FILE_INIT
;
1102 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1105 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1108 if (i
->type
!= IMAGE_SUBVOLUME
)
1111 /* We set the quota both for the subvolume as well as for the
1112 * subtree. The latter is mostly for historical reasons, since
1113 * we didn't use to have a concept of subtree quota, and hence
1114 * only modified the subvolume quota. */
1116 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1117 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1118 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1121 int image_read_metadata(Image
*i
) {
1122 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1127 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1133 case IMAGE_SUBVOLUME
:
1134 case IMAGE_DIRECTORY
: {
1135 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
, **extension_release
= NULL
;
1136 sd_id128_t machine_id
= SD_ID128_NULL
;
1137 _cleanup_free_
char *hostname
= NULL
;
1138 _cleanup_free_
char *path
= NULL
;
1140 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1141 if (r
< 0 && r
!= -ENOENT
)
1142 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1144 r
= read_etc_hostname(path
, &hostname
);
1146 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1151 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1152 if (r
< 0 && r
!= -ENOENT
)
1153 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1155 _cleanup_close_
int fd
= -EBADF
;
1157 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1159 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1161 r
= id128_read_fd(fd
, ID128_FORMAT_PLAIN
, &machine_id
);
1163 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1169 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1170 if (r
< 0 && r
!= -ENOENT
)
1171 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1173 r
= load_env_file_pairs(NULL
, path
, &machine_info
);
1175 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1178 r
= load_os_release_pairs(i
->path
, &os_release
);
1180 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1182 r
= load_extension_release_pairs(i
->path
, i
->name
, /* relax_extension_release_check= */ false, &extension_release
);
1184 log_debug_errno(r
, "Failed to read extension-release in image, ignoring: %m");
1186 free_and_replace(i
->hostname
, hostname
);
1187 i
->machine_id
= machine_id
;
1188 strv_free_and_replace(i
->machine_info
, machine_info
);
1189 strv_free_and_replace(i
->os_release
, os_release
);
1190 strv_free_and_replace(i
->extension_release
, extension_release
);
1197 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1198 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1200 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, LO_FLAGS_PARTSCAN
, LOCK_SH
, &d
);
1204 r
= dissect_loop_device(
1207 DISSECT_IMAGE_GENERIC_ROOT
|
1208 DISSECT_IMAGE_REQUIRE_ROOT
|
1209 DISSECT_IMAGE_RELAX_VAR_CHECK
|
1210 DISSECT_IMAGE_READ_ONLY
|
1211 DISSECT_IMAGE_USR_NO_ROOT
|
1212 DISSECT_IMAGE_ADD_PARTITION_DEVICES
|
1213 DISSECT_IMAGE_PIN_PARTITION_DEVICES
,
1218 r
= dissected_image_acquire_metadata(m
,
1219 DISSECT_IMAGE_VALIDATE_OS
|
1220 DISSECT_IMAGE_VALIDATE_OS_EXT
);
1224 free_and_replace(i
->hostname
, m
->hostname
);
1225 i
->machine_id
= m
->machine_id
;
1226 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1227 strv_free_and_replace(i
->os_release
, m
->os_release
);
1228 strv_free_and_replace(i
->extension_release
, m
->extension_release
);
1237 i
->metadata_valid
= true;
1242 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1248 /* Locks an image name, regardless of the precise path used. */
1250 if (streq(name
, ".host"))
1253 if (!image_name_is_valid(name
))
1256 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1257 *ret
= (LockFile
) LOCK_FILE_INIT
;
1261 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1263 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1264 return make_lock_file(p
, operation
, ret
);
1267 bool image_in_search_path(
1270 const char *image
) {
1274 NULSTR_FOREACH(path
, image_search_path
[class]) {
1278 if (!empty_or_root(root
)) {
1279 q
= path_startswith(path
, root
);
1285 p
= path_startswith(q
, path
);
1289 /* Make sure there's a filename following */
1290 k
= strcspn(p
, "/");
1296 /* Accept trailing slashes */
1297 if (p
[strspn(p
, "/")] == 0)
1305 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1306 [IMAGE_DIRECTORY
] = "directory",
1307 [IMAGE_SUBVOLUME
] = "subvolume",
1308 [IMAGE_RAW
] = "raw",
1309 [IMAGE_BLOCK
] = "block",
1312 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);
1314 static const char* const image_class_table
[_IMAGE_CLASS_MAX
] = {
1315 [IMAGE_MACHINE
] = "machine",
1316 [IMAGE_PORTABLE
] = "portable",
1317 [IMAGE_EXTENSION
] = "extension",
1320 DEFINE_STRING_TABLE_LOOKUP(image_class
, ImageClass
);