]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/volatile-root/volatile-root.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/mount.h>
23 #include "alloc-util.h"
26 #include "mount-util.h"
27 #include "stat-util.h"
28 #include "volatile-util.h"
29 #include "string-util.h"
30 #include "path-util.h"
32 static int make_volatile(const char *path
) {
33 _cleanup_free_
char *old_usr
= NULL
;
36 r
= path_is_mount_point(path
, NULL
, AT_SYMLINK_FOLLOW
);
38 return log_error_errno(r
, "Couldn't determine whether %s is a mount point: %m", path
);
40 log_error("%s is not a mount point.", path
);
44 r
= path_is_temporary_fs(path
);
46 return log_error_errno(r
, "Couldn't determine whether %s is a temporary file system: %m", path
);
48 log_info("%s already is a temporary file system.", path
);
52 r
= chase_symlinks("/usr", path
, CHASE_PREFIX_ROOT
, &old_usr
);
54 return log_error_errno(r
, "/usr not available in old root: %m");
56 r
= mkdir_p("/run/systemd/volatile-sysroot", 0700);
58 return log_error_errno(r
, "Couldn't generate volatile sysroot directory: %m");
60 r
= mount_verbose(LOG_ERR
, "tmpfs", "/run/systemd/volatile-sysroot", "tmpfs", MS_STRICTATIME
, "mode=755");
64 if (mkdir("/run/systemd/volatile-sysroot/usr", 0755) < 0) {
69 r
= mount_verbose(LOG_ERR
, old_usr
, "/run/systemd/volatile-sysroot/usr", NULL
, MS_BIND
|MS_REC
, NULL
);
73 r
= bind_remount_recursive("/run/systemd/volatile-sysroot/usr", true, NULL
);
77 r
= umount_recursive(path
, 0);
79 log_error_errno(r
, "Failed to unmount %s: %m", path
);
83 if (mount(NULL
, "/", NULL
, MS_SLAVE
|MS_REC
, NULL
) < 0)
84 log_warning_errno(errno
, "Failed to remount %s MS_SLAVE|MS_REC: %m", path
);
86 r
= mount_verbose(LOG_ERR
, "/run/systemd/volatile-sysroot", path
, NULL
, MS_MOVE
, NULL
);
89 (void) umount_recursive("/run/systemd/volatile-sysroot", 0);
92 (void) rmdir("/run/systemd/volatile-sysroot");
97 int main(int argc
, char *argv
[]) {
98 VolatileMode m
= _VOLATILE_MODE_INVALID
;
102 log_set_target(LOG_TARGET_AUTO
);
103 log_parse_environment();
107 log_error("Too many arguments. Expected directory and mode.");
112 r
= query_volatile_mode(&m
);
114 log_error_errno(r
, "Failed to determine volatile mode from kernel command line.");
117 if (r
== 0 && argc
>= 2) {
118 /* The kernel command line always wins. However if nothing was set there, the argument passed here wins instead. */
119 m
= volatile_mode_from_string(argv
[1]);
121 log_error("Couldn't parse volatile mode: %s", argv
[1]);
133 log_error("Directory name cannot be empty.");
137 if (!path_is_absolute(path
)) {
138 log_error("Directory must be specified as absolute path.");
142 if (path_equal(path
, "/")) {
143 log_error("Directory cannot be the root directory.");
149 if (m
!= VOLATILE_YES
) {
154 r
= make_volatile(path
);
157 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;