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

systemd / systemd / 14766779411

30 Apr 2025 04:55PM UTC coverage: 72.225% (-0.06%) from 72.282%
14766779411

push

github

web-flow
wait-online: handle varlink connection errors while waiting for DNS (#37283)

Currently, if systemd-networkd-wait-online is started with --dns, and
systemd-resolved is not running, it will exit with an error right away.
Similarly, if systemd-resolved is restarted while waiting for DNS
configuration, systemd-networkd-wait-online will not attempt to
re-connect, and will potentially never see subsequent DNS
configurations.

Improve this by adding socket units for the systemd-resolved varlink
servers, and re-establish the connection in systemd-networkd-wait-online
when we receive `SD_VARLINK_ERROR_DISCONNECTED`.

8 of 16 new or added lines in 2 files covered. (50.0%)

5825 existing lines in 217 files now uncovered.

297168 of 411450 relevant lines covered (72.22%)

695892.62 hits per line

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

68.33
/src/cryptsetup/cryptsetup-generator.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <fcntl.h>
5
#include <sys/stat.h>
6
#include <sys/types.h>
7

8
#include "alloc-util.h"
9
#include "cryptsetup-util.h"
10
#include "dropin.h"
11
#include "escape.h"
12
#include "fd-util.h"
13
#include "fileio.h"
14
#include "fstab-util.h"
15
#include "generator.h"
16
#include "hashmap.h"
17
#include "id128-util.h"
18
#include "log.h"
19
#include "mkdir.h"
20
#include "parse-util.h"
21
#include "path-util.h"
22
#include "proc-cmdline.h"
23
#include "specifier.h"
24
#include "string-util.h"
25
#include "strv.h"
26
#include "unit-name.h"
27

28
typedef struct crypto_device {
29
        char *uuid;
30
        char *keyfile;
31
        char *keydev;
32
        char *headerdev;
33
        char *datadev;
34
        char *name;
35
        char *options;
36
        bool create;
37
} crypto_device;
38

39
static const char *arg_dest = NULL;
40
static bool arg_enabled = true;
41
static bool arg_read_crypttab = true;
42
static const char *arg_crypttab = NULL;
43
static const char *arg_runtime_directory = NULL;
44
static bool arg_allow_list = false;
45
static Hashmap *arg_disks = NULL;
46
static char *arg_default_options = NULL;
47
static char *arg_default_keyfile = NULL;
48

49
STATIC_DESTRUCTOR_REGISTER(arg_disks, hashmap_freep);
1✔
50
STATIC_DESTRUCTOR_REGISTER(arg_default_options, freep);
1✔
51
STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile, freep);
1✔
52

53
static int split_locationspec(const char *locationspec, char **ret_file, char **ret_device) {
35✔
54
        _cleanup_free_ char *file = NULL, *device = NULL;
35✔
55
        const char *c;
35✔
56

57
        assert(ret_file);
35✔
58
        assert(ret_device);
35✔
59

60
        if (!locationspec) {
35✔
61
                *ret_file = *ret_device = NULL;
×
62
                return 0;
×
63
        }
64

65
        c = strrchr(locationspec, ':');
35✔
66
        if (c) {
35✔
67
                /* The device part has to be either an absolute path to device node (/dev/something,
68
                 * /dev/foo/something, or even possibly /dev/foo/something:part), or a fstab device
69
                 * specification starting with LABEL= or similar. The file part has the same syntax.
70
                 *
71
                 * Let's try to guess if the second part looks like a device specification, or just part of a
72
                 * filename with a colon. fstab_node_to_udev_node() will convert the fstab device syntax to
73
                 * an absolute path. If we didn't get an absolute path, assume that it is just part of the
74
                 * first file argument. */
75

76
                device = fstab_node_to_udev_node(c + 1);
5✔
77
                if (!device)
5✔
78
                        return log_oom();
×
79

80
                if (path_is_absolute(device))
5✔
81
                        file = strndup(locationspec, c-locationspec);
5✔
82
                else {
83
                        log_debug("Location specification argument contains a colon, but \"%s\" doesn't look like a device specification.\n"
×
84
                                  "Assuming that \"%s\" is a single device specification.",
85
                                  c + 1, locationspec);
86
                        device = mfree(device);
×
87
                        c = NULL;
×
88
                }
89
        }
90

91
        if (!c)
5✔
92
                /* No device specified */
93
                file = strdup(locationspec);
30✔
94

95
        if (!file)
35✔
96
                return log_oom();
×
97

98
        *ret_file = TAKE_PTR(file);
35✔
99
        *ret_device = TAKE_PTR(device);
35✔
100

101
        return 0;
35✔
102
}
103

