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