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(
128 const char *filename
,
134 _cleanup_(image_unrefp
) Image
*i
= NULL
;
137 assert(t
< _IMAGE_TYPE_MAX
);
149 .read_only
= read_only
,
153 .usage_exclusive
= UINT64_MAX
,
155 .limit_exclusive
= UINT64_MAX
,
158 i
->name
= strdup(pretty
);
162 i
->path
= path_join(path
, filename
);
166 path_simplify(i
->path
);
173 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
174 _cleanup_free_
char *name
= NULL
;
181 p
= last_path_component(path
);
184 name
= strndup(p
, n
);
191 e
= endswith(name
, suffix
);
198 if (!image_name_is_valid(name
))
201 *ret
= TAKE_PTR(name
);
205 static int image_make(
209 const char *filename
,
210 const struct stat
*st
,
213 _cleanup_free_
char *pretty_buffer
= NULL
, *parent
= NULL
;
218 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
219 assert(path
|| dfd
== AT_FDCWD
);
222 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
223 * devices into /var/lib/machines/, and treat them normally.
225 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
229 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
237 (void) safe_getcwd(&parent
);
239 (void) fd_get_path(dfd
, &parent
);
243 (path
&& path_startswith(path
, "/usr")) ||
244 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
246 if (S_ISDIR(st
->st_mode
)) {
247 _cleanup_close_
int fd
= -1;
248 unsigned file_attr
= 0;
255 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
259 pretty
= pretty_buffer
;
262 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
266 if (btrfs_might_be_subvol(st
)) {
268 r
= fd_is_fs_type(fd
, BTRFS_SUPER_MAGIC
);
272 BtrfsSubvolInfo info
;
274 /* It's a btrfs subvolume */
276 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
280 r
= image_new(IMAGE_SUBVOLUME
,
284 info
.read_only
|| read_only
,
291 if (btrfs_quota_scan_ongoing(fd
) == 0) {
292 BtrfsQuotaInfo quota
;
294 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
296 (*ret
)->usage
= quota
.referenced
;
297 (*ret
)->usage_exclusive
= quota
.exclusive
;
299 (*ret
)->limit
= quota
.referenced_max
;
300 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
308 /* Get directory creation time (not available everywhere, but that's OK */
309 (void) fd_getcrtime(fd
, &crtime
);
311 /* If the IMMUTABLE bit is set, we consider the directory read-only. Since the ioctl is not
312 * supported everywhere we ignore failures. */
313 (void) read_attr_fd(fd
, &file_attr
);
315 /* It's just a normal directory. */
316 r
= image_new(IMAGE_DIRECTORY
,
320 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
322 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 */
329 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
332 /* It's a RAW disk image */
337 (void) fd_getcrtime_at(dfd
, filename
, AT_SYMLINK_FOLLOW
, &crtime
);
340 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
344 pretty
= pretty_buffer
;
347 r
= image_new(IMAGE_RAW
,
351 !(st
->st_mode
& 0222) || read_only
,
353 timespec_load(&st
->st_mtim
),
358 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
359 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
363 } else if (S_ISBLK(st
->st_mode
)) {
364 _cleanup_close_
int block_fd
= -1;
365 uint64_t size
= UINT64_MAX
;
373 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
377 pretty
= pretty_buffer
;
380 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
382 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
384 /* Refresh stat data after opening the node */
385 if (fstat(block_fd
, &stbuf
) < 0)
389 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
395 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
396 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
401 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
402 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
?: strnull(parent
), filename
);
404 block_fd
= safe_close(block_fd
);
407 r
= image_new(IMAGE_BLOCK
,
411 !(st
->st_mode
& 0222) || read_only
,
418 if (!IN_SET(size
, 0, UINT64_MAX
))
419 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
427 int image_find(ImageClass
class,
436 assert(class < _IMAGE_CLASS_MAX
);
439 /* There are no images with invalid names */
440 if (!image_name_is_valid(name
))
443 NULSTR_FOREACH(path
, image_search_path
[class]) {
444 _cleanup_free_
char *resolved
= NULL
;
445 _cleanup_closedir_
DIR *d
= NULL
;
449 r
= chase_symlinks_and_opendir(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, &d
);
455 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
456 * to symlink block devices into the search path. (For now, we disable that when operating
457 * relative to some root directory.) */
458 flags
= root
? AT_SYMLINK_NOFOLLOW
: 0;
459 if (fstatat(dirfd(d
), name
, &st
, flags
) < 0) {
460 _cleanup_free_
char *raw
= NULL
;
465 raw
= strjoin(name
, ".raw");
469 if (fstatat(dirfd(d
), raw
, &st
, flags
) < 0) {
476 if (!S_ISREG(st
.st_mode
))
479 r
= image_make(name
, dirfd(d
), resolved
, raw
, &st
, ret
);
482 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
485 r
= image_make(name
, dirfd(d
), resolved
, name
, &st
, ret
);
487 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
493 (*ret
)->discoverable
= true;
498 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
499 r
= image_make(".host", AT_FDCWD
, NULL
, empty_to_root(root
), NULL
, ret
);
504 (*ret
)->discoverable
= true;
512 int image_from_path(const char *path
, Image
**ret
) {
514 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
515 * the image is in the image search path. And if it is we don't know if the path we used is actually not
516 * overridden by another, different image earlier in the search path */
518 if (path_equal(path
, "/"))
519 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
521 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
524 int image_find_harder(ImageClass
class, const char *name_or_path
, const char *root
, Image
**ret
) {
525 if (image_name_is_valid(name_or_path
))
526 return image_find(class, name_or_path
, root
, ret
);
528 return image_from_path(name_or_path
, ret
);
540 assert(class < _IMAGE_CLASS_MAX
);
543 NULSTR_FOREACH(path
, image_search_path
[class]) {
544 _cleanup_free_
char *resolved
= NULL
;
545 _cleanup_closedir_
DIR *d
= NULL
;
547 r
= chase_symlinks_and_opendir(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, &d
);
553 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
554 _cleanup_(image_unrefp
) Image
*image
= NULL
;
555 _cleanup_free_
char *truncated
= NULL
;
560 if (dot_or_dot_dot(de
->d_name
))
563 /* As mentioned above, we follow symlinks on this fstatat(), because we want to
564 * permit people to symlink block devices into the search path. */
565 flags
= root
? AT_SYMLINK_NOFOLLOW
: 0;
566 if (fstatat(dirfd(d
), de
->d_name
, &st
, flags
) < 0) {
573 if (S_ISREG(st
.st_mode
)) {
576 e
= endswith(de
->d_name
, ".raw");
580 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
585 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
590 if (!image_name_is_valid(pretty
))
593 if (hashmap_contains(h
, pretty
))
596 r
= image_make(pretty
, dirfd(d
), resolved
, de
->d_name
, &st
, &image
);
597 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
602 image
->discoverable
= true;
604 r
= hashmap_put(h
, image
->name
, image
);
612 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
613 _cleanup_(image_unrefp
) Image
*image
= NULL
;
615 r
= image_make(".host", AT_FDCWD
, NULL
, empty_to_root("/"), NULL
, &image
);
619 image
->discoverable
= true;
621 r
= hashmap_put(h
, image
->name
, image
);
631 int image_remove(Image
*i
) {
632 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
633 _cleanup_strv_free_
char **settings
= NULL
;
634 _cleanup_free_
char *roothash
= NULL
;
639 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
642 settings
= image_settings_path(i
);
646 roothash
= image_roothash_path(i
);
650 /* Make sure we don't interfere with a running nspawn */
651 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
657 case IMAGE_SUBVOLUME
:
659 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
661 if (unlink(i
->path
) < 0) {
662 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
669 case IMAGE_DIRECTORY
:
670 /* Allow deletion of read-only directories */
671 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
672 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
680 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
681 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
682 * the thing (it's most likely a symlink after all). */
684 if (path_startswith(i
->path
, "/dev"))
689 if (unlink(i
->path
) < 0)
697 STRV_FOREACH(j
, settings
)
698 if (unlink(*j
) < 0 && errno
!= ENOENT
)
699 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
701 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
702 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
707 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
708 _cleanup_free_
char *rs
= NULL
;
711 fn
= strjoina(new_name
, suffix
);
713 rs
= file_in_same_dir(path
, fn
);
717 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
720 int image_rename(Image
*i
, const char *new_name
) {
721 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
722 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
723 _cleanup_strv_free_
char **settings
= NULL
;
724 unsigned file_attr
= 0;
729 if (!image_name_is_valid(new_name
))
732 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
735 settings
= image_settings_path(i
);
739 roothash
= image_roothash_path(i
);
743 /* Make sure we don't interfere with a running nspawn */
744 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
748 /* Make sure nobody takes the new name, between the time we
749 * checked it is currently unused in all search paths, and the
750 * time we take possession of it */
751 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
755 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
, NULL
);
763 case IMAGE_DIRECTORY
:
764 /* Turn of the immutable bit while we rename the image, so that we can rename it */
765 (void) read_attr_path(i
->path
, &file_attr
);
767 if (file_attr
& FS_IMMUTABLE_FL
)
768 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
771 case IMAGE_SUBVOLUME
:
772 new_path
= file_in_same_dir(i
->path
, new_name
);
777 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
778 if (path_startswith(i
->path
, "/dev"))
781 new_path
= file_in_same_dir(i
->path
, new_name
);
787 fn
= strjoina(new_name
, ".raw");
788 new_path
= file_in_same_dir(i
->path
, fn
);
799 nn
= strdup(new_name
);
803 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
807 /* Restore the immutable bit, if it was set before */
808 if (file_attr
& FS_IMMUTABLE_FL
)
809 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
811 free_and_replace(i
->path
, new_path
);
812 free_and_replace(i
->name
, nn
);
814 STRV_FOREACH(j
, settings
) {
815 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
816 if (r
< 0 && r
!= -ENOENT
)
817 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
820 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
821 if (r
< 0 && r
!= -ENOENT
)
822 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
827 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
828 _cleanup_free_
char *rs
= NULL
;
831 fn
= strjoina(new_name
, suffix
);
833 rs
= file_in_same_dir(path
, fn
);
837 return copy_file_atomic(path
, rs
, 0664, 0, 0, COPY_REFLINK
);
840 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
841 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
842 _cleanup_strv_free_
char **settings
= NULL
;
843 _cleanup_free_
char *roothash
= NULL
;
844 const char *new_path
;
849 if (!image_name_is_valid(new_name
))
852 settings
= image_settings_path(i
);
856 roothash
= image_roothash_path(i
);
860 /* Make sure nobody takes the new name, between the time we
861 * checked it is currently unused in all search paths, and the
862 * time we take possession of it */
863 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
867 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
, NULL
);
875 case IMAGE_SUBVOLUME
:
876 case IMAGE_DIRECTORY
:
877 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
880 new_path
= strjoina("/var/lib/machines/", new_name
);
882 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
883 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
884 BTRFS_SNAPSHOT_FALLBACK_COPY
|
885 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
886 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
887 BTRFS_SNAPSHOT_RECURSIVE
|
888 BTRFS_SNAPSHOT_QUOTA
);
890 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
891 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
896 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
898 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, FS_NOCOW_FL
, COPY_REFLINK
|COPY_CRTIME
);
909 STRV_FOREACH(j
, settings
) {
910 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
911 if (r
< 0 && r
!= -ENOENT
)
912 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
915 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
916 if (r
< 0 && r
!= -ENOENT
)
917 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
922 int image_read_only(Image
*i
, bool b
) {
923 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
928 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
931 /* Make sure we don't interfere with a running nspawn */
932 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
938 case IMAGE_SUBVOLUME
:
940 /* Note that we set the flag only on the top-level
941 * subvolume of the image. */
943 r
= btrfs_subvol_set_read_only(i
->path
, b
);
949 case IMAGE_DIRECTORY
:
950 /* For simple directory trees we cannot use the access
951 mode of the top-level directory, since it has an
952 effect on the container itself. However, we can
953 use the "immutable" flag, to at least make the
954 top-level directory read-only. It's not as good as
955 a read-only subvolume, but at least something, and
956 we can read the value back. */
958 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
, NULL
);
967 if (stat(i
->path
, &st
) < 0)
970 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
973 /* If the images is now read-only, it's a good time to
974 * defrag it, given that no write patterns will
975 * fragment it again. */
977 (void) btrfs_defrag(i
->path
);
982 _cleanup_close_
int fd
= -1;
986 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
990 if (fstat(fd
, &st
) < 0)
992 if (!S_ISBLK(st
.st_mode
))
995 if (ioctl(fd
, BLKROSET
, &state
) < 0)
1008 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
1009 _cleanup_free_
char *p
= NULL
;
1010 LockFile t
= LOCK_FILE_INIT
;
1019 /* Locks an image path. This actually creates two locks: one "local" one, next to the image path
1020 * itself, which might be shared via NFS. And another "global" one, in /run, that uses the
1021 * device/inode number. This has the benefit that we can even lock a tree that is a mount point,
1024 if (!path_is_absolute(path
))
1027 switch (operation
& (LOCK_SH
|LOCK_EX
)) {
1038 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1039 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1043 /* Prohibit taking exclusive locks on the host image. We can't allow this, since we ourselves are
1044 * running off it after all, and we don't want any images to manipulate the host image. We make an
1045 * exception for shared locks however: we allow those (and make them NOPs since there's no point in
1046 * taking them if there can't be exclusive locks). Strictly speaking these are questionable as well,
1047 * since it means changes made to the host might propagate to the container as they happen (and a
1048 * shared lock kinda suggests that no changes happen at all while it is in place), but it's too
1049 * useful not to allow read-only containers off the host root, hence let's support this, and trust
1050 * the user to do the right thing with this. */
1051 if (path_equal(path
, "/")) {
1055 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1059 if (stat(path
, &st
) >= 0) {
1060 if (S_ISBLK(st
.st_mode
))
1061 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1062 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1063 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1070 /* For block devices we don't need the "local" lock, as the major/minor lock above should be
1071 * sufficient, since block devices are host local anyway. */
1072 if (!path_startswith(path
, "/dev/")) {
1073 r
= make_lock_file_for(path
, operation
, &t
);
1075 if (!exclusive
&& r
== -EROFS
)
1076 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1083 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1085 r
= make_lock_file(p
, operation
, global
);
1087 release_lock_file(&t
);
1091 *global
= (LockFile
) LOCK_FILE_INIT
;
1097 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1100 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1103 if (i
->type
!= IMAGE_SUBVOLUME
)
1106 /* We set the quota both for the subvolume as well as for the
1107 * subtree. The latter is mostly for historical reasons, since
1108 * we didn't use to have a concept of subtree quota, and hence
1109 * only modified the subvolume quota. */
1111 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1112 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1113 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1116 int image_read_metadata(Image
*i
) {
1117 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1122 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1128 case IMAGE_SUBVOLUME
:
1129 case IMAGE_DIRECTORY
: {
1130 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
, **extension_release
= NULL
;
1131 sd_id128_t machine_id
= SD_ID128_NULL
;
1132 _cleanup_free_
char *hostname
= NULL
;
1133 _cleanup_free_
char *path
= NULL
;
1135 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1136 if (r
< 0 && r
!= -ENOENT
)
1137 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1139 r
= read_etc_hostname(path
, &hostname
);
1141 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1146 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1147 if (r
< 0 && r
!= -ENOENT
)
1148 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1150 _cleanup_close_
int fd
= -1;
1152 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1154 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1156 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1158 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1164 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
, NULL
);
1165 if (r
< 0 && r
!= -ENOENT
)
1166 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1168 r
= load_env_file_pairs(NULL
, path
, &machine_info
);
1170 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1173 r
= load_os_release_pairs(i
->path
, &os_release
);
1175 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1177 r
= load_extension_release_pairs(i
->path
, i
->name
, /* relax_extension_release_check= */ false, &extension_release
);
1179 log_debug_errno(r
, "Failed to read extension-release in image, ignoring: %m");
1181 free_and_replace(i
->hostname
, hostname
);
1182 i
->machine_id
= machine_id
;
1183 strv_free_and_replace(i
->machine_info
, machine_info
);
1184 strv_free_and_replace(i
->os_release
, os_release
);
1185 strv_free_and_replace(i
->extension_release
, extension_release
);
1192 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1193 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1195 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, LO_FLAGS_PARTSCAN
, LOCK_SH
, &d
);
1199 r
= dissect_loop_device(
1202 DISSECT_IMAGE_GENERIC_ROOT
|
1203 DISSECT_IMAGE_REQUIRE_ROOT
|
1204 DISSECT_IMAGE_RELAX_VAR_CHECK
|
1205 DISSECT_IMAGE_READ_ONLY
|
1206 DISSECT_IMAGE_USR_NO_ROOT
,
1211 r
= dissected_image_acquire_metadata(m
,
1212 DISSECT_IMAGE_VALIDATE_OS
|
1213 DISSECT_IMAGE_VALIDATE_OS_EXT
);
1217 free_and_replace(i
->hostname
, m
->hostname
);
1218 i
->machine_id
= m
->machine_id
;
1219 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1220 strv_free_and_replace(i
->os_release
, m
->os_release
);
1221 strv_free_and_replace(i
->extension_release
, m
->extension_release
);
1230 i
->metadata_valid
= true;
1235 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1241 /* Locks an image name, regardless of the precise path used. */
1243 if (streq(name
, ".host"))
1246 if (!image_name_is_valid(name
))
1249 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1250 *ret
= (LockFile
) LOCK_FILE_INIT
;
1254 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1256 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1257 return make_lock_file(p
, operation
, ret
);
1260 bool image_in_search_path(
1263 const char *image
) {
1269 NULSTR_FOREACH(path
, image_search_path
[class]) {
1273 if (!empty_or_root(root
)) {
1274 q
= path_startswith(path
, root
);
1280 p
= path_startswith(q
, path
);
1284 /* Make sure there's a filename following */
1285 k
= strcspn(p
, "/");
1291 /* Accept trailing slashes */
1292 if (p
[strspn(p
, "/")] == 0)
1300 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1301 [IMAGE_DIRECTORY
] = "directory",
1302 [IMAGE_SUBVOLUME
] = "subvolume",
1303 [IMAGE_RAW
] = "raw",
1304 [IMAGE_BLOCK
] = "block",
1307 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);