]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/volatile-root/volatile-root.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "blockdev-util.h"
11 #include "mount-util.h"
12 #include "mountpoint-util.h"
13 #include "path-util.h"
14 #include "stat-util.h"
15 #include "string-util.h"
16 #include "volatile-util.h"
18 static int make_volatile(const char *path
) {
19 _cleanup_free_
char *old_usr
= NULL
;
24 r
= chase_symlinks("/usr", path
, CHASE_PREFIX_ROOT
, &old_usr
);
26 return log_error_errno(r
, "/usr not available in old root: %m");
28 r
= mkdir_p("/run/systemd/volatile-sysroot", 0700);
30 return log_error_errno(r
, "Couldn't generate volatile sysroot directory: %m");
32 r
= mount_verbose(LOG_ERR
, "tmpfs", "/run/systemd/volatile-sysroot", "tmpfs", MS_STRICTATIME
, "mode=755");
36 if (mkdir("/run/systemd/volatile-sysroot/usr", 0755) < 0) {
37 r
= log_error_errno(errno
, "Failed to create /usr directory: %m");
41 r
= mount_verbose(LOG_ERR
, old_usr
, "/run/systemd/volatile-sysroot/usr", NULL
, MS_BIND
|MS_REC
, NULL
);
45 r
= bind_remount_recursive("/run/systemd/volatile-sysroot/usr", MS_RDONLY
, MS_RDONLY
, NULL
);
47 log_error_errno(r
, "Failed to remount /usr read-only: %m");
51 r
= umount_recursive(path
, 0);
53 log_error_errno(r
, "Failed to unmount %s: %m", path
);
57 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0)
58 log_warning_errno(errno
, "Failed to remount %s MS_SLAVE|MS_REC, ignoring: %m", path
);
60 r
= mount_verbose(LOG_ERR
, "/run/systemd/volatile-sysroot", path
, NULL
, MS_MOVE
, NULL
);
63 (void) umount_recursive("/run/systemd/volatile-sysroot", 0);
66 (void) rmdir("/run/systemd/volatile-sysroot");
71 static int make_overlay(const char *path
) {
72 _cleanup_free_
char *escaped_path
= NULL
;
73 bool tmpfs_mounted
= false;
74 const char *options
= NULL
;
79 r
= mkdir_p("/run/systemd/overlay-sysroot", 0700);
81 return log_error_errno(r
, "Couldn't create overlay sysroot directory: %m");
83 r
= mount_verbose(LOG_ERR
, "tmpfs", "/run/systemd/overlay-sysroot", "tmpfs", MS_STRICTATIME
, "mode=755");
89 if (mkdir("/run/systemd/overlay-sysroot/upper", 0755) < 0) {
90 r
= log_error_errno(errno
, "Failed to create /run/systemd/overlay-sysroot/upper: %m");
94 if (mkdir("/run/systemd/overlay-sysroot/work", 0755) < 0) {
95 r
= log_error_errno(errno
, "Failed to create /run/systemd/overlay-sysroot/work: %m");
99 escaped_path
= shell_escape(path
, ",:");
105 options
= strjoina("lowerdir=", escaped_path
, ",upperdir=/run/systemd/overlay-sysroot/upper,workdir=/run/systemd/overlay-sysroot/work");
106 r
= mount_verbose(LOG_ERR
, "overlay", path
, "overlay", 0, options
);
110 (void) umount_verbose("/run/systemd/overlay-sysroot");
112 (void) rmdir("/run/systemd/overlay-sysroot");
116 static int run(int argc
, char *argv
[]) {
117 VolatileMode m
= _VOLATILE_MODE_INVALID
;
125 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
126 "Too many arguments. Expected directory and mode.");
128 r
= query_volatile_mode(&m
);
130 return log_error_errno(r
, "Failed to determine volatile mode from kernel command line.");
131 if (r
== 0 && argc
>= 2) {
132 /* The kernel command line always wins. However if nothing was set there, the argument passed here wins instead. */
133 m
= volatile_mode_from_string(argv
[1]);
135 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Couldn't parse volatile mode: %s", argv
[1]);
144 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
145 "Directory name cannot be empty.");
146 if (!path_is_absolute(path
))
147 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
148 "Directory must be specified as absolute path.");
149 if (path_equal(path
, "/"))
150 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
151 "Directory cannot be the root directory.");
154 if (!IN_SET(m
, VOLATILE_YES
, VOLATILE_OVERLAY
))
157 r
= path_is_mount_point(path
, NULL
, AT_SYMLINK_FOLLOW
);
159 return log_error_errno(r
, "Couldn't determine whether %s is a mount point: %m", path
);
161 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "%s is not a mount point.", path
);
163 r
= path_is_temporary_fs(path
);
165 return log_error_errno(r
, "Couldn't determine whether %s is a temporary file system: %m", path
);
167 log_info("%s already is a temporary file system.", path
);
171 /* We are about to replace the root directory with something else. Later code might want to know what we
172 * replaced here, hence let's save that information as a symlink we can later use. (This is particularly
173 * relevant for the overlayfs case where we'll fully obstruct the view onto the underlying device, hence
174 * querying the backing device node from the file system directly is no longer possible. */
175 r
= get_block_device_harder(path
, &devt
);
177 return log_error_errno(r
, "Failed to determine device major/minor of %s: %m", path
);
179 _cleanup_free_
char *dn
= NULL
;
181 r
= device_path_make_major_minor(S_IFBLK
, devt
, &dn
);
183 return log_error_errno(r
, "Failed to format device node path: %m");
185 if (symlink(dn
, "/run/systemd/volatile-root") < 0)
186 log_warning_errno(errno
, "Failed to create symlink /run/systemd/volatile-root: %m");
189 if (m
== VOLATILE_YES
)
190 return make_volatile(path
);
192 assert(m
== VOLATILE_OVERLAY
);
193 return make_overlay(path
);
197 DEFINE_MAIN_FUNCTION(run
);