]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/volatile-root/volatile-root.c
cd3a85d0ef8a315880a9fa405a8b497ff9fd5a46
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2016 Lennart Poettering
8 #include "alloc-util.h"
11 #include "mount-util.h"
12 #include "stat-util.h"
13 #include "volatile-util.h"
14 #include "string-util.h"
15 #include "path-util.h"
17 static int make_volatile(const char *path
) {
18 _cleanup_free_
char *old_usr
= NULL
;
21 r
= path_is_mount_point(path
, NULL
, AT_SYMLINK_FOLLOW
);
23 return log_error_errno(r
, "Couldn't determine whether %s is a mount point: %m", path
);
25 log_error("%s is not a mount point.", path
);
29 r
= path_is_temporary_fs(path
);
31 return log_error_errno(r
, "Couldn't determine whether %s is a temporary file system: %m", path
);
33 log_info("%s already is a temporary file system.", path
);
37 r
= chase_symlinks("/usr", path
, CHASE_PREFIX_ROOT
, &old_usr
);
39 return log_error_errno(r
, "/usr not available in old root: %m");
41 r
= mkdir_p("/run/systemd/volatile-sysroot", 0700);
43 return log_error_errno(r
, "Couldn't generate volatile sysroot directory: %m");
45 r
= mount_verbose(LOG_ERR
, "tmpfs", "/run/systemd/volatile-sysroot", "tmpfs", MS_STRICTATIME
, "mode=755");
49 if (mkdir("/run/systemd/volatile-sysroot/usr", 0755) < 0) {
54 r
= mount_verbose(LOG_ERR
, old_usr
, "/run/systemd/volatile-sysroot/usr", NULL
, MS_BIND
|MS_REC
, NULL
);
58 r
= bind_remount_recursive("/run/systemd/volatile-sysroot/usr", true, NULL
);
62 r
= umount_recursive(path
, 0);
64 log_error_errno(r
, "Failed to unmount %s: %m", path
);
68 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0)
69 log_warning_errno(errno
, "Failed to remount %s MS_SLAVE|MS_REC: %m", path
);
71 r
= mount_verbose(LOG_ERR
, "/run/systemd/volatile-sysroot", path
, NULL
, MS_MOVE
, NULL
);
74 (void) umount_recursive("/run/systemd/volatile-sysroot", 0);
77 (void) rmdir("/run/systemd/volatile-sysroot");
82 int main(int argc
, char *argv
[]) {
83 VolatileMode m
= _VOLATILE_MODE_INVALID
;
87 log_set_target(LOG_TARGET_AUTO
);
88 log_parse_environment();
92 log_error("Too many arguments. Expected directory and mode.");
97 r
= query_volatile_mode(&m
);
99 log_error_errno(r
, "Failed to determine volatile mode from kernel command line.");
102 if (r
== 0 && argc
>= 2) {
103 /* The kernel command line always wins. However if nothing was set there, the argument passed here wins instead. */
104 m
= volatile_mode_from_string(argv
[1]);
106 log_error("Couldn't parse volatile mode: %s", argv
[1]);
118 log_error("Directory name cannot be empty.");
122 if (!path_is_absolute(path
)) {
123 log_error("Directory must be specified as absolute path.");
127 if (path_equal(path
, "/")) {
128 log_error("Directory cannot be the root directory.");
134 if (m
!= VOLATILE_YES
) {
139 r
= make_volatile(path
);
142 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;