104
static int generate_device_mount(
4✔
105
                const char *name,
106
                const char *device,
107
                const char *type_prefix, /* "keydev" or "headerdev" */
108
                const char *device_timeout,
109
                bool canfail,
110
                bool readonly,
111
                char **unit,
112
                char **mount) {
113

114
        _cleanup_free_ char *u = NULL, *where = NULL, *name_escaped = NULL, *device_unit = NULL;
4✔
115
        _cleanup_fclose_ FILE *f = NULL;
4✔
116
        int r;
4✔
117
        usec_t timeout_us;
4✔
118

119
        assert(name);
4✔
120
        assert(device);
4✔
121
        assert(unit);
4✔
122
        assert(mount);
4✔
123

124
        r = mkdir_parents(arg_runtime_directory, 0755);
4✔
125
        if (r < 0)
4✔
126
                return r;
127

128
        r = mkdir(arg_runtime_directory, 0700);
4✔
129
        if (r < 0 && errno != EEXIST)
4✔
130
                return -errno;
×
131

132
        name_escaped = cescape(name);
4✔
133
        if (!name_escaped)
4✔
134
                return -ENOMEM;
135

136
        where = strjoin(arg_runtime_directory, "/", type_prefix, "-", name_escaped);
4✔
137
        if (!where)
4✔
138
                return -ENOMEM;
139

140
        r = mkdir(where, 0700);
4✔
141
        if (r < 0 && errno != EEXIST)
4✔
142
                return -errno;
×
143

144
        r = unit_name_from_path(where, ".mount", &u);
4✔
145
        if (r < 0)
4✔
146
                return r;
147

148
        r = generator_open_unit_file(arg_dest, NULL, u, &f);
4✔
149
        if (r < 0)
4✔
150
                return r;
151

152
        fprintf(f,
10✔
153
                "[Unit]\n"
154
                "DefaultDependencies=no\n\n"
155
                "[Mount]\n"
156
                "What=%s\n"
157
                "Where=%s\n"
158
                "Options=%s%s\n", device, where, readonly ? "ro" : "rw", canfail ? ",nofail" : "");
159

160
        if (device_timeout) {
4✔
161
                r = parse_sec_fix_0(device_timeout, &timeout_us);
×
162
                if (r >= 0) {
×
163
                        r = unit_name_from_path(device, ".device", &device_unit);
×
164
                        if (r < 0)
×
165
                                return log_error_errno(r, "Failed to generate unit name: %m");
×
166

167
                        r = write_drop_in_format(arg_dest, device_unit, 90, "device-timeout",
×
168
                                "# Automatically generated by systemd-cryptsetup-generator \n\n"
169
                                "[Unit]\nJobRunningTimeoutSec=%s", device_timeout);
170
                        if (r < 0)
×
171
                                return log_error_errno(r, "Failed to write device drop-in: %m");
×
172

173
                } else
174
                        log_warning_errno(r, "Failed to parse %s, ignoring: %m", device_timeout);
×
175

176
        }
177

178
        r = fflush_and_check(f);
4✔
179
        if (r < 0)
4✔
180
                return r;
181

182
        *unit = TAKE_PTR(u);
4✔
183
        *mount = TAKE_PTR(where);
4✔
184

185
        return 0;
4✔
186
}
187

188
static int generate_device_umount(const char *name,
4✔
189
                                  const char *device_mount,
190
                                  const char *type_prefix, /* "keydev" or "headerdev" */
191
                                  char **ret_umount_unit) {
192
        _cleanup_fclose_ FILE *f = NULL;
4✔
193
        _cleanup_free_ char *u = NULL, *name_escaped = NULL, *mount = NULL;
4✔
194
        int r;
4✔
195

196
        assert(name);
4✔
197
        assert(ret_umount_unit);
4✔
198

199
        name_escaped = cescape(name);
4✔
200
        if (!name_escaped)
4✔
201
                return -ENOMEM;
202

203
        u = strjoin(type_prefix, "-", name_escaped, "-umount.service");
4✔
204
        if (!u)
4✔
205
                return -ENOMEM;
206

207
        r = unit_name_from_path(device_mount, ".mount", &mount);
4✔
208
        if (r < 0)
4✔
209
                return r;
210

211
        r = generator_open_unit_file(arg_dest, NULL, u, &f);
4✔
212
        if (r < 0)
4✔
213
                return r;
214

215
        fprintf(f,
4✔
216
                "[Unit]\n"
217
                "DefaultDependencies=no\n"
218
                "After=%s\n\n"
219
                "[Service]\n"
220
                "ExecStart=-" UMOUNT_PATH " %s\n\n", mount, device_mount);
221

222
        r = fflush_and_check(f);
4✔
223
        if (r < 0)
4✔
224
                return r;
225

226
        *ret_umount_unit = TAKE_PTR(u);
4✔
227
        return 0;
4✔
228
}
229

