]>
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; | |
42 | static bool arg_root_rw = false; | |
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 | |
72e18a98 | 449 | static int open_parent(dev_t devnum, int *ret) { |
85624f01 | 450 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
72e18a98 | 451 | const char *name, *devtype, *node; |
85624f01 | 452 | sd_device *parent; |
61331eab | 453 | dev_t pn; |
85624f01 | 454 | int fd, r; |
72e18a98 LP |
455 | |
456 | assert(ret); | |
1a14a53c | 457 | |
85624f01 YW |
458 | r = sd_device_new_from_devnum(&d, 'b', devnum); |
459 | if (r < 0) | |
460 | return log_debug_errno(r, "Failed to open device: %m"); | |
1a14a53c | 461 | |
85624f01 YW |
462 | if (sd_device_get_devname(d, &name) < 0) { |
463 | r = sd_device_get_syspath(d, &name); | |
464 | if (r < 0) { | |
133432cc | 465 | log_device_debug_errno(d, r, "Device %u:%u does not have a name, ignoring: %m", major(devnum), minor(devnum)); |
8090b41e | 466 | return 0; |
85624f01 | 467 | } |
d2a62382 ZJS |
468 | } |
469 | ||
85624f01 YW |
470 | r = sd_device_get_parent(d, &parent); |
471 | if (r < 0) { | |
133432cc | 472 | log_device_debug_errno(d, r, "Not a partitioned device, ignoring: %m"); |
8090b41e | 473 | return 0; |
fa041593 | 474 | } |
1a14a53c | 475 | |
61331eab | 476 | /* Does it have a devtype? */ |
85624f01 YW |
477 | r = sd_device_get_devtype(parent, &devtype); |
478 | if (r < 0) { | |
133432cc | 479 | log_device_debug_errno(parent, r, "Parent doesn't have a device type, ignoring: %m"); |
8090b41e | 480 | return 0; |
fa041593 | 481 | } |
61331eab LP |
482 | |
483 | /* Is this a disk or a partition? We only care for disks... */ | |
fa041593 | 484 | if (!streq(devtype, "disk")) { |
133432cc | 485 | log_device_debug(parent, "Parent isn't a raw disk, ignoring."); |
8090b41e | 486 | return 0; |
fa041593 | 487 | } |
61331eab LP |
488 | |
489 | /* Does it have a device node? */ | |
85624f01 YW |
490 | r = sd_device_get_devname(parent, &node); |
491 | if (r < 0) { | |
133432cc | 492 | log_device_debug_errno(parent, r, "Parent device does not have device node, ignoring: %m"); |
8090b41e | 493 | return 0; |
fa041593 | 494 | } |
61331eab | 495 | |
133432cc | 496 | log_device_debug(d, "Root device %s.", node); |
61331eab | 497 | |
85624f01 YW |
498 | r = sd_device_get_devnum(parent, &pn); |
499 | if (r < 0) { | |
133432cc | 500 | log_device_debug_errno(parent, r, "Parent device is not a proper block device, ignoring: %m"); |
8090b41e | 501 | return 0; |
61331eab LP |
502 | } |
503 | ||
72e18a98 LP |
504 | fd = open(node, O_RDONLY|O_CLOEXEC|O_NOCTTY); |
505 | if (fd < 0) | |
506 | return log_error_errno(errno, "Failed to open %s: %m", node); | |
61331eab | 507 | |
72e18a98 LP |
508 | *ret = fd; |
509 | return 1; | |
72e18a98 | 510 | } |
cb971249 | 511 | |
72e18a98 | 512 | static int enumerate_partitions(dev_t devnum) { |
72e18a98 LP |
513 | _cleanup_close_ int fd = -1; |
514 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
515 | int r, k; | |
61331eab | 516 | |
72e18a98 LP |
517 | r = open_parent(devnum, &fd); |
518 | if (r <= 0) | |
519 | return r; | |
61331eab | 520 | |
72e18a98 LP |
521 | r = dissect_image(fd, NULL, 0, DISSECT_IMAGE_GPT_ONLY, &m); |
522 | if (r == -ENOPKG) { | |
523 | log_debug_errno(r, "No suitable partition table found, ignoring."); | |
524 | return 0; | |
61331eab | 525 | } |
23bbb0de | 526 | if (r < 0) |
72e18a98 | 527 | return log_error_errno(r, "Failed to dissect: %m"); |
0238d4c6 | 528 | |
72e18a98 LP |
529 | if (m->partitions[PARTITION_SWAP].found) { |
530 | k = add_swap(m->partitions[PARTITION_SWAP].node); | |
531 | if (k < 0) | |
532 | r = k; | |
1a14a53c LP |
533 | } |
534 | ||
72e18a98 LP |
535 | if (m->partitions[PARTITION_ESP].found) { |
536 | k = add_esp(m->partitions + PARTITION_ESP); | |
59512f21 KS |
537 | if (k < 0) |
538 | r = k; | |
539 | } | |
540 | ||
72e18a98 LP |
541 | if (m->partitions[PARTITION_HOME].found) { |
542 | k = add_partition_mount(m->partitions + PARTITION_HOME, "home", "/home", "Home Partition"); | |
73b80ec2 LP |
543 | if (k < 0) |
544 | r = k; | |
545 | } | |
e48fdd84 | 546 | |
72e18a98 LP |
547 | if (m->partitions[PARTITION_SRV].found) { |
548 | k = add_partition_mount(m->partitions + PARTITION_SRV, "srv", "/srv", "Server Data Partition"); | |
73b80ec2 LP |
549 | if (k < 0) |
550 | r = k; | |
551 | } | |
1a14a53c | 552 | |
1a14a53c LP |
553 | return r; |
554 | } | |
555 | ||
96287a49 | 556 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
73b80ec2 | 557 | int r; |
1a14a53c | 558 | |
73b80ec2 | 559 | assert(key); |
1a14a53c | 560 | |
8a9c44ed LP |
561 | if (proc_cmdline_key_streq(key, "systemd.gpt_auto") || |
562 | proc_cmdline_key_streq(key, "rd.systemd.gpt_auto")) { | |
1a14a53c | 563 | |
1d84ad94 | 564 | r = value ? parse_boolean(value) : 1; |
73b80ec2 | 565 | if (r < 0) |
0a1b9449 | 566 | log_warning_errno(r, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value); |
8086ffac ZJS |
567 | else |
568 | arg_enabled = r; | |
1a14a53c | 569 | |
8a9c44ed | 570 | } else if (proc_cmdline_key_streq(key, "root")) { |
1d84ad94 LP |
571 | |
572 | if (proc_cmdline_value_missing(key, value)) | |
573 | return 0; | |
73b80ec2 LP |
574 | |
575 | /* Disable root disk logic if there's a root= value | |
576 | * specified (unless it happens to be "gpt-auto") */ | |
577 | ||
578 | arg_root_enabled = streq(value, "gpt-auto"); | |
579 | ||
8a9c44ed | 580 | } else if (proc_cmdline_key_streq(key, "roothash")) { |
2f3dfc6f LP |
581 | |
582 | if (proc_cmdline_value_missing(key, value)) | |
583 | return 0; | |
584 | ||
585 | /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */ | |
586 | ||
587 | arg_root_enabled = false; | |
588 | ||
8a9c44ed | 589 | } else if (proc_cmdline_key_streq(key, "rw") && !value) |
73b80ec2 | 590 | arg_root_rw = true; |
8a9c44ed | 591 | else if (proc_cmdline_key_streq(key, "ro") && !value) |
73b80ec2 | 592 | arg_root_rw = false; |
73b80ec2 LP |
593 | |
594 | return 0; | |
595 | } | |
596 | ||
349cc4a5 | 597 | #if ENABLE_EFI |
01af8c01 LP |
598 | static int add_root_cryptsetup(void) { |
599 | ||
600 | /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which | |
601 | * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */ | |
602 | ||
603 | return add_cryptsetup("root", "/dev/gpt-auto-root-luks", true, false, NULL); | |
604 | } | |
605 | #endif | |
606 | ||
73b80ec2 LP |
607 | static int add_root_mount(void) { |
608 | ||
349cc4a5 | 609 | #if ENABLE_EFI |
73b80ec2 LP |
610 | int r; |
611 | ||
612 | if (!is_efi_boot()) { | |
613 | log_debug("Not a EFI boot, not creating root mount."); | |
614 | return 0; | |
9a5cb137 ZJS |
615 | } |
616 | ||
73b80ec2 LP |
617 | r = efi_loader_get_device_part_uuid(NULL); |
618 | if (r == -ENOENT) { | |
619 | log_debug("EFI loader partition unknown, exiting."); | |
620 | return 0; | |
23bbb0de MS |
621 | } else if (r < 0) |
622 | return log_error_errno(r, "Failed to read ESP partition UUID: %m"); | |
1a14a53c | 623 | |
73b80ec2 LP |
624 | /* OK, we have an ESP partition, this is fantastic, so let's |
625 | * wait for a root device to show up. A udev rule will create | |
626 | * the link for us under the right name. */ | |
627 | ||
7163e1ca DD |
628 | if (in_initrd()) { |
629 | r = generator_write_initrd_root_device_deps(arg_dest, "/dev/gpt-auto-root"); | |
630 | if (r < 0) | |
631 | return 0; | |
01af8c01 LP |
632 | |
633 | r = add_root_cryptsetup(); | |
634 | if (r < 0) | |
635 | return r; | |
7163e1ca DD |
636 | } |
637 | ||
73b80ec2 LP |
638 | return add_mount( |
639 | "root", | |
98b2f766 | 640 | "/dev/gpt-auto-root", |
73b80ec2 LP |
641 | in_initrd() ? "/sysroot" : "/", |
642 | NULL, | |
cca1dfdd | 643 | arg_root_rw, |
59512f21 | 644 | NULL, |
73b80ec2 LP |
645 | "Root Partition", |
646 | in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_LOCAL_FS_TARGET); | |
647 | #else | |
648 | return 0; | |
649 | #endif | |
650 | } | |
651 | ||
652 | static int add_mounts(void) { | |
73b80ec2 LP |
653 | dev_t devno; |
654 | int r; | |
655 | ||
c6ba0c18 | 656 | r = get_block_device_harder("/", &devno); |
23bbb0de MS |
657 | if (r < 0) |
658 | return log_error_errno(r, "Failed to determine block device of root file system: %m"); | |
57ab9f89 | 659 | if (r == 0) { |
c6ba0c18 | 660 | r = get_block_device_harder("/usr", &devno); |
eafe88e3 TH |
661 | if (r < 0) |
662 | return log_error_errno(r, "Failed to determine block device of /usr file system: %m"); | |
57ab9f89 | 663 | if (r == 0) { |
eafe88e3 TH |
664 | log_debug("Neither root nor /usr file system are on a (single) block device."); |
665 | return 0; | |
666 | } | |
3db604b9 LP |
667 | } |
668 | ||
61331eab | 669 | return enumerate_partitions(devno); |
73b80ec2 LP |
670 | } |
671 | ||
ec6e9597 | 672 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
8f50e86a | 673 | int r, k; |
73b80ec2 | 674 | |
ec6e9597 | 675 | assert_se(arg_dest = dest_late); |
73b80ec2 | 676 | |
75f86906 | 677 | if (detect_container() > 0) { |
73b80ec2 | 678 | log_debug("In a container, exiting."); |
ec6e9597 | 679 | return 0; |
1a14a53c | 680 | } |
3db604b9 | 681 | |
1d84ad94 | 682 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0); |
b5884878 | 683 | if (r < 0) |
da927ba9 | 684 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
1a14a53c | 685 | |
73b80ec2 LP |
686 | if (!arg_enabled) { |
687 | log_debug("Disabled, exiting."); | |
ec6e9597 | 688 | return 0; |
73b80ec2 LP |
689 | } |
690 | ||
691 | if (arg_root_enabled) | |
692 | r = add_root_mount(); | |
693 | ||
694 | if (!in_initrd()) { | |
73b80ec2 | 695 | k = add_mounts(); |
ec6e9597 | 696 | if (r >= 0) |
73b80ec2 LP |
697 | r = k; |
698 | } | |
699 | ||
ec6e9597 | 700 | return r; |
1a14a53c | 701 | } |
ec6e9597 ZJS |
702 | |
703 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |