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

systemd / systemd / 15986406979

30 Jun 2025 05:03PM UTC coverage: 72.045% (-0.09%) from 72.13%
15986406979

push

github

bluca
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options

ephemeral/ephemeral-import are described as possible '--mutable' options but
not present in the list. Note, "systemd-sysext --help" lists them correctly.

300514 of 417119 relevant lines covered (72.05%)

708586.28 hits per line

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

27.71
/src/test/test-loop-block.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <linux/fs.h>
5
#include <linux/loop.h>
6
#include <pthread.h>
7
#include <sys/file.h>
8
#include <sys/ioctl.h>
9
#include <unistd.h>
10

11
#include "alloc-util.h"
12
#include "capability-util.h"
13
#include "dissect-image.h"
14
#include "fd-util.h"
15
#include "gpt.h"
16
#include "loop-util.h"
17
#include "main-func.h"
18
#include "mkfs-util.h"
19
#include "mount-util.h"
20
#include "namespace-util.h"
21
#include "parse-util.h"
22
#include "path-util.h"
23
#include "string-util.h"
24
#include "strv.h"
25
#include "tests.h"
26
#include "time-util.h"
27
#include "tmpfile-util.h"
28
#include "virt.h"
29

30
static unsigned arg_n_threads = 5;
31
static unsigned arg_n_iterations = 3;
32
static usec_t arg_timeout = 0;
33

34
#if HAVE_BLKID
35
static usec_t end = 0;
36

37
static void verify_dissected_image(DissectedImage *dissected) {
1✔
38
        assert_se(dissected->partitions[PARTITION_ESP].found);
1✔
39
        assert_se(dissected->partitions[PARTITION_ESP].node);
1✔
40
        assert_se(dissected->partitions[PARTITION_XBOOTLDR].found);
1✔
41
        assert_se(dissected->partitions[PARTITION_XBOOTLDR].node);
1✔
42
        assert_se(dissected->partitions[PARTITION_ROOT].found);
1✔
43
        assert_se(dissected->partitions[PARTITION_ROOT].node);
1✔
44
        assert_se(dissected->partitions[PARTITION_HOME].found);
1✔
45
        assert_se(dissected->partitions[PARTITION_HOME].node);
1✔
46
}
1✔
47

48
static void verify_dissected_image_harder(DissectedImage *dissected) {
×
49
        verify_dissected_image(dissected);
×
50

51
        ASSERT_STREQ(dissected->partitions[PARTITION_ESP].fstype, "vfat");
×
52
        ASSERT_STREQ(dissected->partitions[PARTITION_XBOOTLDR].fstype, "vfat");
×
53
        ASSERT_STREQ(dissected->partitions[PARTITION_ROOT].fstype, "ext4");
×
54
        ASSERT_STREQ(dissected->partitions[PARTITION_HOME].fstype, "ext4");
×
55
}
×
56