230
static int print_dependencies(FILE *f, const char* device_path, const char* timeout_value, bool canfail) {
30✔
231
        int r;
30✔
232

233
        assert(f);
30✔
234
        assert(device_path);
30✔
235

236
        if (!mangle_none(device_path))
30✔
237
                /* None, nothing to do */
238
                return 0;
30✔
239

240
        if (PATH_IN_SET(device_path,
24✔
241
                        "/dev/urandom",
242
                        "/dev/random",
243
                        "/dev/hw_random",
244
                        "/dev/hwrng")) {
245
                /* RNG device, add random dep */
246
                fputs("After=systemd-random-seed.service\n", f);
×
247
                return 0;
×
248
        }
249

250
        _cleanup_free_ char *udev_node = fstab_node_to_udev_node(device_path);
48✔
251
        if (!udev_node)
24✔
252
                return log_oom();
×
253

254
        if (path_equal(udev_node, "/dev/null"))
24✔
255
                return 0;
256

257
        if (path_startswith(udev_node, "/dev/")) {
24✔
258
                /* We are dealing with a block device, add dependency for corresponding unit */
259
                _cleanup_free_ char *unit = NULL;
×
260

261
                r = unit_name_from_path(udev_node, ".device", &unit);
×
262
                if (r < 0)
×
263
                        return log_error_errno(r, "Failed to generate unit name: %m");
×
264

265
                fprintf(f, "After=%1$s\n", unit);
×
266
                if (canfail) {
×
267
                        fprintf(f, "Wants=%1$s\n", unit);
×
268
                        if (timeout_value) {
×
269
                                r = write_drop_in_format(arg_dest, unit, 90, "device-timeout",
×
270
                                        "# Automatically generated by systemd-cryptsetup-generator \n\n"
271
                                        "[Unit]\nJobRunningTimeoutSec=%s", timeout_value);
272
                                if (r < 0)
×
273
                                        return log_error_errno(r, "Failed to write device drop-in: %m");
×
274
                        }
275
                } else
276
                        fprintf(f, "Requires=%1$s\n", unit);
×
277
        } else {
278
                /* Regular file, add mount dependency */
279
                _cleanup_free_ char *escaped_path = specifier_escape(device_path);
48✔
280
                if (!escaped_path)
24✔
281
                        return log_oom();
×
282

283
                fprintf(f, "%s=%s\n", canfail ? "WantsMountsFor" : "RequiresMountsFor", escaped_path);
46✔
284
        }
285

286
        return 0;
287
}
288

289
static bool attach_in_initrd(const char *name, const char *options) {
22✔
290
        assert(name);
22✔
291

292
        /* Imply x-initrd.attach in case the volume name is among those defined in the Discoverable Partition
293
         * Specification for partitions that we require to be mounted during the initrd → host transition,
294
         * i.e. for the root fs itself, and /usr/. This mirrors similar behaviour in
295
         * systemd-fstab-generator. */
296

297
        return fstab_test_option(options, "x-initrd.attach\0") ||
44✔
298
                STR_IN_SET(name, "root", "usr");
22✔
299
}
300

301
static int create_disk(
22✔
302
                const char *name,
303
                const char *device,
304
                const char *key_file,
305
                const char *keydev,
306
                const char *headerdev,
307
                const char *options,
308
                const char *source) {
309

310
        _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL,
×
311
                *keydev_mount = NULL, *keyfile_timeout_value = NULL,
×
312
                *filtered = NULL, *u_escaped = NULL, *name_escaped = NULL, *header_path = NULL, *key_file_buffer = NULL,
×
313
                *tmp_fstype = NULL, *filtered_header = NULL, *headerdev_mount = NULL;
22✔
314
        _cleanup_fclose_ FILE *f = NULL;
22✔
315
        const char *dmname;
22✔
316
        bool noauto, nofail, swap, netdev;
22✔
317
        int r, detached_header, keyfile_can_timeout, tmp;
22✔
318

319
        assert(name);
22✔
320
        assert(device);
22✔
321

322
        noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0");
22✔
323
        nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
22✔
324
        swap = fstab_test_option(options, "swap\0");
22✔
325
        netdev = fstab_test_option(options, "_netdev\0");
22✔
326

327
        keyfile_can_timeout = fstab_filter_options(options,
22✔
328
                                                   "keyfile-timeout\0",
329
                                                   NULL, &keyfile_timeout_value, NULL, NULL);
330
        if (keyfile_can_timeout < 0)
22✔
331
                return log_error_errno(keyfile_can_timeout, "Failed to parse keyfile-timeout= option value: %m");
×
332

333
        detached_header = fstab_filter_options(
42✔
334
                options,
335
                "header\0",
336
                NULL,
337
                &header_path,
338
                NULL,
339
                headerdev ? &filtered_header : NULL);
340
        if (detached_header < 0)
22✔
341
                return log_error_errno(detached_header, "Failed to parse header= option value: %m");
×
342

343
        tmp = fstab_filter_options(options, "tmp\0", NULL, &tmp_fstype, NULL, NULL);
22✔
344
        if (tmp < 0)
22✔
345
                return log_error_errno(tmp, "Failed to parse tmp= option value: %m");
×
346

347
        if (tmp && swap)
22✔
348
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
349
                                       "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.",
350
                                       name);
