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
) {
74 strv_free(i
->machine_info
);
75 strv_free(i
->os_release
);
80 Image
*image_ref(Image
*i
) {
90 static char **image_settings_path(Image
*image
) {
91 _cleanup_strv_free_
char **l
= NULL
;
101 fn
= strjoina(image
->name
, ".nspawn");
103 FOREACH_STRING(s
, "/etc/systemd/nspawn/", "/run/systemd/nspawn/") {
104 l
[i
] = strappend(s
, fn
);
111 l
[i
] = file_in_same_dir(image
->path
, fn
);
118 static char *image_roothash_path(Image
*image
) {
123 fn
= strjoina(image
->name
, ".roothash");
125 return file_in_same_dir(image
->path
, fn
);
128 static int image_new(
132 const char *filename
,
138 _cleanup_(image_unrefp
) Image
*i
= NULL
;
141 assert(t
< _IMAGE_TYPE_MAX
);
152 i
->read_only
= read_only
;
155 i
->usage
= i
->usage_exclusive
= (uint64_t) -1;
156 i
->limit
= i
->limit_exclusive
= (uint64_t) -1;
158 i
->name
= strdup(pretty
);
163 i
->path
= strjoin(path
, "/", filename
);
165 i
->path
= strdup(filename
);
169 path_kill_slashes(i
->path
);
176 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
177 _cleanup_free_
char *name
= NULL
;
184 p
= last_path_component(path
);
187 name
= strndup(p
, n
);
194 e
= endswith(name
, suffix
);
201 if (!image_name_is_valid(name
))
204 *ret
= TAKE_PTR(name
);
208 static int image_make(
212 const char *filename
,
213 const struct stat
*st
,
216 _cleanup_free_
char *pretty_buffer
= NULL
;
221 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
224 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
225 * devices into /var/lib/machines/, and treat them normally.
227 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
231 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
238 (path
&& path_startswith(path
, "/usr")) ||
239 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
241 if (S_ISDIR(st
->st_mode
)) {
242 _cleanup_close_
int fd
= -1;
243 unsigned file_attr
= 0;
249 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
253 pretty
= pretty_buffer
;
256 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
260 /* btrfs subvolumes have inode 256 */
261 if (st
->st_ino
== 256) {
263 r
= btrfs_is_filesystem(fd
);
267 BtrfsSubvolInfo info
;
269 /* It's a btrfs subvolume */
271 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
275 r
= image_new(IMAGE_SUBVOLUME
,
279 info
.read_only
|| read_only
,
286 if (btrfs_quota_scan_ongoing(fd
) == 0) {
287 BtrfsQuotaInfo quota
;
289 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
291 (*ret
)->usage
= quota
.referenced
;
292 (*ret
)->usage_exclusive
= quota
.exclusive
;
294 (*ret
)->limit
= quota
.referenced_max
;
295 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
303 /* If the IMMUTABLE bit is set, we consider the
304 * directory read-only. Since the ioctl is not
305 * supported everywhere we ignore failures. */
306 (void) read_attr_fd(fd
, &file_attr
);
308 /* It's just a normal directory. */
309 r
= image_new(IMAGE_DIRECTORY
,
313 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
322 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
325 /* It's a RAW disk image */
330 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
333 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
337 pretty
= pretty_buffer
;
340 r
= image_new(IMAGE_RAW
,
344 !(st
->st_mode
& 0222) || read_only
,
346 timespec_load(&st
->st_mtim
),
351 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
352 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
356 } else if (S_ISBLK(st
->st_mode
)) {
357 _cleanup_close_
int block_fd
= -1;
358 uint64_t size
= UINT64_MAX
;
366 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
370 pretty
= pretty_buffer
;
373 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
375 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
377 /* Refresh stat data after opening the node */
378 if (fstat(block_fd
, &stbuf
) < 0)
382 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
388 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
389 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
394 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
395 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
, filename
);
397 block_fd
= safe_close(block_fd
);
400 r
= image_new(IMAGE_BLOCK
,
404 !(st
->st_mode
& 0222) || read_only
,
411 if (size
!= 0 && size
!= UINT64_MAX
)
412 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
420 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
425 assert(class < _IMAGE_CLASS_MAX
);
428 /* There are no images with invalid names */
429 if (!image_name_is_valid(name
))
432 NULSTR_FOREACH(path
, image_search_path
[class]) {
433 _cleanup_closedir_
DIR *d
= NULL
;
444 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
445 * symlink block devices into the search path */
446 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
447 _cleanup_free_
char *raw
= NULL
;
452 raw
= strappend(name
, ".raw");
456 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
464 if (!S_ISREG(st
.st_mode
))
467 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
470 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
473 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
475 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
481 (*ret
)->discoverable
= true;
486 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
487 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
492 (*ret
)->discoverable
= true;
500 int image_from_path(const char *path
, Image
**ret
) {
502 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
503 * the image is in the image search path. And if it is we don't know if the path we used is actually not
504 * overriden by another, different image earlier in the search path */
506 if (path_equal(path
, "/"))
507 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
509 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
512 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
513 if (image_name_is_valid(name_or_path
))
514 return image_find(class, name_or_path
, ret
);
516 return image_from_path(name_or_path
, ret
);
519 int image_discover(ImageClass
class, Hashmap
*h
) {
524 assert(class < _IMAGE_CLASS_MAX
);
527 NULSTR_FOREACH(path
, image_search_path
[class]) {
528 _cleanup_closedir_
DIR *d
= NULL
;
539 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
540 _cleanup_(image_unrefp
) Image
*image
= NULL
;
541 _cleanup_free_
char *truncated
= NULL
;
545 if (dot_or_dot_dot(de
->d_name
))
548 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
549 * to symlink block devices into the search path */
550 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
557 if (S_ISREG(st
.st_mode
)) {
560 e
= endswith(de
->d_name
, ".raw");
564 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
569 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
574 if (!image_name_is_valid(pretty
))
577 if (hashmap_contains(h
, pretty
))
580 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
581 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
586 image
->discoverable
= true;
588 r
= hashmap_put(h
, image
->name
, image
);
596 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
597 _cleanup_(image_unrefp
) Image
*image
= NULL
;
599 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
603 image
->discoverable
= true;
605 r
= hashmap_put(h
, image
->name
, image
);
615 int image_remove(Image
*i
) {
616 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
617 _cleanup_strv_free_
char **settings
= NULL
;
618 _cleanup_free_
char *roothash
= NULL
;
624 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
627 settings
= image_settings_path(i
);
631 roothash
= image_roothash_path(i
);
635 /* Make sure we don't interfere with a running nspawn */
636 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
642 case IMAGE_SUBVOLUME
:
644 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
646 if (unlink(i
->path
) < 0) {
647 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
654 case IMAGE_DIRECTORY
:
655 /* Allow deletion of read-only directories */
656 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
657 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
665 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
666 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
667 * the thing (it's most likely a symlink after all). */
669 if (path_startswith(i
->path
, "/dev"))
674 if (unlink(i
->path
) < 0)
682 STRV_FOREACH(j
, settings
) {
683 if (unlink(*j
) < 0 && errno
!= ENOENT
)
684 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
687 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
688 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
693 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
694 _cleanup_free_
char *rs
= NULL
;
697 fn
= strjoina(new_name
, suffix
);
699 rs
= file_in_same_dir(path
, fn
);
703 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
706 int image_rename(Image
*i
, const char *new_name
) {
707 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
708 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
709 _cleanup_strv_free_
char **settings
= NULL
;
710 unsigned file_attr
= 0;
716 if (!image_name_is_valid(new_name
))
719 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
722 settings
= image_settings_path(i
);
726 roothash
= image_roothash_path(i
);
730 /* Make sure we don't interfere with a running nspawn */
731 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
735 /* Make sure nobody takes the new name, between the time we
736 * checked it is currently unused in all search paths, and the
737 * time we take possession of it */
738 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
742 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
750 case IMAGE_DIRECTORY
:
751 /* Turn of the immutable bit while we rename the image, so that we can rename it */
752 (void) read_attr_path(i
->path
, &file_attr
);
754 if (file_attr
& FS_IMMUTABLE_FL
)
755 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
758 case IMAGE_SUBVOLUME
:
759 new_path
= file_in_same_dir(i
->path
, new_name
);
764 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
765 if (path_startswith(i
->path
, "/dev"))
768 new_path
= file_in_same_dir(i
->path
, new_name
);
774 fn
= strjoina(new_name
, ".raw");
775 new_path
= file_in_same_dir(i
->path
, fn
);
786 nn
= strdup(new_name
);
790 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
794 /* Restore the immutable bit, if it was set before */
795 if (file_attr
& FS_IMMUTABLE_FL
)
796 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
);
798 free_and_replace(i
->path
, new_path
);
799 free_and_replace(i
->name
, nn
);
801 STRV_FOREACH(j
, settings
) {
802 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
803 if (r
< 0 && r
!= -ENOENT
)
804 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
807 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
808 if (r
< 0 && r
!= -ENOENT
)
809 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
814 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
815 _cleanup_free_
char *rs
= NULL
;
818 fn
= strjoina(new_name
, suffix
);
820 rs
= file_in_same_dir(path
, fn
);
824 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
827 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
828 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
829 _cleanup_strv_free_
char **settings
= NULL
;
830 _cleanup_free_
char *roothash
= NULL
;
831 const char *new_path
;
837 if (!image_name_is_valid(new_name
))
840 settings
= image_settings_path(i
);
844 roothash
= image_roothash_path(i
);
848 /* Make sure nobody takes the new name, between the time we
849 * checked it is currently unused in all search paths, and the
850 * time we take possession of it */
851 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
855 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
863 case IMAGE_SUBVOLUME
:
864 case IMAGE_DIRECTORY
:
865 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
868 new_path
= strjoina("/var/lib/machines/", new_name
);
870 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
871 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
872 BTRFS_SNAPSHOT_FALLBACK_COPY
|
873 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
874 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
875 BTRFS_SNAPSHOT_RECURSIVE
|
876 BTRFS_SNAPSHOT_QUOTA
);
878 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
879 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
884 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
886 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
);
897 STRV_FOREACH(j
, settings
) {
898 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
899 if (r
< 0 && r
!= -ENOENT
)
900 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
903 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
904 if (r
< 0 && r
!= -ENOENT
)
905 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
910 int image_read_only(Image
*i
, bool b
) {
911 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
916 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
919 /* Make sure we don't interfere with a running nspawn */
920 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
926 case IMAGE_SUBVOLUME
:
928 /* Note that we set the flag only on the top-level
929 * subvolume of the image. */
931 r
= btrfs_subvol_set_read_only(i
->path
, b
);
937 case IMAGE_DIRECTORY
:
938 /* For simple directory trees we cannot use the access
939 mode of the top-level directory, since it has an
940 effect on the container itself. However, we can
941 use the "immutable" flag, to at least make the
942 top-level directory read-only. It's not as good as
943 a read-only subvolume, but at least something, and
944 we can read the value back. */
946 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
);
955 if (stat(i
->path
, &st
) < 0)
958 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
961 /* If the images is now read-only, it's a good time to
962 * defrag it, given that no write patterns will
963 * fragment it again. */
965 (void) btrfs_defrag(i
->path
);
970 _cleanup_close_
int fd
= -1;
974 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
978 if (fstat(fd
, &st
) < 0)
980 if (!S_ISBLK(st
.st_mode
))
983 if (ioctl(fd
, BLKROSET
, &state
) < 0)
996 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
997 _cleanup_free_
char *p
= NULL
;
998 LockFile t
= LOCK_FILE_INIT
;
1006 /* Locks an image path. This actually creates two locks: one
1007 * "local" one, next to the image path itself, which might be
1008 * shared via NFS. And another "global" one, in /run, that
1009 * uses the device/inode number. This has the benefit that we
1010 * can even lock a tree that is a mount point, correctly. */
1012 if (!path_is_absolute(path
))
1015 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1016 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1020 if (path_equal(path
, "/"))
1023 if (stat(path
, &st
) >= 0) {
1024 if (S_ISBLK(st
.st_mode
))
1025 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1026 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1027 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1035 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1036 * block devices are device local anyway. */
1037 if (!path_startswith(path
, "/dev")) {
1038 r
= make_lock_file_for(path
, operation
, &t
);
1040 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1041 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1048 mkdir_p("/run/systemd/nspawn/locks", 0700);
1050 r
= make_lock_file(p
, operation
, global
);
1052 release_lock_file(&t
);
1056 *global
= (LockFile
) LOCK_FILE_INIT
;
1062 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1065 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1068 if (i
->type
!= IMAGE_SUBVOLUME
)
1071 /* We set the quota both for the subvolume as well as for the
1072 * subtree. The latter is mostly for historical reasons, since
1073 * we didn't use to have a concept of subtree quota, and hence
1074 * only modified the subvolume quota. */
1076 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1077 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1078 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1081 int image_read_metadata(Image
*i
) {
1082 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1087 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1093 case IMAGE_SUBVOLUME
:
1094 case IMAGE_DIRECTORY
: {
1095 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1096 sd_id128_t machine_id
= SD_ID128_NULL
;
1097 _cleanup_free_
char *hostname
= NULL
;
1098 _cleanup_free_
char *path
= NULL
;
1099 _cleanup_fclose_
FILE *f
= NULL
;
1101 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1102 if (r
< 0 && r
!= -ENOENT
)
1103 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1105 r
= read_etc_hostname(path
, &hostname
);
1107 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1112 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1113 if (r
< 0 && r
!= -ENOENT
)
1114 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1116 _cleanup_close_
int fd
= -1;
1118 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1120 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1122 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1124 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1130 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1131 if (r
< 0 && r
!= -ENOENT
)
1132 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1134 r
= load_env_file_pairs(NULL
, path
, NULL
, &machine_info
);
1136 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1139 r
= load_os_release_pairs(i
->path
, &os_release
);
1141 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1143 free_and_replace(i
->hostname
, hostname
);
1144 i
->machine_id
= machine_id
;
1145 strv_free_and_replace(i
->machine_info
, machine_info
);
1146 strv_free_and_replace(i
->os_release
, os_release
);
1153 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1154 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1156 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1160 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1164 r
= dissected_image_acquire_metadata(m
);
1168 free_and_replace(i
->hostname
, m
->hostname
);
1169 i
->machine_id
= m
->machine_id
;
1170 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1171 strv_free_and_replace(i
->os_release
, m
->os_release
);
1180 i
->metadata_valid
= true;
1185 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1191 /* Locks an image name, regardless of the precise path used. */
1193 if (!image_name_is_valid(name
))
1196 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1197 *ret
= (LockFile
) LOCK_FILE_INIT
;
1201 if (streq(name
, ".host"))
1204 mkdir_p("/run/systemd/nspawn/locks", 0700);
1205 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1207 return make_lock_file(p
, operation
, ret
);
1210 bool image_name_is_valid(const char *s
) {
1211 if (!filename_is_valid(s
))
1214 if (string_has_cc(s
, NULL
))
1217 if (!utf8_is_valid(s
))
1220 /* Temporary files for atomically creating new files */
1221 if (startswith(s
, ".#"))
1227 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1228 [IMAGE_DIRECTORY
] = "directory",
1229 [IMAGE_SUBVOLUME
] = "subvolume",
1230 [IMAGE_RAW
] = "raw",
1231 [IMAGE_BLOCK
] = "block",
1234 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);