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