351

352
        name_escaped = specifier_escape(name);
22✔
353
        if (!name_escaped)
22✔
354
                return log_oom();
×
355

356
        e = unit_name_escape(name);
22✔
357
        if (!e)
22✔
358
                return log_oom();
×
359

360
        u = fstab_node_to_udev_node(device);
22✔
361
        if (!u)
22✔
362
                return log_oom();
×
363

364
        r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
22✔
365
        if (r < 0)
22✔
366
                return log_error_errno(r, "Failed to generate unit name: %m");
×
367

368
        u_escaped = specifier_escape(u);
22✔
369
        if (!u_escaped)
22✔
370
                return log_oom();
×
371

372
        r = unit_name_from_path(u, ".device", &d);
22✔
373
        if (r < 0)
22✔
374
                return log_error_errno(r, "Failed to generate unit name: %m");
×
375

376
        if (keydev && !key_file)
22✔
377
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
378
                                       "Key device is specified, but path to the key file is missing.");
379

380
        r = generator_open_unit_file(arg_dest, NULL, n, &f);
22✔
381
        if (r < 0)
22✔
382
                return r;
383

384
        r = generator_write_cryptsetup_unit_section(f, source);
22✔
385
        if (r < 0)
22✔
386
                return r;
387

388
        if (netdev)
22✔
389
                fprintf(f, "After=remote-fs-pre.target\n");
×
390

391
        /* If initrd takes care of attaching the disk then it should also detach it during shutdown. */
392
        if (!attach_in_initrd(name, options))
22✔
393
                fprintf(f,
22✔
394
                        "Conflicts=umount.target\n"
395
                        "Before=umount.target\n");
396

397
        if (keydev) {
22✔
398
                _cleanup_free_ char *unit = NULL, *umount_unit = NULL;
2✔
399

400
                r = generate_device_mount(
2✔
401
                        name,
402
                        keydev,
403
                        "keydev",
404
                        keyfile_timeout_value,
405
                        /* canfail = */ keyfile_can_timeout > 0,
406
                        /* readonly= */ true,
407
                        &unit,
408
                        &keydev_mount);
409
                if (r < 0)
2✔
410
                        return log_error_errno(r, "Failed to generate keydev mount unit: %m");
×
411

412
                r = generate_device_umount(name, keydev_mount, "keydev", &umount_unit);
2✔
413
                if (r < 0)
2✔
414
                        return log_error_errno(r, "Failed to generate keydev umount unit: %m");
×
415

416
                key_file_buffer = path_join(keydev_mount, key_file);
2✔
417
                if (!key_file_buffer)
2✔
418
                        return log_oom();
×
419

420
                key_file = key_file_buffer;
2✔
421

422
                fprintf(f, "After=%s\n", unit);
2✔
423
                if (keyfile_can_timeout > 0)
2✔
424
                        fprintf(f, "Wants=%s\n", unit);
×
425
                else
426
                        fprintf(f, "Requires=%s\n", unit);
2✔
427

428
                if (umount_unit)
2✔
429
                        fprintf(f,
2✔
430
                                "Wants=%s\n"
431
                                "Before=%s\n",
432
                                umount_unit,
433
                                umount_unit
434
                        );
435
        }
436