57
static void* thread_func(void *ptr) {
×
58
        int fd = PTR_TO_FD(ptr);
×
59
        int r;
×
60

61
        for (unsigned i = 0; i < arg_n_iterations; i++) {
×
62
                _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
×
63
                _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
×
64
                _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
×
65

66
                if (now(CLOCK_MONOTONIC) >= end) {
×
67
                        log_notice("Time's up, exiting thread's loop");
×
68
                        break;
×
69
                }
70

71
                log_notice("> Thread iteration #%u.", i);
×
72

73
                assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);
×
74

75
                r = loop_device_make(fd, O_RDONLY, 0, UINT64_MAX, 0, LO_FLAGS_PARTSCAN, LOCK_SH, &loop);
×
76
                if (r < 0)
×
77
                        log_error_errno(r, "Failed to allocate loopback device: %m");
×
78
                assert_se(r >= 0);
×
79
                assert_se(loop->dev);
×
80
                assert_se(loop->backing_file);
×
81

82
                log_notice("Acquired loop device %s, will mount on %s", loop->node, mounted);
×
83

84
                r = dissect_loop_device(
×
85
                                loop,
86
                                /* verity= */ NULL,
87
                                /* mount_options= */ NULL,
88
                                /* image_policy= */ NULL,
89
                                /* image_filter= */ NULL,
90
                                DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES,
91
                                &dissected);
92
                if (r < 0)
×
93
                        log_error_errno(r, "Failed dissect loopback device %s: %m", loop->node);
×
94
                assert_se(r >= 0);
×
95

96
                log_info("Dissected loop device %s", loop->node);
×
97

98
                for (PartitionDesignator d = 0; d < _PARTITION_DESIGNATOR_MAX; d++) {
×
99
                        if (!dissected->partitions[d].found)
×
100
                                continue;
×
101

102
                        log_notice("Found node %s fstype %s designator %s",
×
103
                                   dissected->partitions[d].node,
104
                                   dissected->partitions[d].fstype,
105
                                   partition_designator_to_string(d));
106
                }
107

108
                verify_dissected_image(dissected);
×
109

110
                r = dissected_image_mount(
×
111
                                dissected,
112
                                mounted,
113
                                /* uid_shift= */ UID_INVALID,
114
                                /* uid_range= */ UID_INVALID,
115
                                /* userns_fd= */ -EBADF,
116
                                DISSECT_IMAGE_READ_ONLY);
117
                log_notice_errno(r, "Mounted %s → %s: %m", loop->node, mounted);
×
118
                assert_se(r >= 0);
×
119

120
                /* Now the block device is mounted, we don't need no manual lock anymore, the devices are now
121
                 * pinned by the mounts. */
122
                assert_se(loop_device_flock(loop, LOCK_UN) >= 0);
×
123

124
                log_notice("Unmounting %s", mounted);
×
125
                mounted = umount_and_rmdir_and_free(mounted);
×
126

127
                log_notice("Unmounted.");
×
128

129
                dissected = dissected_image_unref(dissected);
×
130

131
                log_notice("Detaching loop device %s", loop->node);
×
132
                loop = loop_device_unref(loop);
×
133
                log_notice("Detached loop device.");
×
134
        }
135

136
        log_notice("Leaving thread");
×
137

138
        return NULL;
×
139
}
140
#endif
141

142
static bool have_root_gpt_type(void) {
1✔
143
#ifdef SD_GPT_ROOT_NATIVE
144
        return true;
1✔
145
#else
146
        return false;
147
#endif
148
}
149

150
static int run(int argc, char *argv[]) {
1✔
151
#if HAVE_BLKID
152
        _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
1✔
153
        _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
×
154
        pthread_t threads[arg_n_threads];
1✔
155
        sd_id128_t id;
1✔
156
#endif
157
        _cleanup_free_ char *p = NULL, *cmd = NULL;
1✔
158
        _cleanup_pclose_ FILE *sfdisk = NULL;
1✔
159
        _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
×
160
        _cleanup_close_ int fd = -EBADF;
1✔
161
        int r;
1✔
162

163
        test_setup_logging(LOG_DEBUG);
1✔
164
        log_show_tid(true);
1✔
165
        log_show_time(true);
1✔
166
        log_show_color(true);
1✔
167

168
        if (argc >= 2) {
1✔
169
                r = safe_atou(argv[1], &arg_n_threads);
×
170
                if (r < 0)
×
171
                        return log_error_errno(r, "Failed to parse first argument (number of threads): %s", argv[1]);
×
172
                if (arg_n_threads <= 0)
×
173
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Number of threads must be at least 1, refusing.");
×
174
        }
175

176
        if (argc >= 3) {
×
177
                r = safe_atou(argv[2], &arg_n_iterations);
×
178
                if (r < 0)
×
179
                        return log_error_errno(r, "Failed to parse second argument (number of iterations): %s", argv[2]);
×
180
                if (arg_n_iterations <= 0)
×
181
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Number of iterations must be at least 1, refusing.");
×
182
        }
183

184
        if (argc >= 4) {
×
185
                r = parse_sec(argv[3], &arg_timeout);
×
186
                if (r < 0)
×
187
                        return log_error_errno(r, "Failed to parse third argument (timeout): %s", argv[3]);
×
188
        }
189

190
        if (argc >= 5)
×
191
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments (expected 3 at max).");
×
192

193
        if (!have_root_gpt_type())
1✔
194
                return log_tests_skipped("No root partition GPT defined for this architecture");
×
195

196
        r = find_executable("sfdisk", NULL);
1✔
197
        if (r < 0)
1✔
198
                return log_tests_skipped_errno(r, "Could not find sfdisk command");
×
199

200
        assert_se(tempfn_random_child("/var/tmp", "sfdisk", &p) >= 0);
1✔
201
        fd = open(p, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOFOLLOW, 0666);
1✔
202
        assert_se(fd >= 0);
1✔
203
        assert_se(ftruncate(fd, 256*1024*1024) >= 0);
1✔
204

205
        assert_se(cmd = strjoin("sfdisk ", p));
1✔
206
        assert_se(sfdisk = popen(cmd, "we"));
1✔
207

208
        /* A reasonably complex partition table that fits on a 64K disk */
209
        fputs("label: gpt\n"
1✔
210
              "size=32M, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B\n"
211
              "size=32M, type=BC13C2FF-59E6-4262-A352-B275FD6F7172\n"
212
              "size=32M, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n"
213
              "size=32M, type=", sfdisk);
214

215
#ifdef SD_GPT_ROOT_NATIVE
216
        fprintf(sfdisk, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_GPT_ROOT_NATIVE));
