1 /* SPDX-License-Identifier: LGPL-2.1+ */
14 #include "alloc-util.h"
15 #include "btrfs-util.h"
16 #include "chattr-util.h"
18 #include "dirent-util.h"
19 #include "dissect-image.h"
25 #include "hostname-util.h"
26 #include "id128-util.h"
27 #include "lockfile-util.h"
29 #include "loop-util.h"
30 #include "machine-image.h"
34 #include "path-util.h"
36 #include "string-table.h"
37 #include "string-util.h"
39 #include "time-util.h"
42 #include "xattr-util.h"
44 static const char* const image_search_path
[_IMAGE_CLASS_MAX
] = {
45 [IMAGE_MACHINE
] = "/etc/machines\0" /* only place symlinks here */
46 "/run/machines\0" /* and here too */
47 "/var/lib/machines\0" /* the main place for images */
48 "/var/lib/container\0" /* legacy */
49 "/usr/local/lib/machines\0"
50 "/usr/lib/machines\0",
52 [IMAGE_PORTABLE
] = "/etc/portables\0" /* only place symlinks here */
53 "/run/portables\0" /* and here too */
54 "/var/lib/portables\0" /* the main place for images */
55 "/usr/local/lib/portables\0"
56 "/usr/lib/portables\0",
59 static Image
*image_free(Image
*i
) {
66 strv_free(i
->machine_info
);
67 strv_free(i
->os_release
);
72 DEFINE_TRIVIAL_REF_UNREF_FUNC(Image
, image
, image_free
);
74 static char **image_settings_path(Image
*image
) {
75 _cleanup_strv_free_
char **l
= NULL
;
85 fn
= strjoina(image
->name
, ".nspawn");
87 FOREACH_STRING(s
, "/etc/systemd/nspawn/", "/run/systemd/nspawn/") {
88 l
[i
] = strappend(s
, fn
);
95 l
[i
] = file_in_same_dir(image
->path
, fn
);
102 static char *image_roothash_path(Image
*image
) {
107 fn
= strjoina(image
->name
, ".roothash");
109 return file_in_same_dir(image
->path
, fn
);
112 static int image_new(
116 const char *filename
,
122 _cleanup_(image_unrefp
) Image
*i
= NULL
;
125 assert(t
< _IMAGE_TYPE_MAX
);
136 i
->read_only
= read_only
;
139 i
->usage
= i
->usage_exclusive
= (uint64_t) -1;
140 i
->limit
= i
->limit_exclusive
= (uint64_t) -1;
142 i
->name
= strdup(pretty
);
147 i
->path
= strjoin(path
, "/", filename
);
149 i
->path
= strdup(filename
);
153 path_simplify(i
->path
, false);
160 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
161 _cleanup_free_
char *name
= NULL
;
168 p
= last_path_component(path
);
171 name
= strndup(p
, n
);
178 e
= endswith(name
, suffix
);
185 if (!image_name_is_valid(name
))
188 *ret
= TAKE_PTR(name
);
192 static int image_make(
196 const char *filename
,
197 const struct stat
*st
,
200 _cleanup_free_
char *pretty_buffer
= NULL
;
205 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
208 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
209 * devices into /var/lib/machines/, and treat them normally.
211 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
215 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
222 (path
&& path_startswith(path
, "/usr")) ||
223 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
225 if (S_ISDIR(st
->st_mode
)) {
226 _cleanup_close_
int fd
= -1;
227 unsigned file_attr
= 0;
233 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
237 pretty
= pretty_buffer
;
240 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
244 /* btrfs subvolumes have inode 256 */
245 if (st
->st_ino
== 256) {
247 r
= btrfs_is_filesystem(fd
);
251 BtrfsSubvolInfo info
;
253 /* It's a btrfs subvolume */
255 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
259 r
= image_new(IMAGE_SUBVOLUME
,
263 info
.read_only
|| read_only
,
270 if (btrfs_quota_scan_ongoing(fd
) == 0) {
271 BtrfsQuotaInfo quota
;
273 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
275 (*ret
)->usage
= quota
.referenced
;
276 (*ret
)->usage_exclusive
= quota
.exclusive
;
278 (*ret
)->limit
= quota
.referenced_max
;
279 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
287 /* If the IMMUTABLE bit is set, we consider the
288 * directory read-only. Since the ioctl is not
289 * supported everywhere we ignore failures. */
290 (void) read_attr_fd(fd
, &file_attr
);
292 /* It's just a normal directory. */
293 r
= image_new(IMAGE_DIRECTORY
,
297 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
306 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
309 /* It's a RAW disk image */
314 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
317 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
321 pretty
= pretty_buffer
;
324 r
= image_new(IMAGE_RAW
,
328 !(st
->st_mode
& 0222) || read_only
,
330 timespec_load(&st
->st_mtim
),
335 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
336 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
340 } else if (S_ISBLK(st
->st_mode
)) {
341 _cleanup_close_
int block_fd
= -1;
342 uint64_t size
= UINT64_MAX
;
350 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
354 pretty
= pretty_buffer
;
357 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
359 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
361 /* Refresh stat data after opening the node */
362 if (fstat(block_fd
, &stbuf
) < 0)
366 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
372 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
373 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
378 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
379 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
, filename
);
381 block_fd
= safe_close(block_fd
);
384 r
= image_new(IMAGE_BLOCK
,
388 !(st
->st_mode
& 0222) || read_only
,
395 if (size
!= 0 && size
!= UINT64_MAX
)
396 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
404 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
409 assert(class < _IMAGE_CLASS_MAX
);
412 /* There are no images with invalid names */
413 if (!image_name_is_valid(name
))
416 NULSTR_FOREACH(path
, image_search_path
[class]) {
417 _cleanup_closedir_
DIR *d
= NULL
;
428 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
429 * symlink block devices into the search path */
430 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
431 _cleanup_free_
char *raw
= NULL
;
436 raw
= strappend(name
, ".raw");
440 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
448 if (!S_ISREG(st
.st_mode
))
451 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
454 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
457 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
459 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
465 (*ret
)->discoverable
= true;
470 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
471 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
476 (*ret
)->discoverable
= true;
484 int image_from_path(const char *path
, Image
**ret
) {
486 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
487 * the image is in the image search path. And if it is we don't know if the path we used is actually not
488 * overridden by another, different image earlier in the search path */
490 if (path_equal(path
, "/"))
491 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
493 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
496 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
497 if (image_name_is_valid(name_or_path
))
498 return image_find(class, name_or_path
, ret
);
500 return image_from_path(name_or_path
, ret
);
503 int image_discover(ImageClass
class, Hashmap
*h
) {
508 assert(class < _IMAGE_CLASS_MAX
);
511 NULSTR_FOREACH(path
, image_search_path
[class]) {
512 _cleanup_closedir_
DIR *d
= NULL
;
523 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
524 _cleanup_(image_unrefp
) Image
*image
= NULL
;
525 _cleanup_free_
char *truncated
= NULL
;
529 if (dot_or_dot_dot(de
->d_name
))
532 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
533 * to symlink block devices into the search path */
534 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
541 if (S_ISREG(st
.st_mode
)) {
544 e
= endswith(de
->d_name
, ".raw");
548 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
553 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
558 if (!image_name_is_valid(pretty
))
561 if (hashmap_contains(h
, pretty
))
564 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
565 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
570 image
->discoverable
= true;
572 r
= hashmap_put(h
, image
->name
, image
);
580 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
581 _cleanup_(image_unrefp
) Image
*image
= NULL
;
583 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
587 image
->discoverable
= true;
589 r
= hashmap_put(h
, image
->name
, image
);
599 int image_remove(Image
*i
) {
600 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
601 _cleanup_strv_free_
char **settings
= NULL
;
602 _cleanup_free_
char *roothash
= NULL
;
608 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
611 settings
= image_settings_path(i
);
615 roothash
= image_roothash_path(i
);
619 /* Make sure we don't interfere with a running nspawn */
620 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
626 case IMAGE_SUBVOLUME
:
628 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
630 if (unlink(i
->path
) < 0) {
631 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
638 case IMAGE_DIRECTORY
:
639 /* Allow deletion of read-only directories */
640 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
641 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
649 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
650 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
651 * the thing (it's most likely a symlink after all). */
653 if (path_startswith(i
->path
, "/dev"))
658 if (unlink(i
->path
) < 0)
666 STRV_FOREACH(j
, settings
) {
667 if (unlink(*j
) < 0 && errno
!= ENOENT
)
668 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
671 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
672 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
677 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
678 _cleanup_free_
char *rs
= NULL
;
681 fn
= strjoina(new_name
, suffix
);
683 rs
= file_in_same_dir(path
, fn
);
687 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
690 int image_rename(Image
*i
, const char *new_name
) {
691 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
692 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
693 _cleanup_strv_free_
char **settings
= NULL
;
694 unsigned file_attr
= 0;
700 if (!image_name_is_valid(new_name
))
703 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
706 settings
= image_settings_path(i
);
710 roothash
= image_roothash_path(i
);
714 /* Make sure we don't interfere with a running nspawn */
715 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
719 /* Make sure nobody takes the new name, between the time we
720 * checked it is currently unused in all search paths, and the
721 * time we take possession of it */
722 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
726 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
734 case IMAGE_DIRECTORY
:
735 /* Turn of the immutable bit while we rename the image, so that we can rename it */
736 (void) read_attr_path(i
->path
, &file_attr
);
738 if (file_attr
& FS_IMMUTABLE_FL
)
739 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
742 case IMAGE_SUBVOLUME
:
743 new_path
= file_in_same_dir(i
->path
, new_name
);
748 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
749 if (path_startswith(i
->path
, "/dev"))
752 new_path
= file_in_same_dir(i
->path
, new_name
);
758 fn
= strjoina(new_name
, ".raw");
759 new_path
= file_in_same_dir(i
->path
, fn
);
770 nn
= strdup(new_name
);
774 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
778 /* Restore the immutable bit, if it was set before */
779 if (file_attr
& FS_IMMUTABLE_FL
)
780 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
782 free_and_replace(i
->path
, new_path
);
783 free_and_replace(i
->name
, nn
);
785 STRV_FOREACH(j
, settings
) {
786 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
787 if (r
< 0 && r
!= -ENOENT
)
788 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
791 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
792 if (r
< 0 && r
!= -ENOENT
)
793 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
798 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
799 _cleanup_free_
char *rs
= NULL
;
802 fn
= strjoina(new_name
, suffix
);
804 rs
= file_in_same_dir(path
, fn
);
808 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
811 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
812 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
813 _cleanup_strv_free_
char **settings
= NULL
;
814 _cleanup_free_
char *roothash
= NULL
;
815 const char *new_path
;
821 if (!image_name_is_valid(new_name
))
824 settings
= image_settings_path(i
);
828 roothash
= image_roothash_path(i
);
832 /* Make sure nobody takes the new name, between the time we
833 * checked it is currently unused in all search paths, and the
834 * time we take possession of it */
835 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
839 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
847 case IMAGE_SUBVOLUME
:
848 case IMAGE_DIRECTORY
:
849 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
852 new_path
= strjoina("/var/lib/machines/", new_name
);
854 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
855 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
856 BTRFS_SNAPSHOT_FALLBACK_COPY
|
857 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
858 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
859 BTRFS_SNAPSHOT_RECURSIVE
|
860 BTRFS_SNAPSHOT_QUOTA
);
862 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
863 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
868 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
870 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
);
881 STRV_FOREACH(j
, settings
) {
882 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
883 if (r
< 0 && r
!= -ENOENT
)
884 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
887 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
888 if (r
< 0 && r
!= -ENOENT
)
889 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
894 int image_read_only(Image
*i
, bool b
) {
895 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
900 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
903 /* Make sure we don't interfere with a running nspawn */
904 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
910 case IMAGE_SUBVOLUME
:
912 /* Note that we set the flag only on the top-level
913 * subvolume of the image. */
915 r
= btrfs_subvol_set_read_only(i
->path
, b
);
921 case IMAGE_DIRECTORY
:
922 /* For simple directory trees we cannot use the access
923 mode of the top-level directory, since it has an
924 effect on the container itself. However, we can
925 use the "immutable" flag, to at least make the
926 top-level directory read-only. It's not as good as
927 a read-only subvolume, but at least something, and
928 we can read the value back. */
930 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
, NULL
);
939 if (stat(i
->path
, &st
) < 0)
942 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
945 /* If the images is now read-only, it's a good time to
946 * defrag it, given that no write patterns will
947 * fragment it again. */
949 (void) btrfs_defrag(i
->path
);
954 _cleanup_close_
int fd
= -1;
958 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
962 if (fstat(fd
, &st
) < 0)
964 if (!S_ISBLK(st
.st_mode
))
967 if (ioctl(fd
, BLKROSET
, &state
) < 0)
980 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
981 _cleanup_free_
char *p
= NULL
;
982 LockFile t
= LOCK_FILE_INIT
;
990 /* Locks an image path. This actually creates two locks: one
991 * "local" one, next to the image path itself, which might be
992 * shared via NFS. And another "global" one, in /run, that
993 * uses the device/inode number. This has the benefit that we
994 * can even lock a tree that is a mount point, correctly. */
996 if (!path_is_absolute(path
))
999 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1000 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1004 if (path_equal(path
, "/"))
1007 if (stat(path
, &st
) >= 0) {
1008 if (S_ISBLK(st
.st_mode
))
1009 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1010 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1011 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1019 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1020 * block devices are device local anyway. */
1021 if (!path_startswith(path
, "/dev")) {
1022 r
= make_lock_file_for(path
, operation
, &t
);
1024 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1025 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1032 mkdir_p("/run/systemd/nspawn/locks", 0700);
1034 r
= make_lock_file(p
, operation
, global
);
1036 release_lock_file(&t
);
1040 *global
= (LockFile
) LOCK_FILE_INIT
;
1046 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1049 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1052 if (i
->type
!= IMAGE_SUBVOLUME
)
1055 /* We set the quota both for the subvolume as well as for the
1056 * subtree. The latter is mostly for historical reasons, since
1057 * we didn't use to have a concept of subtree quota, and hence
1058 * only modified the subvolume quota. */
1060 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1061 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1062 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1065 int image_read_metadata(Image
*i
) {
1066 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1071 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1077 case IMAGE_SUBVOLUME
:
1078 case IMAGE_DIRECTORY
: {
1079 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1080 sd_id128_t machine_id
= SD_ID128_NULL
;
1081 _cleanup_free_
char *hostname
= NULL
;
1082 _cleanup_free_
char *path
= NULL
;
1084 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1085 if (r
< 0 && r
!= -ENOENT
)
1086 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1088 r
= read_etc_hostname(path
, &hostname
);
1090 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1095 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1096 if (r
< 0 && r
!= -ENOENT
)
1097 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1099 _cleanup_close_
int fd
= -1;
1101 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1103 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1105 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1107 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1113 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1114 if (r
< 0 && r
!= -ENOENT
)
1115 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1117 r
= load_env_file_pairs(NULL
, path
, &machine_info
);
1119 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1122 r
= load_os_release_pairs(i
->path
, &os_release
);
1124 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1126 free_and_replace(i
->hostname
, hostname
);
1127 i
->machine_id
= machine_id
;
1128 strv_free_and_replace(i
->machine_info
, machine_info
);
1129 strv_free_and_replace(i
->os_release
, os_release
);
1136 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1137 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1139 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1143 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1147 r
= dissected_image_acquire_metadata(m
);
1151 free_and_replace(i
->hostname
, m
->hostname
);
1152 i
->machine_id
= m
->machine_id
;
1153 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1154 strv_free_and_replace(i
->os_release
, m
->os_release
);
1163 i
->metadata_valid
= true;
1168 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1174 /* Locks an image name, regardless of the precise path used. */
1176 if (!image_name_is_valid(name
))
1179 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1180 *ret
= (LockFile
) LOCK_FILE_INIT
;
1184 if (streq(name
, ".host"))
1187 mkdir_p("/run/systemd/nspawn/locks", 0700);
1188 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1190 return make_lock_file(p
, operation
, ret
);
1193 bool image_name_is_valid(const char *s
) {
1194 if (!filename_is_valid(s
))
1197 if (string_has_cc(s
, NULL
))
1200 if (!utf8_is_valid(s
))
1203 /* Temporary files for atomically creating new files */
1204 if (startswith(s
, ".#"))
1210 bool image_in_search_path(ImageClass
class, const char *image
) {
1215 NULSTR_FOREACH(path
, image_search_path
[class]) {
1219 p
= path_startswith(image
, path
);
1223 /* Make sure there's a filename following */
1224 k
= strcspn(p
, "/");
1230 /* Accept trailing slashes */
1231 if (p
[strspn(p
, "/")] == 0)
1239 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1240 [IMAGE_DIRECTORY
] = "directory",
1241 [IMAGE_SUBVOLUME
] = "subvolume",
1242 [IMAGE_RAW
] = "raw",
1243 [IMAGE_BLOCK
] = "block",
1246 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);