]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
91214a37 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2016 Lennart Poettering | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. | |
19 | ***/ | |
20 | ||
21 | #include <sys/mount.h> | |
22 | ||
23 | #include "alloc-util.h" | |
24 | #include "fs-util.h" | |
25 | #include "mkdir.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" | |
31 | ||
32 | static int make_volatile(const char *path) { | |
33 | _cleanup_free_ char *old_usr = NULL; | |
34 | int r; | |
35 | ||
36 | r = path_is_mount_point(path, NULL, AT_SYMLINK_FOLLOW); | |
37 | if (r < 0) | |
38 | return log_error_errno(r, "Couldn't determine whether %s is a mount point: %m", path); | |
39 | if (r == 0) { | |
40 | log_error("%s is not a mount point.", path); | |
41 | return -EINVAL; | |
42 | } | |
43 | ||
44 | r = path_is_temporary_fs(path); | |
45 | if (r < 0) | |
46 | return log_error_errno(r, "Couldn't determine whether %s is a temporary file system: %m", path); | |
47 | if (r > 0) { | |
48 | log_info("%s already is a temporary file system.", path); | |
49 | return 0; | |
50 | } | |
51 | ||
52 | r = chase_symlinks("/usr", path, CHASE_PREFIX_ROOT, &old_usr); | |
53 | if (r < 0) | |
54 | return log_error_errno(r, "/usr not available in old root: %m"); | |
55 | ||
56 | r = mkdir_p("/run/systemd/volatile-sysroot", 0700); | |
57 | if (r < 0) | |
58 | return log_error_errno(r, "Couldn't generate volatile sysroot directory: %m"); | |
59 | ||
60 | r = mount_verbose(LOG_ERR, "tmpfs", "/run/systemd/volatile-sysroot", "tmpfs", MS_STRICTATIME, "mode=755"); | |
61 | if (r < 0) | |
62 | goto finish_rmdir; | |
63 | ||
64 | if (mkdir("/run/systemd/volatile-sysroot/usr", 0755) < 0) { | |
65 | r = -errno; | |
66 | goto finish_umount; | |
67 | } | |
68 | ||
69 | r = mount_verbose(LOG_ERR, old_usr, "/run/systemd/volatile-sysroot/usr", NULL, MS_BIND|MS_REC, NULL); | |
70 | if (r < 0) | |
71 | goto finish_umount; | |
72 | ||
73 | r = bind_remount_recursive("/run/systemd/volatile-sysroot/usr", true, NULL); | |
74 | if (r < 0) | |
75 | goto finish_umount; | |
76 | ||
77 | r = umount_recursive(path, 0); | |
78 | if (r < 0) { | |
79 | log_error_errno(r, "Failed to unmount %s: %m", path); | |
80 | goto finish_umount; | |
81 | } | |
82 | ||
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); | |
85 | ||
86 | r = mount_verbose(LOG_ERR, "/run/systemd/volatile-sysroot", path, NULL, MS_MOVE, NULL); | |
87 | ||
88 | finish_umount: | |
89 | (void) umount_recursive("/run/systemd/volatile-sysroot", 0); | |
90 | ||
91 | finish_rmdir: | |
92 | (void) rmdir("/run/systemd/volatile-sysroot"); | |
93 | ||
94 | return r; | |
95 | } | |
96 | ||
97 | int main(int argc, char *argv[]) { | |
98 | VolatileMode m = _VOLATILE_MODE_INVALID; | |
99 | const char *path; | |
100 | int r; | |
101 | ||
102 | log_set_target(LOG_TARGET_AUTO); | |
103 | log_parse_environment(); | |
104 | log_open(); | |
105 | ||
106 | if (argc > 3) { | |
107 | log_error("Too many arguments. Expected directory and mode."); | |
108 | r = -EINVAL; | |
109 | goto finish; | |
110 | } | |
111 | ||
112 | r = query_volatile_mode(&m); | |
113 | if (r < 0) { | |
114 | log_error_errno(r, "Failed to determine volatile mode from kernel command line."); | |
115 | goto finish; | |
116 | } | |
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]); | |
120 | if (m < 0) { | |
121 | log_error("Couldn't parse volatile mode: %s", argv[1]); | |
122 | r = -EINVAL; | |
123 | goto finish; | |
124 | } | |
125 | } | |
126 | ||
127 | if (argc < 3) | |
128 | path = "/sysroot"; | |
129 | else { | |
130 | path = argv[2]; | |
131 | ||
132 | if (isempty(path)) { | |
133 | log_error("Directory name cannot be empty."); | |
134 | r = -EINVAL; | |
135 | goto finish; | |
136 | } | |
137 | if (!path_is_absolute(path)) { | |
138 | log_error("Directory must be specified as absolute path."); | |
139 | r = -EINVAL; | |
140 | goto finish; | |
141 | } | |
142 | if (path_equal(path, "/")) { | |
143 | log_error("Directory cannot be the root directory."); | |
144 | r = -EINVAL; | |
145 | goto finish; | |
146 | } | |
147 | } | |
148 | ||
149 | if (m != VOLATILE_YES) { | |
150 | r = 0; | |
151 | goto finish; | |
152 | } | |
153 | ||
154 | r = make_volatile(path); | |
155 | ||
156 | finish: | |
157 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
158 | } |