1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/ioctl.h>
15 #include "alloc-util.h"
16 #include "btrfs-util.h"
17 #include "chattr-util.h"
19 #include "dirent-util.h"
20 #include "dissect-image.h"
26 #include "hostname-util.h"
27 #include "id128-util.h"
28 #include "lockfile-util.h"
30 #include "loop-util.h"
31 #include "machine-image.h"
34 #include "nulstr-util.h"
36 #include "path-util.h"
38 #include "string-table.h"
39 #include "string-util.h"
41 #include "time-util.h"
43 #include "xattr-util.h"
45 static const char* const image_search_path
[_IMAGE_CLASS_MAX
] = {
46 [IMAGE_MACHINE
] = "/etc/machines\0" /* only place symlinks here */
47 "/run/machines\0" /* and here too */
48 "/var/lib/machines\0" /* the main place for images */
49 "/var/lib/container\0" /* legacy */
50 "/usr/local/lib/machines\0"
51 "/usr/lib/machines\0",
53 [IMAGE_PORTABLE
] = "/etc/portables\0" /* only place symlinks here */
54 "/run/portables\0" /* and here too */
55 "/var/lib/portables\0" /* the main place for images */
56 "/usr/local/lib/portables\0"
57 "/usr/lib/portables\0",
60 static Image
*image_free(Image
*i
) {
67 strv_free(i
->machine_info
);
68 strv_free(i
->os_release
);
73 DEFINE_TRIVIAL_REF_UNREF_FUNC(Image
, image
, image_free
);
74 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(image_hash_ops
, char, string_hash_func
, string_compare_func
,
77 static char **image_settings_path(Image
*image
) {
78 _cleanup_strv_free_
char **l
= NULL
;
88 fn
= strjoina(image
->name
, ".nspawn");
90 FOREACH_STRING(s
, "/etc/systemd/nspawn/", "/run/systemd/nspawn/") {
91 l
[i
] = strappend(s
, fn
);
98 l
[i
] = file_in_same_dir(image
->path
, fn
);
105 static char *image_roothash_path(Image
*image
) {
110 fn
= strjoina(image
->name
, ".roothash");
112 return file_in_same_dir(image
->path
, fn
);
115 static int image_new(
119 const char *filename
,
125 _cleanup_(image_unrefp
) Image
*i
= NULL
;
128 assert(t
< _IMAGE_TYPE_MAX
);
139 i
->read_only
= read_only
;
142 i
->usage
= i
->usage_exclusive
= (uint64_t) -1;
143 i
->limit
= i
->limit_exclusive
= (uint64_t) -1;
145 i
->name
= strdup(pretty
);
150 i
->path
= strjoin(path
, "/", filename
);
152 i
->path
= strdup(filename
);
156 path_simplify(i
->path
, false);
163 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
164 _cleanup_free_
char *name
= NULL
;
171 p
= last_path_component(path
);
174 name
= strndup(p
, n
);
181 e
= endswith(name
, suffix
);
188 if (!image_name_is_valid(name
))
191 *ret
= TAKE_PTR(name
);
195 static int image_make(
199 const char *filename
,
200 const struct stat
*st
,
203 _cleanup_free_
char *pretty_buffer
= NULL
;
204 _cleanup_free_
char *cwd
= NULL
;
209 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
210 assert(path
|| dfd
== AT_FDCWD
);
213 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
214 * devices into /var/lib/machines/, and treat them normally.
216 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
220 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
230 (path
&& path_startswith(path
, "/usr")) ||
231 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
233 if (S_ISDIR(st
->st_mode
)) {
234 _cleanup_close_
int fd
= -1;
235 unsigned file_attr
= 0;
241 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
245 pretty
= pretty_buffer
;
248 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
252 /* btrfs subvolumes have inode 256 */
253 if (st
->st_ino
== 256) {
255 r
= btrfs_is_filesystem(fd
);
259 BtrfsSubvolInfo info
;
261 /* It's a btrfs subvolume */
263 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
267 r
= image_new(IMAGE_SUBVOLUME
,
271 info
.read_only
|| read_only
,
278 if (btrfs_quota_scan_ongoing(fd
) == 0) {
279 BtrfsQuotaInfo quota
;
281 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
283 (*ret
)->usage
= quota
.referenced
;
284 (*ret
)->usage_exclusive
= quota
.exclusive
;
286 (*ret
)->limit
= quota
.referenced_max
;
287 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
295 /* If the IMMUTABLE bit is set, we consider the
296 * directory read-only. Since the ioctl is not
297 * supported everywhere we ignore failures. */
298 (void) read_attr_fd(fd
, &file_attr
);
300 /* It's just a normal directory. */
301 r
= image_new(IMAGE_DIRECTORY
,
305 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
314 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
317 /* It's a RAW disk image */
322 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
325 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
329 pretty
= pretty_buffer
;
332 r
= image_new(IMAGE_RAW
,
336 !(st
->st_mode
& 0222) || read_only
,
338 timespec_load(&st
->st_mtim
),
343 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
344 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
348 } else if (S_ISBLK(st
->st_mode
)) {
349 _cleanup_close_
int block_fd
= -1;
350 uint64_t size
= UINT64_MAX
;
358 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
362 pretty
= pretty_buffer
;
365 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
367 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
?: strnull(cwd
), filename
);
369 /* Refresh stat data after opening the node */
370 if (fstat(block_fd
, &stbuf
) < 0)
374 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
380 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
381 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
?: strnull(cwd
), filename
);
386 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
387 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
?: strnull(cwd
), filename
);
389 block_fd
= safe_close(block_fd
);
392 r
= image_new(IMAGE_BLOCK
,
396 !(st
->st_mode
& 0222) || read_only
,
403 if (!IN_SET(size
, 0, UINT64_MAX
))
404 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
412 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
417 assert(class < _IMAGE_CLASS_MAX
);
420 /* There are no images with invalid names */
421 if (!image_name_is_valid(name
))
424 NULSTR_FOREACH(path
, image_search_path
[class]) {
425 _cleanup_closedir_
DIR *d
= NULL
;
436 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
437 * symlink block devices into the search path */
438 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
439 _cleanup_free_
char *raw
= NULL
;
444 raw
= strappend(name
, ".raw");
448 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
456 if (!S_ISREG(st
.st_mode
))
459 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
462 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
465 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
467 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
473 (*ret
)->discoverable
= true;
478 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
479 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
484 (*ret
)->discoverable
= true;
492 int image_from_path(const char *path
, Image
**ret
) {
494 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
495 * the image is in the image search path. And if it is we don't know if the path we used is actually not
496 * overridden by another, different image earlier in the search path */
498 if (path_equal(path
, "/"))
499 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
501 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
504 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
505 if (image_name_is_valid(name_or_path
))
506 return image_find(class, name_or_path
, ret
);
508 return image_from_path(name_or_path
, ret
);
511 int image_discover(ImageClass
class, Hashmap
*h
) {
516 assert(class < _IMAGE_CLASS_MAX
);
519 NULSTR_FOREACH(path
, image_search_path
[class]) {
520 _cleanup_closedir_
DIR *d
= NULL
;
531 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
532 _cleanup_(image_unrefp
) Image
*image
= NULL
;
533 _cleanup_free_
char *truncated
= NULL
;
537 if (dot_or_dot_dot(de
->d_name
))
540 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
541 * to symlink block devices into the search path */
542 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
549 if (S_ISREG(st
.st_mode
)) {
552 e
= endswith(de
->d_name
, ".raw");
556 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
561 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
566 if (!image_name_is_valid(pretty
))
569 if (hashmap_contains(h
, pretty
))
572 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
573 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
578 image
->discoverable
= true;
580 r
= hashmap_put(h
, image
->name
, image
);
588 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
589 _cleanup_(image_unrefp
) Image
*image
= NULL
;
591 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
595 image
->discoverable
= true;
597 r
= hashmap_put(h
, image
->name
, image
);
607 int image_remove(Image
*i
) {
608 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
609 _cleanup_strv_free_
char **settings
= NULL
;
610 _cleanup_free_
char *roothash
= NULL
;
616 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
619 settings
= image_settings_path(i
);
623 roothash
= image_roothash_path(i
);
627 /* Make sure we don't interfere with a running nspawn */
628 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
634 case IMAGE_SUBVOLUME
:
636 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
638 if (unlink(i
->path
) < 0) {
639 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
646 case IMAGE_DIRECTORY
:
647 /* Allow deletion of read-only directories */
648 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
649 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
657 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
658 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
659 * the thing (it's most likely a symlink after all). */
661 if (path_startswith(i
->path
, "/dev"))
666 if (unlink(i
->path
) < 0)
674 STRV_FOREACH(j
, settings
) {
675 if (unlink(*j
) < 0 && errno
!= ENOENT
)
676 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
679 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
680 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
685 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
686 _cleanup_free_
char *rs
= NULL
;
689 fn
= strjoina(new_name
, suffix
);
691 rs
= file_in_same_dir(path
, fn
);
695 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
698 int image_rename(Image
*i
, const char *new_name
) {
699 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
700 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
701 _cleanup_strv_free_
char **settings
= NULL
;
702 unsigned file_attr
= 0;
708 if (!image_name_is_valid(new_name
))
711 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
714 settings
= image_settings_path(i
);
718 roothash
= image_roothash_path(i
);
722 /* Make sure we don't interfere with a running nspawn */
723 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
727 /* Make sure nobody takes the new name, between the time we
728 * checked it is currently unused in all search paths, and the
729 * time we take possession of it */
730 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
734 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
742 case IMAGE_DIRECTORY
:
743 /* Turn of the immutable bit while we rename the image, so that we can rename it */
744 (void) read_attr_path(i
->path
, &file_attr
);
746 if (file_attr
& FS_IMMUTABLE_FL
)
747 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
750 case IMAGE_SUBVOLUME
:
751 new_path
= file_in_same_dir(i
->path
, new_name
);
756 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
757 if (path_startswith(i
->path
, "/dev"))
760 new_path
= file_in_same_dir(i
->path
, new_name
);
766 fn
= strjoina(new_name
, ".raw");
767 new_path
= file_in_same_dir(i
->path
, fn
);
778 nn
= strdup(new_name
);
782 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
786 /* Restore the immutable bit, if it was set before */
787 if (file_attr
& FS_IMMUTABLE_FL
)
788 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
790 free_and_replace(i
->path
, new_path
);
791 free_and_replace(i
->name
, nn
);
793 STRV_FOREACH(j
, settings
) {
794 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
795 if (r
< 0 && r
!= -ENOENT
)
796 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
799 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
800 if (r
< 0 && r
!= -ENOENT
)
801 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
806 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
807 _cleanup_free_
char *rs
= NULL
;
810 fn
= strjoina(new_name
, suffix
);
812 rs
= file_in_same_dir(path
, fn
);
816 return copy_file_atomic(path
, rs
, 0664, 0, 0, COPY_REFLINK
);
819 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
820 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
821 _cleanup_strv_free_
char **settings
= NULL
;
822 _cleanup_free_
char *roothash
= NULL
;
823 const char *new_path
;
829 if (!image_name_is_valid(new_name
))
832 settings
= image_settings_path(i
);
836 roothash
= image_roothash_path(i
);
840 /* Make sure nobody takes the new name, between the time we
841 * checked it is currently unused in all search paths, and the
842 * time we take possession of it */
843 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
847 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
855 case IMAGE_SUBVOLUME
:
856 case IMAGE_DIRECTORY
:
857 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
860 new_path
= strjoina("/var/lib/machines/", new_name
);
862 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
863 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
864 BTRFS_SNAPSHOT_FALLBACK_COPY
|
865 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
866 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
867 BTRFS_SNAPSHOT_RECURSIVE
|
868 BTRFS_SNAPSHOT_QUOTA
);
870 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
871 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
876 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
878 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, FS_NOCOW_FL
, COPY_REFLINK
|COPY_CRTIME
);
889 STRV_FOREACH(j
, settings
) {
890 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
891 if (r
< 0 && r
!= -ENOENT
)
892 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
895 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
896 if (r
< 0 && r
!= -ENOENT
)
897 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
902 int image_read_only(Image
*i
, bool b
) {
903 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
908 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
911 /* Make sure we don't interfere with a running nspawn */
912 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
918 case IMAGE_SUBVOLUME
:
920 /* Note that we set the flag only on the top-level
921 * subvolume of the image. */
923 r
= btrfs_subvol_set_read_only(i
->path
, b
);
929 case IMAGE_DIRECTORY
:
930 /* For simple directory trees we cannot use the access
931 mode of the top-level directory, since it has an
932 effect on the container itself. However, we can
933 use the "immutable" flag, to at least make the
934 top-level directory read-only. It's not as good as
935 a read-only subvolume, but at least something, and
936 we can read the value back. */
938 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
, NULL
);
947 if (stat(i
->path
, &st
) < 0)
950 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
953 /* If the images is now read-only, it's a good time to
954 * defrag it, given that no write patterns will
955 * fragment it again. */
957 (void) btrfs_defrag(i
->path
);
962 _cleanup_close_
int fd
= -1;
966 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
970 if (fstat(fd
, &st
) < 0)
972 if (!S_ISBLK(st
.st_mode
))
975 if (ioctl(fd
, BLKROSET
, &state
) < 0)
988 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
989 _cleanup_free_
char *p
= NULL
;
990 LockFile t
= LOCK_FILE_INIT
;
998 /* Locks an image path. This actually creates two locks: one
999 * "local" one, next to the image path itself, which might be
1000 * shared via NFS. And another "global" one, in /run, that
1001 * uses the device/inode number. This has the benefit that we
1002 * can even lock a tree that is a mount point, correctly. */
1004 if (!path_is_absolute(path
))
1007 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1008 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1012 if (path_equal(path
, "/"))
1015 if (stat(path
, &st
) >= 0) {
1016 if (S_ISBLK(st
.st_mode
))
1017 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1018 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1019 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1027 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1028 * block devices are device local anyway. */
1029 if (!path_startswith(path
, "/dev")) {
1030 r
= make_lock_file_for(path
, operation
, &t
);
1032 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1033 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1040 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1042 r
= make_lock_file(p
, operation
, global
);
1044 release_lock_file(&t
);
1048 *global
= (LockFile
) LOCK_FILE_INIT
;
1054 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1057 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1060 if (i
->type
!= IMAGE_SUBVOLUME
)
1063 /* We set the quota both for the subvolume as well as for the
1064 * subtree. The latter is mostly for historical reasons, since
1065 * we didn't use to have a concept of subtree quota, and hence
1066 * only modified the subvolume quota. */
1068 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1069 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1070 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1073 int image_read_metadata(Image
*i
) {
1074 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1079 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1085 case IMAGE_SUBVOLUME
:
1086 case IMAGE_DIRECTORY
: {
1087 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1088 sd_id128_t machine_id
= SD_ID128_NULL
;
1089 _cleanup_free_
char *hostname
= NULL
;
1090 _cleanup_free_
char *path
= NULL
;
1092 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1093 if (r
< 0 && r
!= -ENOENT
)
1094 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1096 r
= read_etc_hostname(path
, &hostname
);
1098 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1103 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1104 if (r
< 0 && r
!= -ENOENT
)
1105 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1107 _cleanup_close_
int fd
= -1;
1109 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1111 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1113 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1115 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1121 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1122 if (r
< 0 && r
!= -ENOENT
)
1123 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1125 r
= load_env_file_pairs(NULL
, path
, &machine_info
);
1127 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1130 r
= load_os_release_pairs(i
->path
, &os_release
);
1132 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1134 free_and_replace(i
->hostname
, hostname
);
1135 i
->machine_id
= machine_id
;
1136 strv_free_and_replace(i
->machine_info
, machine_info
);
1137 strv_free_and_replace(i
->os_release
, os_release
);
1144 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1145 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1147 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1151 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1155 r
= dissected_image_acquire_metadata(m
);
1159 free_and_replace(i
->hostname
, m
->hostname
);
1160 i
->machine_id
= m
->machine_id
;
1161 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1162 strv_free_and_replace(i
->os_release
, m
->os_release
);
1171 i
->metadata_valid
= true;
1176 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1180 /* Locks an image name, regardless of the precise path used. */
1182 if (!image_name_is_valid(name
))
1185 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1186 *ret
= (LockFile
) LOCK_FILE_INIT
;
1190 if (streq(name
, ".host"))
1193 const char *p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1194 (void) mkdir_p("/run/systemd/nspawn/locks", 0700);
1195 return make_lock_file(p
, operation
, ret
);
1198 bool image_name_is_valid(const char *s
) {
1199 if (!filename_is_valid(s
))
1202 if (string_has_cc(s
, NULL
))
1205 if (!utf8_is_valid(s
))
1208 /* Temporary files for atomically creating new files */
1209 if (startswith(s
, ".#"))
1215 bool image_in_search_path(ImageClass
class, const char *image
) {
1220 NULSTR_FOREACH(path
, image_search_path
[class]) {
1224 p
= path_startswith(image
, path
);
1228 /* Make sure there's a filename following */
1229 k
= strcspn(p
, "/");
1235 /* Accept trailing slashes */
1236 if (p
[strspn(p
, "/")] == 0)
1244 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1245 [IMAGE_DIRECTORY
] = "directory",
1246 [IMAGE_SUBVOLUME
] = "subvolume",
1247 [IMAGE_RAW
] = "raw",
1248 [IMAGE_BLOCK
] = "block",
1251 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);