• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14872145375

06 May 2025 09:07PM UTC coverage: 72.232% (+0.02%) from 72.214%
14872145375

push

github

DaanDeMeyer
string-table: annotate _to_string and _from_string with _const_ and _pure_, respectively

Follow-up for c94f6ab1b

297286 of 411572 relevant lines covered (72.23%)

695615.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/volatile-root/volatile-root.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/mount.h>
4

5
#include "alloc-util.h"
6
#include "blockdev-util.h"
7
#include "chase.h"
8
#include "devnum-util.h"
9
#include "escape.h"
10
#include "log.h"
11
#include "main-func.h"
12
#include "mkdir.h"
13
#include "mount-util.h"
14
#include "mountpoint-util.h"
15
#include "path-util.h"
16
#include "string-util.h"
17
#include "volatile-util.h"
18

19
static int make_volatile(const char *path) {
×
20
        _cleanup_free_ char *old_usr = NULL;
×
21
        int r;
×
22

23
        assert(path);
×
24

25
        r = chase("/usr", path, CHASE_PREFIX_ROOT, &old_usr, NULL);
×
26
        if (r < 0)
×
27
                return log_error_errno(r, "/usr not available in old root: %m");
×
28

29
        r = mkdir_p("/run/systemd/volatile-sysroot", 0700);
×
30
        if (r < 0)
×
31
                return log_error_errno(r, "Couldn't generate volatile sysroot directory: %m");
×
32

33
        r = mount_nofollow_verbose(LOG_ERR, "tmpfs", "/run/systemd/volatile-sysroot", "tmpfs", MS_STRICTATIME, "mode=0755" TMPFS_LIMITS_ROOTFS);
×
34
        if (r < 0)
×
35
                goto finish_rmdir;
×
36

37
        if (mkdir("/run/systemd/volatile-sysroot/usr", 0755) < 0) {
×
38
                r = log_error_errno(errno, "Failed to create /usr directory: %m");
×
39
                goto finish_umount;
×
40
        }
41

42
        r = mount_nofollow_verbose(LOG_ERR, old_usr, "/run/systemd/volatile-sysroot/usr", NULL, MS_BIND|MS_REC, NULL);
×
43
        if (r < 0)
×
44
                goto finish_umount;
×
45

46
        r = bind_remount_recursive("/run/systemd/volatile-sysroot/usr", MS_RDONLY, MS_RDONLY, NULL);
×
47
        if (r < 0) {
×
48
                log_error_errno(r, "Failed to remount /usr read-only: %m");
×
49
                goto finish_umount;
×
50
        }
51

52
        r = umount_recursive(path, 0);
×
53
        if (r < 0) {
×
54
                log_error_errno(r, "Failed to unmount %s: %m", path);
×
55
                goto finish_umount;
×
56
        }
57

58
        if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
×
59
                log_warning_errno(errno, "Failed to remount %s MS_SLAVE|MS_REC, ignoring: %m", path);
×
60

61
        r = mount_nofollow_verbose(LOG_ERR, "/run/systemd/volatile-sysroot", path, NULL, MS_MOVE, NULL);
×
62

63
finish_umount:
×
64
        (void) umount_recursive("/run/systemd/volatile-sysroot", 0);
×
65

66
finish_rmdir:
×
67
        (void) rmdir("/run/systemd/volatile-sysroot");
×
68

69
        return r;
×
70
}
71

72
static int make_overlay(const char *path) {
×
73
        _cleanup_free_ char *escaped_path = NULL;
×
74
        bool tmpfs_mounted = false;
×
75
        const char *options = NULL;
×
76
        int r;
×
77

78
        assert(path);
×
79

80
        r = mkdir_p("/run/systemd/overlay-sysroot", 0700);
×
81
        if (r < 0)
×
82
                return log_error_errno(r, "Couldn't create overlay sysroot directory: %m");
×
83

84
        r = mount_nofollow_verbose(LOG_ERR, "tmpfs", "/run/systemd/overlay-sysroot", "tmpfs", MS_STRICTATIME, "mode=0755" TMPFS_LIMITS_ROOTFS);
×
85
        if (r < 0)
×
86
                goto finish;
×
87

88
        tmpfs_mounted = true;
×
89

90
        if (mkdir("/run/systemd/overlay-sysroot/upper", 0755) < 0) {
×
91
                r = log_error_errno(errno, "Failed to create /run/systemd/overlay-sysroot/upper: %m");
×
92
                goto finish;
×
93
        }
94

95
        if (mkdir("/run/systemd/overlay-sysroot/work", 0755) < 0) {
×
96
                r = log_error_errno(errno, "Failed to create /run/systemd/overlay-sysroot/work: %m");
×
97
                goto finish;
×
98
        }
99

100
        escaped_path = shell_escape(path, ",:");
×
101
        if (!escaped_path) {
×
102
                r = log_oom();
×
103
                goto finish;
×
104
        }
105

106
        options = strjoina("lowerdir=", escaped_path, ",upperdir=/run/systemd/overlay-sysroot/upper,workdir=/run/systemd/overlay-sysroot/work");
×
107
        r = mount_nofollow_verbose(LOG_ERR, "overlay", path, "overlay", 0, options);
×
108

109
finish:
110
        if (tmpfs_mounted)
×
111
                (void) umount_verbose(LOG_ERR, "/run/systemd/overlay-sysroot", UMOUNT_NOFOLLOW);
×
112

113
        (void) rmdir("/run/systemd/overlay-sysroot");
×
114
        return r;
×
115
}
116

