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
;
208 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
211 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
212 * devices into /var/lib/machines/, and treat them normally.
214 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
218 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
225 (path
&& path_startswith(path
, "/usr")) ||
226 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
228 if (S_ISDIR(st
->st_mode
)) {
229 _cleanup_close_
int fd
= -1;
230 unsigned file_attr
= 0;
236 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
240 pretty
= pretty_buffer
;
243 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
247 /* btrfs subvolumes have inode 256 */
248 if (st
->st_ino
== 256) {
250 r
= btrfs_is_filesystem(fd
);
254 BtrfsSubvolInfo info
;
256 /* It's a btrfs subvolume */
258 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
262 r
= image_new(IMAGE_SUBVOLUME
,
266 info
.read_only
|| read_only
,
273 if (btrfs_quota_scan_ongoing(fd
) == 0) {
274 BtrfsQuotaInfo quota
;
276 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
278 (*ret
)->usage
= quota
.referenced
;
279 (*ret
)->usage_exclusive
= quota
.exclusive
;
281 (*ret
)->limit
= quota
.referenced_max
;
282 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
290 /* If the IMMUTABLE bit is set, we consider the
291 * directory read-only. Since the ioctl is not
292 * supported everywhere we ignore failures. */
293 (void) read_attr_fd(fd
, &file_attr
);
295 /* It's just a normal directory. */
296 r
= image_new(IMAGE_DIRECTORY
,
300 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
309 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
312 /* It's a RAW disk image */
317 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
320 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
324 pretty
= pretty_buffer
;
327 r
= image_new(IMAGE_RAW
,
331 !(st
->st_mode
& 0222) || read_only
,
333 timespec_load(&st
->st_mtim
),
338 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
339 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
343 } else if (S_ISBLK(st
->st_mode
)) {
344 _cleanup_close_
int block_fd
= -1;
345 uint64_t size
= UINT64_MAX
;
353 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
357 pretty
= pretty_buffer
;
360 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
362 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
364 /* Refresh stat data after opening the node */
365 if (fstat(block_fd
, &stbuf
) < 0)
369 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
375 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
376 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
381 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
382 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
, filename
);
384 block_fd
= safe_close(block_fd
);
387 r
= image_new(IMAGE_BLOCK
,
391 !(st
->st_mode
& 0222) || read_only
,
398 if (size
!= 0 && size
!= UINT64_MAX
)
399 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
407 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
412 assert(class < _IMAGE_CLASS_MAX
);
415 /* There are no images with invalid names */
416 if (!image_name_is_valid(name
))
419 NULSTR_FOREACH(path
, image_search_path
[class]) {
420 _cleanup_closedir_
DIR *d
= NULL
;
431 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
432 * symlink block devices into the search path */
433 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
434 _cleanup_free_
char *raw
= NULL
;
439 raw
= strappend(name
, ".raw");
443 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
451 if (!S_ISREG(st
.st_mode
))
454 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
457 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
460 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
462 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
468 (*ret
)->discoverable
= true;
473 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
474 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
479 (*ret
)->discoverable
= true;
487 int image_from_path(const char *path
, Image
**ret
) {
489 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
490 * the image is in the image search path. And if it is we don't know if the path we used is actually not
491 * overridden by another, different image earlier in the search path */
493 if (path_equal(path
, "/"))
494 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
496 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
499 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
500 if (image_name_is_valid(name_or_path
))
501 return image_find(class, name_or_path
, ret
);
503 return image_from_path(name_or_path
, ret
);
506 int image_discover(ImageClass
class, Hashmap
*h
) {
511 assert(class < _IMAGE_CLASS_MAX
);
514 NULSTR_FOREACH(path
, image_search_path
[class]) {
515 _cleanup_closedir_
DIR *d
= NULL
;
526 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
527 _cleanup_(image_unrefp
) Image
*image
= NULL
;
528 _cleanup_free_
char *truncated
= NULL
;
532 if (dot_or_dot_dot(de
->d_name
))
535 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
536 * to symlink block devices into the search path */
537 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
544 if (S_ISREG(st
.st_mode
)) {
547 e
= endswith(de
->d_name
, ".raw");
551 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
556 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
561 if (!image_name_is_valid(pretty
))
564 if (hashmap_contains(h
, pretty
))
567 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
568 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
573 image
->discoverable
= true;
575 r
= hashmap_put(h
, image
->name
, image
);
583 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
584 _cleanup_(image_unrefp
) Image
*image
= NULL
;
586 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
590 image
->discoverable
= true;
592 r
= hashmap_put(h
, image
->name
, image
);
602 int image_remove(Image
*i
) {
603 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
604 _cleanup_strv_free_
char **settings
= NULL
;
605 _cleanup_free_
char *roothash
= NULL
;
611 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
614 settings
= image_settings_path(i
);
618 roothash
= image_roothash_path(i
);
622 /* Make sure we don't interfere with a running nspawn */
623 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
629 case IMAGE_SUBVOLUME
:
631 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
633 if (unlink(i
->path
) < 0) {
634 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
641 case IMAGE_DIRECTORY
:
642 /* Allow deletion of read-only directories */
643 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
644 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
652 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
653 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
654 * the thing (it's most likely a symlink after all). */
656 if (path_startswith(i
->path
, "/dev"))
661 if (unlink(i
->path
) < 0)
669 STRV_FOREACH(j
, settings
) {
670 if (unlink(*j
) < 0 && errno
!= ENOENT
)
671 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
674 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
675 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
680 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
681 _cleanup_free_
char *rs
= NULL
;
684 fn
= strjoina(new_name
, suffix
);
686 rs
= file_in_same_dir(path
, fn
);
690 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
693 int image_rename(Image
*i
, const char *new_name
) {
694 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
695 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
696 _cleanup_strv_free_
char **settings
= NULL
;
697 unsigned file_attr
= 0;
703 if (!image_name_is_valid(new_name
))
706 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
709 settings
= image_settings_path(i
);
713 roothash
= image_roothash_path(i
);
717 /* Make sure we don't interfere with a running nspawn */
718 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
722 /* Make sure nobody takes the new name, between the time we
723 * checked it is currently unused in all search paths, and the
724 * time we take possession of it */
725 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
729 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
737 case IMAGE_DIRECTORY
:
738 /* Turn of the immutable bit while we rename the image, so that we can rename it */
739 (void) read_attr_path(i
->path
, &file_attr
);
741 if (file_attr
& FS_IMMUTABLE_FL
)
742 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
, NULL
);
745 case IMAGE_SUBVOLUME
:
746 new_path
= file_in_same_dir(i
->path
, new_name
);
751 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
752 if (path_startswith(i
->path
, "/dev"))
755 new_path
= file_in_same_dir(i
->path
, new_name
);
761 fn
= strjoina(new_name
, ".raw");
762 new_path
= file_in_same_dir(i
->path
, fn
);
773 nn
= strdup(new_name
);
777 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
781 /* Restore the immutable bit, if it was set before */
782 if (file_attr
& FS_IMMUTABLE_FL
)
783 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
, NULL
);
785 free_and_replace(i
->path
, new_path
);
786 free_and_replace(i
->name
, nn
);
788 STRV_FOREACH(j
, settings
) {
789 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
790 if (r
< 0 && r
!= -ENOENT
)
791 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
794 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
795 if (r
< 0 && r
!= -ENOENT
)
796 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
801 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
802 _cleanup_free_
char *rs
= NULL
;
805 fn
= strjoina(new_name
, suffix
);
807 rs
= file_in_same_dir(path
, fn
);
811 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
814 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
815 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
816 _cleanup_strv_free_
char **settings
= NULL
;
817 _cleanup_free_
char *roothash
= NULL
;
818 const char *new_path
;
824 if (!image_name_is_valid(new_name
))
827 settings
= image_settings_path(i
);
831 roothash
= image_roothash_path(i
);
835 /* Make sure nobody takes the new name, between the time we
836 * checked it is currently unused in all search paths, and the
837 * time we take possession of it */
838 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
842 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
850 case IMAGE_SUBVOLUME
:
851 case IMAGE_DIRECTORY
:
852 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
855 new_path
= strjoina("/var/lib/machines/", new_name
);
857 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
858 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
859 BTRFS_SNAPSHOT_FALLBACK_COPY
|
860 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
861 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
862 BTRFS_SNAPSHOT_RECURSIVE
|
863 BTRFS_SNAPSHOT_QUOTA
);
865 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
866 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
871 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
873 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
|COPY_CRTIME
);
884 STRV_FOREACH(j
, settings
) {
885 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
886 if (r
< 0 && r
!= -ENOENT
)
887 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
890 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
891 if (r
< 0 && r
!= -ENOENT
)
892 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
897 int image_read_only(Image
*i
, bool b
) {
898 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
903 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
906 /* Make sure we don't interfere with a running nspawn */
907 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
913 case IMAGE_SUBVOLUME
:
915 /* Note that we set the flag only on the top-level
916 * subvolume of the image. */
918 r
= btrfs_subvol_set_read_only(i
->path
, b
);
924 case IMAGE_DIRECTORY
:
925 /* For simple directory trees we cannot use the access
926 mode of the top-level directory, since it has an
927 effect on the container itself. However, we can
928 use the "immutable" flag, to at least make the
929 top-level directory read-only. It's not as good as
930 a read-only subvolume, but at least something, and
931 we can read the value back. */
933 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
, NULL
);
942 if (stat(i
->path
, &st
) < 0)
945 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
948 /* If the images is now read-only, it's a good time to
949 * defrag it, given that no write patterns will
950 * fragment it again. */
952 (void) btrfs_defrag(i
->path
);
957 _cleanup_close_
int fd
= -1;
961 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
965 if (fstat(fd
, &st
) < 0)
967 if (!S_ISBLK(st
.st_mode
))
970 if (ioctl(fd
, BLKROSET
, &state
) < 0)
983 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
984 _cleanup_free_
char *p
= NULL
;
985 LockFile t
= LOCK_FILE_INIT
;
993 /* Locks an image path. This actually creates two locks: one
994 * "local" one, next to the image path itself, which might be
995 * shared via NFS. And another "global" one, in /run, that
996 * uses the device/inode number. This has the benefit that we
997 * can even lock a tree that is a mount point, correctly. */
999 if (!path_is_absolute(path
))
1002 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1003 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1007 if (path_equal(path
, "/"))
1010 if (stat(path
, &st
) >= 0) {
1011 if (S_ISBLK(st
.st_mode
))
1012 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1013 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1014 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1022 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1023 * block devices are device local anyway. */
1024 if (!path_startswith(path
, "/dev")) {
1025 r
= make_lock_file_for(path
, operation
, &t
);
1027 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1028 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1035 mkdir_p("/run/systemd/nspawn/locks", 0700);
1037 r
= make_lock_file(p
, operation
, global
);
1039 release_lock_file(&t
);
1043 *global
= (LockFile
) LOCK_FILE_INIT
;
1049 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1052 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1055 if (i
->type
!= IMAGE_SUBVOLUME
)
1058 /* We set the quota both for the subvolume as well as for the
1059 * subtree. The latter is mostly for historical reasons, since
1060 * we didn't use to have a concept of subtree quota, and hence
1061 * only modified the subvolume quota. */
1063 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1064 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1065 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1068 int image_read_metadata(Image
*i
) {
1069 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1074 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1080 case IMAGE_SUBVOLUME
:
1081 case IMAGE_DIRECTORY
: {
1082 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1083 sd_id128_t machine_id
= SD_ID128_NULL
;
1084 _cleanup_free_
char *hostname
= NULL
;
1085 _cleanup_free_
char *path
= NULL
;
1087 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1088 if (r
< 0 && r
!= -ENOENT
)
1089 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1091 r
= read_etc_hostname(path
, &hostname
);
1093 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1098 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1099 if (r
< 0 && r
!= -ENOENT
)
1100 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1102 _cleanup_close_
int fd
= -1;
1104 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1106 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1108 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1110 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1116 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1117 if (r
< 0 && r
!= -ENOENT
)
1118 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1120 r
= load_env_file_pairs(NULL
, path
, &machine_info
);
1122 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1125 r
= load_os_release_pairs(i
->path
, &os_release
);
1127 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1129 free_and_replace(i
->hostname
, hostname
);
1130 i
->machine_id
= machine_id
;
1131 strv_free_and_replace(i
->machine_info
, machine_info
);
1132 strv_free_and_replace(i
->os_release
, os_release
);
1139 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1140 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1142 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1146 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1150 r
= dissected_image_acquire_metadata(m
);
1154 free_and_replace(i
->hostname
, m
->hostname
);
1155 i
->machine_id
= m
->machine_id
;
1156 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1157 strv_free_and_replace(i
->os_release
, m
->os_release
);
1166 i
->metadata_valid
= true;
1171 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1177 /* Locks an image name, regardless of the precise path used. */
1179 if (!image_name_is_valid(name
))
1182 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1183 *ret
= (LockFile
) LOCK_FILE_INIT
;
1187 if (streq(name
, ".host"))
1190 mkdir_p("/run/systemd/nspawn/locks", 0700);
1191 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1193 return make_lock_file(p
, operation
, ret
);
1196 bool image_name_is_valid(const char *s
) {
1197 if (!filename_is_valid(s
))
1200 if (string_has_cc(s
, NULL
))
1203 if (!utf8_is_valid(s
))
1206 /* Temporary files for atomically creating new files */
1207 if (startswith(s
, ".#"))
1213 bool image_in_search_path(ImageClass
class, const char *image
) {
1218 NULSTR_FOREACH(path
, image_search_path
[class]) {
1222 p
= path_startswith(image
, path
);
1226 /* Make sure there's a filename following */
1227 k
= strcspn(p
, "/");
1233 /* Accept trailing slashes */
1234 if (p
[strspn(p
, "/")] == 0)
1242 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1243 [IMAGE_DIRECTORY
] = "directory",
1244 [IMAGE_SUBVOLUME
] = "subvolume",
1245 [IMAGE_RAW
] = "raw",
1246 [IMAGE_BLOCK
] = "block",
1249 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);