1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
17 #include "alloc-util.h"
18 #include "btrfs-util.h"
19 #include "chattr-util.h"
21 #include "dirent-util.h"
22 #include "dissect-image.h"
28 #include "hostname-util.h"
29 #include "id128-util.h"
30 #include "lockfile-util.h"
32 #include "loop-util.h"
33 #include "machine-image.h"
37 #include "path-util.h"
39 #include "string-table.h"
40 #include "string-util.h"
42 #include "time-util.h"
45 #include "xattr-util.h"
47 static const char* const image_search_path
[_IMAGE_CLASS_MAX
] = {
48 [IMAGE_MACHINE
] = "/etc/machines\0" /* only place symlinks here */
49 "/run/machines\0" /* and here too */
50 "/var/lib/machines\0" /* the main place for images */
51 "/var/lib/container\0" /* legacy */
52 "/usr/local/lib/machines\0"
53 "/usr/lib/machines\0",
55 [IMAGE_PORTABLE
] = "/etc/portables\0" /* only place symlinks here */
56 "/run/portables\0" /* and here too */
57 "/var/lib/portables\0" /* the main place for images */
58 "/usr/local/lib/portables\0"
59 "/usr/lib/portables\0",
62 Image
*image_unref(Image
*i
) {
76 strv_free(i
->machine_info
);
77 strv_free(i
->os_release
);
82 Image
*image_ref(Image
*i
) {
92 static char **image_settings_path(Image
*image
) {
93 _cleanup_strv_free_
char **l
= NULL
;
103 fn
= strjoina(image
->name
, ".nspawn");
105 FOREACH_STRING(s
, "/etc/systemd/nspawn/", "/run/systemd/nspawn/") {
106 l
[i
] = strappend(s
, fn
);
113 l
[i
] = file_in_same_dir(image
->path
, fn
);
120 static char *image_roothash_path(Image
*image
) {
125 fn
= strjoina(image
->name
, ".roothash");
127 return file_in_same_dir(image
->path
, fn
);
130 static int image_new(
134 const char *filename
,
140 _cleanup_(image_unrefp
) Image
*i
= NULL
;
143 assert(t
< _IMAGE_TYPE_MAX
);
154 i
->read_only
= read_only
;
157 i
->usage
= i
->usage_exclusive
= (uint64_t) -1;
158 i
->limit
= i
->limit_exclusive
= (uint64_t) -1;
160 i
->name
= strdup(pretty
);
165 i
->path
= strjoin(path
, "/", filename
);
167 i
->path
= strdup(filename
);
171 path_simplify(i
->path
, false);
178 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
179 _cleanup_free_
char *name
= NULL
;
186 p
= last_path_component(path
);
189 name
= strndup(p
, n
);
196 e
= endswith(name
, suffix
);
203 if (!image_name_is_valid(name
))
206 *ret
= TAKE_PTR(name
);
210 static int image_make(
214 const char *filename
,
215 const struct stat
*st
,
218 _cleanup_free_
char *pretty_buffer
= NULL
;
223 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
226 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
227 * devices into /var/lib/machines/, and treat them normally.
229 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
233 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
240 (path
&& path_startswith(path
, "/usr")) ||
241 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
243 if (S_ISDIR(st
->st_mode
)) {
244 _cleanup_close_
int fd
= -1;
245 unsigned file_attr
= 0;
251 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
255 pretty
= pretty_buffer
;
258 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
262 /* btrfs subvolumes have inode 256 */
263 if (st
->st_ino
== 256) {
265 r
= btrfs_is_filesystem(fd
);
269 BtrfsSubvolInfo info
;
271 /* It's a btrfs subvolume */
273 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
277 r
= image_new(IMAGE_SUBVOLUME
,
281 info
.read_only
|| read_only
,
288 if (btrfs_quota_scan_ongoing(fd
) == 0) {
289 BtrfsQuotaInfo quota
;
291 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
293 (*ret
)->usage
= quota
.referenced
;
294 (*ret
)->usage_exclusive
= quota
.exclusive
;
296 (*ret
)->limit
= quota
.referenced_max
;
297 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
305 /* If the IMMUTABLE bit is set, we consider the
306 * directory read-only. Since the ioctl is not
307 * supported everywhere we ignore failures. */
308 (void) read_attr_fd(fd
, &file_attr
);
310 /* It's just a normal directory. */
311 r
= image_new(IMAGE_DIRECTORY
,
315 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
324 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
327 /* It's a RAW disk image */
332 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
335 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
339 pretty
= pretty_buffer
;
342 r
= image_new(IMAGE_RAW
,
346 !(st
->st_mode
& 0222) || read_only
,
348 timespec_load(&st
->st_mtim
),
353 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
354 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
358 } else if (S_ISBLK(st
->st_mode
)) {
359 _cleanup_close_
int block_fd
= -1;
360 uint64_t size
= UINT64_MAX
;
368 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
372 pretty
= pretty_buffer
;
375 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
377 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
379 /* Refresh stat data after opening the node */
380 if (fstat(block_fd
, &stbuf
) < 0)
384 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
390 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
391 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
396 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
397 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
, filename
);
399 block_fd
= safe_close(block_fd
);
402 r
= image_new(IMAGE_BLOCK
,
406 !(st
->st_mode
& 0222) || read_only
,
413 if (size
!= 0 && size
!= UINT64_MAX
)
414 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
422 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
427 assert(class < _IMAGE_CLASS_MAX
);
430 /* There are no images with invalid names */
431 if (!image_name_is_valid(name
))
434 NULSTR_FOREACH(path
, image_search_path
[class]) {
435 _cleanup_closedir_
DIR *d
= NULL
;
446 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
447 * symlink block devices into the search path */
448 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
449 _cleanup_free_
char *raw
= NULL
;
454 raw
= strappend(name
, ".raw");
458 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
466 if (!S_ISREG(st
.st_mode
))
469 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
472 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
475 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
477 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
483 (*ret
)->discoverable
= true;
488 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
489 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
494 (*ret
)->discoverable
= true;
502 int image_from_path(const char *path
, Image
**ret
) {
504 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
505 * the image is in the image search path. And if it is we don't know if the path we used is actually not
506 * overriden by another, different image earlier in the search path */
508 if (path_equal(path
, "/"))
509 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
511 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
514 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
515 if (image_name_is_valid(name_or_path
))
516 return image_find(class, name_or_path
, ret
);
518 return image_from_path(name_or_path
, ret
);
521 int image_discover(ImageClass
class, Hashmap
*h
) {
526 assert(class < _IMAGE_CLASS_MAX
);
529 NULSTR_FOREACH(path
, image_search_path
[class]) {
530 _cleanup_closedir_
DIR *d
= NULL
;
541 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
542 _cleanup_(image_unrefp
) Image
*image
= NULL
;
543 _cleanup_free_
char *truncated
= NULL
;
547 if (dot_or_dot_dot(de
->d_name
))
550 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
551 * to symlink block devices into the search path */
552 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
559 if (S_ISREG(st
.st_mode
)) {
562 e
= endswith(de
->d_name
, ".raw");
566 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
571 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
576 if (!image_name_is_valid(pretty
))
579 if (hashmap_contains(h
, pretty
))
582 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
583 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
588 image
->discoverable
= true;
590 r
= hashmap_put(h
, image
->name
, image
);
598 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
599 _cleanup_(image_unrefp
) Image
*image
= NULL
;
601 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
605 image
->discoverable
= true;
607 r
= hashmap_put(h
, image
->name
, image
);
617 int image_remove(Image
*i
) {
618 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
619 _cleanup_strv_free_
char **settings
= NULL
;
620 _cleanup_free_
char *roothash
= NULL
;
626 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
629 settings
= image_settings_path(i
);
633 roothash
= image_roothash_path(i
);
637 /* Make sure we don't interfere with a running nspawn */
638 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
644 case IMAGE_SUBVOLUME
:
646 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
648 if (unlink(i
->path
) < 0) {
649 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
656 case IMAGE_DIRECTORY
:
657 /* Allow deletion of read-only directories */
658 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
659 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
667 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
668 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
669 * the thing (it's most likely a symlink after all). */
671 if (path_startswith(i
->path
, "/dev"))
676 if (unlink(i
->path
) < 0)
684 STRV_FOREACH(j
, settings
) {
685 if (unlink(*j
) < 0 && errno
!= ENOENT
)
686 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
689 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
690 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
695 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
696 _cleanup_free_
char *rs
= NULL
;
699 fn
= strjoina(new_name
, suffix
);
701 rs
= file_in_same_dir(path
, fn
);
705 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
708 int image_rename(Image
*i
, const char *new_name
) {
709 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
710 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
711 _cleanup_strv_free_
char **settings
= NULL
;
712 unsigned file_attr
= 0;
718 if (!image_name_is_valid(new_name
))
721 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
724 settings
= image_settings_path(i
);
728 roothash
= image_roothash_path(i
);
732 /* Make sure we don't interfere with a running nspawn */
733 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
737 /* Make sure nobody takes the new name, between the time we
738 * checked it is currently unused in all search paths, and the
739 * time we take possession of it */
740 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
744 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
752 case IMAGE_DIRECTORY
:
753 /* Turn of the immutable bit while we rename the image, so that we can rename it */
754 (void) read_attr_path(i
->path
, &file_attr
);
756 if (file_attr
& FS_IMMUTABLE_FL
)
757 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
760 case IMAGE_SUBVOLUME
:
761 new_path
= file_in_same_dir(i
->path
, new_name
);
766 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
767 if (path_startswith(i
->path
, "/dev"))
770 new_path
= file_in_same_dir(i
->path
, new_name
);
776 fn
= strjoina(new_name
, ".raw");
777 new_path
= file_in_same_dir(i
->path
, fn
);
788 nn
= strdup(new_name
);
792 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
796 /* Restore the immutable bit, if it was set before */
797 if (file_attr
& FS_IMMUTABLE_FL
)
798 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
);
800 free_and_replace(i
->path
, new_path
);
801 free_and_replace(i
->name
, nn
);
803 STRV_FOREACH(j
, settings
) {
804 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
805 if (r
< 0 && r
!= -ENOENT
)
806 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
809 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
810 if (r
< 0 && r
!= -ENOENT
)
811 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
816 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
817 _cleanup_free_
char *rs
= NULL
;
820 fn
= strjoina(new_name
, suffix
);
822 rs
= file_in_same_dir(path
, fn
);
826 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
829 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
830 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
831 _cleanup_strv_free_
char **settings
= NULL
;
832 _cleanup_free_
char *roothash
= NULL
;
833 const char *new_path
;
839 if (!image_name_is_valid(new_name
))
842 settings
= image_settings_path(i
);
846 roothash
= image_roothash_path(i
);
850 /* Make sure nobody takes the new name, between the time we
851 * checked it is currently unused in all search paths, and the
852 * time we take possession of it */
853 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
857 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
865 case IMAGE_SUBVOLUME
:
866 case IMAGE_DIRECTORY
:
867 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
870 new_path
= strjoina("/var/lib/machines/", new_name
);
872 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
873 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
874 BTRFS_SNAPSHOT_FALLBACK_COPY
|
875 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
876 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
877 BTRFS_SNAPSHOT_RECURSIVE
|
878 BTRFS_SNAPSHOT_QUOTA
);
880 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
881 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
886 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
888 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
);
899 STRV_FOREACH(j
, settings
) {
900 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
901 if (r
< 0 && r
!= -ENOENT
)
902 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
905 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
906 if (r
< 0 && r
!= -ENOENT
)
907 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
912 int image_read_only(Image
*i
, bool b
) {
913 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
918 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
921 /* Make sure we don't interfere with a running nspawn */
922 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
928 case IMAGE_SUBVOLUME
:
930 /* Note that we set the flag only on the top-level
931 * subvolume of the image. */
933 r
= btrfs_subvol_set_read_only(i
->path
, b
);
939 case IMAGE_DIRECTORY
:
940 /* For simple directory trees we cannot use the access
941 mode of the top-level directory, since it has an
942 effect on the container itself. However, we can
943 use the "immutable" flag, to at least make the
944 top-level directory read-only. It's not as good as
945 a read-only subvolume, but at least something, and
946 we can read the value back. */
948 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
);
957 if (stat(i
->path
, &st
) < 0)
960 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
963 /* If the images is now read-only, it's a good time to
964 * defrag it, given that no write patterns will
965 * fragment it again. */
967 (void) btrfs_defrag(i
->path
);
972 _cleanup_close_
int fd
= -1;
976 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
980 if (fstat(fd
, &st
) < 0)
982 if (!S_ISBLK(st
.st_mode
))
985 if (ioctl(fd
, BLKROSET
, &state
) < 0)
998 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
999 _cleanup_free_
char *p
= NULL
;
1000 LockFile t
= LOCK_FILE_INIT
;
1008 /* Locks an image path. This actually creates two locks: one
1009 * "local" one, next to the image path itself, which might be
1010 * shared via NFS. And another "global" one, in /run, that
1011 * uses the device/inode number. This has the benefit that we
1012 * can even lock a tree that is a mount point, correctly. */
1014 if (!path_is_absolute(path
))
1017 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1018 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1022 if (path_equal(path
, "/"))
1025 if (stat(path
, &st
) >= 0) {
1026 if (S_ISBLK(st
.st_mode
))
1027 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1028 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1029 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1037 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1038 * block devices are device local anyway. */
1039 if (!path_startswith(path
, "/dev")) {
1040 r
= make_lock_file_for(path
, operation
, &t
);
1042 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1043 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1050 mkdir_p("/run/systemd/nspawn/locks", 0700);
1052 r
= make_lock_file(p
, operation
, global
);
1054 release_lock_file(&t
);
1058 *global
= (LockFile
) LOCK_FILE_INIT
;
1064 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1067 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1070 if (i
->type
!= IMAGE_SUBVOLUME
)
1073 /* We set the quota both for the subvolume as well as for the
1074 * subtree. The latter is mostly for historical reasons, since
1075 * we didn't use to have a concept of subtree quota, and hence
1076 * only modified the subvolume quota. */
1078 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1079 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1080 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1083 int image_read_metadata(Image
*i
) {
1084 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1089 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1095 case IMAGE_SUBVOLUME
:
1096 case IMAGE_DIRECTORY
: {
1097 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1098 sd_id128_t machine_id
= SD_ID128_NULL
;
1099 _cleanup_free_
char *hostname
= NULL
;
1100 _cleanup_free_
char *path
= NULL
;
1102 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1103 if (r
< 0 && r
!= -ENOENT
)
1104 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1106 r
= read_etc_hostname(path
, &hostname
);
1108 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1113 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1114 if (r
< 0 && r
!= -ENOENT
)
1115 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1117 _cleanup_close_
int fd
= -1;
1119 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1121 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1123 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1125 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1131 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1132 if (r
< 0 && r
!= -ENOENT
)
1133 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1135 r
= load_env_file_pairs(NULL
, path
, NULL
, &machine_info
);
1137 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1140 r
= load_os_release_pairs(i
->path
, &os_release
);
1142 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1144 free_and_replace(i
->hostname
, hostname
);
1145 i
->machine_id
= machine_id
;
1146 strv_free_and_replace(i
->machine_info
, machine_info
);
1147 strv_free_and_replace(i
->os_release
, os_release
);
1154 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1155 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1157 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1161 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1165 r
= dissected_image_acquire_metadata(m
);
1169 free_and_replace(i
->hostname
, m
->hostname
);
1170 i
->machine_id
= m
->machine_id
;
1171 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1172 strv_free_and_replace(i
->os_release
, m
->os_release
);
1181 i
->metadata_valid
= true;
1186 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1192 /* Locks an image name, regardless of the precise path used. */
1194 if (!image_name_is_valid(name
))
1197 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1198 *ret
= (LockFile
) LOCK_FILE_INIT
;
1202 if (streq(name
, ".host"))
1205 mkdir_p("/run/systemd/nspawn/locks", 0700);
1206 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1208 return make_lock_file(p
, operation
, ret
);
1211 bool image_name_is_valid(const char *s
) {
1212 if (!filename_is_valid(s
))
1215 if (string_has_cc(s
, NULL
))
1218 if (!utf8_is_valid(s
))
1221 /* Temporary files for atomically creating new files */
1222 if (startswith(s
, ".#"))
1228 bool image_in_search_path(ImageClass
class, const char *image
) {
1233 NULSTR_FOREACH(path
, image_search_path
[class]) {
1237 p
= path_startswith(image
, path
);
1241 /* Make sure there's a filename following */
1242 k
= strcspn(p
, "/");
1248 /* Accept trailing slashes */
1249 if (p
[strspn(p
, "/")] == 0)
1257 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1258 [IMAGE_DIRECTORY
] = "directory",
1259 [IMAGE_SUBVOLUME
] = "subvolume",
1260 [IMAGE_RAW
] = "raw",
1261 [IMAGE_BLOCK
] = "block",
1264 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);