]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
1a14a53c | 2 | |
6b5cf3ea | 3 | #include <blkid.h> |
1a14a53c | 4 | #include <stdlib.h> |
1a14a53c | 5 | #include <sys/statfs.h> |
cf0fbc49 | 6 | #include <unistd.h> |
1a14a53c | 7 | |
85624f01 | 8 | #include "sd-device.h" |
07630cea LP |
9 | #include "sd-id128.h" |
10 | ||
b5efdb8a | 11 | #include "alloc-util.h" |
07630cea | 12 | #include "blkid-util.h" |
18c528e9 | 13 | #include "blockdev-util.h" |
07630cea | 14 | #include "btrfs-util.h" |
133432cc | 15 | #include "device-util.h" |
a0956174 | 16 | #include "dirent-util.h" |
72e18a98 | 17 | #include "dissect-image.h" |
07630cea | 18 | #include "efivars.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
07630cea LP |
20 | #include "fileio.h" |
21 | #include "fstab-util.h" | |
22 | #include "generator.h" | |
23 | #include "gpt.h" | |
1a14a53c | 24 | #include "missing.h" |
07630cea | 25 | #include "mkdir.h" |
049af8ad | 26 | #include "mountpoint-util.h" |
6bedfcbb | 27 | #include "parse-util.h" |
07630cea | 28 | #include "path-util.h" |
4e731273 | 29 | #include "proc-cmdline.h" |
1a14a53c | 30 | #include "special.h" |
98bad05e | 31 | #include "specifier.h" |
8fcde012 | 32 | #include "stat-util.h" |
07630cea | 33 | #include "string-util.h" |
85624f01 | 34 | #include "strv.h" |
1a14a53c | 35 | #include "unit-name.h" |
07630cea | 36 | #include "util.h" |
9a5cb137 | 37 | #include "virt.h" |
1a14a53c | 38 | |
ec6e9597 | 39 | static const char *arg_dest = NULL; |
73b80ec2 LP |
40 | static bool arg_enabled = true; |
41 | static bool arg_root_enabled = true; | |
c94b2417 | 42 | static int arg_root_rw = -1; |
1a14a53c | 43 | |
01af8c01 | 44 | static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) { |
2aa2860b | 45 | _cleanup_free_ char *e = NULL, *n = NULL, *d = NULL, *id_escaped = NULL, *what_escaped = NULL; |
1af72119 | 46 | _cleanup_fclose_ FILE *f = NULL; |
2aa2860b | 47 | const char *p; |
1af72119 LP |
48 | int r; |
49 | ||
50 | assert(id); | |
51 | assert(what); | |
1af72119 | 52 | |
7410616c LP |
53 | r = unit_name_from_path(what, ".device", &d); |
54 | if (r < 0) | |
55 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
1af72119 LP |
56 | |
57 | e = unit_name_escape(id); | |
58 | if (!e) | |
59 | return log_oom(); | |
60 | ||
7410616c LP |
61 | r = unit_name_build("systemd-cryptsetup", e, ".service", &n); |
62 | if (r < 0) | |
63 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
1af72119 | 64 | |
98bad05e LP |
65 | id_escaped = specifier_escape(id); |
66 | if (!id_escaped) | |
67 | return log_oom(); | |
68 | ||
69 | what_escaped = specifier_escape(what); | |
70 | if (!what_escaped) | |
71 | return log_oom(); | |
72 | ||
2aa2860b | 73 | p = strjoina(arg_dest, "/", n); |
1af72119 | 74 | f = fopen(p, "wxe"); |
4a62c710 MS |
75 | if (!f) |
76 | return log_error_errno(errno, "Failed to create unit file %s: %m", p); | |
1af72119 LP |
77 | |
78 | fprintf(f, | |
79 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
80 | "[Unit]\n" | |
81 | "Description=Cryptography Setup for %%I\n" | |
c3834f9b | 82 | "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n" |
1af72119 LP |
83 | "DefaultDependencies=no\n" |
84 | "Conflicts=umount.target\n" | |
85 | "BindsTo=dev-mapper-%%i.device %s\n" | |
86 | "Before=umount.target cryptsetup.target\n" | |
87 | "After=%s\n" | |
88 | "IgnoreOnIsolate=true\n" | |
1af72119 LP |
89 | "[Service]\n" |
90 | "Type=oneshot\n" | |
91 | "RemainAfterExit=yes\n" | |
92 | "TimeoutSec=0\n" /* the binary handles timeouts anyway */ | |
0b1f68ac | 93 | "KeyringMode=shared\n" /* make sure we can share cached keys among instances */ |
cca1dfdd | 94 | "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '' '%s'\n" |
1af72119 LP |
95 | "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", |
96 | d, d, | |
98bad05e LP |
97 | id_escaped, what_escaped, rw ? "" : "read-only", |
98 | id_escaped); | |
1af72119 | 99 | |
dacd6cee LP |
100 | r = fflush_and_check(f); |
101 | if (r < 0) | |
102 | return log_error_errno(r, "Failed to write file %s: %m", p); | |
1af72119 | 103 | |
9cdcf368 ZJS |
104 | r = generator_add_symlink(arg_dest, d, "wants", n); |
105 | if (r < 0) | |
106 | return r; | |
1af72119 | 107 | |
01af8c01 | 108 | if (require) { |
9cdcf368 | 109 | const char *dmname; |
1af72119 | 110 | |
9cdcf368 ZJS |
111 | r = generator_add_symlink(arg_dest, "cryptsetup.target", "requires", n); |
112 | if (r < 0) | |
113 | return r; | |
01af8c01 | 114 | |
9cdcf368 ZJS |
115 | dmname = strjoina("dev-mapper-", e, ".device"); |
116 | r = generator_add_symlink(arg_dest, dmname, "requires", n); | |
117 | if (r < 0) | |
118 | return r; | |
01af8c01 | 119 | } |
1af72119 | 120 | |
2aa2860b | 121 | p = strjoina(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf"); |
1af72119 LP |
122 | mkdir_parents_label(p, 0755); |
123 | r = write_string_file(p, | |
124 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
125 | "[Unit]\n" | |
4c1fc3e4 DM |
126 | "JobTimeoutSec=0\n", |
127 | WRITE_STRING_FILE_CREATE); /* the binary handles timeouts anyway */ | |
23bbb0de MS |
128 | if (r < 0) |
129 | return log_error_errno(r, "Failed to write device drop-in: %m"); | |
1af72119 | 130 | |
2aa2860b ZJS |
131 | if (device) { |
132 | char *ret; | |
133 | ||
134 | ret = strappend("/dev/mapper/", id); | |
135 | if (!ret) | |
136 | return log_oom(); | |
1af72119 | 137 | |
01af8c01 | 138 | *device = ret; |
2aa2860b ZJS |
139 | } |
140 | ||
1af72119 LP |
141 | return 0; |
142 | } | |
143 | ||
73b80ec2 LP |
144 | static int add_mount( |
145 | const char *id, | |
146 | const char *what, | |
147 | const char *where, | |
148 | const char *fstype, | |
cca1dfdd | 149 | bool rw, |
59512f21 | 150 | const char *options, |
73b80ec2 LP |
151 | const char *description, |
152 | const char *post) { | |
153 | ||
9cdcf368 | 154 | _cleanup_free_ char *unit = NULL, *crypto_what = NULL, *p = NULL; |
1a14a53c | 155 | _cleanup_fclose_ FILE *f = NULL; |
e48fdd84 | 156 | int r; |
1a14a53c | 157 | |
98bad05e LP |
158 | /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured |
159 | * externally, but all originate from our own sources here, and hence we know they contain no % characters that | |
160 | * could potentially be understood as specifiers. */ | |
161 | ||
1af72119 LP |
162 | assert(id); |
163 | assert(what); | |
164 | assert(where); | |
1af72119 LP |
165 | assert(description); |
166 | ||
73b80ec2 | 167 | log_debug("Adding %s: %s %s", where, what, strna(fstype)); |
1a14a53c | 168 | |
73b80ec2 | 169 | if (streq_ptr(fstype, "crypto_LUKS")) { |
1af72119 | 170 | |
01af8c01 | 171 | r = add_cryptsetup(id, what, rw, true, &crypto_what); |
1af72119 LP |
172 | if (r < 0) |
173 | return r; | |
174 | ||
175 | what = crypto_what; | |
176 | fstype = NULL; | |
177 | } | |
178 | ||
7410616c LP |
179 | r = unit_name_from_path(where, ".mount", &unit); |
180 | if (r < 0) | |
181 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
1a14a53c | 182 | |
605405c6 | 183 | p = strjoin(arg_dest, "/", unit); |
e48fdd84 LP |
184 | if (!p) |
185 | return log_oom(); | |
186 | ||
187 | f = fopen(p, "wxe"); | |
4a62c710 MS |
188 | if (!f) |
189 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
1a14a53c LP |
190 | |
191 | fprintf(f, | |
192 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
193 | "[Unit]\n" | |
c3834f9b LP |
194 | "Description=%s\n" |
195 | "Documentation=man:systemd-gpt-auto-generator(8)\n", | |
e48fdd84 LP |
196 | description); |
197 | ||
73b80ec2 LP |
198 | if (post) |
199 | fprintf(f, "Before=%s\n", post); | |
200 | ||
e48fdd84 LP |
201 | r = generator_write_fsck_deps(f, arg_dest, what, where, fstype); |
202 | if (r < 0) | |
203 | return r; | |
204 | ||
205 | fprintf(f, | |
206 | "\n" | |
1a14a53c LP |
207 | "[Mount]\n" |
208 | "What=%s\n" | |
1af72119 LP |
209 | "Where=%s\n", |
210 | what, where); | |
211 | ||
73b80ec2 LP |
212 | if (fstype) |
213 | fprintf(f, "Type=%s\n", fstype); | |
214 | ||
59512f21 KS |
215 | if (options) |
216 | fprintf(f, "Options=%s,%s\n", options, rw ? "rw" : "ro"); | |
217 | else | |
218 | fprintf(f, "Options=%s\n", rw ? "rw" : "ro"); | |
1a14a53c | 219 | |
dacd6cee LP |
220 | r = fflush_and_check(f); |
221 | if (r < 0) | |
222 | return log_error_errno(r, "Failed to write unit file %s: %m", p); | |
1a14a53c | 223 | |
9cdcf368 ZJS |
224 | if (post) |
225 | return generator_add_symlink(arg_dest, post, "requires", unit); | |
1a14a53c LP |
226 | return 0; |
227 | } | |
228 | ||
e137880b | 229 | static int path_is_busy(const char *where) { |
59512f21 KS |
230 | int r; |
231 | ||
232 | /* already a mountpoint; generators run during reload */ | |
e1873695 | 233 | r = path_is_mount_point(where, NULL, AT_SYMLINK_FOLLOW); |
59512f21 KS |
234 | if (r > 0) |
235 | return false; | |
236 | ||
237 | /* the directory might not exist on a stateless system */ | |
238 | if (r == -ENOENT) | |
239 | return false; | |
240 | ||
241 | if (r < 0) | |
e137880b | 242 | return log_warning_errno(r, "Cannot check if \"%s\" is a mount point: %m", where); |
59512f21 KS |
243 | |
244 | /* not a mountpoint but it contains files */ | |
e137880b ZJS |
245 | r = dir_is_empty(where); |
246 | if (r < 0) | |
247 | return log_warning_errno(r, "Cannot check if \"%s\" is empty: %m", where); | |
248 | if (r > 0) | |
249 | return false; | |
59512f21 | 250 | |
e137880b ZJS |
251 | log_debug("\"%s\" already populated, ignoring.", where); |
252 | return true; | |
59512f21 KS |
253 | } |
254 | ||
72e18a98 LP |
255 | static int add_partition_mount( |
256 | DissectedPartition *p, | |
61331eab | 257 | const char *id, |
61331eab | 258 | const char *where, |
72e18a98 | 259 | const char *description) { |
61331eab | 260 | |
e137880b | 261 | int r; |
72e18a98 | 262 | assert(p); |
61331eab | 263 | |
e137880b ZJS |
264 | r = path_is_busy(where); |
265 | if (r != 0) | |
266 | return r < 0 ? r : 0; | |
61331eab | 267 | |
61331eab LP |
268 | return add_mount( |
269 | id, | |
72e18a98 | 270 | p->node, |
61331eab | 271 | where, |
72e18a98 LP |
272 | p->fstype, |
273 | p->rw, | |
59512f21 | 274 | NULL, |
61331eab | 275 | description, |
72e18a98 | 276 | SPECIAL_LOCAL_FS_TARGET); |
61331eab LP |
277 | } |
278 | ||
59512f21 | 279 | static int add_swap(const char *path) { |
9cdcf368 | 280 | _cleanup_free_ char *name = NULL, *unit = NULL; |
59512f21 KS |
281 | _cleanup_fclose_ FILE *f = NULL; |
282 | int r; | |
283 | ||
284 | assert(path); | |
285 | ||
fc5bc384 FB |
286 | /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */ |
287 | r = fstab_has_fstype("swap"); | |
288 | if (r < 0) | |
289 | return log_error_errno(r, "Failed to parse fstab: %m"); | |
1a680ae3 | 290 | if (r > 0) { |
fc5bc384 FB |
291 | log_debug("swap specified in fstab, ignoring."); |
292 | return 0; | |
293 | } | |
294 | ||
59512f21 KS |
295 | log_debug("Adding swap: %s", path); |
296 | ||
297 | r = unit_name_from_path(path, ".swap", &name); | |
298 | if (r < 0) | |
299 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
300 | ||
605405c6 | 301 | unit = strjoin(arg_dest, "/", name); |
59512f21 KS |
302 | if (!unit) |
303 | return log_oom(); | |
304 | ||
305 | f = fopen(unit, "wxe"); | |
306 | if (!f) | |
307 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
308 | ||
309 | fprintf(f, | |
310 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
311 | "[Unit]\n" | |
312 | "Description=Swap Partition\n" | |
313 | "Documentation=man:systemd-gpt-auto-generator(8)\n\n" | |
314 | "[Swap]\n" | |
315 | "What=%s\n", | |
316 | path); | |
317 | ||
dacd6cee LP |
318 | r = fflush_and_check(f); |
319 | if (r < 0) | |
320 | return log_error_errno(r, "Failed to write unit file %s: %m", unit); | |
59512f21 | 321 | |
9cdcf368 | 322 | return generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, "wants", name); |
59512f21 KS |
323 | } |
324 | ||
349cc4a5 | 325 | #if ENABLE_EFI |
7a1494aa TG |
326 | static int add_automount( |
327 | const char *id, | |
328 | const char *what, | |
329 | const char *where, | |
330 | const char *fstype, | |
331 | bool rw, | |
332 | const char *options, | |
333 | const char *description, | |
334 | usec_t timeout) { | |
335 | ||
9cdcf368 | 336 | _cleanup_free_ char *unit = NULL; |
7a1494aa | 337 | _cleanup_fclose_ FILE *f = NULL; |
2aa2860b | 338 | const char *opt = "noauto", *p; |
7a1494aa TG |
339 | int r; |
340 | ||
341 | assert(id); | |
342 | assert(where); | |
343 | assert(description); | |
344 | ||
345 | if (options) | |
2aa2860b | 346 | opt = strjoina(options, ",", opt); |
7a1494aa TG |
347 | |
348 | r = add_mount(id, | |
349 | what, | |
350 | where, | |
351 | fstype, | |
352 | rw, | |
353 | opt, | |
354 | description, | |
355 | NULL); | |
356 | if (r < 0) | |
357 | return r; | |
358 | ||
359 | r = unit_name_from_path(where, ".automount", &unit); | |
360 | if (r < 0) | |
361 | return log_error_errno(r, "Failed to generate unit name: %m"); | |
362 | ||
2aa2860b | 363 | p = strjoina(arg_dest, "/", unit); |
7a1494aa TG |
364 | f = fopen(p, "wxe"); |
365 | if (!f) | |
366 | return log_error_errno(errno, "Failed to create unit file %s: %m", unit); | |
367 | ||
368 | fprintf(f, | |
369 | "# Automatically generated by systemd-gpt-auto-generator\n\n" | |
370 | "[Unit]\n" | |
371 | "Description=%s\n" | |
372 | "Documentation=man:systemd-gpt-auto-generator(8)\n" | |
373 | "[Automount]\n" | |
374 | "Where=%s\n" | |
70887c5f | 375 | "TimeoutIdleSec="USEC_FMT"\n", |
7a1494aa TG |
376 | description, |
377 | where, | |
70887c5f | 378 | timeout / USEC_PER_SEC); |
7a1494aa TG |
379 | |
380 | r = fflush_and_check(f); | |
381 | if (r < 0) | |
382 | return log_error_errno(r, "Failed to write unit file %s: %m", p); | |
383 | ||
9cdcf368 | 384 | return generator_add_symlink(arg_dest, SPECIAL_LOCAL_FS_TARGET, "wants", unit); |
7a1494aa TG |
385 | } |
386 | ||
72e18a98 | 387 | static int add_esp(DissectedPartition *p) { |
b52a109a | 388 | const char *esp; |
59512f21 KS |
389 | int r; |
390 | ||
72e18a98 | 391 | assert(p); |
59512f21 | 392 | |
59512f21 | 393 | if (in_initrd()) { |
b52a109a | 394 | log_debug("In initrd, ignoring the ESP."); |
59512f21 KS |
395 | return 0; |
396 | } | |
397 | ||
b52a109a LP |
398 | /* If /efi exists we'll use that. Otherwise we'll use /boot, as that's usually the better choice */ |
399 | esp = access("/efi/", F_OK) >= 0 ? "/efi" : "/boot"; | |
59512f21 | 400 | |
0b6b6787 | 401 | /* We create an .automount which is not overridden by the .mount from the fstab generator. */ |
b9088048 FB |
402 | r = fstab_is_mount_point(esp); |
403 | if (r < 0) | |
404 | return log_error_errno(r, "Failed to parse fstab: %m"); | |
39b6a511 | 405 | if (r > 0) { |
b52a109a | 406 | log_debug("%s specified in fstab, ignoring.", esp); |
59512f21 KS |
407 | return 0; |
408 | } | |
409 | ||
e137880b ZJS |
410 | r = path_is_busy(esp); |
411 | if (r != 0) | |
412 | return r < 0 ? r : 0; | |
59512f21 | 413 | |
7ba25ab5 | 414 | if (is_efi_boot()) { |
72e18a98 | 415 | sd_id128_t loader_uuid; |
59512f21 | 416 | |
7ba25ab5 | 417 | /* If this is an EFI boot, be extra careful, and only mount the ESP if it was the ESP used for booting. */ |
59512f21 | 418 | |
7ba25ab5 LP |
419 | r = efi_loader_get_device_part_uuid(&loader_uuid); |
420 | if (r == -ENOENT) { | |
421 | log_debug("EFI loader partition unknown."); | |
422 | return 0; | |
423 | } | |
e28973ee ZJS |
424 | if (r < 0) |
425 | return log_error_errno(r, "Failed to read ESP partition UUID: %m"); | |
7ba25ab5 | 426 | |
72e18a98 | 427 | if (!sd_id128_equal(p->uuid, loader_uuid)) { |
7ba25ab5 LP |
428 | log_debug("Partition for %s does not appear to be the partition we are booted from.", esp); |
429 | return 0; | |
430 | } | |
431 | } else | |
432 | log_debug("Not an EFI boot, skipping ESP check."); | |
433 | ||
434 | return add_automount("boot", | |
72e18a98 LP |
435 | p->node, |
436 | esp, | |
437 | p->fstype, | |
438 | true, | |
439 | "umask=0077", | |
440 | "EFI System Partition Automount", | |
441 | 120 * USEC_PER_SEC); | |
7a1494aa | 442 | } |
59512f21 | 443 | #else |
d97beb0e | 444 | static int add_esp(DissectedPartition *p) { |
59512f21 | 445 | return 0; |
59512f21 | 446 | } |
7a1494aa | 447 | #endif |
59512f21 | 448 | |
fd89051e LP |
449 | static int add_root_rw(DissectedPartition *p) { |
450 | const char *path; | |
451 | int r; | |
452 | ||
453 | assert(p); | |
454 | ||
455 | if (in_initrd()) { | |
456 | log_debug("In initrd, not generating drop-in for systemd-remount-fs.service."); | |
457 | return 0; | |
458 | } | |
459 | ||
460 | if (arg_root_rw >= 0) { | |
461 | log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service."); | |
462 | return 0; | |
463 | } | |
464 | ||
465 | if (!p->rw) { | |
466 | log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service."); | |
467 | return 0; | |
468 | } | |
469 | ||
9b69569d ZJS |
470 | (void) generator_enable_remount_fs_service(arg_dest); |
471 | ||
fd89051e LP |
472 | path = strjoina(arg_dest, "/systemd-remount-fs.service.d/50-remount-rw.conf"); |
473 | (void) mkdir_parents(path, 0755); | |
474 | ||
475 | r = write_string_file(path, | |
476 | "# Automatically generated by systemd-gpt-generator\n\n" | |
fd89051e LP |
477 | "[Service]\n" |
478 | "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n", | |
479 | WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_NOFOLLOW); | |
480 | if (r < 0) | |
481 | return log_error_errno(r, "Failed to write drop-in file %s: %m", path); | |
482 | ||
483 | return 0; | |
484 | } | |
485 | ||
72e18a98 | 486 | static int open_parent(dev_t devnum, int *ret) { |
85624f01 | 487 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
72e18a98 | 488 | const char *name, *devtype, *node; |
85624f01 | 489 | sd_device *parent; |
61331eab | 490 | dev_t pn; |
85624f01 | 491 | int fd, r; |
72e18a98 LP |
492 | |
493 | assert(ret); | |
1a14a53c | 494 | |
85624f01 YW |
495 | r = sd_device_new_from_devnum(&d, 'b', devnum); |
496 | if (r < 0) | |
497 | return log_debug_errno(r, "Failed to open device: %m"); | |
1a14a53c | 498 | |
85624f01 YW |
499 | if (sd_device_get_devname(d, &name) < 0) { |
500 | r = sd_device_get_syspath(d, &name); | |
501 | if (r < 0) { | |
133432cc | 502 | log_device_debug_errno(d, r, "Device %u:%u does not have a name, ignoring: %m", major(devnum), minor(devnum)); |
8090b41e | 503 | return 0; |
85624f01 | 504 | } |
d2a62382 ZJS |
505 | } |
506 | ||
85624f01 YW |
507 | r = sd_device_get_parent(d, &parent); |
508 | if (r < 0) { | |
133432cc | 509 | log_device_debug_errno(d, r, "Not a partitioned device, ignoring: %m"); |
8090b41e | 510 | return 0; |
fa041593 | 511 | } |
1a14a53c | 512 | |
61331eab | 513 | /* Does it have a devtype? */ |
85624f01 YW |
514 | r = sd_device_get_devtype(parent, &devtype); |
515 | if (r < 0) { | |
133432cc | 516 | log_device_debug_errno(parent, r, "Parent doesn't have a device type, ignoring: %m"); |
8090b41e | 517 | return 0; |
fa041593 | 518 | } |
61331eab LP |
519 | |
520 | /* Is this a disk or a partition? We only care for disks... */ | |
fa041593 | 521 | if (!streq(devtype, "disk")) { |
133432cc | 522 | log_device_debug(parent, "Parent isn't a raw disk, ignoring."); |
8090b41e | 523 | return 0; |
fa041593 | 524 | } |
61331eab LP |
525 | |
526 | /* Does it have a device node? */ | |
85624f01 YW |
527 | r = sd_device_get_devname(parent, &node); |
528 | if (r < 0) { | |
133432cc | 529 | log_device_debug_errno(parent, r, "Parent device does not have device node, ignoring: %m"); |
8090b41e | 530 | return 0; |
fa041593 | 531 | } |
61331eab | 532 | |
133432cc | 533 | log_device_debug(d, "Root device %s.", node); |
61331eab | 534 | |
85624f01 YW |
535 | r = sd_device_get_devnum(parent, &pn); |
536 | if (r < 0) { | |
133432cc | 537 | log_device_debug_errno(parent, r, "Parent device is not a proper block device, ignoring: %m"); |
8090b41e | 538 | return 0; |
61331eab LP |
539 | } |
540 | ||
72e18a98 LP |
541 | fd = open(node, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
542 | if (fd < 0) | |
543 | return log_error_errno(errno, "Failed to open %s: %m", node); | |
61331eab | 544 | |
72e18a98 LP |
545 | *ret = fd; |
546 | return 1; | |
72e18a98 | 547 | } |
cb971249 | 548 | |
72e18a98 | 549 | static int enumerate_partitions(dev_t devnum) { |
72e18a98 LP |
550 | _cleanup_close_ int fd = -1; |
551 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
552 | int r, k; | |
61331eab | 553 | |
72e18a98 LP |
554 | r = open_parent(devnum, &fd); |
555 | if (r <= 0) | |
556 | return r; | |
61331eab | 557 | |
052eaf5c | 558 | r = dissect_image(fd, NULL, 0, DISSECT_IMAGE_GPT_ONLY|DISSECT_IMAGE_NO_UDEV, &m); |
72e18a98 LP |
559 | if (r == -ENOPKG) { |
560 | log_debug_errno(r, "No suitable partition table found, ignoring."); | |
561 | return 0; | |
61331eab | 562 | } |
23bbb0de | 563 | if (r < 0) |
72e18a98 | 564 | return log_error_errno(r, "Failed to dissect: %m"); |
0238d4c6 | 565 | |
72e18a98 LP |
566 | if (m->partitions[PARTITION_SWAP].found) { |
567 | k = add_swap(m->partitions[PARTITION_SWAP].node); | |
568 | if (k < 0) | |
569 | r = k; | |
1a14a53c LP |
570 | } |
571 | ||
72e18a98 LP |
572 | if (m->partitions[PARTITION_ESP].found) { |
573 | k = add_esp(m->partitions + PARTITION_ESP); | |
59512f21 KS |
574 | if (k < 0) |
575 | r = k; | |
576 | } | |
577 | ||
72e18a98 LP |
578 | if (m->partitions[PARTITION_HOME].found) { |
579 | k = add_partition_mount(m->partitions + PARTITION_HOME, "home", "/home", "Home Partition"); | |
73b80ec2 LP |
580 | if (k < 0) |
581 | r = k; | |
582 | } | |
e48fdd84 | 583 | |
72e18a98 LP |
584 | if (m->partitions[PARTITION_SRV].found) { |
585 | k = add_partition_mount(m->partitions + PARTITION_SRV, "srv", "/srv", "Server Data Partition"); | |
73b80ec2 LP |
586 | if (k < 0) |
587 | r = k; | |
588 | } | |
1a14a53c | 589 | |
fd89051e LP |
590 | if (m->partitions[PARTITION_ROOT].found) { |
591 | k = add_root_rw(m->partitions + PARTITION_ROOT); | |
592 | if (k < 0) | |
593 | r = k; | |
594 | } | |
595 | ||
1a14a53c LP |
596 | return r; |
597 | } | |
598 | ||
96287a49 | 599 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
73b80ec2 | 600 | int r; |
1a14a53c | 601 | |
73b80ec2 | 602 | assert(key); |
1a14a53c | 603 | |
8a9c44ed LP |
604 | if (proc_cmdline_key_streq(key, "systemd.gpt_auto") || |
605 | proc_cmdline_key_streq(key, "rd.systemd.gpt_auto")) { | |
1a14a53c | 606 | |
1d84ad94 | 607 | r = value ? parse_boolean(value) : 1; |
73b80ec2 | 608 | if (r < 0) |
0a1b9449 | 609 | log_warning_errno(r, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value); |
8086ffac ZJS |
610 | else |
611 | arg_enabled = r; | |
1a14a53c | 612 | |
8a9c44ed | 613 | } else if (proc_cmdline_key_streq(key, "root")) { |
1d84ad94 LP |
614 | |
615 | if (proc_cmdline_value_missing(key, value)) | |
616 | return 0; | |
73b80ec2 LP |
617 | |
618 | /* Disable root disk logic if there's a root= value | |
619 | * specified (unless it happens to be "gpt-auto") */ | |
620 | ||
621 | arg_root_enabled = streq(value, "gpt-auto"); | |
622 | ||
8a9c44ed | 623 | } else if (proc_cmdline_key_streq(key, "roothash")) { |
2f3dfc6f LP |
624 | |
625 | if (proc_cmdline_value_missing(key, value)) | |
626 | return 0; | |
627 | ||
628 | /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */ | |
629 | ||
630 | arg_root_enabled = false; | |
631 | ||
8a9c44ed | 632 | } else if (proc_cmdline_key_streq(key, "rw") && !value) |
73b80ec2 | 633 | arg_root_rw = true; |
8a9c44ed | 634 | else if (proc_cmdline_key_streq(key, "ro") && !value) |
73b80ec2 | 635 | arg_root_rw = false; |
73b80ec2 LP |
636 | |
637 | return 0; | |
638 | } | |
639 | ||
349cc4a5 | 640 | #if ENABLE_EFI |
01af8c01 LP |
641 | static int add_root_cryptsetup(void) { |
642 | ||
643 | /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which | |
644 | * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */ | |
645 | ||
646 | return add_cryptsetup("root", "/dev/gpt-auto-root-luks", true, false, NULL); | |
647 | } | |
648 | #endif | |
649 | ||
73b80ec2 LP |
650 | static int add_root_mount(void) { |
651 | ||
349cc4a5 | 652 | #if ENABLE_EFI |
73b80ec2 LP |
653 | int r; |
654 | ||
655 | if (!is_efi_boot()) { | |
656 | log_debug("Not a EFI boot, not creating root mount."); | |
657 | return 0; | |
9a5cb137 ZJS |
658 | } |
659 | ||
73b80ec2 LP |
660 | r = efi_loader_get_device_part_uuid(NULL); |
661 | if (r == -ENOENT) { | |
662 | log_debug("EFI loader partition unknown, exiting."); | |
663 | return 0; | |
23bbb0de MS |
664 | } else if (r < 0) |
665 | return log_error_errno(r, "Failed to read ESP partition UUID: %m"); | |
1a14a53c | 666 | |
73b80ec2 LP |
667 | /* OK, we have an ESP partition, this is fantastic, so let's |
668 | * wait for a root device to show up. A udev rule will create | |
669 | * the link for us under the right name. */ | |
670 | ||
7163e1ca DD |
671 | if (in_initrd()) { |
672 | r = generator_write_initrd_root_device_deps(arg_dest, "/dev/gpt-auto-root"); | |
673 | if (r < 0) | |
674 | return 0; | |
01af8c01 LP |
675 | |
676 | r = add_root_cryptsetup(); | |
677 | if (r < 0) | |
678 | return r; | |
7163e1ca DD |
679 | } |
680 | ||
9b69569d ZJS |
681 | /* Note that we do not need to enable systemd-remount-fs.service here. If |
682 | * /etc/fstab exists, systemd-fstab-generator will pull it in for us. */ | |
683 | ||
73b80ec2 LP |
684 | return add_mount( |
685 | "root", | |
98b2f766 | 686 | "/dev/gpt-auto-root", |
73b80ec2 LP |
687 | in_initrd() ? "/sysroot" : "/", |
688 | NULL, | |
c94b2417 | 689 | arg_root_rw > 0, |
59512f21 | 690 | NULL, |
73b80ec2 LP |
691 | "Root Partition", |
692 | in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_LOCAL_FS_TARGET); | |
693 | #else | |
694 | return 0; | |
695 | #endif | |
696 | } | |
697 | ||
698 | static int add_mounts(void) { | |
73b80ec2 LP |
699 | dev_t devno; |
700 | int r; | |
701 | ||
c6ba0c18 | 702 | r = get_block_device_harder("/", &devno); |
23bbb0de MS |
703 | if (r < 0) |
704 | return log_error_errno(r, "Failed to determine block device of root file system: %m"); | |
57ab9f89 | 705 | if (r == 0) { |
c6ba0c18 | 706 | r = get_block_device_harder("/usr", &devno); |
eafe88e3 TH |
707 | if (r < 0) |
708 | return log_error_errno(r, "Failed to determine block device of /usr file system: %m"); | |
57ab9f89 | 709 | if (r == 0) { |
eafe88e3 TH |
710 | log_debug("Neither root nor /usr file system are on a (single) block device."); |
711 | return 0; | |
712 | } | |
3db604b9 LP |
713 | } |
714 | ||
61331eab | 715 | return enumerate_partitions(devno); |
73b80ec2 LP |
716 | } |
717 | ||
ec6e9597 | 718 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
8f50e86a | 719 | int r, k; |
73b80ec2 | 720 | |
ec6e9597 | 721 | assert_se(arg_dest = dest_late); |
73b80ec2 | 722 | |
75f86906 | 723 | if (detect_container() > 0) { |
73b80ec2 | 724 | log_debug("In a container, exiting."); |
ec6e9597 | 725 | return 0; |
1a14a53c | 726 | } |
3db604b9 | 727 | |
1d84ad94 | 728 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); |
b5884878 | 729 | if (r < 0) |
da927ba9 | 730 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
1a14a53c | 731 | |
73b80ec2 LP |
732 | if (!arg_enabled) { |
733 | log_debug("Disabled, exiting."); | |
ec6e9597 | 734 | return 0; |
73b80ec2 LP |
735 | } |
736 | ||
737 | if (arg_root_enabled) | |
738 | r = add_root_mount(); | |
739 | ||
740 | if (!in_initrd()) { | |
73b80ec2 | 741 | k = add_mounts(); |
ec6e9597 | 742 | if (r >= 0) |
73b80ec2 LP |
743 | r = k; |
744 | } | |
745 | ||
ec6e9597 | 746 | return r; |
1a14a53c | 747 | } |
ec6e9597 ZJS |
748 | |
749 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |