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
] = "/var/lib/machines\0"
51 "/var/lib/container\0" /* legacy */
52 "/usr/local/lib/machines\0"
53 "/usr/lib/machines\0",
55 [IMAGE_PORTABLE
] = "/var/lib/portables\0"
56 "/usr/local/lib/portables\0"
57 "/usr/lib/portables\0",
60 Image
*image_unref(Image
*i
) {
68 strv_free(i
->machine_info
);
69 strv_free(i
->os_release
);
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
);
135 i
->read_only
= read_only
;
138 i
->usage
= i
->usage_exclusive
= (uint64_t) -1;
139 i
->limit
= i
->limit_exclusive
= (uint64_t) -1;
141 i
->name
= strdup(pretty
);
146 i
->path
= strjoin(path
, "/", filename
);
148 i
->path
= strdup(filename
);
152 path_kill_slashes(i
->path
);
159 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
160 _cleanup_free_
char *name
= NULL
;
167 p
= last_path_component(path
);
170 name
= strndup(p
, n
);
177 e
= endswith(name
, suffix
);
184 if (!image_name_is_valid(name
))
187 *ret
= TAKE_PTR(name
);
191 static int image_make(
195 const char *filename
,
196 const struct stat
*st
,
199 _cleanup_free_
char *pretty_buffer
= NULL
;
204 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
207 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
208 * devices into /var/lib/machines/, and treat them normally.
210 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
214 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
221 (path
&& path_startswith(path
, "/usr")) ||
222 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
224 if (S_ISDIR(st
->st_mode
)) {
225 _cleanup_close_
int fd
= -1;
226 unsigned file_attr
= 0;
232 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
236 pretty
= pretty_buffer
;
239 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
243 /* btrfs subvolumes have inode 256 */
244 if (st
->st_ino
== 256) {
246 r
= btrfs_is_filesystem(fd
);
250 BtrfsSubvolInfo info
;
252 /* It's a btrfs subvolume */
254 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
258 r
= image_new(IMAGE_SUBVOLUME
,
262 info
.read_only
|| read_only
,
269 if (btrfs_quota_scan_ongoing(fd
) == 0) {
270 BtrfsQuotaInfo quota
;
272 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
274 (*ret
)->usage
= quota
.referenced
;
275 (*ret
)->usage_exclusive
= quota
.exclusive
;
277 (*ret
)->limit
= quota
.referenced_max
;
278 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
286 /* If the IMMUTABLE bit is set, we consider the
287 * directory read-only. Since the ioctl is not
288 * supported everywhere we ignore failures. */
289 (void) read_attr_fd(fd
, &file_attr
);
291 /* It's just a normal directory. */
292 r
= image_new(IMAGE_DIRECTORY
,
296 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
305 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
308 /* It's a RAW disk image */
313 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
316 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
320 pretty
= pretty_buffer
;
323 r
= image_new(IMAGE_RAW
,
327 !(st
->st_mode
& 0222) || read_only
,
329 timespec_load(&st
->st_mtim
),
334 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
335 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
339 } else if (S_ISBLK(st
->st_mode
)) {
340 _cleanup_close_
int block_fd
= -1;
341 uint64_t size
= UINT64_MAX
;
349 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
353 pretty
= pretty_buffer
;
356 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
358 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
360 /* Refresh stat data after opening the node */
361 if (fstat(block_fd
, &stbuf
) < 0)
365 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
371 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
372 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
377 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
378 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
, filename
);
380 block_fd
= safe_close(block_fd
);
383 r
= image_new(IMAGE_BLOCK
,
387 !(st
->st_mode
& 0222) || read_only
,
394 if (size
!= 0 && size
!= UINT64_MAX
)
395 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
403 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
408 assert(class < _IMAGE_CLASS_MAX
);
411 /* There are no images with invalid names */
412 if (!image_name_is_valid(name
))
415 NULSTR_FOREACH(path
, image_search_path
[class]) {
416 _cleanup_closedir_
DIR *d
= NULL
;
427 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
428 * symlink block devices into the search path */
429 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
430 _cleanup_free_
char *raw
= NULL
;
435 raw
= strappend(name
, ".raw");
439 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
447 if (!S_ISREG(st
.st_mode
))
450 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
453 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
456 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
458 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
466 if (class == IMAGE_MACHINE
&& streq(name
, ".host"))
467 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
472 int image_from_path(const char *path
, Image
**ret
) {
473 if (path_equal(path
, "/"))
474 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
476 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
479 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
480 if (image_name_is_valid(name_or_path
))
481 return image_find(class, name_or_path
, ret
);
483 return image_from_path(name_or_path
, ret
);
486 int image_discover(ImageClass
class, Hashmap
*h
) {
491 assert(class < _IMAGE_CLASS_MAX
);
494 NULSTR_FOREACH(path
, image_search_path
[class]) {
495 _cleanup_closedir_
DIR *d
= NULL
;
506 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
507 _cleanup_(image_unrefp
) Image
*image
= NULL
;
508 _cleanup_free_
char *truncated
= NULL
;
512 if (dot_or_dot_dot(de
->d_name
))
515 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
516 * to symlink block devices into the search path */
517 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
524 if (S_ISREG(st
.st_mode
)) {
527 e
= endswith(de
->d_name
, ".raw");
531 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
536 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
541 if (!image_name_is_valid(pretty
))
544 if (hashmap_contains(h
, pretty
))
547 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
548 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
553 r
= hashmap_put(h
, image
->name
, image
);
561 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
562 _cleanup_(image_unrefp
) Image
*image
= NULL
;
564 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
568 r
= hashmap_put(h
, image
->name
, image
);
579 int image_remove(Image
*i
) {
580 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
581 _cleanup_strv_free_
char **settings
= NULL
;
582 _cleanup_free_
char *roothash
= NULL
;
588 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
591 settings
= image_settings_path(i
);
595 roothash
= image_roothash_path(i
);
599 /* Make sure we don't interfere with a running nspawn */
600 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
606 case IMAGE_SUBVOLUME
:
608 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
610 if (unlink(i
->path
) < 0) {
611 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
618 case IMAGE_DIRECTORY
:
619 /* Allow deletion of read-only directories */
620 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
621 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
629 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
630 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
631 * the thing (it's most likely a symlink after all). */
633 if (path_startswith(i
->path
, "/dev"))
638 if (unlink(i
->path
) < 0)
646 STRV_FOREACH(j
, settings
) {
647 if (unlink(*j
) < 0 && errno
!= ENOENT
)
648 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
651 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
652 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
657 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
658 _cleanup_free_
char *rs
= NULL
;
661 fn
= strjoina(new_name
, suffix
);
663 rs
= file_in_same_dir(path
, fn
);
667 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
670 int image_rename(Image
*i
, const char *new_name
) {
671 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
672 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
673 _cleanup_strv_free_
char **settings
= NULL
;
674 unsigned file_attr
= 0;
680 if (!image_name_is_valid(new_name
))
683 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
686 settings
= image_settings_path(i
);
690 roothash
= image_roothash_path(i
);
694 /* Make sure we don't interfere with a running nspawn */
695 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
699 /* Make sure nobody takes the new name, between the time we
700 * checked it is currently unused in all search paths, and the
701 * time we take possession of it */
702 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
706 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
714 case IMAGE_DIRECTORY
:
715 /* Turn of the immutable bit while we rename the image, so that we can rename it */
716 (void) read_attr_path(i
->path
, &file_attr
);
718 if (file_attr
& FS_IMMUTABLE_FL
)
719 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
722 case IMAGE_SUBVOLUME
:
723 new_path
= file_in_same_dir(i
->path
, new_name
);
728 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
729 if (path_startswith(i
->path
, "/dev"))
732 new_path
= file_in_same_dir(i
->path
, new_name
);
738 fn
= strjoina(new_name
, ".raw");
739 new_path
= file_in_same_dir(i
->path
, fn
);
750 nn
= strdup(new_name
);
754 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
758 /* Restore the immutable bit, if it was set before */
759 if (file_attr
& FS_IMMUTABLE_FL
)
760 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
);
762 free_and_replace(i
->path
, new_path
);
763 free_and_replace(i
->name
, nn
);
765 STRV_FOREACH(j
, settings
) {
766 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
767 if (r
< 0 && r
!= -ENOENT
)
768 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
771 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
772 if (r
< 0 && r
!= -ENOENT
)
773 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
778 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
779 _cleanup_free_
char *rs
= NULL
;
782 fn
= strjoina(new_name
, suffix
);
784 rs
= file_in_same_dir(path
, fn
);
788 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
791 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
792 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
793 _cleanup_strv_free_
char **settings
= NULL
;
794 _cleanup_free_
char *roothash
= NULL
;
795 const char *new_path
;
801 if (!image_name_is_valid(new_name
))
804 settings
= image_settings_path(i
);
808 roothash
= image_roothash_path(i
);
812 /* Make sure nobody takes the new name, between the time we
813 * checked it is currently unused in all search paths, and the
814 * time we take possession of it */
815 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
819 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
827 case IMAGE_SUBVOLUME
:
828 case IMAGE_DIRECTORY
:
829 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
832 new_path
= strjoina("/var/lib/machines/", new_name
);
834 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
835 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
836 BTRFS_SNAPSHOT_FALLBACK_COPY
|
837 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
838 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
839 BTRFS_SNAPSHOT_RECURSIVE
|
840 BTRFS_SNAPSHOT_QUOTA
);
842 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
843 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
848 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
850 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
);
861 STRV_FOREACH(j
, settings
) {
862 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
863 if (r
< 0 && r
!= -ENOENT
)
864 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
867 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
868 if (r
< 0 && r
!= -ENOENT
)
869 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
874 int image_read_only(Image
*i
, bool b
) {
875 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
880 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
883 /* Make sure we don't interfere with a running nspawn */
884 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
890 case IMAGE_SUBVOLUME
:
892 /* Note that we set the flag only on the top-level
893 * subvolume of the image. */
895 r
= btrfs_subvol_set_read_only(i
->path
, b
);
901 case IMAGE_DIRECTORY
:
902 /* For simple directory trees we cannot use the access
903 mode of the top-level directory, since it has an
904 effect on the container itself. However, we can
905 use the "immutable" flag, to at least make the
906 top-level directory read-only. It's not as good as
907 a read-only subvolume, but at least something, and
908 we can read the value back. */
910 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
);
919 if (stat(i
->path
, &st
) < 0)
922 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
925 /* If the images is now read-only, it's a good time to
926 * defrag it, given that no write patterns will
927 * fragment it again. */
929 (void) btrfs_defrag(i
->path
);
934 _cleanup_close_
int fd
= -1;
938 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
942 if (fstat(fd
, &st
) < 0)
944 if (!S_ISBLK(st
.st_mode
))
947 if (ioctl(fd
, BLKROSET
, &state
) < 0)
960 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
961 _cleanup_free_
char *p
= NULL
;
962 LockFile t
= LOCK_FILE_INIT
;
970 /* Locks an image path. This actually creates two locks: one
971 * "local" one, next to the image path itself, which might be
972 * shared via NFS. And another "global" one, in /run, that
973 * uses the device/inode number. This has the benefit that we
974 * can even lock a tree that is a mount point, correctly. */
976 if (!path_is_absolute(path
))
979 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
980 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
984 if (path_equal(path
, "/"))
987 if (stat(path
, &st
) >= 0) {
988 if (S_ISBLK(st
.st_mode
))
989 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
990 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
991 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
999 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1000 * block devices are device local anyway. */
1001 if (!path_startswith(path
, "/dev")) {
1002 r
= make_lock_file_for(path
, operation
, &t
);
1004 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1005 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1012 mkdir_p("/run/systemd/nspawn/locks", 0700);
1014 r
= make_lock_file(p
, operation
, global
);
1016 release_lock_file(&t
);
1020 *global
= (LockFile
) LOCK_FILE_INIT
;
1026 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1029 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1032 if (i
->type
!= IMAGE_SUBVOLUME
)
1035 /* We set the quota both for the subvolume as well as for the
1036 * subtree. The latter is mostly for historical reasons, since
1037 * we didn't use to have a concept of subtree quota, and hence
1038 * only modified the subvolume quota. */
1040 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1041 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1042 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1045 int image_read_metadata(Image
*i
) {
1046 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1051 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1057 case IMAGE_SUBVOLUME
:
1058 case IMAGE_DIRECTORY
: {
1059 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1060 sd_id128_t machine_id
= SD_ID128_NULL
;
1061 _cleanup_free_
char *hostname
= NULL
;
1062 _cleanup_free_
char *path
= NULL
;
1063 _cleanup_fclose_
FILE *f
= NULL
;
1065 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1066 if (r
< 0 && r
!= -ENOENT
)
1067 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1069 r
= read_etc_hostname(path
, &hostname
);
1071 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1076 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1077 if (r
< 0 && r
!= -ENOENT
)
1078 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1080 _cleanup_close_
int fd
= -1;
1082 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1084 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1086 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1088 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1094 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1095 if (r
< 0 && r
!= -ENOENT
)
1096 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1098 r
= load_env_file_pairs(NULL
, path
, NULL
, &machine_info
);
1100 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1103 r
= load_os_release_pairs(i
->path
, &os_release
);
1105 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1107 free_and_replace(i
->hostname
, hostname
);
1108 i
->machine_id
= machine_id
;
1109 strv_free_and_replace(i
->machine_info
, machine_info
);
1110 strv_free_and_replace(i
->os_release
, os_release
);
1117 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1118 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1120 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1124 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1128 r
= dissected_image_acquire_metadata(m
);
1132 free_and_replace(i
->hostname
, m
->hostname
);
1133 i
->machine_id
= m
->machine_id
;
1134 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1135 strv_free_and_replace(i
->os_release
, m
->os_release
);
1144 i
->metadata_valid
= true;
1149 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1155 /* Locks an image name, regardless of the precise path used. */
1157 if (!image_name_is_valid(name
))
1160 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1161 *ret
= (LockFile
) LOCK_FILE_INIT
;
1165 if (streq(name
, ".host"))
1168 mkdir_p("/run/systemd/nspawn/locks", 0700);
1169 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1171 return make_lock_file(p
, operation
, ret
);
1174 bool image_name_is_valid(const char *s
) {
1175 if (!filename_is_valid(s
))
1178 if (string_has_cc(s
, NULL
))
1181 if (!utf8_is_valid(s
))
1184 /* Temporary files for atomically creating new files */
1185 if (startswith(s
, ".#"))
1191 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1192 [IMAGE_DIRECTORY
] = "directory",
1193 [IMAGE_SUBVOLUME
] = "subvolume",
1194 [IMAGE_RAW
] = "raw",
1195 [IMAGE_BLOCK
] = "block",
1198 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);