]>
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"
9 #include "mount-util.h"
10 #include "mountpoint-util.h"
11 #include "path-util.h"
12 #include "stat-util.h"
13 #include "string-util.h"
14 #include "volatile-util.h"
16 static int make_volatile(const char *path
) {
17 _cleanup_free_
char *old_usr
= NULL
;
20 r
= path_is_mount_point(path
, NULL
, AT_SYMLINK_FOLLOW
);
22 return log_error_errno(r
, "Couldn't determine whether %s is a mount point: %m", path
);
24 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
25 "%s is not a mount point.", path
);
27 r
= path_is_temporary_fs(path
);
29 return log_error_errno(r
, "Couldn't determine whether %s is a temporary file system: %m", path
);
31 log_info("%s already is a temporary file system.", path
);
35 r
= chase_symlinks("/usr", path
, CHASE_PREFIX_ROOT
, &old_usr
);
37 return log_error_errno(r
, "/usr not available in old root: %m");
39 r
= mkdir_p("/run/systemd/volatile-sysroot", 0700);
41 return log_error_errno(r
, "Couldn't generate volatile sysroot directory: %m");
43 r
= mount_verbose(LOG_ERR
, "tmpfs", "/run/systemd/volatile-sysroot", "tmpfs", MS_STRICTATIME
, "mode=755");
47 if (mkdir("/run/systemd/volatile-sysroot/usr", 0755) < 0) {
48 r
= log_error_errno(errno
, "Failed to create /usr directory: %m");
52 r
= mount_verbose(LOG_ERR
, old_usr
, "/run/systemd/volatile-sysroot/usr", NULL
, MS_BIND
|MS_REC
, NULL
);
56 r
= bind_remount_recursive("/run/systemd/volatile-sysroot/usr", true, NULL
);
58 log_error_errno(r
, "Failed to remount /usr read-only: %m");
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, ignoring: %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 static int run(int argc
, char *argv
[]) {
83 VolatileMode m
= _VOLATILE_MODE_INVALID
;
90 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
91 "Too many arguments. Expected directory and mode.");
93 r
= query_volatile_mode(&m
);
95 return log_error_errno(r
, "Failed to determine volatile mode from kernel command line.");
96 if (r
== 0 && argc
>= 2) {
97 /* The kernel command line always wins. However if nothing was set there, the argument passed here wins instead. */
98 m
= volatile_mode_from_string(argv
[1]);
100 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Couldn't parse volatile mode: %s", argv
[1]);
109 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
110 "Directory name cannot be empty.");
111 if (!path_is_absolute(path
))
112 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
113 "Directory must be specified as absolute path.");
114 if (path_equal(path
, "/"))
115 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
116 "Directory cannot be the root directory.");
119 if (m
!= VOLATILE_YES
)
122 return make_volatile(path
);
125 DEFINE_MAIN_FUNCTION(run
);