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 Image
*image_unref(Image
*i
) {
73 strv_free(i
->machine_info
);
74 strv_free(i
->os_release
);
79 Image
*image_ref(Image
*i
) {
89 static char **image_settings_path(Image
*image
) {
90 _cleanup_strv_free_
char **l
= NULL
;
100 fn
= strjoina(image
->name
, ".nspawn");
102 FOREACH_STRING(s
, "/etc/systemd/nspawn/", "/run/systemd/nspawn/") {
103 l
[i
] = strappend(s
, fn
);
110 l
[i
] = file_in_same_dir(image
->path
, fn
);
117 static char *image_roothash_path(Image
*image
) {
122 fn
= strjoina(image
->name
, ".roothash");
124 return file_in_same_dir(image
->path
, fn
);
127 static int image_new(
131 const char *filename
,
137 _cleanup_(image_unrefp
) Image
*i
= NULL
;
140 assert(t
< _IMAGE_TYPE_MAX
);
151 i
->read_only
= read_only
;
154 i
->usage
= i
->usage_exclusive
= (uint64_t) -1;
155 i
->limit
= i
->limit_exclusive
= (uint64_t) -1;
157 i
->name
= strdup(pretty
);
162 i
->path
= strjoin(path
, "/", filename
);
164 i
->path
= strdup(filename
);
168 path_simplify(i
->path
, false);
175 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
176 _cleanup_free_
char *name
= NULL
;
183 p
= last_path_component(path
);
186 name
= strndup(p
, n
);
193 e
= endswith(name
, suffix
);
200 if (!image_name_is_valid(name
))
203 *ret
= TAKE_PTR(name
);
207 static int image_make(
211 const char *filename
,
212 const struct stat
*st
,
215 _cleanup_free_
char *pretty_buffer
= NULL
;
220 assert(dfd
>= 0 || dfd
== AT_FDCWD
);
223 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
224 * devices into /var/lib/machines/, and treat them normally.
226 * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we
230 if (fstatat(dfd
, filename
, &stbuf
, 0) < 0)
237 (path
&& path_startswith(path
, "/usr")) ||
238 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
240 if (S_ISDIR(st
->st_mode
)) {
241 _cleanup_close_
int fd
= -1;
242 unsigned file_attr
= 0;
248 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
252 pretty
= pretty_buffer
;
255 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
259 /* btrfs subvolumes have inode 256 */
260 if (st
->st_ino
== 256) {
262 r
= btrfs_is_filesystem(fd
);
266 BtrfsSubvolInfo info
;
268 /* It's a btrfs subvolume */
270 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
274 r
= image_new(IMAGE_SUBVOLUME
,
278 info
.read_only
|| read_only
,
285 if (btrfs_quota_scan_ongoing(fd
) == 0) {
286 BtrfsQuotaInfo quota
;
288 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
290 (*ret
)->usage
= quota
.referenced
;
291 (*ret
)->usage_exclusive
= quota
.exclusive
;
293 (*ret
)->limit
= quota
.referenced_max
;
294 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
302 /* If the IMMUTABLE bit is set, we consider the
303 * directory read-only. Since the ioctl is not
304 * supported everywhere we ignore failures. */
305 (void) read_attr_fd(fd
, &file_attr
);
307 /* It's just a normal directory. */
308 r
= image_new(IMAGE_DIRECTORY
,
312 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
321 } else if (S_ISREG(st
->st_mode
) && endswith(filename
, ".raw")) {
324 /* It's a RAW disk image */
329 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
332 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
336 pretty
= pretty_buffer
;
339 r
= image_new(IMAGE_RAW
,
343 !(st
->st_mode
& 0222) || read_only
,
345 timespec_load(&st
->st_mtim
),
350 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
->st_blocks
* 512;
351 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
->st_size
;
355 } else if (S_ISBLK(st
->st_mode
)) {
356 _cleanup_close_
int block_fd
= -1;
357 uint64_t size
= UINT64_MAX
;
365 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
369 pretty
= pretty_buffer
;
372 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
374 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
376 /* Refresh stat data after opening the node */
377 if (fstat(block_fd
, &stbuf
) < 0)
381 if (!S_ISBLK(st
->st_mode
)) /* Verify that what we opened is actually what we think it is */
387 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
388 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
393 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
394 log_debug_errno(errno
, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path
, filename
);
396 block_fd
= safe_close(block_fd
);
399 r
= image_new(IMAGE_BLOCK
,
403 !(st
->st_mode
& 0222) || read_only
,
410 if (size
!= 0 && size
!= UINT64_MAX
)
411 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
419 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
424 assert(class < _IMAGE_CLASS_MAX
);
427 /* There are no images with invalid names */
428 if (!image_name_is_valid(name
))
431 NULSTR_FOREACH(path
, image_search_path
[class]) {
432 _cleanup_closedir_
DIR *d
= NULL
;
443 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people to
444 * symlink block devices into the search path */
445 if (fstatat(dirfd(d
), name
, &st
, 0) < 0) {
446 _cleanup_free_
char *raw
= NULL
;
451 raw
= strappend(name
, ".raw");
455 if (fstatat(dirfd(d
), raw
, &st
, 0) < 0) {
463 if (!S_ISREG(st
.st_mode
))
466 r
= image_make(name
, dirfd(d
), path
, raw
, &st
, ret
);
469 if (!S_ISDIR(st
.st_mode
) && !S_ISBLK(st
.st_mode
))
472 r
= image_make(name
, dirfd(d
), path
, name
, &st
, ret
);
474 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
480 (*ret
)->discoverable
= true;
485 if (class == IMAGE_MACHINE
&& streq(name
, ".host")) {
486 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
491 (*ret
)->discoverable
= true;
499 int image_from_path(const char *path
, Image
**ret
) {
501 /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether
502 * the image is in the image search path. And if it is we don't know if the path we used is actually not
503 * overriden by another, different image earlier in the search path */
505 if (path_equal(path
, "/"))
506 return image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, ret
);
508 return image_make(NULL
, AT_FDCWD
, NULL
, path
, NULL
, ret
);
511 int image_find_harder(ImageClass
class, const char *name_or_path
, Image
**ret
) {
512 if (image_name_is_valid(name_or_path
))
513 return image_find(class, name_or_path
, ret
);
515 return image_from_path(name_or_path
, ret
);
518 int image_discover(ImageClass
class, Hashmap
*h
) {
523 assert(class < _IMAGE_CLASS_MAX
);
526 NULSTR_FOREACH(path
, image_search_path
[class]) {
527 _cleanup_closedir_
DIR *d
= NULL
;
538 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
539 _cleanup_(image_unrefp
) Image
*image
= NULL
;
540 _cleanup_free_
char *truncated
= NULL
;
544 if (dot_or_dot_dot(de
->d_name
))
547 /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people
548 * to symlink block devices into the search path */
549 if (fstatat(dirfd(d
), de
->d_name
, &st
, 0) < 0) {
556 if (S_ISREG(st
.st_mode
)) {
559 e
= endswith(de
->d_name
, ".raw");
563 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
568 } else if (S_ISDIR(st
.st_mode
) || S_ISBLK(st
.st_mode
))
573 if (!image_name_is_valid(pretty
))
576 if (hashmap_contains(h
, pretty
))
579 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &st
, &image
);
580 if (IN_SET(r
, -ENOENT
, -EMEDIUMTYPE
))
585 image
->discoverable
= true;
587 r
= hashmap_put(h
, image
->name
, image
);
595 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
596 _cleanup_(image_unrefp
) Image
*image
= NULL
;
598 r
= image_make(".host", AT_FDCWD
, NULL
, "/", NULL
, &image
);
602 image
->discoverable
= true;
604 r
= hashmap_put(h
, image
->name
, image
);
614 int image_remove(Image
*i
) {
615 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
616 _cleanup_strv_free_
char **settings
= NULL
;
617 _cleanup_free_
char *roothash
= NULL
;
623 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
626 settings
= image_settings_path(i
);
630 roothash
= image_roothash_path(i
);
634 /* Make sure we don't interfere with a running nspawn */
635 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
641 case IMAGE_SUBVOLUME
:
643 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
645 if (unlink(i
->path
) < 0) {
646 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
653 case IMAGE_DIRECTORY
:
654 /* Allow deletion of read-only directories */
655 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
656 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
664 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
665 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
666 * the thing (it's most likely a symlink after all). */
668 if (path_startswith(i
->path
, "/dev"))
673 if (unlink(i
->path
) < 0)
681 STRV_FOREACH(j
, settings
) {
682 if (unlink(*j
) < 0 && errno
!= ENOENT
)
683 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
686 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
687 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
692 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
693 _cleanup_free_
char *rs
= NULL
;
696 fn
= strjoina(new_name
, suffix
);
698 rs
= file_in_same_dir(path
, fn
);
702 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
705 int image_rename(Image
*i
, const char *new_name
) {
706 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
707 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
708 _cleanup_strv_free_
char **settings
= NULL
;
709 unsigned file_attr
= 0;
715 if (!image_name_is_valid(new_name
))
718 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
721 settings
= image_settings_path(i
);
725 roothash
= image_roothash_path(i
);
729 /* Make sure we don't interfere with a running nspawn */
730 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
734 /* Make sure nobody takes the new name, between the time we
735 * checked it is currently unused in all search paths, and the
736 * time we take possession of it */
737 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
741 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
749 case IMAGE_DIRECTORY
:
750 /* Turn of the immutable bit while we rename the image, so that we can rename it */
751 (void) read_attr_path(i
->path
, &file_attr
);
753 if (file_attr
& FS_IMMUTABLE_FL
)
754 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
757 case IMAGE_SUBVOLUME
:
758 new_path
= file_in_same_dir(i
->path
, new_name
);
763 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
764 if (path_startswith(i
->path
, "/dev"))
767 new_path
= file_in_same_dir(i
->path
, new_name
);
773 fn
= strjoina(new_name
, ".raw");
774 new_path
= file_in_same_dir(i
->path
, fn
);
785 nn
= strdup(new_name
);
789 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
793 /* Restore the immutable bit, if it was set before */
794 if (file_attr
& FS_IMMUTABLE_FL
)
795 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
);
797 free_and_replace(i
->path
, new_path
);
798 free_and_replace(i
->name
, nn
);
800 STRV_FOREACH(j
, settings
) {
801 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
802 if (r
< 0 && r
!= -ENOENT
)
803 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
806 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
807 if (r
< 0 && r
!= -ENOENT
)
808 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
813 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
814 _cleanup_free_
char *rs
= NULL
;
817 fn
= strjoina(new_name
, suffix
);
819 rs
= file_in_same_dir(path
, fn
);
823 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
826 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
827 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
828 _cleanup_strv_free_
char **settings
= NULL
;
829 _cleanup_free_
char *roothash
= NULL
;
830 const char *new_path
;
836 if (!image_name_is_valid(new_name
))
839 settings
= image_settings_path(i
);
843 roothash
= image_roothash_path(i
);
847 /* Make sure nobody takes the new name, between the time we
848 * checked it is currently unused in all search paths, and the
849 * time we take possession of it */
850 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
854 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
862 case IMAGE_SUBVOLUME
:
863 case IMAGE_DIRECTORY
:
864 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
867 new_path
= strjoina("/var/lib/machines/", new_name
);
869 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
870 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
871 BTRFS_SNAPSHOT_FALLBACK_COPY
|
872 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
873 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
874 BTRFS_SNAPSHOT_RECURSIVE
|
875 BTRFS_SNAPSHOT_QUOTA
);
877 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
878 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
883 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
885 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
);
896 STRV_FOREACH(j
, settings
) {
897 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
898 if (r
< 0 && r
!= -ENOENT
)
899 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
902 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
903 if (r
< 0 && r
!= -ENOENT
)
904 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
909 int image_read_only(Image
*i
, bool b
) {
910 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
915 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
918 /* Make sure we don't interfere with a running nspawn */
919 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
925 case IMAGE_SUBVOLUME
:
927 /* Note that we set the flag only on the top-level
928 * subvolume of the image. */
930 r
= btrfs_subvol_set_read_only(i
->path
, b
);
936 case IMAGE_DIRECTORY
:
937 /* For simple directory trees we cannot use the access
938 mode of the top-level directory, since it has an
939 effect on the container itself. However, we can
940 use the "immutable" flag, to at least make the
941 top-level directory read-only. It's not as good as
942 a read-only subvolume, but at least something, and
943 we can read the value back. */
945 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
);
954 if (stat(i
->path
, &st
) < 0)
957 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
960 /* If the images is now read-only, it's a good time to
961 * defrag it, given that no write patterns will
962 * fragment it again. */
964 (void) btrfs_defrag(i
->path
);
969 _cleanup_close_
int fd
= -1;
973 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
977 if (fstat(fd
, &st
) < 0)
979 if (!S_ISBLK(st
.st_mode
))
982 if (ioctl(fd
, BLKROSET
, &state
) < 0)
995 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
996 _cleanup_free_
char *p
= NULL
;
997 LockFile t
= LOCK_FILE_INIT
;
1005 /* Locks an image path. This actually creates two locks: one
1006 * "local" one, next to the image path itself, which might be
1007 * shared via NFS. And another "global" one, in /run, that
1008 * uses the device/inode number. This has the benefit that we
1009 * can even lock a tree that is a mount point, correctly. */
1011 if (!path_is_absolute(path
))
1014 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1015 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
1019 if (path_equal(path
, "/"))
1022 if (stat(path
, &st
) >= 0) {
1023 if (S_ISBLK(st
.st_mode
))
1024 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
1025 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
1026 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
1034 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
1035 * block devices are device local anyway. */
1036 if (!path_startswith(path
, "/dev")) {
1037 r
= make_lock_file_for(path
, operation
, &t
);
1039 if ((operation
& LOCK_SH
) && r
== -EROFS
)
1040 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
1047 mkdir_p("/run/systemd/nspawn/locks", 0700);
1049 r
= make_lock_file(p
, operation
, global
);
1051 release_lock_file(&t
);
1055 *global
= (LockFile
) LOCK_FILE_INIT
;
1061 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
1064 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
1067 if (i
->type
!= IMAGE_SUBVOLUME
)
1070 /* We set the quota both for the subvolume as well as for the
1071 * subtree. The latter is mostly for historical reasons, since
1072 * we didn't use to have a concept of subtree quota, and hence
1073 * only modified the subvolume quota. */
1075 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
1076 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
1077 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
1080 int image_read_metadata(Image
*i
) {
1081 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
1086 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
1092 case IMAGE_SUBVOLUME
:
1093 case IMAGE_DIRECTORY
: {
1094 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
1095 sd_id128_t machine_id
= SD_ID128_NULL
;
1096 _cleanup_free_
char *hostname
= NULL
;
1097 _cleanup_free_
char *path
= NULL
;
1099 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1100 if (r
< 0 && r
!= -ENOENT
)
1101 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1103 r
= read_etc_hostname(path
, &hostname
);
1105 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1110 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1111 if (r
< 0 && r
!= -ENOENT
)
1112 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1114 _cleanup_close_
int fd
= -1;
1116 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1118 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1120 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1122 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1128 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1129 if (r
< 0 && r
!= -ENOENT
)
1130 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1132 r
= load_env_file_pairs(NULL
, path
, NULL
, &machine_info
);
1134 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1137 r
= load_os_release_pairs(i
->path
, &os_release
);
1139 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1141 free_and_replace(i
->hostname
, hostname
);
1142 i
->machine_id
= machine_id
;
1143 strv_free_and_replace(i
->machine_info
, machine_info
);
1144 strv_free_and_replace(i
->os_release
, os_release
);
1151 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1152 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1154 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1158 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1162 r
= dissected_image_acquire_metadata(m
);
1166 free_and_replace(i
->hostname
, m
->hostname
);
1167 i
->machine_id
= m
->machine_id
;
1168 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1169 strv_free_and_replace(i
->os_release
, m
->os_release
);
1178 i
->metadata_valid
= true;
1183 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1189 /* Locks an image name, regardless of the precise path used. */
1191 if (!image_name_is_valid(name
))
1194 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1195 *ret
= (LockFile
) LOCK_FILE_INIT
;
1199 if (streq(name
, ".host"))
1202 mkdir_p("/run/systemd/nspawn/locks", 0700);
1203 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1205 return make_lock_file(p
, operation
, ret
);
1208 bool image_name_is_valid(const char *s
) {
1209 if (!filename_is_valid(s
))
1212 if (string_has_cc(s
, NULL
))
1215 if (!utf8_is_valid(s
))
1218 /* Temporary files for atomically creating new files */
1219 if (startswith(s
, ".#"))
1225 bool image_in_search_path(ImageClass
class, const char *image
) {
1230 NULSTR_FOREACH(path
, image_search_path
[class]) {
1234 p
= path_startswith(image
, path
);
1238 /* Make sure there's a filename following */
1239 k
= strcspn(p
, "/");
1245 /* Accept trailing slashes */
1246 if (p
[strspn(p
, "/")] == 0)
1254 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1255 [IMAGE_DIRECTORY
] = "directory",
1256 [IMAGE_SUBVOLUME
] = "subvolume",
1257 [IMAGE_RAW
] = "raw",
1258 [IMAGE_BLOCK
] = "block",
1261 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);