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
);
153 path_kill_slashes(i
->path
);
160 static int extract_pretty(const char *path
, const char *suffix
, char **ret
) {
161 _cleanup_free_
char *name
= NULL
;
168 p
= last_path_component(path
);
171 name
= strndup(p
, n
);
178 e
= endswith(name
, suffix
);
185 if (!image_name_is_valid(name
))
188 *ret
= TAKE_PTR(name
);
192 static int image_make(
196 const char *filename
,
199 _cleanup_free_
char *pretty_buffer
= NULL
;
206 /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block
207 * devices into /var/lib/machines/, and treat them normally. */
209 if (fstatat(dfd
, filename
, &st
, 0) < 0)
213 (path
&& path_startswith(path
, "/usr")) ||
214 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
216 if (S_ISDIR(st
.st_mode
)) {
217 _cleanup_close_
int fd
= -1;
218 unsigned file_attr
= 0;
224 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
228 pretty
= pretty_buffer
;
231 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
235 /* btrfs subvolumes have inode 256 */
236 if (st
.st_ino
== 256) {
238 r
= btrfs_is_filesystem(fd
);
242 BtrfsSubvolInfo info
;
244 /* It's a btrfs subvolume */
246 r
= btrfs_subvol_get_info_fd(fd
, 0, &info
);
250 r
= image_new(IMAGE_SUBVOLUME
,
254 info
.read_only
|| read_only
,
261 if (btrfs_quota_scan_ongoing(fd
) == 0) {
262 BtrfsQuotaInfo quota
;
264 r
= btrfs_subvol_get_subtree_quota_fd(fd
, 0, "a
);
266 (*ret
)->usage
= quota
.referenced
;
267 (*ret
)->usage_exclusive
= quota
.exclusive
;
269 (*ret
)->limit
= quota
.referenced_max
;
270 (*ret
)->limit_exclusive
= quota
.exclusive_max
;
278 /* If the IMMUTABLE bit is set, we consider the
279 * directory read-only. Since the ioctl is not
280 * supported everywhere we ignore failures. */
281 (void) read_attr_fd(fd
, &file_attr
);
283 /* It's just a normal directory. */
284 r
= image_new(IMAGE_DIRECTORY
,
288 read_only
|| (file_attr
& FS_IMMUTABLE_FL
),
297 } else if (S_ISREG(st
.st_mode
) && endswith(filename
, ".raw")) {
300 /* It's a RAW disk image */
305 (void) fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
308 r
= extract_pretty(filename
, ".raw", &pretty_buffer
);
312 pretty
= pretty_buffer
;
315 r
= image_new(IMAGE_RAW
,
319 !(st
.st_mode
& 0222) || read_only
,
321 timespec_load(&st
.st_mtim
),
326 (*ret
)->usage
= (*ret
)->usage_exclusive
= st
.st_blocks
* 512;
327 (*ret
)->limit
= (*ret
)->limit_exclusive
= st
.st_size
;
331 } else if (S_ISBLK(st
.st_mode
)) {
332 _cleanup_close_
int block_fd
= -1;
333 uint64_t size
= UINT64_MAX
;
341 r
= extract_pretty(filename
, NULL
, &pretty_buffer
);
345 pretty
= pretty_buffer
;
348 block_fd
= openat(dfd
, filename
, O_RDONLY
|O_NONBLOCK
|O_CLOEXEC
|O_NOCTTY
);
350 log_debug_errno(errno
, "Failed to open block device %s/%s, ignoring: %m", path
, filename
);
352 if (fstat(block_fd
, &st
) < 0)
354 if (!S_ISBLK(st
.st_mode
)) /* Verify that what we opened is actually what we think it is */
360 if (ioctl(block_fd
, BLKROGET
, &state
) < 0)
361 log_debug_errno(errno
, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path
, filename
);
366 if (ioctl(block_fd
, BLKGETSIZE64
, &size
) < 0)
367 log_debug_errno(errno
, "Failed to issue BLKFLSBUF on device %s/%s, ignoring: %m", path
, filename
);
369 block_fd
= safe_close(block_fd
);
372 r
= image_new(IMAGE_BLOCK
,
376 !(st
.st_mode
& 0222) || read_only
,
383 if (size
!= 0 && size
!= UINT64_MAX
)
384 (*ret
)->usage
= (*ret
)->usage_exclusive
= (*ret
)->limit
= (*ret
)->limit_exclusive
= size
;
392 int image_find(ImageClass
class, const char *name
, Image
**ret
) {
397 assert(class < _IMAGE_CLASS_MAX
);
400 /* There are no images with invalid names */
401 if (!image_name_is_valid(name
))
404 NULSTR_FOREACH(path
, image_search_path
[class]) {
405 _cleanup_closedir_
DIR *d
= NULL
;
415 r
= image_make(name
, dirfd(d
), path
, name
, ret
);
416 if (IN_SET(r
, 0, -ENOENT
)) {
417 _cleanup_free_
char *raw
= NULL
;
419 raw
= strappend(name
, ".raw");
423 r
= image_make(name
, dirfd(d
), path
, raw
, ret
);
424 if (IN_SET(r
, 0, -ENOENT
))
433 if (class == IMAGE_MACHINE
&& streq(name
, ".host"))
434 return image_make(".host", AT_FDCWD
, NULL
, "/", ret
);
439 int image_discover(ImageClass
class, Hashmap
*h
) {
444 assert(class < _IMAGE_CLASS_MAX
);
447 NULSTR_FOREACH(path
, image_search_path
[class]) {
448 _cleanup_closedir_
DIR *d
= NULL
;
459 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
460 _cleanup_(image_unrefp
) Image
*image
= NULL
;
461 _cleanup_free_
char *truncated
= NULL
;
462 const char *pretty
, *e
;
464 if (dot_or_dot_dot(de
->d_name
))
467 e
= endswith(de
->d_name
, ".raw");
469 truncated
= strndup(de
->d_name
, e
- de
->d_name
);
477 if (!image_name_is_valid(pretty
))
480 if (hashmap_contains(h
, pretty
))
483 r
= image_make(pretty
, dirfd(d
), path
, de
->d_name
, &image
);
484 if (IN_SET(r
, 0, -ENOENT
))
489 r
= hashmap_put(h
, image
->name
, image
);
497 if (class == IMAGE_MACHINE
&& !hashmap_contains(h
, ".host")) {
498 _cleanup_(image_unrefp
) Image
*image
= NULL
;
500 r
= image_make(".host", AT_FDCWD
, NULL
, "/", &image
);
504 r
= hashmap_put(h
, image
->name
, image
);
515 int image_remove(Image
*i
) {
516 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
517 _cleanup_strv_free_
char **settings
= NULL
;
518 _cleanup_free_
char *roothash
= NULL
;
524 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
527 settings
= image_settings_path(i
);
531 roothash
= image_roothash_path(i
);
535 /* Make sure we don't interfere with a running nspawn */
536 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
542 case IMAGE_SUBVOLUME
:
544 /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the
546 if (unlink(i
->path
) < 0) {
547 r
= btrfs_subvol_remove(i
->path
, BTRFS_REMOVE_RECURSIVE
|BTRFS_REMOVE_QUOTA
);
554 case IMAGE_DIRECTORY
:
555 /* Allow deletion of read-only directories */
556 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
557 r
= rm_rf(i
->path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
565 /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node
566 * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink
567 * the thing (it's most likely a symlink after all). */
569 if (path_startswith(i
->path
, "/dev"))
574 if (unlink(i
->path
) < 0)
582 STRV_FOREACH(j
, settings
) {
583 if (unlink(*j
) < 0 && errno
!= ENOENT
)
584 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", *j
);
587 if (unlink(roothash
) < 0 && errno
!= ENOENT
)
588 log_debug_errno(errno
, "Failed to unlink %s, ignoring: %m", roothash
);
593 static int rename_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
594 _cleanup_free_
char *rs
= NULL
;
597 fn
= strjoina(new_name
, suffix
);
599 rs
= file_in_same_dir(path
, fn
);
603 return rename_noreplace(AT_FDCWD
, path
, AT_FDCWD
, rs
);
606 int image_rename(Image
*i
, const char *new_name
) {
607 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
, name_lock
= LOCK_FILE_INIT
;
608 _cleanup_free_
char *new_path
= NULL
, *nn
= NULL
, *roothash
= NULL
;
609 _cleanup_strv_free_
char **settings
= NULL
;
610 unsigned file_attr
= 0;
616 if (!image_name_is_valid(new_name
))
619 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
622 settings
= image_settings_path(i
);
626 roothash
= image_roothash_path(i
);
630 /* Make sure we don't interfere with a running nspawn */
631 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
635 /* Make sure nobody takes the new name, between the time we
636 * checked it is currently unused in all search paths, and the
637 * time we take possession of it */
638 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
642 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
650 case IMAGE_DIRECTORY
:
651 /* Turn of the immutable bit while we rename the image, so that we can rename it */
652 (void) read_attr_path(i
->path
, &file_attr
);
654 if (file_attr
& FS_IMMUTABLE_FL
)
655 (void) chattr_path(i
->path
, 0, FS_IMMUTABLE_FL
);
658 case IMAGE_SUBVOLUME
:
659 new_path
= file_in_same_dir(i
->path
, new_name
);
664 /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */
665 if (path_startswith(i
->path
, "/dev"))
668 new_path
= file_in_same_dir(i
->path
, new_name
);
674 fn
= strjoina(new_name
, ".raw");
675 new_path
= file_in_same_dir(i
->path
, fn
);
686 nn
= strdup(new_name
);
690 r
= rename_noreplace(AT_FDCWD
, i
->path
, AT_FDCWD
, new_path
);
694 /* Restore the immutable bit, if it was set before */
695 if (file_attr
& FS_IMMUTABLE_FL
)
696 (void) chattr_path(new_path
, FS_IMMUTABLE_FL
, FS_IMMUTABLE_FL
);
698 free_and_replace(i
->path
, new_path
);
699 free_and_replace(i
->name
, nn
);
701 STRV_FOREACH(j
, settings
) {
702 r
= rename_auxiliary_file(*j
, new_name
, ".nspawn");
703 if (r
< 0 && r
!= -ENOENT
)
704 log_debug_errno(r
, "Failed to rename settings file %s, ignoring: %m", *j
);
707 r
= rename_auxiliary_file(roothash
, new_name
, ".roothash");
708 if (r
< 0 && r
!= -ENOENT
)
709 log_debug_errno(r
, "Failed to rename roothash file %s, ignoring: %m", roothash
);
714 static int clone_auxiliary_file(const char *path
, const char *new_name
, const char *suffix
) {
715 _cleanup_free_
char *rs
= NULL
;
718 fn
= strjoina(new_name
, suffix
);
720 rs
= file_in_same_dir(path
, fn
);
724 return copy_file_atomic(path
, rs
, 0664, 0, COPY_REFLINK
);
727 int image_clone(Image
*i
, const char *new_name
, bool read_only
) {
728 _cleanup_(release_lock_file
) LockFile name_lock
= LOCK_FILE_INIT
;
729 _cleanup_strv_free_
char **settings
= NULL
;
730 _cleanup_free_
char *roothash
= NULL
;
731 const char *new_path
;
737 if (!image_name_is_valid(new_name
))
740 settings
= image_settings_path(i
);
744 roothash
= image_roothash_path(i
);
748 /* Make sure nobody takes the new name, between the time we
749 * checked it is currently unused in all search paths, and the
750 * time we take possession of it */
751 r
= image_name_lock(new_name
, LOCK_EX
|LOCK_NB
, &name_lock
);
755 r
= image_find(IMAGE_MACHINE
, new_name
, NULL
);
763 case IMAGE_SUBVOLUME
:
764 case IMAGE_DIRECTORY
:
765 /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain
768 new_path
= strjoina("/var/lib/machines/", new_name
);
770 r
= btrfs_subvol_snapshot(i
->path
, new_path
,
771 (read_only
? BTRFS_SNAPSHOT_READ_ONLY
: 0) |
772 BTRFS_SNAPSHOT_FALLBACK_COPY
|
773 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
774 BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE
|
775 BTRFS_SNAPSHOT_RECURSIVE
|
776 BTRFS_SNAPSHOT_QUOTA
);
778 /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */
779 (void) btrfs_subvol_auto_qgroup(new_path
, 0, true);
784 new_path
= strjoina("/var/lib/machines/", new_name
, ".raw");
786 r
= copy_file_atomic(i
->path
, new_path
, read_only
? 0444 : 0644, FS_NOCOW_FL
, COPY_REFLINK
);
797 STRV_FOREACH(j
, settings
) {
798 r
= clone_auxiliary_file(*j
, new_name
, ".nspawn");
799 if (r
< 0 && r
!= -ENOENT
)
800 log_debug_errno(r
, "Failed to clone settings %s, ignoring: %m", *j
);
803 r
= clone_auxiliary_file(roothash
, new_name
, ".roothash");
804 if (r
< 0 && r
!= -ENOENT
)
805 log_debug_errno(r
, "Failed to clone root hash file %s, ignoring: %m", roothash
);
810 int image_read_only(Image
*i
, bool b
) {
811 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
816 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
819 /* Make sure we don't interfere with a running nspawn */
820 r
= image_path_lock(i
->path
, LOCK_EX
|LOCK_NB
, &global_lock
, &local_lock
);
826 case IMAGE_SUBVOLUME
:
828 /* Note that we set the flag only on the top-level
829 * subvolume of the image. */
831 r
= btrfs_subvol_set_read_only(i
->path
, b
);
837 case IMAGE_DIRECTORY
:
838 /* For simple directory trees we cannot use the access
839 mode of the top-level directory, since it has an
840 effect on the container itself. However, we can
841 use the "immutable" flag, to at least make the
842 top-level directory read-only. It's not as good as
843 a read-only subvolume, but at least something, and
844 we can read the value back. */
846 r
= chattr_path(i
->path
, b
? FS_IMMUTABLE_FL
: 0, FS_IMMUTABLE_FL
);
855 if (stat(i
->path
, &st
) < 0)
858 if (chmod(i
->path
, (st
.st_mode
& 0444) | (b
? 0000 : 0200)) < 0)
861 /* If the images is now read-only, it's a good time to
862 * defrag it, given that no write patterns will
863 * fragment it again. */
865 (void) btrfs_defrag(i
->path
);
870 _cleanup_close_
int fd
= -1;
874 fd
= open(i
->path
, O_CLOEXEC
|O_RDONLY
|O_NONBLOCK
|O_NOCTTY
);
878 if (fstat(fd
, &st
) < 0)
880 if (!S_ISBLK(st
.st_mode
))
883 if (ioctl(fd
, BLKROSET
, &state
) < 0)
896 int image_path_lock(const char *path
, int operation
, LockFile
*global
, LockFile
*local
) {
897 _cleanup_free_
char *p
= NULL
;
898 LockFile t
= LOCK_FILE_INIT
;
906 /* Locks an image path. This actually creates two locks: one
907 * "local" one, next to the image path itself, which might be
908 * shared via NFS. And another "global" one, in /run, that
909 * uses the device/inode number. This has the benefit that we
910 * can even lock a tree that is a mount point, correctly. */
912 if (!path_is_absolute(path
))
915 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
916 *local
= *global
= (LockFile
) LOCK_FILE_INIT
;
920 if (path_equal(path
, "/"))
923 if (stat(path
, &st
) >= 0) {
924 if (S_ISBLK(st
.st_mode
))
925 r
= asprintf(&p
, "/run/systemd/nspawn/locks/block-%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
926 else if (S_ISDIR(st
.st_mode
) || S_ISREG(st
.st_mode
))
927 r
= asprintf(&p
, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st
.st_dev
, (unsigned long) st
.st_ino
);
935 /* For block devices we don't need the "local" lock, as the major/minor lock above should be sufficient, since
936 * block devices are device local anyway. */
937 if (!path_startswith(path
, "/dev")) {
938 r
= make_lock_file_for(path
, operation
, &t
);
940 if ((operation
& LOCK_SH
) && r
== -EROFS
)
941 log_debug_errno(r
, "Failed to create shared lock for '%s', ignoring: %m", path
);
948 mkdir_p("/run/systemd/nspawn/locks", 0700);
950 r
= make_lock_file(p
, operation
, global
);
952 release_lock_file(&t
);
956 *global
= (LockFile
) LOCK_FILE_INIT
;
962 int image_set_limit(Image
*i
, uint64_t referenced_max
) {
965 if (IMAGE_IS_VENDOR(i
) || IMAGE_IS_HOST(i
))
968 if (i
->type
!= IMAGE_SUBVOLUME
)
971 /* We set the quota both for the subvolume as well as for the
972 * subtree. The latter is mostly for historical reasons, since
973 * we didn't use to have a concept of subtree quota, and hence
974 * only modified the subvolume quota. */
976 (void) btrfs_qgroup_set_limit(i
->path
, 0, referenced_max
);
977 (void) btrfs_subvol_auto_qgroup(i
->path
, 0, true);
978 return btrfs_subvol_set_subtree_quota_limit(i
->path
, 0, referenced_max
);
981 int image_read_metadata(Image
*i
) {
982 _cleanup_(release_lock_file
) LockFile global_lock
= LOCK_FILE_INIT
, local_lock
= LOCK_FILE_INIT
;
987 r
= image_path_lock(i
->path
, LOCK_SH
|LOCK_NB
, &global_lock
, &local_lock
);
993 case IMAGE_SUBVOLUME
:
994 case IMAGE_DIRECTORY
: {
995 _cleanup_strv_free_
char **machine_info
= NULL
, **os_release
= NULL
;
996 sd_id128_t machine_id
= SD_ID128_NULL
;
997 _cleanup_free_
char *hostname
= NULL
;
998 _cleanup_free_
char *path
= NULL
;
999 _cleanup_fclose_
FILE *f
= NULL
;
1001 r
= chase_symlinks("/etc/hostname", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1002 if (r
< 0 && r
!= -ENOENT
)
1003 log_debug_errno(r
, "Failed to chase /etc/hostname in image %s: %m", i
->name
);
1005 r
= read_etc_hostname(path
, &hostname
);
1007 log_debug_errno(errno
, "Failed to read /etc/hostname of image %s: %m", i
->name
);
1012 r
= chase_symlinks("/etc/machine-id", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1013 if (r
< 0 && r
!= -ENOENT
)
1014 log_debug_errno(r
, "Failed to chase /etc/machine-id in image %s: %m", i
->name
);
1016 _cleanup_close_
int fd
= -1;
1018 fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1020 log_debug_errno(errno
, "Failed to open %s: %m", path
);
1022 r
= id128_read_fd(fd
, ID128_PLAIN
, &machine_id
);
1024 log_debug_errno(r
, "Image %s contains invalid machine ID.", i
->name
);
1030 r
= chase_symlinks("/etc/machine-info", i
->path
, CHASE_PREFIX_ROOT
|CHASE_TRAIL_SLASH
, &path
);
1031 if (r
< 0 && r
!= -ENOENT
)
1032 log_debug_errno(r
, "Failed to chase /etc/machine-info in image %s: %m", i
->name
);
1034 r
= load_env_file_pairs(NULL
, path
, NULL
, &machine_info
);
1036 log_debug_errno(r
, "Failed to parse machine-info data of %s: %m", i
->name
);
1039 r
= load_os_release_pairs(i
->path
, &os_release
);
1041 log_debug_errno(r
, "Failed to read os-release in image, ignoring: %m");
1043 free_and_replace(i
->hostname
, hostname
);
1044 i
->machine_id
= machine_id
;
1045 strv_free_and_replace(i
->machine_info
, machine_info
);
1046 strv_free_and_replace(i
->os_release
, os_release
);
1053 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
1054 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
1056 r
= loop_device_make_by_path(i
->path
, O_RDONLY
, &d
);
1060 r
= dissect_image(d
->fd
, NULL
, 0, DISSECT_IMAGE_REQUIRE_ROOT
, &m
);
1064 r
= dissected_image_acquire_metadata(m
);
1068 free_and_replace(i
->hostname
, m
->hostname
);
1069 i
->machine_id
= m
->machine_id
;
1070 strv_free_and_replace(i
->machine_info
, m
->machine_info
);
1071 strv_free_and_replace(i
->os_release
, m
->os_release
);
1080 i
->metadata_valid
= true;
1085 int image_name_lock(const char *name
, int operation
, LockFile
*ret
) {
1091 /* Locks an image name, regardless of the precise path used. */
1093 if (!image_name_is_valid(name
))
1096 if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) {
1097 *ret
= (LockFile
) LOCK_FILE_INIT
;
1101 if (streq(name
, ".host"))
1104 mkdir_p("/run/systemd/nspawn/locks", 0700);
1105 p
= strjoina("/run/systemd/nspawn/locks/name-", name
);
1107 return make_lock_file(p
, operation
, ret
);
1110 bool image_name_is_valid(const char *s
) {
1111 if (!filename_is_valid(s
))
1114 if (string_has_cc(s
, NULL
))
1117 if (!utf8_is_valid(s
))
1120 /* Temporary files for atomically creating new files */
1121 if (startswith(s
, ".#"))
1127 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
1128 [IMAGE_DIRECTORY
] = "directory",
1129 [IMAGE_SUBVOLUME
] = "subvolume",
1130 [IMAGE_RAW
] = "raw",
1131 [IMAGE_BLOCK
] = "block",
1134 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);