1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
19 #include "alloc-util.h"
20 #include "btrfs-util.h"
21 #include "chattr-util.h"
23 #include "dirent-util.h"
24 #include "dissect-image.h"
30 #include "hostname-util.h"
31 #include "id128-util.h"
32 #include "lockfile-util.h"
34 #include "loop-util.h"
35 #include "machine-image.h"
39 #include "path-util.h"
41 #include "string-table.h"
42 #include "string-util.h"
44 #include "time-util.h"
47 #include "xattr-util.h"
49 static const char* const image_search_path
[_IMAGE_CLASS_MAX
] = {
50 [IMAGE_MACHINE
] = "/etc/machines\0" /* only place symlinks here */
51 "/run/machines\0" /* and here too */
52 "/var/lib/machines\0" /* the main place for images */
53 "/var/lib/container\0" /* legacy */
54 "/usr/local/lib/machines\0"
55 "/usr/lib/machines\0",
57 [IMAGE_PORTABLE
] = "/etc/portables\0" /* only place symlinks here */
58 "/run/portables\0" /* and here too */
59 "/var/lib/portables\0" /* the main place for images */
60 "/usr/local/lib/portables\0"
61 "/usr/lib/portables\0",
64 Image
*image_unref(Image
*i
) {
78 strv_free(i
->machine_info
);
79 strv_free(i
->os_release
);
84 Image
*image_ref(Image
*i
) {
94 static char **image_settings_path(Image
*image
) {
95 _cleanup_strv_free_
char **l
= NULL
;
105 fn
= strjoina(image
->name
, ".nspawn");
107 FOREACH_STRING(s
, "/etc/systemd/nspawn/", "/run/systemd/nspawn/") {
108 l
[i
] = strappend(s
, fn
);
115 l
[i
] = file_in_same_dir(image
->path
, fn
);
122 static char *image_roothash_path(Image
*image
) {
127 fn
= strjoina(image
->name
, ".roothash");
129 return file_in_same_dir(image
->path
, fn
);
132 static int image_new(
136 const char *filename
,
142 _cleanup_(image_unrefp
) Image
*i
= NULL
;
145 assert(t
< _IMAGE_TYPE_MAX
);
156 i
->read_only
= read_only
;
159 i
->usage
= i
->usage_exclusive
= (uint64_t) -1;
160 i
->limit
= i
->limit_exclusive
= (uint64_t) -1;
162 i
->name
= strdup(pretty
);
167 i
->path
= strjoin(path
, "/", filename
);
169 i
->path
= strdup(filename
);
173 path_simplify(i
->path
, false);
180 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
181 _cleanup_free_
char *name
= NULL
;
188 p
= last_path_component(path
);
191 name
= strndup(p
, n
);
198 e
= endswith(name
, suffix
);
205 if (!image_name_is_valid(name
))
208 *ret
= TAKE_PTR(name
);
212 static int image_make(
216 const char *filename
,
217 const struct stat
*st
,
220 _cleanup_free_
char *pretty_buffer
= NULL
;
225 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
228 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
229 * devices into /var/lib/machines/, and treat them normally.
231 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
235 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
242 (path
&& path_startswith(path
, "/usr")) ||
243 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
245 if (S_ISDIR(st
->st_mode
)) {
246 _cleanup_close_
int fd
= -1;
247 unsigned file_attr
= 0;
253 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
257 pretty
= pretty_buffer
;
260 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
264 /* btrfs subvolumes have inode 256 */
265 if (st
->st_ino
== 256) {
267 r
= btrfs_is_filesystem(fd
);
271 BtrfsSubvolInfo info
;
273 /* It's a btrfs subvolume */
275 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
279 r
= image_new(IMAGE_SUBVOLUME
,
283 info
.read_only
|| read_only
,
290 if (btrfs_quota_scan_ongoing(fd
) == 0) {
291 BtrfsQuotaInfo quota
;
293 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
295 (*ret
)->usage
= quota
.referenced
;
296 (*ret
)->usage_exclusive
= quota
.exclusive
;
298 (*ret
)->limit
= quota
.referenced_max
;
299 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
307 /* If the IMMUTABLE bit is set, we consider the
308 * directory read-only. Since the ioctl is not
309 * supported everywhere we ignore failures. */
310 (void) read_attr_fd(fd
, &file_attr
);
312 /* It's just a normal directory. */
313 r
= image_new(IMAGE_DIRECTORY
,
317 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
326 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
329 /* It's a RAW disk image */
334 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
337 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
341 pretty
= pretty_buffer
;
344 r
= image_new(IMAGE_RAW
,
348 !(st
->st_mode
& 0222) || read_only
,
350 timespec_load(&st
->st_mtim
),
355 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
356 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
360 } else if (S_ISBLK(st
->st_mode
)) {
361 _cleanup_close_
int block_fd
= -1;
362 uint64_t size
= UINT64_MAX
;
370 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
374 pretty
= pretty_buffer
;
377 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
379 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
381 /* Refresh stat data after opening the node */
382 if (fstat(block_fd
, &stbuf
) < 0)
386 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
392 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
393 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
398 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
399 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
, filename
);
401 block_fd
= safe_close(block_fd
);
404 r
= image_new(IMAGE_BLOCK
,
408 !(st
->st_mode
& 0222) || read_only
,
415 if (size
!= 0 && size
!= UINT64_MAX
)
416 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
424 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
429 assert(class < _IMAGE_CLASS_MAX
);
432 /* There are no images with invalid names */
433 if (!image_name_is_valid(name
))
436 NULSTR_FOREACH(path
, image_search_path
[class]) {
437 _cleanup_closedir_
DIR *d
= NULL
;
448 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
449 * symlink block devices into the search path */
450 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
451 _cleanup_free_
char *raw
= NULL
;
456 raw
= strappend(name
, ".raw");
460 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
468 if (!S_ISREG(st
.st_mode
))
471 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
474 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
477 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
479 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
485 (*ret
)->discoverable
= true;
490 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
491 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
496 (*ret
)->discoverable
= true;
504 int image_from_path(const char *path
, Image
**ret
) {
506 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
507 * the image is in the image search path. And if it is we don't know if the path we used is actually not
508 * overriden by another, different image earlier in the search path */
510 if (path_equal(path
, "/"))
511 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
513 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
516 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
517 if (image_name_is_valid(name_or_path
))
518 return image_find(class, name_or_path
, ret
);
520 return image_from_path(name_or_path
, ret
);
523 int image_discover(ImageClass
class, Hashmap
*h
) {
528 assert(class < _IMAGE_CLASS_MAX
);
531 NULSTR_FOREACH(path
, image_search_path
[class]) {
532 _cleanup_closedir_
DIR *d
= NULL
;
543 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
544 _cleanup_(image_unrefp
) Image
*image
= NULL
;
545 _cleanup_free_
char *truncated
= NULL
;
549 if (dot_or_dot_dot(de
->d_name
))
552 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
553 * to symlink block devices into the search path */
554 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
561 if (S_ISREG(st
.st_mode
)) {
564 e
= endswith(de
->d_name
, ".raw");
568 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
573 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
578 if (!image_name_is_valid(pretty
))
581 if (hashmap_contains(h
, pretty
))
584 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
585 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
590 image
->discoverable
= true;
592 r
= hashmap_put(h
, image
->name
, image
);
600 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
601 _cleanup_(image_unrefp
) Image
*image
= NULL
;
603 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
607 image
->discoverable
= true;
609 r
= hashmap_put(h
, image
->name
, image
);
619 int image_remove(Image
*i
) {
620 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
621 _cleanup_strv_free_
char **settings
= NULL
;
622 _cleanup_free_
char *roothash
= NULL
;
628 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
631 settings
= image_settings_path(i
);
635 roothash
= image_roothash_path(i
);
639 /* Make sure we don't interfere with a running nspawn */
640 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
646 case IMAGE_SUBVOLUME
:
648 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
650 if (unlink(i
->path
) < 0) {
651 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
658 case IMAGE_DIRECTORY
:
659 /* Allow deletion of read-only directories */
660 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
661 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
669 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
670 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
671 * the thing (it's most likely a symlink after all). */
673 if (path_startswith(i
->path
, "/dev"))
678 if (unlink(i
->path
) < 0)
686 STRV_FOREACH(j
, settings
) {
687 if (unlink(*j
) < 0 && errno
!= ENOENT
)
688 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
691 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
692 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
697 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
698 _cleanup_free_
char *rs
= NULL
;
701 fn
= strjoina(new_name
, suffix
);
703 rs
= file_in_same_dir(path
, fn
);
707 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
710 int image_rename(Image
*i
, const char *new_name
) {
711 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
712 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
713 _cleanup_strv_free_
char **settings
= NULL
;
714 unsigned file_attr
= 0;
720 if (!image_name_is_valid(new_name
))
723 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
726 settings
= image_settings_path(i
);
730 roothash
= image_roothash_path(i
);
734 /* Make sure we don't interfere with a running nspawn */
735 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
739 /* Make sure nobody takes the new name, between the time we
740 * checked it is currently unused in all search paths, and the
741 * time we take possession of it */
742 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
746 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
754 case IMAGE_DIRECTORY
:
755 /* Turn of the immutable bit while we rename the image, so that we can rename it */
756 (void) read_attr_path(i
->path
, &file_attr
);
758 if (file_attr
& FS_IMMUTABLE_FL
)
759 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
762 case IMAGE_SUBVOLUME
:
763 new_path
= file_in_same_dir(i
->path
, new_name
);
768 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
769 if (path_startswith(i
->path
, "/dev"))
772 new_path
= file_in_same_dir(i
->path
, new_name
);
778 fn
= strjoina(new_name
, ".raw");
779 new_path
= file_in_same_dir(i
->path
, fn
);
790 nn
= strdup(new_name
);
794 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
798 /* Restore the immutable bit, if it was set before */
799 if (file_attr
& FS_IMMUTABLE_FL
)
800 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
);
802 free_and_replace(i
->path
, new_path
);
803 free_and_replace(i
->name
, nn
);
805 STRV_FOREACH(j
, settings
) {
806 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
807 if (r
< 0 && r
!= -ENOENT
)
808 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
811 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
812 if (r
< 0 && r
!= -ENOENT
)
813 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
818 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
819 _cleanup_free_
char *rs
= NULL
;
822 fn
= strjoina(new_name
, suffix
);
824 rs
= file_in_same_dir(path
, fn
);
828 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
831 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
832 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
833 _cleanup_strv_free_
char **settings
= NULL
;
834 _cleanup_free_
char *roothash
= NULL
;
835 const char *new_path
;
841 if (!image_name_is_valid(new_name
))
844 settings
= image_settings_path(i
);
848 roothash
= image_roothash_path(i
);
852 /* Make sure nobody takes the new name, between the time we
853 * checked it is currently unused in all search paths, and the
854 * time we take possession of it */
855 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
859 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
867 case IMAGE_SUBVOLUME
:
868 case IMAGE_DIRECTORY
:
869 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
872 new_path
= strjoina("/var/lib/machines/", new_name
);
874 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
875 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
876 BTRFS_SNAPSHOT_FALLBACK_COPY
|
877 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
878 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
879 BTRFS_SNAPSHOT_RECURSIVE
|
880 BTRFS_SNAPSHOT_QUOTA
);
882 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
883 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
888 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
890 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
);
901 STRV_FOREACH(j
, settings
) {
902 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
903 if (r
< 0 && r
!= -ENOENT
)
904 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
907 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
908 if (r
< 0 && r
!= -ENOENT
)
909 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
914 int image_read_only(Image
*i
, bool b
) {
915 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
920 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
923 /* Make sure we don't interfere with a running nspawn */
924 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
930 case IMAGE_SUBVOLUME
:
932 /* Note that we set the flag only on the top-level
933 * subvolume of the image. */
935 r
= btrfs_subvol_set_read_only(i
->path
, b
);
941 case IMAGE_DIRECTORY
:
942 /* For simple directory trees we cannot use the access
943 mode of the top-level directory, since it has an
944 effect on the container itself. However, we can
945 use the "immutable" flag, to at least make the
946 top-level directory read-only. It's not as good as
947 a read-only subvolume, but at least something, and
948 we can read the value back. */
950 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
);
959 if (stat(i
->path
, &st
) < 0)
962 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
965 /* If the images is now read-only, it's a good time to
966 * defrag it, given that no write patterns will
967 * fragment it again. */
969 (void) btrfs_defrag(i
->path
);
974 _cleanup_close_
int fd
= -1;
978 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
982 if (fstat(fd
, &st
) < 0)
984 if (!S_ISBLK(st
.st_mode
))
987 if (ioctl(fd
, BLKROSET
, &state
) < 0)
1000 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
1001 _cleanup_free_
char *p
= NULL
;
1002 LockFile t
= LOCK_FILE_INIT
;
1010 /* Locks an image path. This actually creates two locks: one
1011 * "local" one, next to the image path itself, which might be
1012 * shared via NFS. And another "global" one, in /run, that
1013 * uses the device/inode number. This has the benefit that we
1014 * can even lock a tree that is a mount point, correctly. */
1016 if (!path_is_absolute(path
))
1019 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1020 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1024 if (path_equal(path
, "/"))
1027 if (stat(path
, &st
) >= 0) {
1028 if (S_ISBLK(st
.st_mode
))
1029 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1030 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1031 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1039 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1040 * block devices are device local anyway. */
1041 if (!path_startswith(path
, "/dev")) {
1042 r
= make_lock_file_for(path
, operation
, &t
);
1044 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1045 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1052 mkdir_p("/run/systemd/nspawn/locks", 0700);
1054 r
= make_lock_file(p
, operation
, global
);
1056 release_lock_file(&t
);
1060 *global
= (LockFile
) LOCK_FILE_INIT
;
1066 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1069 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1072 if (i
->type
!= IMAGE_SUBVOLUME
)
1075 /* We set the quota both for the subvolume as well as for the
1076 * subtree. The latter is mostly for historical reasons, since
1077 * we didn't use to have a concept of subtree quota, and hence
1078 * only modified the subvolume quota. */
1080 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1081 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1082 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1085 int image_read_metadata(Image
*i
) {
1086 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1091 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1097 case IMAGE_SUBVOLUME
:
1098 case IMAGE_DIRECTORY
: {
1099 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1100 sd_id128_t machine_id
= SD_ID128_NULL
;
1101 _cleanup_free_
char *hostname
= NULL
;
1102 _cleanup_free_
char *path
= NULL
;
1104 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1105 if (r
< 0 && r
!= -ENOENT
)
1106 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1108 r
= read_etc_hostname(path
, &hostname
);
1110 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1115 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1116 if (r
< 0 && r
!= -ENOENT
)
1117 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1119 _cleanup_close_
int fd
= -1;
1121 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1123 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1125 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1127 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1133 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1134 if (r
< 0 && r
!= -ENOENT
)
1135 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1137 r
= load_env_file_pairs(NULL
, path
, NULL
, &machine_info
);
1139 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1142 r
= load_os_release_pairs(i
->path
, &os_release
);
1144 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1146 free_and_replace(i
->hostname
, hostname
);
1147 i
->machine_id
= machine_id
;
1148 strv_free_and_replace(i
->machine_info
, machine_info
);
1149 strv_free_and_replace(i
->os_release
, os_release
);
1156 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1157 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1159 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1163 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1167 r
= dissected_image_acquire_metadata(m
);
1171 free_and_replace(i
->hostname
, m
->hostname
);
1172 i
->machine_id
= m
->machine_id
;
1173 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1174 strv_free_and_replace(i
->os_release
, m
->os_release
);
1183 i
->metadata_valid
= true;
1188 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1194 /* Locks an image name, regardless of the precise path used. */
1196 if (!image_name_is_valid(name
))
1199 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1200 *ret
= (LockFile
) LOCK_FILE_INIT
;
1204 if (streq(name
, ".host"))
1207 mkdir_p("/run/systemd/nspawn/locks", 0700);
1208 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1210 return make_lock_file(p
, operation
, ret
);
1213 bool image_name_is_valid(const char *s
) {
1214 if (!filename_is_valid(s
))
1217 if (string_has_cc(s
, NULL
))
1220 if (!utf8_is_valid(s
))
1223 /* Temporary files for atomically creating new files */
1224 if (startswith(s
, ".#"))
1230 bool image_in_search_path(ImageClass
class, const char *image
) {
1235 NULSTR_FOREACH(path
, image_search_path
[class]) {
1239 p
= path_startswith(image
, path
);
1243 /* Make sure there's a filename following */
1244 k
= strcspn(p
, "/");
1250 /* Accept trailing slashes */
1251 if (p
[strspn(p
, "/")] == 0)
1259 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1260 [IMAGE_DIRECTORY
] = "directory",
1261 [IMAGE_SUBVOLUME
] = "subvolume",
1262 [IMAGE_RAW
] = "raw",
1263 [IMAGE_BLOCK
] = "block",
1266 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);