1✔
217
#else
218
        fprintf(sfdisk, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_GPT_ROOT_X86_64));
219
#endif
220

221
        fputs("\n"
1✔
222
              "size=32M, type=933AC7E1-2EB4-4F13-B844-0E14E2AEF915\n", sfdisk);
223

224
        assert_se(pclose(sfdisk) == 0);
1✔
225
        sfdisk = NULL;
1✔
226

227
#if HAVE_BLKID
228
        assert_se(dissect_image_file(
1✔
229
                                  p,
230
                                  /* verity= */ NULL,
231
                                  /* mount_options= */ NULL,
232
                                  /* image_policy= */ NULL,
233
                                  /* image_filter= */ NULL,
234
                                  /* flags= */ 0,
235
                                  &dissected) >= 0);
236
        verify_dissected_image(dissected);
1✔
237
        dissected = dissected_image_unref(dissected);
1✔
238
#endif
239

240
        if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
1✔
241
                return log_tests_skipped("not running privileged");
×
242

243
        if (detect_container() > 0)
1✔
244
                return log_tests_skipped("Test not supported in a container, requires udev/uevent notifications");
1✔
245

246
        assert_se(loop_device_make(fd, O_RDWR, 0, UINT64_MAX, 0, LO_FLAGS_PARTSCAN, LOCK_EX, &loop) >= 0);
×
247

248
#if HAVE_BLKID
249
        assert_se(dissect_loop_device(
×
250
                                  loop,
251
                                  /* verity= */ NULL,
252
                                  /* mount_options= */ NULL,
253
                                  /* image_policy= */ NULL,
254
                                  /* image_filter= */ NULL,
255
                                  DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES,
256
                                  &dissected) >= 0);
257
        verify_dissected_image(dissected);
×
258

259
        FOREACH_STRING(fs, "vfat", "ext4") {
×
260
                r = mkfs_exists(fs);
×
261
                assert_se(r >= 0);
×
262
                if (!r) {
×
263
                        log_tests_skipped("mkfs.{vfat|ext4} not installed");
×
264
                        return 0;
×
265
                }
266
        }
267
        assert_se(r >= 0);
×
268

269
        assert_se(sd_id128_randomize(&id) >= 0);
×
270
        assert_se(make_filesystem(dissected->partitions[PARTITION_ESP].node, "vfat", "EFI", NULL, id, MKFS_DISCARD, 0, NULL, NULL, NULL) >= 0);
×
271

272
        assert_se(sd_id128_randomize(&id) >= 0);
×
273
        assert_se(make_filesystem(dissected->partitions[PARTITION_XBOOTLDR].node, "vfat", "xbootldr", NULL, id, MKFS_DISCARD, 0, NULL, NULL, NULL) >= 0);
×
274

275
        assert_se(sd_id128_randomize(&id) >= 0);
×
276
        assert_se(make_filesystem(dissected->partitions[PARTITION_ROOT].node, "ext4", "root", NULL, id, MKFS_DISCARD, 0, NULL, NULL, NULL) >= 0);
×
277

278
        assert_se(sd_id128_randomize(&id) >= 0);
×
279
        assert_se(make_filesystem(dissected->partitions[PARTITION_HOME].node, "ext4", "home", NULL, id, MKFS_DISCARD, 0, NULL, NULL, NULL) >= 0);
×
280

281
        dissected = dissected_image_unref(dissected);
×
282

283
        /* We created the file systems now via the per-partition block devices. But the dissection code might
284
         * probe them via the whole block device. These block devices have separate buffer caches though,
285
         * hence what was written via the partition device might not appear on the whole block device
286
         * yet. Let's hence explicitly flush the whole block device, so that the read-back definitely
287
         * works. */
