]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e23a0ce8 | 2 | |
e23a0ce8 | 3 | #include <errno.h> |
ca78ad1d | 4 | #include <fcntl.h> |
ca78ad1d ZJS |
5 | #include <sys/stat.h> |
6 | #include <sys/types.h> | |
e23a0ce8 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
0fa9e53d | 9 | #include "dropin.h" |
7949dfa7 | 10 | #include "escape.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
0d39fa9c | 12 | #include "fileio.h" |
07630cea | 13 | #include "fstab-util.h" |
0fa9e53d JJ |
14 | #include "generator.h" |
15 | #include "hashmap.h" | |
7949dfa7 | 16 | #include "id128-util.h" |
e23a0ce8 | 17 | #include "log.h" |
49e942b2 | 18 | #include "mkdir.h" |
6bedfcbb | 19 | #include "parse-util.h" |
bde29068 | 20 | #include "path-util.h" |
4e731273 | 21 | #include "proc-cmdline.h" |
98bad05e | 22 | #include "specifier.h" |
07630cea | 23 | #include "string-util.h" |
0fa9e53d JJ |
24 | #include "strv.h" |
25 | #include "unit-name.h" | |
26 | #include "util.h" | |
27 | ||
28 | typedef struct crypto_device { | |
29 | char *uuid; | |
6cd5b12a | 30 | char *keyfile; |
70f5f48e | 31 | char *keydev; |
baade8cc | 32 | char *name; |
0fa9e53d JJ |
33 | char *options; |
34 | bool create; | |
35 | } crypto_device; | |
e23a0ce8 | 36 | |
7a44c7e3 | 37 | static const char *arg_dest = NULL; |
66a78c2b LP |
38 | static bool arg_enabled = true; |
39 | static bool arg_read_crypttab = true; | |
0fa9e53d JJ |
40 | static bool arg_whitelist = false; |
41 | static Hashmap *arg_disks = NULL; | |
42 | static char *arg_default_options = NULL; | |
43 | static char *arg_default_keyfile = NULL; | |
141a79f4 | 44 | |
a4a90e65 YW |
45 | STATIC_DESTRUCTOR_REGISTER(arg_disks, hashmap_freep); |
46 | STATIC_DESTRUCTOR_REGISTER(arg_default_options, freep); | |
47 | STATIC_DESTRUCTOR_REGISTER(arg_default_keyfile, freep); | |
48 | ||
70f5f48e | 49 | static int generate_keydev_mount(const char *name, const char *keydev, char **unit, char **mount) { |
7949dfa7 | 50 | _cleanup_free_ char *u = NULL, *what = NULL, *where = NULL, *name_escaped = NULL; |
70f5f48e MS |
51 | _cleanup_fclose_ FILE *f = NULL; |
52 | int r; | |
53 | ||
54 | assert(name); | |
55 | assert(keydev); | |
56 | assert(unit); | |
57 | assert(mount); | |
58 | ||
59 | r = mkdir_parents("/run/systemd/cryptsetup", 0755); | |
60 | if (r < 0) | |
61 | return r; | |
62 | ||
63 | r = mkdir("/run/systemd/cryptsetup", 0700); | |
579875bc MS |
64 | if (r < 0 && errno != EEXIST) |
65 | return -errno; | |
70f5f48e | 66 | |
7949dfa7 MS |
67 | name_escaped = cescape(name); |
68 | if (!name_escaped) | |
69 | return -ENOMEM; | |
70 | ||
71 | where = strjoin("/run/systemd/cryptsetup/keydev-", name_escaped); | |
70f5f48e MS |
72 | if (!where) |
73 | return -ENOMEM; | |
74 | ||
75 | r = mkdir(where, 0700); | |
579875bc MS |
76 | if (r < 0 && errno != EEXIST) |
77 | return -errno; | |
70f5f48e MS |
78 | |
79 | r = unit_name_from_path(where, ".mount", &u); | |
80 | if (r < 0) | |
81 | return r; | |
82 | ||
83 | r = generator_open_unit_file(arg_dest, NULL, u, &f); | |
84 | if (r < 0) | |
85 | return r; | |
86 | ||
87 | what = fstab_node_to_udev_node(keydev); | |
88 | if (!what) | |
89 | return -ENOMEM; | |
90 | ||
91 | fprintf(f, | |
92 | "[Unit]\n" | |
93 | "DefaultDependencies=no\n\n" | |
94 | "[Mount]\n" | |
95 | "What=%s\n" | |
96 | "Where=%s\n" | |
97 | "Options=ro\n", what, where); | |
98 | ||
99 | r = fflush_and_check(f); | |
100 | if (r < 0) | |
101 | return r; | |
102 | ||
103 | *unit = TAKE_PTR(u); | |
104 | *mount = TAKE_PTR(where); | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
e23a0ce8 LP |
109 | static int create_disk( |
110 | const char *name, | |
111 | const char *device, | |
70f5f48e | 112 | const char *keydev, |
e23a0ce8 LP |
113 | const char *password, |
114 | const char *options) { | |
115 | ||
fb883e75 | 116 | _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL, |
70f5f48e | 117 | *filtered = NULL, *u_escaped = NULL, *password_escaped = NULL, *filtered_escaped = NULL, *name_escaped = NULL, *keydev_mount = NULL; |
7fd1b19b | 118 | _cleanup_fclose_ FILE *f = NULL; |
b559616f | 119 | const char *dmname; |
b001ad61 | 120 | bool noauto, nofail, tmp, swap, netdev; |
744198e9 | 121 | int r; |
e23a0ce8 LP |
122 | |
123 | assert(name); | |
124 | assert(device); | |
125 | ||
b9f111b9 ZJS |
126 | noauto = fstab_test_yes_no_option(options, "noauto\0" "auto\0"); |
127 | nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0"); | |
a6dba978 ZJS |
128 | tmp = fstab_test_option(options, "tmp\0"); |
129 | swap = fstab_test_option(options, "swap\0"); | |
b001ad61 | 130 | netdev = fstab_test_option(options, "_netdev\0"); |
8c11d3c1 | 131 | |
baaa35ad ZJS |
132 | if (tmp && swap) |
133 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
134 | "Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", | |
135 | name); | |
155da457 | 136 | |
98bad05e LP |
137 | name_escaped = specifier_escape(name); |
138 | if (!name_escaped) | |
139 | return log_oom(); | |
140 | ||
744198e9 LP |
141 | e = unit_name_escape(name); |
142 | if (!e) | |
143 | return log_oom(); | |
144 | ||
f7f21d33 | 145 | u = fstab_node_to_udev_node(device); |
24a988e9 HH |
146 | if (!u) |
147 | return log_oom(); | |
e23a0ce8 | 148 | |
fb883e75 ZJS |
149 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); |
150 | if (r < 0) | |
151 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
152 | ||
98bad05e LP |
153 | u_escaped = specifier_escape(u); |
154 | if (!u_escaped) | |
155 | return log_oom(); | |
156 | ||
7410616c LP |
157 | r = unit_name_from_path(u, ".device", &d); |
158 | if (r < 0) | |
159 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
e23a0ce8 | 160 | |
d31eb24f LP |
161 | if (password) { |
162 | password_escaped = specifier_escape(password); | |
163 | if (!password_escaped) | |
164 | return log_oom(); | |
165 | } | |
98bad05e | 166 | |
baaa35ad ZJS |
167 | if (keydev && !password) |
168 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
169 | "Key device is specified, but path to the password file is missing."); | |
70f5f48e | 170 | |
fb883e75 ZJS |
171 | r = generator_open_unit_file(arg_dest, NULL, n, &f); |
172 | if (r < 0) | |
173 | return r; | |
0d536673 | 174 | |
b001ad61 | 175 | fprintf(f, |
b001ad61 ZJS |
176 | "[Unit]\n" |
177 | "Description=Cryptography Setup for %%I\n" | |
178 | "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n" | |
179 | "SourcePath=/etc/crypttab\n" | |
180 | "DefaultDependencies=no\n" | |
181 | "Conflicts=umount.target\n" | |
182 | "IgnoreOnIsolate=true\n" | |
183 | "After=%s\n", | |
a0dd2097 | 184 | netdev ? "remote-fs-pre.target" : "cryptsetup-pre.target"); |
e23a0ce8 | 185 | |
70f5f48e MS |
186 | if (keydev) { |
187 | _cleanup_free_ char *unit = NULL, *p = NULL; | |
188 | ||
189 | r = generate_keydev_mount(name, keydev, &unit, &keydev_mount); | |
190 | if (r < 0) | |
191 | return log_error_errno(r, "Failed to generate keydev mount unit: %m"); | |
192 | ||
193 | p = prefix_root(keydev_mount, password_escaped); | |
194 | if (!p) | |
195 | return log_oom(); | |
196 | ||
197 | free_and_replace(password_escaped, p); | |
198 | } | |
199 | ||
155da457 LP |
200 | if (!nofail) |
201 | fprintf(f, | |
b001ad61 ZJS |
202 | "Before=%s\n", |
203 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target"); | |
155da457 | 204 | |
ceca9501 | 205 | if (password) { |
e3ca6580 | 206 | if (PATH_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random")) |
0d536673 | 207 | fputs("After=systemd-random-seed.service\n", f); |
b559616f | 208 | else if (!STR_IN_SET(password, "-", "none")) { |
744198e9 LP |
209 | _cleanup_free_ char *uu; |
210 | ||
211 | uu = fstab_node_to_udev_node(password); | |
212 | if (!uu) | |
66a5dbdf DR |
213 | return log_oom(); |
214 | ||
bde29068 | 215 | if (!path_equal(uu, "/dev/null")) { |
744198e9 | 216 | |
048dd629 | 217 | if (path_startswith(uu, "/dev/")) { |
7410616c | 218 | _cleanup_free_ char *dd = NULL; |
66a5dbdf | 219 | |
7410616c LP |
220 | r = unit_name_from_path(uu, ".device", &dd); |
221 | if (r < 0) | |
222 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
bde29068 LP |
223 | |
224 | fprintf(f, "After=%1$s\nRequires=%1$s\n", dd); | |
225 | } else | |
98bad05e | 226 | fprintf(f, "RequiresMountsFor=%s\n", password_escaped); |
bde29068 | 227 | } |
66a5dbdf | 228 | } |
ceca9501 | 229 | } |
e23a0ce8 | 230 | |
048dd629 | 231 | if (path_startswith(u, "/dev/")) { |
9ece938a TW |
232 | fprintf(f, |
233 | "BindsTo=%s\n" | |
234 | "After=%s\n" | |
235 | "Before=umount.target\n", | |
236 | d, d); | |
a6f8786a MFO |
237 | |
238 | if (swap) | |
0d536673 LP |
239 | fputs("Before=dev-mapper-%i.swap\n", |
240 | f); | |
a6f8786a | 241 | } else |
b90cbe66 LHS |
242 | /* For loopback devices, add systemd-tmpfiles-setup-dev.service |
243 | dependency to ensure that loopback support is available in | |
244 | the kernel (/dev/loop-control needs to exist) */ | |
9ece938a | 245 | fprintf(f, |
b90cbe66 LHS |
246 | "RequiresMountsFor=%s\n" |
247 | "Requires=systemd-tmpfiles-setup-dev.service\n" | |
248 | "After=systemd-tmpfiles-setup-dev.service\n", | |
98bad05e LP |
249 | u_escaped); |
250 | ||
8eea8687 ZJS |
251 | r = generator_write_timeouts(arg_dest, device, name, options, &filtered); |
252 | if (r < 0) | |
253 | return r; | |
254 | ||
d31eb24f LP |
255 | if (filtered) { |
256 | filtered_escaped = specifier_escape(filtered); | |
257 | if (!filtered_escaped) | |
258 | return log_oom(); | |
259 | } | |
98bad05e | 260 | |
e23a0ce8 LP |
261 | fprintf(f, |
262 | "\n[Service]\n" | |
263 | "Type=oneshot\n" | |
264 | "RemainAfterExit=yes\n" | |
90724929 | 265 | "TimeoutSec=0\n" /* the binary handles timeouts anyway */ |
0b1f68ac | 266 | "KeyringMode=shared\n" /* make sure we can share cached keys among instances */ |
48da47ee | 267 | "OOMScoreAdjust=500\n" /* unlocking can allocate a lot of memory if Argon2 is used */ |
260ab287 | 268 | "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" |
7f4e0805 | 269 | "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", |
98bad05e LP |
270 | name_escaped, u_escaped, strempty(password_escaped), strempty(filtered_escaped), |
271 | name_escaped); | |
e23a0ce8 | 272 | |
8c11d3c1 | 273 | if (tmp) |
e23a0ce8 | 274 | fprintf(f, |
50109038 | 275 | "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n", |
98bad05e | 276 | name_escaped); |
e23a0ce8 | 277 | |
8c11d3c1 | 278 | if (swap) |
e23a0ce8 | 279 | fprintf(f, |
50109038 | 280 | "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n", |
98bad05e | 281 | name_escaped); |
e23a0ce8 | 282 | |
70f5f48e MS |
283 | if (keydev) |
284 | fprintf(f, | |
285 | "ExecStartPost=" UMOUNT_PATH " %s\n\n", | |
286 | keydev_mount); | |
287 | ||
4652c56c ZJS |
288 | r = fflush_and_check(f); |
289 | if (r < 0) | |
fb883e75 | 290 | return log_error_errno(r, "Failed to write unit file %s: %m", n); |
e23a0ce8 | 291 | |
155da457 | 292 | if (!noauto) { |
b001ad61 ZJS |
293 | r = generator_add_symlink(arg_dest, |
294 | netdev ? "remote-cryptsetup.target" : "cryptsetup.target", | |
b559616f ZJS |
295 | nofail ? "wants" : "requires", n); |
296 | if (r < 0) | |
297 | return r; | |
f7f21d33 | 298 | } |
74715b82 | 299 | |
b559616f ZJS |
300 | dmname = strjoina("dev-mapper-", e, ".device"); |
301 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
302 | if (r < 0) | |
303 | return r; | |
74715b82 | 304 | |
68395007 | 305 | if (!noauto && !nofail) { |
a6fb0dc1 | 306 | r = write_drop_in(arg_dest, dmname, 90, "device-timeout", |
8eea8687 ZJS |
307 | "# Automatically generated by systemd-cryptsetup-generator \n\n" |
308 | "[Unit]\nJobTimeoutSec=0"); | |
23bbb0de MS |
309 | if (r < 0) |
310 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
68395007 HH |
311 | } |
312 | ||
24a988e9 | 313 | return 0; |
e23a0ce8 LP |
314 | } |
315 | ||
a4a90e65 YW |
316 | static crypto_device* crypt_device_free(crypto_device *d) { |
317 | if (!d) | |
318 | return NULL; | |
319 | ||
6dd1c368 ZJS |
320 | free(d->uuid); |
321 | free(d->keyfile); | |
70f5f48e | 322 | free(d->keydev); |
6dd1c368 ZJS |
323 | free(d->name); |
324 | free(d->options); | |
a4a90e65 | 325 | return mfree(d); |
0fa9e53d JJ |
326 | } |
327 | ||
328 | static crypto_device *get_crypto_device(const char *uuid) { | |
329 | int r; | |
330 | crypto_device *d; | |
331 | ||
332 | assert(uuid); | |
333 | ||
334 | d = hashmap_get(arg_disks, uuid); | |
335 | if (!d) { | |
336 | d = new0(struct crypto_device, 1); | |
337 | if (!d) | |
338 | return NULL; | |
339 | ||
340 | d->create = false; | |
baade8cc | 341 | d->keyfile = d->options = d->name = NULL; |
0fa9e53d JJ |
342 | |
343 | d->uuid = strdup(uuid); | |
6b430fdb ZJS |
344 | if (!d->uuid) |
345 | return mfree(d); | |
0fa9e53d JJ |
346 | |
347 | r = hashmap_put(arg_disks, d->uuid, d); | |
348 | if (r < 0) { | |
349 | free(d->uuid); | |
6b430fdb | 350 | return mfree(d); |
0fa9e53d JJ |
351 | } |
352 | } | |
353 | ||
354 | return d; | |
355 | } | |
356 | ||
96287a49 | 357 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
0fa9e53d | 358 | _cleanup_free_ char *uuid = NULL, *uuid_value = NULL; |
1d84ad94 LP |
359 | crypto_device *d; |
360 | int r; | |
66a78c2b | 361 | |
1d84ad94 | 362 | if (streq(key, "luks")) { |
059cb385 | 363 | |
1d84ad94 | 364 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 365 | if (r < 0) |
1d84ad94 | 366 | log_warning("Failed to parse luks= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
367 | else |
368 | arg_enabled = r; | |
66a78c2b | 369 | |
1d84ad94 | 370 | } else if (streq(key, "luks.crypttab")) { |
66a78c2b | 371 | |
1d84ad94 | 372 | r = value ? parse_boolean(value) : 1; |
141a79f4 | 373 | if (r < 0) |
1d84ad94 | 374 | log_warning("Failed to parse luks.crypttab= kernel command line switch %s. Ignoring.", value); |
141a79f4 ZJS |
375 | else |
376 | arg_read_crypttab = r; | |
66a78c2b | 377 | |
1d84ad94 LP |
378 | } else if (streq(key, "luks.uuid")) { |
379 | ||
380 | if (proc_cmdline_value_missing(key, value)) | |
381 | return 0; | |
66a78c2b | 382 | |
0fa9e53d JJ |
383 | d = get_crypto_device(startswith(value, "luks-") ? value+5 : value); |
384 | if (!d) | |
141a79f4 | 385 | return log_oom(); |
66a78c2b | 386 | |
0fa9e53d JJ |
387 | d->create = arg_whitelist = true; |
388 | ||
1d84ad94 LP |
389 | } else if (streq(key, "luks.options")) { |
390 | ||
391 | if (proc_cmdline_value_missing(key, value)) | |
392 | return 0; | |
66a78c2b | 393 | |
0fa9e53d JJ |
394 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); |
395 | if (r == 2) { | |
396 | d = get_crypto_device(uuid); | |
397 | if (!d) | |
398 | return log_oom(); | |
399 | ||
1d84ad94 | 400 | free_and_replace(d->options, uuid_value); |
0fa9e53d | 401 | } else if (free_and_strdup(&arg_default_options, value) < 0) |
141a79f4 | 402 | return log_oom(); |
66a78c2b | 403 | |
1d84ad94 | 404 | } else if (streq(key, "luks.key")) { |
7949dfa7 MS |
405 | size_t n; |
406 | _cleanup_free_ char *keyfile = NULL, *keydev = NULL; | |
407 | char *c; | |
408 | const char *keyspec; | |
1d84ad94 LP |
409 | |
410 | if (proc_cmdline_value_missing(key, value)) | |
411 | return 0; | |
66a78c2b | 412 | |
7949dfa7 MS |
413 | n = strspn(value, LETTERS DIGITS "-"); |
414 | if (value[n] != '=') { | |
415 | if (free_and_strdup(&arg_default_keyfile, value) < 0) | |
416 | return log_oom(); | |
417 | return 0; | |
418 | } | |
70f5f48e | 419 | |
7949dfa7 MS |
420 | uuid = strndup(value, n); |
421 | if (!uuid) | |
422 | return log_oom(); | |
6cd5b12a | 423 | |
7949dfa7 MS |
424 | if (!id128_is_valid(uuid)) { |
425 | log_warning("Failed to parse luks.key= kernel command line switch. UUID is invalid, ignoring."); | |
426 | return 0; | |
427 | } | |
428 | ||
429 | d = get_crypto_device(uuid); | |
430 | if (!d) | |
431 | return log_oom(); | |
70f5f48e | 432 | |
7949dfa7 MS |
433 | keyspec = value + n + 1; |
434 | c = strrchr(keyspec, ':'); | |
435 | if (c) { | |
436 | *c = '\0'; | |
437 | keyfile = strdup(keyspec); | |
438 | keydev = strdup(c + 1); | |
70f5f48e MS |
439 | |
440 | if (!keyfile || !keydev) | |
441 | return log_oom(); | |
7949dfa7 MS |
442 | } else { |
443 | /* No keydev specified */ | |
444 | keyfile = strdup(keyspec); | |
445 | if (!keyfile) | |
446 | return log_oom(); | |
447 | } | |
70f5f48e | 448 | |
7949dfa7 MS |
449 | free_and_replace(d->keyfile, keyfile); |
450 | free_and_replace(d->keydev, keydev); | |
1d84ad94 LP |
451 | } else if (streq(key, "luks.name")) { |
452 | ||
453 | if (proc_cmdline_value_missing(key, value)) | |
454 | return 0; | |
baade8cc JJ |
455 | |
456 | r = sscanf(value, "%m[0-9a-fA-F-]=%ms", &uuid, &uuid_value); | |
457 | if (r == 2) { | |
458 | d = get_crypto_device(uuid); | |
459 | if (!d) | |
460 | return log_oom(); | |
461 | ||
462 | d->create = arg_whitelist = true; | |
463 | ||
f9ecfd3b | 464 | free_and_replace(d->name, uuid_value); |
baade8cc JJ |
465 | } else |
466 | log_warning("Failed to parse luks name switch %s. Ignoring.", value); | |
85013844 | 467 | } |
66a78c2b | 468 | |
24a988e9 | 469 | return 0; |
66a78c2b LP |
470 | } |
471 | ||
0fa9e53d | 472 | static int add_crypttab_devices(void) { |
7fd1b19b | 473 | _cleanup_fclose_ FILE *f = NULL; |
b42674a1 LP |
474 | unsigned crypttab_line = 0; |
475 | struct stat st; | |
476 | int r; | |
e23a0ce8 | 477 | |
0fa9e53d JJ |
478 | if (!arg_read_crypttab) |
479 | return 0; | |
480 | ||
fdeea3f4 ZJS |
481 | r = fopen_unlocked("/etc/crypttab", "re", &f); |
482 | if (r < 0) { | |
0fa9e53d JJ |
483 | if (errno != ENOENT) |
484 | log_error_errno(errno, "Failed to open /etc/crypttab: %m"); | |
485 | return 0; | |
e23a0ce8 LP |
486 | } |
487 | ||
0fa9e53d JJ |
488 | if (fstat(fileno(f), &st) < 0) { |
489 | log_error_errno(errno, "Failed to stat /etc/crypttab: %m"); | |
490 | return 0; | |
491 | } | |
e23a0ce8 | 492 | |
0fa9e53d | 493 | for (;;) { |
b42674a1 | 494 | _cleanup_free_ char *line = NULL, *name = NULL, *device = NULL, *keyfile = NULL, *options = NULL; |
0fa9e53d | 495 | crypto_device *d = NULL; |
b42674a1 LP |
496 | char *l, *uuid; |
497 | int k; | |
4c12626c | 498 | |
b42674a1 LP |
499 | r = read_line(f, LONG_LINE_MAX, &line); |
500 | if (r < 0) | |
501 | return log_error_errno(r, "Failed to read /etc/crypttab: %m"); | |
502 | if (r == 0) | |
0fa9e53d | 503 | break; |
66a78c2b | 504 | |
0fa9e53d | 505 | crypttab_line++; |
141a79f4 | 506 | |
0fa9e53d | 507 | l = strstrip(line); |
b42674a1 | 508 | if (IN_SET(l[0], 0, '#')) |
0fa9e53d | 509 | continue; |
66a78c2b | 510 | |
0fa9e53d JJ |
511 | k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyfile, &options); |
512 | if (k < 2 || k > 4) { | |
513 | log_error("Failed to parse /etc/crypttab:%u, ignoring.", crypttab_line); | |
514 | continue; | |
515 | } | |
66a78c2b | 516 | |
49fe5c09 | 517 | uuid = STARTSWITH_SET(device, "UUID=", "luks-"); |
0fa9e53d JJ |
518 | if (!uuid) |
519 | uuid = path_startswith(device, "/dev/disk/by-uuid/"); | |
0fa9e53d JJ |
520 | if (uuid) |
521 | d = hashmap_get(arg_disks, uuid); | |
8973790e | 522 | |
0fa9e53d JJ |
523 | if (arg_whitelist && !d) { |
524 | log_info("Not creating device '%s' because it was not specified on the kernel command line.", name); | |
525 | continue; | |
8973790e LP |
526 | } |
527 | ||
70f5f48e | 528 | r = create_disk(name, device, NULL, keyfile, (d && d->options) ? d->options : options); |
0fa9e53d JJ |
529 | if (r < 0) |
530 | return r; | |
8973790e | 531 | |
0fa9e53d JJ |
532 | if (d) |
533 | d->create = false; | |
534 | } | |
8973790e | 535 | |
0fa9e53d JJ |
536 | return 0; |
537 | } | |
66a78c2b | 538 | |
0fa9e53d JJ |
539 | static int add_proc_cmdline_devices(void) { |
540 | int r; | |
541 | Iterator i; | |
542 | crypto_device *d; | |
66a78c2b | 543 | |
0fa9e53d JJ |
544 | HASHMAP_FOREACH(d, arg_disks, i) { |
545 | const char *options; | |
baade8cc | 546 | _cleanup_free_ char *device = NULL; |
66a78c2b | 547 | |
0fa9e53d JJ |
548 | if (!d->create) |
549 | continue; | |
e23a0ce8 | 550 | |
baade8cc JJ |
551 | if (!d->name) { |
552 | d->name = strappend("luks-", d->uuid); | |
553 | if (!d->name) | |
554 | return log_oom(); | |
555 | } | |
e23a0ce8 | 556 | |
0fa9e53d JJ |
557 | device = strappend("UUID=", d->uuid); |
558 | if (!device) | |
559 | return log_oom(); | |
7ab064a6 | 560 | |
0fa9e53d JJ |
561 | if (d->options) |
562 | options = d->options; | |
563 | else if (arg_default_options) | |
564 | options = arg_default_options; | |
565 | else | |
566 | options = "timeout=0"; | |
141a79f4 | 567 | |
70f5f48e | 568 | r = create_disk(d->name, device, d->keydev, d->keyfile ?: arg_default_keyfile, options); |
0fa9e53d JJ |
569 | if (r < 0) |
570 | return r; | |
e23a0ce8 LP |
571 | } |
572 | ||
0fa9e53d JJ |
573 | return 0; |
574 | } | |
e23a0ce8 | 575 | |
a4a90e65 YW |
576 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(crypt_device_hash_ops, char, string_hash_func, string_compare_func, |
577 | crypto_device, crypt_device_free); | |
578 | ||
7a44c7e3 | 579 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
5f4bfe56 | 580 | int r; |
e23a0ce8 | 581 | |
7a44c7e3 | 582 | assert_se(arg_dest = dest); |
e23a0ce8 | 583 | |
a4a90e65 YW |
584 | arg_disks = hashmap_new(&crypt_device_hash_ops); |
585 | if (!arg_disks) | |
586 | return log_oom(); | |
7ab064a6 | 587 | |
1d84ad94 | 588 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
a4a90e65 YW |
589 | if (r < 0) |
590 | return log_warning_errno(r, "Failed to parse kernel command line: %m"); | |
7ab064a6 | 591 | |
a4a90e65 YW |
592 | if (!arg_enabled) |
593 | return 0; | |
e23a0ce8 | 594 | |
5f4bfe56 LP |
595 | r = add_crypttab_devices(); |
596 | if (r < 0) | |
a4a90e65 | 597 | return r; |
0fa9e53d | 598 | |
5f4bfe56 LP |
599 | r = add_proc_cmdline_devices(); |
600 | if (r < 0) | |
a4a90e65 | 601 | return r; |
141a79f4 | 602 | |
a4a90e65 | 603 | return 0; |
e23a0ce8 | 604 | } |
a4a90e65 | 605 | |
7a44c7e3 | 606 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |