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