117
static int run(int argc, char *argv[]) {
×
118
        VolatileMode m = _VOLATILE_MODE_INVALID;
×
119
        const char *path;
×
120
        dev_t devt;
×
121
        int r;
×
122

123
        log_setup();
×
124

125
        if (argc > 3)
×
126
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
127
                                       "Too many arguments. Expected directory and mode.");
128

129
        r = query_volatile_mode(&m);
×
130
        if (r < 0)
×
131
                return log_error_errno(r, "Failed to determine volatile mode from kernel command line: %m");
×
132
        if (r == 0 && argc >= 2) {
×
133
                /* The kernel command line always wins. However if nothing was set there, the argument passed here wins instead. */
134
                m = volatile_mode_from_string(argv[1]);
×
135
                if (m < 0)
×
136
                        return log_error_errno(m, "Couldn't parse volatile mode: %s", argv[1]);
×
137
        }
138

139
        if (argc < 3)
×
140
                path = "/sysroot";
141
        else {
142
                path = argv[2];
×
143

144
                if (isempty(path))
×
145
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
146
                                               "Directory name cannot be empty.");
147
                if (!path_is_absolute(path))
×
148
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
149
                                               "Directory must be specified as absolute path.");
150
                if (path_equal(path, "/"))
×
151
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
152
                                               "Directory cannot be the root directory.");
153
        }
154

155
        if (!IN_SET(m, VOLATILE_YES, VOLATILE_OVERLAY))
×
156
                return 0;
157

158
        r = path_is_mount_point_full(path, /* root = */ NULL, AT_SYMLINK_FOLLOW);
×
159
        if (r < 0)
×
160
                return log_error_errno(r, "Couldn't determine whether %s is a mount point: %m", path);
×
161
        if (r == 0)
×
162
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a mount point.", path);
×
163

164
        r = path_is_temporary_fs(path);
×
165
        if (r < 0)
×
166
                return log_error_errno(r, "Couldn't determine whether %s is a temporary file system: %m", path);
×
167
        if (r > 0) {
×
168
                log_info("%s already is a temporary file system.", path);
×
169
                return 0;
×
170
        }
171

172
        /* We are about to replace the root directory with something else. Later code might want to know what we
173
         * replaced here, hence let's save that information as a symlink we can later use. (This is particularly
174
         * relevant for the overlayfs case where we'll fully obstruct the view onto the underlying device, hence
175
         * querying the backing device node from the file system directly is no longer possible. */
176
        r = get_block_device_harder(path, &devt);
×
177
        if (r < 0)
×
178
                return log_error_errno(r, "Failed to determine device major/minor of %s: %m", path);
×
179
        else if (r > 0) { /* backed by block device */
×
180
                _cleanup_free_ char *dn = NULL;
×
181

182
                r = device_path_make_major_minor(S_IFBLK, devt, &dn);
×
183
                if (r < 0)
×
184
                        return log_error_errno(r, "Failed to format device node path: %m");
×
185

186
                if (symlink(dn, "/run/systemd/volatile-root") < 0)
×
187
                        log_warning_errno(errno, "Failed to create symlink /run/systemd/volatile-root: %m");
×
188
        }
189

190
        if (m == VOLATILE_YES)
×
191
                return make_volatile(path);
×
192
        else {
193
                assert(m == VOLATILE_OVERLAY);
×
194
                return make_overlay(path);
×
195
        }
196
}
197

198
DEFINE_MAIN_FUNCTION(run);
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc