]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/volatile-root/volatile-root.c
fstab-generator: add support for volatile boots
[thirdparty/systemd.git] / src / volatile-root / volatile-root.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2016 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sys/mount.h>
21
22 #include "alloc-util.h"
23 #include "fs-util.h"
24 #include "mkdir.h"
25 #include "mount-util.h"
26 #include "stat-util.h"
27 #include "volatile-util.h"
28 #include "string-util.h"
29 #include "path-util.h"
30
31 static int make_volatile(const char *path) {
32 _cleanup_free_ char *old_usr = NULL;
33 int r;
34
35 r = path_is_mount_point(path, NULL, AT_SYMLINK_FOLLOW);
36 if (r < 0)
37 return log_error_errno(r, "Couldn't determine whether %s is a mount point: %m", path);
38 if (r == 0) {
39 log_error("%s is not a mount point.", path);
40 return -EINVAL;
41 }
42
43 r = path_is_temporary_fs(path);
44 if (r < 0)
45 return log_error_errno(r, "Couldn't determine whether %s is a temporary file system: %m", path);
46 if (r > 0) {
47 log_info("%s already is a temporary file system.", path);
48 return 0;
49 }
50
51 r = chase_symlinks("/usr", path, CHASE_PREFIX_ROOT, &old_usr);
52 if (r < 0)
53 return log_error_errno(r, "/usr not available in old root: %m");
54
55 r = mkdir_p("/run/systemd/volatile-sysroot", 0700);
56 if (r < 0)
57 return log_error_errno(r, "Couldn't generate volatile sysroot directory: %m");
58
59 r = mount_verbose(LOG_ERR, "tmpfs", "/run/systemd/volatile-sysroot", "tmpfs", MS_STRICTATIME, "mode=755");
60 if (r < 0)
61 goto finish_rmdir;
62
63 if (mkdir("/run/systemd/volatile-sysroot/usr", 0755) < 0) {
64 r = -errno;
65 goto finish_umount;
66 }
67
68 r = mount_verbose(LOG_ERR, old_usr, "/run/systemd/volatile-sysroot/usr", NULL, MS_BIND|MS_REC, NULL);
69 if (r < 0)
70 goto finish_umount;
71
72 r = bind_remount_recursive("/run/systemd/volatile-sysroot/usr", true, NULL);
73 if (r < 0)
74 goto finish_umount;
75
76 r = umount_recursive(path, 0);
77 if (r < 0) {
78 log_error_errno(r, "Failed to unmount %s: %m", path);
79 goto finish_umount;
80 }
81
82 if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
83 log_warning_errno(errno, "Failed to remount %s MS_SLAVE|MS_REC: %m", path);
84
85 r = mount_verbose(LOG_ERR, "/run/systemd/volatile-sysroot", path, NULL, MS_MOVE, NULL);
86
87 finish_umount:
88 (void) umount_recursive("/run/systemd/volatile-sysroot", 0);
89
90 finish_rmdir:
91 (void) rmdir("/run/systemd/volatile-sysroot");
92
93 return r;
94 }
95
96 int main(int argc, char *argv[]) {
97 VolatileMode m = _VOLATILE_MODE_INVALID;
98 const char *path;
99 int r;
100
101 log_set_target(LOG_TARGET_AUTO);
102 log_parse_environment();
103 log_open();
104
105 if (argc > 3) {
106 log_error("Too many arguments. Expected directory and mode.");
107 r = -EINVAL;
108 goto finish;
109 }
110
111 r = query_volatile_mode(&m);
112 if (r < 0) {
113 log_error_errno(r, "Failed to determine volatile mode from kernel command line.");
114 goto finish;
115 }
116 if (r == 0 && argc >= 2) {
117 /* The kernel command line always wins. However if nothing was set there, the argument passed here wins instead. */
118 m = volatile_mode_from_string(argv[1]);
119 if (m < 0) {
120 log_error("Couldn't parse volatile mode: %s", argv[1]);
121 r = -EINVAL;
122 goto finish;
123 }
124 }
125
126 if (argc < 3)
127 path = "/sysroot";
128 else {
129 path = argv[2];
130
131 if (isempty(path)) {
132 log_error("Directory name cannot be empty.");
133 r = -EINVAL;
134 goto finish;
135 }
136 if (!path_is_absolute(path)) {
137 log_error("Directory must be specified as absolute path.");
138 r = -EINVAL;
139 goto finish;
140 }
141 if (path_equal(path, "/")) {
142 log_error("Directory cannot be the root directory.");
143 r = -EINVAL;
144 goto finish;
145 }
146 }
147
148 if (m != VOLATILE_YES) {
149 r = 0;
150 goto finish;
151 }
152
153 r = make_volatile(path);
154
155 finish:
156 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
157 }