288
        assert_se(ioctl(loop->fd, BLKFLSBUF, 0) >= 0);
×
289

290
        /* Try to read once, without pinning or adding partitions, i.e. by only accessing the whole block
291
         * device. */
292
        assert_se(dissect_loop_device(
×
293
                                  loop,
294
                                  /* verity= */ NULL,
295
                                  /* mount_options= */ NULL,
296
                                  /* image_policy= */ NULL,
297
                                  /* image_filter= */ NULL,
298
                                  /* flags= */ 0,
299
                                  &dissected) >= 0);
300
        verify_dissected_image_harder(dissected);
×
301
        dissected = dissected_image_unref(dissected);
×
302

303
        /* Now go via the loopback device after all, but this time add/pin, because now we want to mount it. */
304
        assert_se(dissect_loop_device(
×
305
                                  loop,
306
                                  /* verity= */ NULL,
307
                                  /* mount_options= */ NULL,
308
                                  /* image_policy= */ NULL,
309
                                  /* image_filter= */ NULL,
310
                                  DISSECT_IMAGE_ADD_PARTITION_DEVICES|DISSECT_IMAGE_PIN_PARTITION_DEVICES,
311
                                  &dissected) >= 0);
312
        verify_dissected_image_harder(dissected);
×
313

314
        assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);
×
315

316
        /* We are particularly correct here, and now downgrade LOCK → LOCK_SH. That's because we are done
317
         * with formatting the file systems, so we don't need the exclusive lock anymore. From now on a
318
         * shared one is fine. This way udev can now probe the device if it wants, but still won't call
319
         * BLKRRPART on it, and that's good, because that would destroy our partition table while we are at
320
         * it. */
321
        assert_se(loop_device_flock(loop, LOCK_SH) >= 0);
×
322

323
        /* This is a test for the loopback block device setup code and it's use by the image dissection
324
         * logic: since the kernel APIs are hard use and prone to races, let's test this in a heavy duty
325
         * test: we open a bunch of threads and repeatedly allocate and deallocate loopback block devices in
326
         * them in parallel, with an image file with a number of partitions. */
327
        assert_se(detach_mount_namespace() >= 0);
×
328

329
        /* This first (writable) mount will initialize the mount point dirs, so that the subsequent read-only ones can work */
330
        assert_se(dissected_image_mount(
×
331
                                  dissected,
332
                                  mounted,
333
                                  /* uid_shift= */ UID_INVALID,
334
                                  /* uid_range= */ UID_INVALID,
335
                                  /* userns_fd= */ -EBADF,
336
                                  0) >= 0);
337

338
        /* Now we mounted everything, the partitions are pinned. Now it's fine to release the lock
339
         * fully. This means udev could now issue BLKRRPART again, but that's OK given this will fail because
340
         * we now mounted the device. */
341
        assert_se(loop_device_flock(loop, LOCK_UN) >= 0);
×
342

343
        assert_se(umount_recursive(mounted, 0) >= 0);
×
344
        loop = loop_device_unref(loop);
×
345

346
        log_notice("Threads are being started now");
×
347

348
        /* zero timeout means pick default: let's make sure we run for 10s on slow systems at max */
349
        if (arg_timeout == 0)
×
350
                arg_timeout = slow_tests_enabled() ? 5 * USEC_PER_SEC : 1 * USEC_PER_SEC;
×
351

352
        end = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
×
353

354
        if (arg_n_threads > 1)
×
355
                for (unsigned i = 0; i < arg_n_threads; i++)
×
356
                        assert_se(pthread_create(threads + i, NULL, thread_func, FD_TO_PTR(fd)) == 0);
×
357

358
        log_notice("All threads started now.");
×
359

360
        if (arg_n_threads == 1)
×
361
                ASSERT_NULL(thread_func(FD_TO_PTR(fd)));
×
362
        else
363
                for (unsigned i = 0; i < arg_n_threads; i++) {
×
364
                        log_notice("Joining thread #%u.", i);
×
365

366
                        void *k;
×
367
                        assert_se(pthread_join(threads[i], &k) == 0);
×
368
                        assert_se(!k);
×
369

370
                        log_notice("Joined thread #%u.", i);
×
371
                }
372

373
        log_notice("Threads are all terminated now.");
×
374
#else
375
        log_notice("Cutting test short, since we do not have libblkid.");
376
#endif
377
        return 0;
378
}
379

380
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
1✔
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