437
        if (headerdev) {
22✔
438
                _cleanup_free_ char *unit = NULL, *umount_unit = NULL, *p = NULL;
2✔
439

440
                r = generate_device_mount(
2✔
441
                        name,
442
                        headerdev,
443
                        "headerdev",
444
                        NULL,
445
                        /* canfail=  */ false, /* header is always necessary */
446
                        /* readonly= */ false, /* LUKS2 recovery requires rw header access */
447
                        &unit,
448
                        &headerdev_mount);
449
                if (r < 0)
2✔
450
                        return log_error_errno(r, "Failed to generate header device mount unit: %m");
×
451

452
                r = generate_device_umount(name, headerdev_mount, "headerdev", &umount_unit);
2✔
453
                if (r < 0)
2✔
454
                        return log_error_errno(r, "Failed to generate header device umount unit: %m");
×
455

456
                p = path_join(headerdev_mount, header_path);
2✔
457
                if (!p)
2✔
458
                        return log_oom();
×
459

460
                free_and_replace(header_path, p);
2✔
461

462
                if (isempty(filtered_header))
2✔
463
                        p = strjoin("header=", header_path);
×
464
                else
465
                        p = strjoin(filtered_header, ",header=", header_path);
2✔
466

467
                if (!p)
2✔
468
                        return log_oom();
×
469

470
                free_and_replace(filtered_header, p);
2✔
471
                options = filtered_header;
2✔
472

473
                fprintf(f, "After=%s\n"
2✔
474
                           "Requires=%s\n", unit, unit);
475

476
                if (umount_unit)
2✔
477
                        fprintf(f,
2✔
478
                                "Wants=%s\n"
479
                                "Before=%s\n",
480
                                umount_unit,
481
                                umount_unit
482
                        );
483
        }
484

485
        if (!nofail)
22✔
486
                fprintf(f,
42✔
487
                        "Before=%s\n",
488
                        netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
489

490
        if (key_file && !keydev) {
22✔
491
                r = print_dependencies(f, key_file,
40✔
492
                        keyfile_timeout_value,
493
                        /* canfail= */ keyfile_can_timeout > 0 || nofail);
20✔
494
                if (r < 0)
20✔
495
                        return r;
496
        }
497

498
        /* Check if a header option was specified */
499
        if (detached_header > 0 && !headerdev) {
22✔
500
                r = print_dependencies(f, header_path,
10✔
501
                        /* timeout_value= */ NULL,
502
                        /* canfail= */ nofail);
503
                if (r < 0)
10✔
504
                        return r;
505
        }
506

507
        if (path_startswith(u, "/dev/"))
22✔
508
                fprintf(f,
×
509
                        "BindsTo=%s\n"
510
                        "After=%s\n",
511
                        d, d);
512
        else
513
                /* For loopback devices make sure to explicitly load loop.ko, as this code might run very
514
                 * early where device nodes created via systemd-tmpfiles-setup-dev.service might not be
515
                 * around yet. Hence let's sync on the module itself. */
516
                fprintf(f,
22✔
517
                        "RequiresMountsFor=%s\n"
518
                        "Wants=modprobe@loop.service\n"
519
                        "After=modprobe@loop.service\n",
520
                        u_escaped);
521

522
        r = generator_write_device_timeout(arg_dest, device, options, &filtered);
22✔
523
        if (r < 0)
22✔
524
                log_warning_errno(r, "Failed to write device timeout drop-in: %m");
×
525

526
        r = generator_write_cryptsetup_service_section(f, name, u, key_file, filtered);
22✔
527
        if (r < 0)
22✔
528
                return r;
529

530
        if (tmp) {
22✔
531
                _cleanup_free_ char *tmp_fstype_escaped = NULL;
×
532

533
                if (tmp_fstype) {
×
534
                        tmp_fstype_escaped = specifier_escape(tmp_fstype);
×
535
                        if (!tmp_fstype_escaped)
×
536
                                return log_oom();
×
537
                }
538

539
                fprintf(f,
×
540
                        "ExecStartPost=" LIBEXECDIR "/systemd-makefs '%s' '/dev/mapper/%s'\n",
541
                        tmp_fstype_escaped ?: "ext4", name_escaped);
542
        }
543

544
        if (swap)
22✔
545
                fprintf(f,
×
546
                        "ExecStartPost=" LIBEXECDIR "/systemd-makefs swap '/dev/mapper/%s'\n",
547
                        name_escaped);
548

549
        r = fflush_and_check(f);
22✔
550
        if (r < 0)
22✔
551
                return log_error_errno(r, "Failed to write unit file %s: %m", n);
×
552

553
        if (!noauto) {
22✔
554
                r = generator_add_symlink(arg_dest,
65✔
555
                                          netdev ? "remote-cryptsetup.target" : "cryptsetup.target",
556
                                          nofail ? "wants" : "requires", n);
557
                if (r < 0)
22✔
558
                        return r;
559
        }
560

561
        dmname = strjoina("dev-mapper-", e, ".device");
154✔
562
        r = generator_add_symlink(arg_dest, dmname, "requires", n);
22✔
563
        if (r < 0)
22✔
564
                return r;
565

566
        if (!noauto && !nofail) {
22✔
567
                r = write_drop_in(arg_dest, dmname, 40, "device-timeout",
21✔
568
                                  "# Automatically generated by systemd-cryptsetup-generator\n\n"
569
                                  "[Unit]\n"
570
                                  "JobTimeoutSec=infinity\n");
571
                if (r < 0)
21✔
572
                        log_warning_errno(r, "Failed to write device timeout drop-in: %m");
22✔
573
        }
574

575
        return 0;
576
}
577

