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