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
);
73 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(image_hash_ops
, char, string_hash_func
, string_compare_func
,
76 static char **image_settings_path(Image
*image
) {
77 _cleanup_strv_free_
char **l
= NULL
;
87 fn
= strjoina(image
->name
, ".nspawn");
89 FOREACH_STRING(s
, "/etc/systemd/nspawn/", "/run/systemd/nspawn/") {
90 l
[i
] = strappend(s
, fn
);
97 l
[i
] = file_in_same_dir(image
->path
, fn
);
104 static char *image_roothash_path(Image
*image
) {
109 fn
= strjoina(image
->name
, ".roothash");
111 return file_in_same_dir(image
->path
, fn
);
114 static int image_new(
118 const char *filename
,
124 _cleanup_(image_unrefp
) Image
*i
= NULL
;
127 assert(t
< _IMAGE_TYPE_MAX
);
138 i
->read_only
= read_only
;
141 i
->usage
= i
->usage_exclusive
= (uint64_t) -1;
142 i
->limit
= i
->limit_exclusive
= (uint64_t) -1;
144 i
->name
= strdup(pretty
);
149 i
->path
= strjoin(path
, "/", filename
);
151 i
->path
= strdup(filename
);
155 path_simplify(i
->path
, false);
162 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
163 _cleanup_free_
char *name
= NULL
;
170 p
= last_path_component(path
);
173 name
= strndup(p
, n
);
180 e
= endswith(name
, suffix
);
187 if (!image_name_is_valid(name
))
190 *ret
= TAKE_PTR(name
);
194 static int image_make(
198 const char *filename
,
199 const struct stat
*st
,
202 _cleanup_free_
char *pretty_buffer
= NULL
;
207 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
210 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
211 * devices into /var/lib/machines/, and treat them normally.
213 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
217 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
224 (path
&& path_startswith(path
, "/usr")) ||
225 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
227 if (S_ISDIR(st
->st_mode
)) {
228 _cleanup_close_
int fd
= -1;
229 unsigned file_attr
= 0;
235 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
239 pretty
= pretty_buffer
;
242 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
246 /* btrfs subvolumes have inode 256 */
247 if (st
->st_ino
== 256) {
249 r
= btrfs_is_filesystem(fd
);
253 BtrfsSubvolInfo info
;
255 /* It's a btrfs subvolume */
257 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
261 r
= image_new(IMAGE_SUBVOLUME
,
265 info
.read_only
|| read_only
,
272 if (btrfs_quota_scan_ongoing(fd
) == 0) {
273 BtrfsQuotaInfo quota
;
275 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
277 (*ret
)->usage
= quota
.referenced
;
278 (*ret
)->usage_exclusive
= quota
.exclusive
;
280 (*ret
)->limit
= quota
.referenced_max
;
281 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
289 /* If the IMMUTABLE bit is set, we consider the
290 * directory read-only. Since the ioctl is not
291 * supported everywhere we ignore failures. */
292 (void) read_attr_fd(fd
, &file_attr
);
294 /* It's just a normal directory. */
295 r
= image_new(IMAGE_DIRECTORY
,
299 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
308 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
311 /* It's a RAW disk image */
316 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
319 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
323 pretty
= pretty_buffer
;
326 r
= image_new(IMAGE_RAW
,
330 !(st
->st_mode
& 0222) || read_only
,
332 timespec_load(&st
->st_mtim
),
337 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
338 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
342 } else if (S_ISBLK(st
->st_mode
)) {
343 _cleanup_close_
int block_fd
= -1;
344 uint64_t size
= UINT64_MAX
;
352 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
356 pretty
= pretty_buffer
;
359 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
361 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
363 /* Refresh stat data after opening the node */
364 if (fstat(block_fd
, &stbuf
) < 0)
368 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
374 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
375 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
380 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
381 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
, filename
);
383 block_fd
= safe_close(block_fd
);
386 r
= image_new(IMAGE_BLOCK
,
390 !(st
->st_mode
& 0222) || read_only
,
397 if (size
!= 0 && size
!= UINT64_MAX
)
398 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
406 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
411 assert(class < _IMAGE_CLASS_MAX
);
414 /* There are no images with invalid names */
415 if (!image_name_is_valid(name
))
418 NULSTR_FOREACH(path
, image_search_path
[class]) {
419 _cleanup_closedir_
DIR *d
= NULL
;
430 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
431 * symlink block devices into the search path */
432 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
433 _cleanup_free_
char *raw
= NULL
;
438 raw
= strappend(name
, ".raw");
442 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
450 if (!S_ISREG(st
.st_mode
))
453 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
456 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
459 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
461 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
467 (*ret
)->discoverable
= true;
472 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
473 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
478 (*ret
)->discoverable
= true;
486 int image_from_path(const char *path
, Image
**ret
) {
488 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
489 * the image is in the image search path. And if it is we don't know if the path we used is actually not
490 * overridden by another, different image earlier in the search path */
492 if (path_equal(path
, "/"))
493 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
495 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
498 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
499 if (image_name_is_valid(name_or_path
))
500 return image_find(class, name_or_path
, ret
);
502 return image_from_path(name_or_path
, ret
);
505 int image_discover(ImageClass
class, Hashmap
*h
) {
510 assert(class < _IMAGE_CLASS_MAX
);
513 NULSTR_FOREACH(path
, image_search_path
[class]) {
514 _cleanup_closedir_
DIR *d
= NULL
;
525 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
526 _cleanup_(image_unrefp
) Image
*image
= NULL
;
527 _cleanup_free_
char *truncated
= NULL
;
531 if (dot_or_dot_dot(de
->d_name
))
534 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
535 * to symlink block devices into the search path */
536 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
543 if (S_ISREG(st
.st_mode
)) {
546 e
= endswith(de
->d_name
, ".raw");
550 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
555 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
560 if (!image_name_is_valid(pretty
))
563 if (hashmap_contains(h
, pretty
))
566 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
567 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
572 image
->discoverable
= true;
574 r
= hashmap_put(h
, image
->name
, image
);
582 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
583 _cleanup_(image_unrefp
) Image
*image
= NULL
;
585 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
589 image
->discoverable
= true;
591 r
= hashmap_put(h
, image
->name
, image
);
601 int image_remove(Image
*i
) {
602 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
603 _cleanup_strv_free_
char **settings
= NULL
;
604 _cleanup_free_
char *roothash
= NULL
;
610 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
613 settings
= image_settings_path(i
);
617 roothash
= image_roothash_path(i
);
621 /* Make sure we don't interfere with a running nspawn */
622 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
628 case IMAGE_SUBVOLUME
:
630 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
632 if (unlink(i
->path
) < 0) {
633 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
640 case IMAGE_DIRECTORY
:
641 /* Allow deletion of read-only directories */
642 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
643 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
651 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
652 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
653 * the thing (it's most likely a symlink after all). */
655 if (path_startswith(i
->path
, "/dev"))
660 if (unlink(i
->path
) < 0)
668 STRV_FOREACH(j
, settings
) {
669 if (unlink(*j
) < 0 && errno
!= ENOENT
)
670 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
673 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
674 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
679 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
680 _cleanup_free_
char *rs
= NULL
;
683 fn
= strjoina(new_name
, suffix
);
685 rs
= file_in_same_dir(path
, fn
);
689 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
692 int image_rename(Image
*i
, const char *new_name
) {
693 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
694 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
695 _cleanup_strv_free_
char **settings
= NULL
;
696 unsigned file_attr
= 0;
702 if (!image_name_is_valid(new_name
))
705 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
708 settings
= image_settings_path(i
);
712 roothash
= image_roothash_path(i
);
716 /* Make sure we don't interfere with a running nspawn */
717 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
721 /* Make sure nobody takes the new name, between the time we
722 * checked it is currently unused in all search paths, and the
723 * time we take possession of it */
724 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
728 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
736 case IMAGE_DIRECTORY
:
737 /* Turn of the immutable bit while we rename the image, so that we can rename it */
738 (void) read_attr_path(i
->path
, &file_attr
);
740 if (file_attr
& FS_IMMUTABLE_FL
)
741 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
744 case IMAGE_SUBVOLUME
:
745 new_path
= file_in_same_dir(i
->path
, new_name
);
750 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
751 if (path_startswith(i
->path
, "/dev"))
754 new_path
= file_in_same_dir(i
->path
, new_name
);
760 fn
= strjoina(new_name
, ".raw");
761 new_path
= file_in_same_dir(i
->path
, fn
);
772 nn
= strdup(new_name
);
776 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
780 /* Restore the immutable bit, if it was set before */
781 if (file_attr
& FS_IMMUTABLE_FL
)
782 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
784 free_and_replace(i
->path
, new_path
);
785 free_and_replace(i
->name
, nn
);
787 STRV_FOREACH(j
, settings
) {
788 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
789 if (r
< 0 && r
!= -ENOENT
)
790 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
793 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
794 if (r
< 0 && r
!= -ENOENT
)
795 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
800 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
801 _cleanup_free_
char *rs
= NULL
;
804 fn
= strjoina(new_name
, suffix
);
806 rs
= file_in_same_dir(path
, fn
);
810 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
813 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
814 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
815 _cleanup_strv_free_
char **settings
= NULL
;
816 _cleanup_free_
char *roothash
= NULL
;
817 const char *new_path
;
823 if (!image_name_is_valid(new_name
))
826 settings
= image_settings_path(i
);
830 roothash
= image_roothash_path(i
);
834 /* Make sure nobody takes the new name, between the time we
835 * checked it is currently unused in all search paths, and the
836 * time we take possession of it */
837 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
841 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
849 case IMAGE_SUBVOLUME
:
850 case IMAGE_DIRECTORY
:
851 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
854 new_path
= strjoina("/var/lib/machines/", new_name
);
856 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
857 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
858 BTRFS_SNAPSHOT_FALLBACK_COPY
|
859 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
860 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
861 BTRFS_SNAPSHOT_RECURSIVE
|
862 BTRFS_SNAPSHOT_QUOTA
);
864 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
865 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
870 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
872 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
);
883 STRV_FOREACH(j
, settings
) {
884 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
885 if (r
< 0 && r
!= -ENOENT
)
886 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
889 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
890 if (r
< 0 && r
!= -ENOENT
)
891 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
896 int image_read_only(Image
*i
, bool b
) {
897 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
902 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
905 /* Make sure we don't interfere with a running nspawn */
906 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
912 case IMAGE_SUBVOLUME
:
914 /* Note that we set the flag only on the top-level
915 * subvolume of the image. */
917 r
= btrfs_subvol_set_read_only(i
->path
, b
);
923 case IMAGE_DIRECTORY
:
924 /* For simple directory trees we cannot use the access
925 mode of the top-level directory, since it has an
926 effect on the container itself. However, we can
927 use the "immutable" flag, to at least make the
928 top-level directory read-only. It's not as good as
929 a read-only subvolume, but at least something, and
930 we can read the value back. */
932 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
, NULL
);
941 if (stat(i
->path
, &st
) < 0)
944 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
947 /* If the images is now read-only, it's a good time to
948 * defrag it, given that no write patterns will
949 * fragment it again. */
951 (void) btrfs_defrag(i
->path
);
956 _cleanup_close_
int fd
= -1;
960 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
964 if (fstat(fd
, &st
) < 0)
966 if (!S_ISBLK(st
.st_mode
))
969 if (ioctl(fd
, BLKROSET
, &state
) < 0)
982 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
983 _cleanup_free_
char *p
= NULL
;
984 LockFile t
= LOCK_FILE_INIT
;
992 /* Locks an image path. This actually creates two locks: one
993 * "local" one, next to the image path itself, which might be
994 * shared via NFS. And another "global" one, in /run, that
995 * uses the device/inode number. This has the benefit that we
996 * can even lock a tree that is a mount point, correctly. */
998 if (!path_is_absolute(path
))
1001 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1002 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1006 if (path_equal(path
, "/"))
1009 if (stat(path
, &st
) >= 0) {
1010 if (S_ISBLK(st
.st_mode
))
1011 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1012 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1013 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1021 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1022 * block devices are device local anyway. */
1023 if (!path_startswith(path
, "/dev")) {
1024 r
= make_lock_file_for(path
, operation
, &t
);
1026 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1027 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1034 mkdir_p("/run/systemd/nspawn/locks", 0700);
1036 r
= make_lock_file(p
, operation
, global
);
1038 release_lock_file(&t
);
1042 *global
= (LockFile
) LOCK_FILE_INIT
;
1048 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1051 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1054 if (i
->type
!= IMAGE_SUBVOLUME
)
1057 /* We set the quota both for the subvolume as well as for the
1058 * subtree. The latter is mostly for historical reasons, since
1059 * we didn't use to have a concept of subtree quota, and hence
1060 * only modified the subvolume quota. */
1062 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1063 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1064 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1067 int image_read_metadata(Image
*i
) {
1068 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1073 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1079 case IMAGE_SUBVOLUME
:
1080 case IMAGE_DIRECTORY
: {
1081 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1082 sd_id128_t machine_id
= SD_ID128_NULL
;
1083 _cleanup_free_
char *hostname
= NULL
;
1084 _cleanup_free_
char *path
= NULL
;
1086 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1087 if (r
< 0 && r
!= -ENOENT
)
1088 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1090 r
= read_etc_hostname(path
, &hostname
);
1092 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1097 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1098 if (r
< 0 && r
!= -ENOENT
)
1099 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1101 _cleanup_close_
int fd
= -1;
1103 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1105 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1107 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1109 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1115 r
= chase_symlinks("/etc/machine-info", 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-info in image %s: %m", i
->name
);
1119 r
= load_env_file_pairs(NULL
, path
, &machine_info
);
1121 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1124 r
= load_os_release_pairs(i
->path
, &os_release
);
1126 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1128 free_and_replace(i
->hostname
, hostname
);
1129 i
->machine_id
= machine_id
;
1130 strv_free_and_replace(i
->machine_info
, machine_info
);
1131 strv_free_and_replace(i
->os_release
, os_release
);
1138 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1139 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1141 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1145 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1149 r
= dissected_image_acquire_metadata(m
);
1153 free_and_replace(i
->hostname
, m
->hostname
);
1154 i
->machine_id
= m
->machine_id
;
1155 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1156 strv_free_and_replace(i
->os_release
, m
->os_release
);
1165 i
->metadata_valid
= true;
1170 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1176 /* Locks an image name, regardless of the precise path used. */
1178 if (!image_name_is_valid(name
))
1181 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1182 *ret
= (LockFile
) LOCK_FILE_INIT
;
1186 if (streq(name
, ".host"))
1189 mkdir_p("/run/systemd/nspawn/locks", 0700);
1190 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1192 return make_lock_file(p
, operation
, ret
);
1195 bool image_name_is_valid(const char *s
) {
1196 if (!filename_is_valid(s
))
1199 if (string_has_cc(s
, NULL
))
1202 if (!utf8_is_valid(s
))
1205 /* Temporary files for atomically creating new files */
1206 if (startswith(s
, ".#"))
1212 bool image_in_search_path(ImageClass
class, const char *image
) {
1217 NULSTR_FOREACH(path
, image_search_path
[class]) {
1221 p
= path_startswith(image
, path
);
1225 /* Make sure there's a filename following */
1226 k
= strcspn(p
, "/");
1232 /* Accept trailing slashes */
1233 if (p
[strspn(p
, "/")] == 0)
1241 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1242 [IMAGE_DIRECTORY
] = "directory",
1243 [IMAGE_SUBVOLUME
] = "subvolume",
1244 [IMAGE_RAW
] = "raw",
1245 [IMAGE_BLOCK
] = "block",
1248 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);