578
static crypto_device* crypt_device_free(crypto_device *d) {
1✔
579
        if (!d)
1✔
580
                return NULL;
581

582
        free(d->uuid);
1✔
583
        free(d->keyfile);
1✔
584
        free(d->keydev);
1✔
585
        free(d->headerdev);
1✔
586
        free(d->datadev);
1✔
587
        free(d->name);
1✔
588
        free(d->options);
1✔
589
        return mfree(d);
1✔
590
}
591

592
static crypto_device *get_crypto_device(const char *uuid) {
2✔
593
        int r;
2✔
594
        crypto_device *d;
2✔
595

596
        assert(uuid);
2✔
597

598
        d = hashmap_get(arg_disks, uuid);
2✔
599
        if (!d) {
2✔
600
                d = new0(struct crypto_device, 1);
1✔
601
                if (!d)
1✔
602
                        return NULL;
603

604
                d->uuid = strdup(uuid);
1✔
605
                if (!d->uuid)
1✔
606
                        return mfree(d);
×
607

608
                r = hashmap_put(arg_disks, d->uuid, d);
1✔
609
                if (r < 0) {
1✔
610
                        free(d->uuid);
×
611
                        return mfree(d);
×
612
                }
613
        }
614

615
        return d;
616
}
617

618
static bool warn_uuid_invalid(const char *uuid, const char *key) {
2✔
619
        assert(key);
2✔
620

621
        if (!id128_is_valid(uuid)) {
2✔
622
                log_warning("Failed to parse %s= kernel command line switch. UUID is invalid, ignoring.", key);
×
623
                return true;
×
624
        }
625

626
        return false;
627
}
628

629
static int filter_header_device(const char *options,
23✔
630
                                char **ret_headerdev,
631
                                char **ret_filtered_headerdev_options) {
632
        int r;
23✔
633
        _cleanup_free_ char *headerfile = NULL, *headerdev = NULL, *headerspec = NULL,
×
634
                            *filtered_headerdev = NULL, *filtered_headerspec = NULL;
23✔
635

636
        assert(ret_headerdev);
23✔
637
        assert(ret_filtered_headerdev_options);
23✔
638

639
        r = fstab_filter_options(options, "header\0", NULL, &headerspec, NULL, &filtered_headerspec);
23✔
640
        if (r < 0)
23✔
641
                return log_error_errno(r, "Failed to parse header= option value: %m");
×
642

643
        if (r > 0) {
23✔
644
                r = split_locationspec(headerspec, &headerfile, &headerdev);
12✔
645
                if (r < 0)
12✔
646
                        return r;
647

648
                if (isempty(filtered_headerspec))
12✔
649
                        filtered_headerdev = strjoin("header=", headerfile);
×
650
                else
651
                        filtered_headerdev = strjoin(filtered_headerspec, ",header=", headerfile);
12✔
652

653
                if (!filtered_headerdev)
12✔
654
                        return log_oom();
×
655
        } else
656
                filtered_headerdev = TAKE_PTR(filtered_headerspec);
11✔
657

658
        *ret_filtered_headerdev_options = TAKE_PTR(filtered_headerdev);
23✔
659
        *ret_headerdev = TAKE_PTR(headerdev);
23✔
660

661
        return 0;
23✔
662
}
663

664
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
47✔
665
        _cleanup_free_ char *uuid = NULL, *uuid_value = NULL;
47✔
666
        crypto_device *d;
47✔
667
        int r;
47✔
668

669
        if (streq(key, "luks")) {
47✔
670

671
                r = value ? parse_boolean(value) : 1;
×
672
                if (r < 0)
×
673
                        log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value);
×
674
                else
675
                        arg_enabled = r;
×
676

677
        } else if (streq(key, "luks.crypttab")) {
47✔
678

679
                r = value ? parse_boolean(value) : 1;
×
680
                if (r < 0)
×
681
                        log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value);
×
682
                else
683
                        arg_read_crypttab = r;
×
684

