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