685
        } else if (streq(key, "luks.uuid")) {
47✔
686

687
                if (proc_cmdline_value_missing(key, value))
×
688
                        return 0;
689

690
                d = get_crypto_device(startswith(value, "luks-") ?: value);
×
691
                if (!d)
×
692
                        return log_oom();
×
693

694
                d->create = arg_allow_list = true;
×
695

696
        } else if (streq(key, "luks.options")) {
47✔
697
                _cleanup_free_ char *headerdev = NULL, *filtered_headerdev_options = NULL;
1✔
698

699
                if (proc_cmdline_value_missing(key, value))
1✔
700
                        return 0;
701

702
                r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
1✔
703
                if (r != 2)
1✔
704
                        return free_and_strdup_warn(&arg_default_options, value);
×
705

706
                if (warn_uuid_invalid(uuid, key))
1✔
707
                        return 0;
708

709
                d = get_crypto_device(uuid);
1✔
710
                if (!d)
1✔
711
                        return log_oom();
×
712

713
                r = filter_header_device(uuid_value, &headerdev, &filtered_headerdev_options);
1✔
714
                if (r < 0)
1✔
715
                        return r;
716

717
                free_and_replace(d->options, filtered_headerdev_options);
1✔
718
                free_and_replace(d->headerdev, headerdev);
1✔
719
        } else if (streq(key, "luks.key")) {
46✔
720
                size_t n;
1✔
721
                _cleanup_free_ char *keyfile = NULL, *keydev = NULL;
×
722
                const char *keyspec;
1✔
723

724
                if (proc_cmdline_value_missing(key, value))
1✔
725
                        return 0;
726

727
                n = strspn(value, ALPHANUMERICAL "-");
1✔
728
                if (value[n] != '=')
1✔
729
                        return free_and_strdup_warn(&arg_default_keyfile, value);
×
730

731
                uuid = strndup(value, n);
1✔
732
                if (!uuid)
1✔
733
                        return log_oom();
×
734

735
                if (warn_uuid_invalid(uuid, key))
1✔
736
                        return 0;
737

738
                d = get_crypto_device(uuid);
1✔
739
                if (!d)
1✔
740
                        return log_oom();
×
741

742
                keyspec = value + n + 1;
1✔
743
                r = split_locationspec(keyspec, &keyfile, &keydev);
1✔
744
                if (r < 0)
1✔
745
                        return r;
746

747
                free_and_replace(d->keyfile, keyfile);
1✔
748
                free_and_replace(d->keydev, keydev);
1✔
749
        } else if (streq(key, "luks.data")) {
45✔
750
                size_t n;
×
751
                _cleanup_free_ char *datadev = NULL;
×
752

753
                if (proc_cmdline_value_missing(key, value))
×
754
                        return 0;
755

756
                n = strspn(value, ALPHANUMERICAL "-");
×
757
                if (value[n] != '=') {
×
758
                        log_warning("Failed to parse luks.data= kernel command line switch. UUID is invalid, ignoring.");
×
759
                        return 0;
×
760
                }
761

762
                uuid = strndup(value, n);
×
763
                if (!uuid)
×
764
                        return log_oom();
×
765

766
                if (warn_uuid_invalid(uuid, key))
×
767
                        return 0;
768

769
                d = get_crypto_device(uuid);
×
770
                if (!d)
×
771
                        return log_oom();
×
772

773
                datadev = fstab_node_to_udev_node(value + n + 1);
×
774
                if (!datadev)
×
775
                        return log_oom();
×
776

777
                free_and_replace(d->datadev, datadev);
×
778
        } else if (streq(key, "luks.name")) {
45✔
779

780
                if (proc_cmdline_value_missing(key, value))
×
781
                        return 0;
782

783
                r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value);
×
784
                if (r == 2) {
×
785
                        d = get_crypto_device(uuid);
×
786
                        if (!d)
×
787
                                return log_oom();
×
788

789
                        d->create = arg_allow_list = true;
×
790

791
                        free_and_replace(d->name, uuid_value);
×
792
                } else
UNCOV
793
                        log_warning("Failed to parse luks name switch %s. Ignoring.", value);
×
794
        }
795

796
        return 0;
797
}
798

799
static int add_crypttab_device(const char *name, const char *device, const char *keyspec, const char *options) {
22✔
800
        _cleanup_free_ char *keyfile = NULL, *keydev = NULL, *headerdev = NULL, *filtered_header = NULL;
22✔
801
        crypto_device *d = NULL;
22✔
802
        char *uuid;
22✔
803
        int r;
22✔
804

805
        uuid = startswith(device, "UUID=");
22✔
806
        if (!uuid)
22✔
807
                uuid = path_startswith(device, "/dev/disk/by-uuid/");
22✔
808
        if (!uuid)
22✔
809
                uuid = startswith(name, "luks-");
22✔
810
        if (uuid)
22✔
811
                d = hashmap_get(arg_disks, uuid);
×
812

813
        if (arg_allow_list && !d) {
22✔
814
                log_info("Not creating device '%s' because it was not specified on the kernel command line.", name);
×
815
                return 0;
×
816
        }
817

818
        r = split_locationspec(keyspec, &keyfile, &keydev);
22✔
819
        if (r < 0)
22✔
820
                return r;
821

822
        if (options && (!d || !d->options)) {
22✔
823
                r = filter_header_device(options, &headerdev, &filtered_header);
22✔
824
                if (r < 0)
22✔
825
                        return r;
826
                options = filtered_header;
22✔
827
        }
828

829
        r = create_disk(name,
22✔
830
                        device,
831
                        keyfile,
832
                        keydev,
833
                        (d && d->options) ? d->headerdev : headerdev,
×
834
                        (d && d->options) ? d->options : options,
×
835
                        arg_crypttab);
836
        if (r < 0)
22✔
837
                return r;
838

839
        if (d)
22✔
840
                d->create = false;
×
841

842
        return 0;
843
}
844

845
static int add_crypttab_devices(void) {
1✔
846
        _cleanup_fclose_ FILE *f = NULL;
1✔
847
        unsigned crypttab_line = 0;
1✔
848
        int r, ret = 0;
1✔
849

850
        if (!arg_read_crypttab)
1✔
851
                return 0;
852

853
        r = fopen_unlocked(arg_crypttab, "re", &f);
1✔
854
        if (r < 0) {
1✔
855
                if (errno != ENOENT)
×
856
                        log_error_errno(errno, "Failed to open %s: %m", arg_crypttab);
×
857
                return 0;
×
858
        }
859

860
        for (;;) {
27✔
861
                _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyspec = NULL, *options = NULL;
26✔
862
                int k;
27✔
863

864
                r = read_stripped_line(f, LONG_LINE_MAX, &line);
27✔
865
                if (r < 0)
27✔
866
                        return log_error_errno(r, "Failed to read %s: %m", arg_crypttab);
×
867
                if (r == 0)
27✔
868
                        break;
869

870
                crypttab_line++;
26✔
871

872
                if (IN_SET(line[0], 0, '#'))
26✔
873
                        continue;
4✔
874

875
                k = sscanf(line, "%ms %ms %ms %ms", &name, &device, &keyspec, &options);
22✔
876
                if (k < 2 || k > 4) {
22✔
877
                        log_error("Failed to parse %s:%u, ignoring.", arg_crypttab, crypttab_line);
×
878
                        continue;
×
879
                }
880

881
                RET_GATHER(ret, add_crypttab_device(name, device, keyspec, options));
22✔
882
        }
883

884
        return ret;
1✔
885
}
886

887
static int add_proc_cmdline_devices(void) {
1✔
888
        int r, ret = 0;
1✔
889
        crypto_device *d;
1✔
890

891
        HASHMAP_FOREACH(d, arg_disks) {
2✔
892
                _cleanup_free_ char *device = NULL;
×
893

894
                if (!d->create)
1✔
895
                        continue;
1✔
896

897
                if (!d->name) {
×
898
                        d->name = strjoin("luks-", d->uuid);
×
899
                        if (!d->name)
×
900
                                return log_oom();
×
901
                }
902

903
                device = strjoin("UUID=", d->uuid);
×
904
                if (!device)
×
905
                        return log_oom();
×
906

907
                r = create_disk(d->name,
×
908
                                d->datadev ?: device,
×
909
                                d->keyfile ?: arg_default_keyfile,
×
910
                                d->keydev,
×
911
                                d->headerdev,
×
912
                                d->options ?: arg_default_options,
×
913
                                "/proc/cmdline");
914
                RET_GATHER(ret, r);
×
915
        }
916

917
        return ret;
1✔
918
}
919

920
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func,
1✔
921
                                              crypto_device, crypt_device_free);
922

923
static int run(const char *dest, const char *dest_early, const char *dest_late) {
1✔
924
        int r;
1✔
925

926
        assert_se(arg_dest = dest);
1✔
927

928
        arg_crypttab = getenv("SYSTEMD_CRYPTTAB") ?: "/etc/crypttab";
1✔
929
        arg_runtime_directory = getenv("RUNTIME_DIRECTORY") ?: "/run/systemd/cryptsetup";
1✔
930

931
        arg_disks = hashmap_new(&crypt_device_hash_ops);
1✔
932
        if (!arg_disks)
1✔
933
                return log_oom();
×
934

935
        r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
1✔
936
        if (r < 0)
1✔
937
                return log_warning_errno(r, "Failed to parse kernel command line: %m");
×
938

939
        if (!arg_enabled)
1✔
940
                return 0;
941

942
        r = add_crypttab_devices();
1✔
943
        RET_GATHER(r, add_proc_cmdline_devices());
1✔
944

945
        return r;
946
}
947

948
DEFINE_MAIN_GENERATOR_FUNCTION(run);
4✔
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