]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/gpt-auto-generator/gpt-auto-generator.c
test-loop-block: reenable test on CI
[thirdparty/systemd.git] / src / gpt-auto-generator / gpt-auto-generator.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
1a14a53c 2
1a14a53c 3#include <stdlib.h>
cf0fbc49 4#include <unistd.h>
1a14a53c 5
85624f01 6#include "sd-device.h"
07630cea
LP
7#include "sd-id128.h"
8
b5efdb8a 9#include "alloc-util.h"
07630cea 10#include "blkid-util.h"
18c528e9 11#include "blockdev-util.h"
07630cea 12#include "btrfs-util.h"
133432cc 13#include "device-util.h"
a0956174 14#include "dirent-util.h"
72e18a98 15#include "dissect-image.h"
1fac34b9 16#include "dropin.h"
0bb2f0f1 17#include "efi-loader.h"
3ffd4af2 18#include "fd-util.h"
07630cea 19#include "fileio.h"
2bef2582 20#include "fs-util.h"
07630cea
LP
21#include "fstab-util.h"
22#include "generator.h"
23#include "gpt.h"
07630cea 24#include "mkdir.h"
049af8ad 25#include "mountpoint-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
ec6e9597 38static const char *arg_dest = NULL;
73b80ec2
LP
39static bool arg_enabled = true;
40static bool arg_root_enabled = true;
c94b2417 41static int arg_root_rw = -1;
1a14a53c 42
3d92aa45 43static int open_parent_block_device(dev_t devnum, int *ret_fd) {
9fe6f5cc
ZJS
44 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
45 const char *name, *devtype, *node;
46 sd_device *parent;
47 dev_t pn;
48 int fd, r;
49
3d92aa45 50 assert(ret_fd);
9fe6f5cc
ZJS
51
52 r = sd_device_new_from_devnum(&d, 'b', devnum);
53 if (r < 0)
54 return log_debug_errno(r, "Failed to open device: %m");
55
56 if (sd_device_get_devname(d, &name) < 0) {
57 r = sd_device_get_syspath(d, &name);
58 if (r < 0) {
3d92aa45
ZJS
59 log_device_debug_errno(d, r, "Device %u:%u does not have a name, ignoring: %m",
60 major(devnum), minor(devnum));
9fe6f5cc
ZJS
61 return 0;
62 }
63 }
64
65 r = sd_device_get_parent(d, &parent);
66 if (r < 0) {
67 log_device_debug_errno(d, r, "Not a partitioned device, ignoring: %m");
68 return 0;
69 }
70
71 /* Does it have a devtype? */
72 r = sd_device_get_devtype(parent, &devtype);
73 if (r < 0) {
74 log_device_debug_errno(parent, r, "Parent doesn't have a device type, ignoring: %m");
75 return 0;
76 }
77
78 /* Is this a disk or a partition? We only care for disks... */
79 if (!streq(devtype, "disk")) {
80 log_device_debug(parent, "Parent isn't a raw disk, ignoring.");
81 return 0;
82 }
83
84 /* Does it have a device node? */
85 r = sd_device_get_devname(parent, &node);
86 if (r < 0) {
87 log_device_debug_errno(parent, r, "Parent device does not have device node, ignoring: %m");
88 return 0;
89 }
90
91 log_device_debug(d, "Root device %s.", node);
92
93 r = sd_device_get_devnum(parent, &pn);
94 if (r < 0) {
95 log_device_debug_errno(parent, r, "Parent device is not a proper block device, ignoring: %m");
96 return 0;
97 }
98
99 fd = open(node, O_RDONLY|O_CLOEXEC|O_NOCTTY);
100 if (fd < 0)
101 return log_error_errno(errno, "Failed to open %s: %m", node);
102
3d92aa45 103 *ret_fd = fd;
9fe6f5cc
ZJS
104 return 1;
105}
106
01af8c01 107static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) {
5b137503 108#if HAVE_LIBCRYPTSETUP
a7e88558 109 _cleanup_free_ char *e = NULL, *n = NULL, *d = NULL;
1af72119 110 _cleanup_fclose_ FILE *f = NULL;
1af72119
LP
111 int r;
112
113 assert(id);
114 assert(what);
1af72119 115
7410616c
LP
116 r = unit_name_from_path(what, ".device", &d);
117 if (r < 0)
118 return log_error_errno(r, "Failed to generate unit name: %m");
1af72119
LP
119
120 e = unit_name_escape(id);
121 if (!e)
122 return log_oom();
123
7410616c
LP
124 r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
125 if (r < 0)
126 return log_error_errno(r, "Failed to generate unit name: %m");
1af72119 127
a7e88558
LP
128 r = generator_open_unit_file(arg_dest, NULL, n, &f);
129 if (r < 0)
130 return r;
98bad05e 131
a7e88558
LP
132 r = generator_write_cryptsetup_unit_section(f, NULL);
133 if (r < 0)
134 return r;
1af72119
LP
135
136 fprintf(f,
1af72119 137 "Before=umount.target cryptsetup.target\n"
a7e88558
LP
138 "Conflicts=umount.target\n"
139 "BindsTo=%s\n"
140 "After=%s\n",
141 d, d);
142
143 r = generator_write_cryptsetup_service_section(f, id, what, NULL, rw ? NULL : "read-only");
144 if (r < 0)
145 return r;
1af72119 146
dacd6cee
LP
147 r = fflush_and_check(f);
148 if (r < 0)
a7e88558 149 return log_error_errno(r, "Failed to write file %s: %m", n);
1af72119 150
9cdcf368
ZJS
151 r = generator_add_symlink(arg_dest, d, "wants", n);
152 if (r < 0)
153 return r;
1af72119 154
1fac34b9
ZJS
155 const char *dmname;
156 dmname = strjoina("dev-mapper-", e, ".device");
1af72119 157
1fac34b9 158 if (require) {
9cdcf368
ZJS
159 r = generator_add_symlink(arg_dest, "cryptsetup.target", "requires", n);
160 if (r < 0)
161 return r;
01af8c01 162
9cdcf368
ZJS
163 r = generator_add_symlink(arg_dest, dmname, "requires", n);
164 if (r < 0)
165 return r;
01af8c01 166 }
1af72119 167
1fac34b9
ZJS
168 r = write_drop_in_format(arg_dest, dmname, 50, "job-timeout",
169 "# Automatically generated by systemd-gpt-auto-generator\n\n"
170 "[Unit]\n"
171 "JobTimeoutSec=0"); /* the binary handles timeouts anyway */
23bbb0de 172 if (r < 0)
1fac34b9 173 log_warning_errno(r, "Failed to write device timeout drop-in, ignoring: %m");
1af72119 174
2aa2860b
ZJS
175 if (device) {
176 char *ret;
177
b910cc72 178 ret = path_join("/dev/mapper", id);
2aa2860b
ZJS
179 if (!ret)
180 return log_oom();
1af72119 181
01af8c01 182 *device = ret;
2aa2860b
ZJS
183 }
184
1af72119 185 return 0;
5b137503
YG
186#else
187 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Partition is encrypted, but the project was compiled without libcryptsetup support");
188#endif
1af72119
LP
189}
190
73b80ec2
LP
191static int add_mount(
192 const char *id,
193 const char *what,
194 const char *where,
195 const char *fstype,
cca1dfdd 196 bool rw,
400c1e8f 197 bool growfs,
59512f21 198 const char *options,
73b80ec2
LP
199 const char *description,
200 const char *post) {
201
9cdcf368 202 _cleanup_free_ char *unit = NULL, *crypto_what = NULL, *p = NULL;
1a14a53c 203 _cleanup_fclose_ FILE *f = NULL;
e48fdd84 204 int r;
1a14a53c 205
98bad05e
LP
206 /* Note that we don't apply specifier escaping on the input strings here, since we know they are not configured
207 * externally, but all originate from our own sources here, and hence we know they contain no % characters that
208 * could potentially be understood as specifiers. */
209
1af72119
LP
210 assert(id);
211 assert(what);
212 assert(where);
1af72119
LP
213 assert(description);
214
074cdb95 215 log_debug("Adding %s: %s fstype=%s", where, what, fstype ?: "(any)");
1a14a53c 216
73b80ec2 217 if (streq_ptr(fstype, "crypto_LUKS")) {
01af8c01 218 r = add_cryptsetup(id, what, rw, true, &crypto_what);
1af72119
LP
219 if (r < 0)
220 return r;
221
222 what = crypto_what;
223 fstype = NULL;
224 }
225
7410616c
LP
226 r = unit_name_from_path(where, ".mount", &unit);
227 if (r < 0)
228 return log_error_errno(r, "Failed to generate unit name: %m");
1a14a53c 229
657ee2d8 230 p = path_join(empty_to_root(arg_dest), unit);
e48fdd84
LP
231 if (!p)
232 return log_oom();
233
234 f = fopen(p, "wxe");
4a62c710
MS
235 if (!f)
236 return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
1a14a53c
LP
237
238 fprintf(f,
239 "# Automatically generated by systemd-gpt-auto-generator\n\n"
240 "[Unit]\n"
c3834f9b
LP
241 "Description=%s\n"
242 "Documentation=man:systemd-gpt-auto-generator(8)\n",
e48fdd84
LP
243 description);
244
73b80ec2
LP
245 if (post)
246 fprintf(f, "Before=%s\n", post);
247
e48fdd84
LP
248 r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
249 if (r < 0)
250 return r;
251
a7e88558
LP
252 r = generator_write_blockdev_dependency(f, what);
253 if (r < 0)
254 return r;
255
e48fdd84
LP
256 fprintf(f,
257 "\n"
1a14a53c
LP
258 "[Mount]\n"
259 "What=%s\n"
1af72119
LP
260 "Where=%s\n",
261 what, where);
262
73b80ec2
LP
263 if (fstype)
264 fprintf(f, "Type=%s\n", fstype);
265
59512f21
KS
266 if (options)
267 fprintf(f, "Options=%s,%s\n", options, rw ? "rw" : "ro");
268 else
269 fprintf(f, "Options=%s\n", rw ? "rw" : "ro");
1a14a53c 270
dacd6cee
LP
271 r = fflush_and_check(f);
272 if (r < 0)
273 return log_error_errno(r, "Failed to write unit file %s: %m", p);
1a14a53c 274
400c1e8f
LP
275 if (growfs) {
276 r = generator_hook_up_growfs(arg_dest, where, post);
277 if (r < 0)
278 return r;
279 }
280
281 if (post) {
282 r = generator_add_symlink(arg_dest, post, "requires", unit);
283 if (r < 0)
284 return r;
285 }
286
1a14a53c
LP
287 return 0;
288}
289
e137880b 290static int path_is_busy(const char *where) {
59512f21
KS
291 int r;
292
293 /* already a mountpoint; generators run during reload */
e1873695 294 r = path_is_mount_point(where, NULL, AT_SYMLINK_FOLLOW);
59512f21
KS
295 if (r > 0)
296 return false;
297
298 /* the directory might not exist on a stateless system */
299 if (r == -ENOENT)
300 return false;
301
302 if (r < 0)
e137880b 303 return log_warning_errno(r, "Cannot check if \"%s\" is a mount point: %m", where);
59512f21
KS
304
305 /* not a mountpoint but it contains files */
e137880b
ZJS
306 r = dir_is_empty(where);
307 if (r < 0)
308 return log_warning_errno(r, "Cannot check if \"%s\" is empty: %m", where);
309 if (r > 0)
310 return false;
59512f21 311
e137880b
ZJS
312 log_debug("\"%s\" already populated, ignoring.", where);
313 return true;
59512f21
KS
314}
315
72e18a98
LP
316static int add_partition_mount(
317 DissectedPartition *p,
61331eab 318 const char *id,
61331eab 319 const char *where,
72e18a98 320 const char *description) {
61331eab 321
e137880b 322 int r;
72e18a98 323 assert(p);
61331eab 324
e137880b
ZJS
325 r = path_is_busy(where);
326 if (r != 0)
327 return r < 0 ? r : 0;
61331eab 328
61331eab
LP
329 return add_mount(
330 id,
72e18a98 331 p->node,
61331eab 332 where,
72e18a98
LP
333 p->fstype,
334 p->rw,
400c1e8f 335 p->growfs,
59512f21 336 NULL,
61331eab 337 description,
72e18a98 338 SPECIAL_LOCAL_FS_TARGET);
61331eab
LP
339}
340
8859b8f7
HOB
341static int add_swap(DissectedPartition *p) {
342 const char *what;
343 _cleanup_free_ char *name = NULL, *unit = NULL, *crypto_what = NULL;
59512f21
KS
344 _cleanup_fclose_ FILE *f = NULL;
345 int r;
346
8859b8f7
HOB
347 assert(p);
348 assert(p->node);
59512f21 349
fc5bc384
FB
350 /* Disable the swap auto logic if at least one swap is defined in /etc/fstab, see #6192. */
351 r = fstab_has_fstype("swap");
352 if (r < 0)
353 return log_error_errno(r, "Failed to parse fstab: %m");
1a680ae3 354 if (r > 0) {
fc5bc384
FB
355 log_debug("swap specified in fstab, ignoring.");
356 return 0;
357 }
358
8859b8f7
HOB
359 if (streq_ptr(p->fstype, "crypto_LUKS")) {
360 r = add_cryptsetup("swap", p->node, true, true, &crypto_what);
361 if (r < 0)
362 return r;
363 what = crypto_what;
364 } else
365 what = p->node;
366
367 log_debug("Adding swap: %s", what);
59512f21 368
8859b8f7 369 r = unit_name_from_path(what, ".swap", &name);
59512f21
KS
370 if (r < 0)
371 return log_error_errno(r, "Failed to generate unit name: %m");
372
657ee2d8 373 unit = path_join(empty_to_root(arg_dest), name);
59512f21
KS
374 if (!unit)
375 return log_oom();
376
377 f = fopen(unit, "wxe");
378 if (!f)
379 return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
380
381 fprintf(f,
382 "# Automatically generated by systemd-gpt-auto-generator\n\n"
383 "[Unit]\n"
384 "Description=Swap Partition\n"
a7e88558
LP
385 "Documentation=man:systemd-gpt-auto-generator(8)\n");
386
8859b8f7 387 r = generator_write_blockdev_dependency(f, what);
a7e88558
LP
388 if (r < 0)
389 return r;
390
391 fprintf(f,
392 "\n"
59512f21
KS
393 "[Swap]\n"
394 "What=%s\n",
8859b8f7 395 what);
59512f21 396
dacd6cee
LP
397 r = fflush_and_check(f);
398 if (r < 0)
399 return log_error_errno(r, "Failed to write unit file %s: %m", unit);
59512f21 400
9cdcf368 401 return generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, "wants", name);
59512f21
KS
402}
403
7a1494aa
TG
404static int add_automount(
405 const char *id,
406 const char *what,
407 const char *where,
408 const char *fstype,
409 bool rw,
400c1e8f 410 bool growfs,
7a1494aa
TG
411 const char *options,
412 const char *description,
413 usec_t timeout) {
414
9cdcf368 415 _cleanup_free_ char *unit = NULL;
7a1494aa 416 _cleanup_fclose_ FILE *f = NULL;
2aa2860b 417 const char *opt = "noauto", *p;
7a1494aa
TG
418 int r;
419
420 assert(id);
421 assert(where);
422 assert(description);
423
424 if (options)
2aa2860b 425 opt = strjoina(options, ",", opt);
7a1494aa
TG
426
427 r = add_mount(id,
428 what,
429 where,
430 fstype,
431 rw,
400c1e8f 432 growfs,
7a1494aa
TG
433 opt,
434 description,
435 NULL);
436 if (r < 0)
437 return r;
438
439 r = unit_name_from_path(where, ".automount", &unit);
440 if (r < 0)
441 return log_error_errno(r, "Failed to generate unit name: %m");
442
270384b2 443 p = prefix_roota(arg_dest, unit);
7a1494aa
TG
444 f = fopen(p, "wxe");
445 if (!f)
446 return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
447
448 fprintf(f,
449 "# Automatically generated by systemd-gpt-auto-generator\n\n"
450 "[Unit]\n"
451 "Description=%s\n"
452 "Documentation=man:systemd-gpt-auto-generator(8)\n"
453 "[Automount]\n"
454 "Where=%s\n"
70887c5f 455 "TimeoutIdleSec="USEC_FMT"\n",
7a1494aa
TG
456 description,
457 where,
70887c5f 458 timeout / USEC_PER_SEC);
7a1494aa
TG
459
460 r = fflush_and_check(f);
461 if (r < 0)
462 return log_error_errno(r, "Failed to write unit file %s: %m", p);
463
9cdcf368 464 return generator_add_symlink(arg_dest, SPECIAL_LOCAL_FS_TARGET, "wants", unit);
7a1494aa
TG
465}
466
4f084066
LP
467static const char *esp_or_xbootldr_options(const DissectedPartition *p) {
468 assert(p);
469
470 /* if we probed vfat or have no idea about the file system then assume these file systems are vfat
471 * and thus understand "umask=0077". If we detected something else then don't specify any options and
472 * use kernel defaults. */
473
474 if (!p->fstype || streq(p->fstype, "vfat"))
475 return "umask=0077";
476
477 return NULL;
478}
479
9f1cb0c1
LP
480static int add_xbootldr(DissectedPartition *p) {
481 int r;
482
483 assert(p);
484
485 if (in_initrd()) {
486 log_debug("In initrd, ignoring the XBOOTLDR partition.");
487 return 0;
488 }
489
490 r = fstab_is_mount_point("/boot");
491 if (r < 0)
492 return log_error_errno(r, "Failed to parse fstab: %m");
493 if (r > 0) {
494 log_debug("/boot specified in fstab, ignoring XBOOTLDR partition.");
495 return 0;
496 }
497
498 r = path_is_busy("/boot");
499 if (r < 0)
500 return r;
501 if (r > 0)
502 return 0;
503
504 return add_automount("boot",
505 p->node,
506 "/boot",
507 p->fstype,
400c1e8f
LP
508 /* rw= */ true,
509 /* growfs= */ false,
4f084066 510 esp_or_xbootldr_options(p),
9f1cb0c1
LP
511 "Boot Loader Partition",
512 120 * USEC_PER_SEC);
513}
514
515#if ENABLE_EFI
516static int add_esp(DissectedPartition *p, bool has_xbootldr) {
517 const char *esp_path = NULL, *id = NULL;
59512f21
KS
518 int r;
519
72e18a98 520 assert(p);
59512f21 521
59512f21 522 if (in_initrd()) {
b52a109a 523 log_debug("In initrd, ignoring the ESP.");
59512f21
KS
524 return 0;
525 }
526
9f1cb0c1
LP
527 /* If /efi exists we'll use that. Otherwise we'll use /boot, as that's usually the better choice, but
528 * only if there's no explicit XBOOTLDR partition around. */
529 if (access("/efi", F_OK) < 0) {
530 if (errno != ENOENT)
531 return log_error_errno(errno, "Failed to determine whether /efi exists: %m");
532
533 /* Use /boot as fallback, but only if there's no XBOOTLDR partition */
534 if (!has_xbootldr) {
535 esp_path = "/boot";
536 id = "boot";
537 }
538 }
539 if (!esp_path)
540 esp_path = "/efi";
541 if (!id)
542 id = "efi";
59512f21 543
0b6b6787 544 /* We create an .automount which is not overridden by the .mount from the fstab generator. */
9f1cb0c1 545 r = fstab_is_mount_point(esp_path);
b9088048
FB
546 if (r < 0)
547 return log_error_errno(r, "Failed to parse fstab: %m");
39b6a511 548 if (r > 0) {
9f1cb0c1 549 log_debug("%s specified in fstab, ignoring.", esp_path);
59512f21
KS
550 return 0;
551 }
552
9f1cb0c1
LP
553 r = path_is_busy(esp_path);
554 if (r < 0)
555 return r;
556 if (r > 0)
557 return 0;
59512f21 558
7ba25ab5 559 if (is_efi_boot()) {
72e18a98 560 sd_id128_t loader_uuid;
59512f21 561
7ba25ab5 562 /* If this is an EFI boot, be extra careful, and only mount the ESP if it was the ESP used for booting. */
59512f21 563
7ba25ab5
LP
564 r = efi_loader_get_device_part_uuid(&loader_uuid);
565 if (r == -ENOENT) {
566 log_debug("EFI loader partition unknown.");
567 return 0;
568 }
e28973ee
ZJS
569 if (r < 0)
570 return log_error_errno(r, "Failed to read ESP partition UUID: %m");
7ba25ab5 571
72e18a98 572 if (!sd_id128_equal(p->uuid, loader_uuid)) {
9f1cb0c1 573 log_debug("Partition for %s does not appear to be the partition we are booted from.", p->node);
7ba25ab5
LP
574 return 0;
575 }
576 } else
577 log_debug("Not an EFI boot, skipping ESP check.");
578
9f1cb0c1 579 return add_automount(id,
72e18a98 580 p->node,
9f1cb0c1 581 esp_path,
72e18a98 582 p->fstype,
400c1e8f
LP
583 /* rw= */ true,
584 /* growfs= */ false,
4f084066 585 esp_or_xbootldr_options(p),
72e18a98
LP
586 "EFI System Partition Automount",
587 120 * USEC_PER_SEC);
7a1494aa 588}
59512f21 589#else
9f1cb0c1 590static int add_esp(DissectedPartition *p, bool has_xbootldr) {
59512f21 591 return 0;
59512f21 592}
7a1494aa 593#endif
59512f21 594
fd89051e
LP
595static int add_root_rw(DissectedPartition *p) {
596 const char *path;
597 int r;
598
599 assert(p);
600
601 if (in_initrd()) {
602 log_debug("In initrd, not generating drop-in for systemd-remount-fs.service.");
603 return 0;
604 }
605
606 if (arg_root_rw >= 0) {
607 log_debug("Parameter ro/rw specified on kernel command line, not generating drop-in for systemd-remount-fs.service.");
608 return 0;
609 }
610
611 if (!p->rw) {
612 log_debug("Root partition marked read-only in GPT partition table, not generating drop-in for systemd-remount-fs.service.");
613 return 0;
614 }
615
9b69569d
ZJS
616 (void) generator_enable_remount_fs_service(arg_dest);
617
fd89051e 618 path = strjoina(arg_dest, "/systemd-remount-fs.service.d/50-remount-rw.conf");
fd89051e
LP
619
620 r = write_string_file(path,
621 "# Automatically generated by systemd-gpt-generator\n\n"
fd89051e
LP
622 "[Service]\n"
623 "Environment=SYSTEMD_REMOUNT_ROOT_RW=1\n",
e82e549f 624 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_NOFOLLOW|WRITE_STRING_FILE_MKDIR_0755);
fd89051e
LP
625 if (r < 0)
626 return log_error_errno(r, "Failed to write drop-in file %s: %m", path);
627
628 return 0;
629}
630
9fe6f5cc
ZJS
631#if ENABLE_EFI
632static int add_root_cryptsetup(void) {
1a14a53c 633
9fe6f5cc
ZJS
634 /* If a device /dev/gpt-auto-root-luks appears, then make it pull in systemd-cryptsetup-root.service, which
635 * sets it up, and causes /dev/gpt-auto-root to appear which is all we are looking for. */
1a14a53c 636
9fe6f5cc
ZJS
637 return add_cryptsetup("root", "/dev/gpt-auto-root-luks", true, false, NULL);
638}
639#endif
d2a62382 640
9fe6f5cc
ZJS
641static int add_root_mount(void) {
642#if ENABLE_EFI
643 int r;
1a14a53c 644
9fe6f5cc 645 if (!is_efi_boot()) {
387f6955 646 log_debug("Not an EFI boot, not creating root mount.");
8090b41e 647 return 0;
fa041593 648 }
61331eab 649
9fe6f5cc
ZJS
650 r = efi_loader_get_device_part_uuid(NULL);
651 if (r == -ENOENT) {
b50a3a15
ZJS
652 log_notice("EFI loader partition unknown, exiting.\n"
653 "(The boot loader did not set EFI variable LoaderDevicePartUUID.)");
8090b41e 654 return 0;
9fe6f5cc
ZJS
655 } else if (r < 0)
656 return log_error_errno(r, "Failed to read ESP partition UUID: %m");
61331eab 657
9fe6f5cc
ZJS
658 /* OK, we have an ESP partition, this is fantastic, so let's
659 * wait for a root device to show up. A udev rule will create
660 * the link for us under the right name. */
61331eab 661
9fe6f5cc
ZJS
662 if (in_initrd()) {
663 r = generator_write_initrd_root_device_deps(arg_dest, "/dev/gpt-auto-root");
664 if (r < 0)
665 return 0;
61331eab 666
9fe6f5cc
ZJS
667 r = add_root_cryptsetup();
668 if (r < 0)
669 return r;
61331eab
LP
670 }
671
9fe6f5cc
ZJS
672 /* Note that we do not need to enable systemd-remount-fs.service here. If
673 * /etc/fstab exists, systemd-fstab-generator will pull it in for us. */
61331eab 674
9fe6f5cc
ZJS
675 return add_mount(
676 "root",
677 "/dev/gpt-auto-root",
678 in_initrd() ? "/sysroot" : "/",
679 NULL,
400c1e8f
LP
680 /* rw= */ arg_root_rw > 0,
681 /* growfs= */ false,
9fe6f5cc
ZJS
682 NULL,
683 "Root Partition",
684 in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_LOCAL_FS_TARGET);
685#else
686 return 0;
687#endif
72e18a98 688}
cb971249 689
72e18a98 690static int enumerate_partitions(dev_t devnum) {
72e18a98
LP
691 _cleanup_close_ int fd = -1;
692 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
693 int r, k;
61331eab 694
3d92aa45 695 r = open_parent_block_device(devnum, &fd);
72e18a98
LP
696 if (r <= 0)
697 return r;
61331eab 698
d04faa4e
LP
699 r = dissect_image(
700 fd,
701 NULL, NULL,
a3642997 702 /* diskseq= */ 0,
75dc190d 703 UINT64_MAX,
4a62257d 704 USEC_INFINITY,
d04faa4e 705 DISSECT_IMAGE_GPT_ONLY|
d04faa4e
LP
706 DISSECT_IMAGE_USR_NO_ROOT,
707 &m);
72e18a98
LP
708 if (r == -ENOPKG) {
709 log_debug_errno(r, "No suitable partition table found, ignoring.");
710 return 0;
61331eab 711 }
23bbb0de 712 if (r < 0)
72e18a98 713 return log_error_errno(r, "Failed to dissect: %m");
0238d4c6 714
72e18a98 715 if (m->partitions[PARTITION_SWAP].found) {
8859b8f7 716 k = add_swap(m->partitions + PARTITION_SWAP);
72e18a98
LP
717 if (k < 0)
718 r = k;
1a14a53c
LP
719 }
720
9f1cb0c1
LP
721 if (m->partitions[PARTITION_XBOOTLDR].found) {
722 k = add_xbootldr(m->partitions + PARTITION_XBOOTLDR);
723 if (k < 0)
724 r = k;
725 }
726
72e18a98 727 if (m->partitions[PARTITION_ESP].found) {
9f1cb0c1 728 k = add_esp(m->partitions + PARTITION_ESP, m->partitions[PARTITION_XBOOTLDR].found);
59512f21
KS
729 if (k < 0)
730 r = k;
731 }
732
72e18a98
LP
733 if (m->partitions[PARTITION_HOME].found) {
734 k = add_partition_mount(m->partitions + PARTITION_HOME, "home", "/home", "Home Partition");
73b80ec2
LP
735 if (k < 0)
736 r = k;
737 }
e48fdd84 738
72e18a98
LP
739 if (m->partitions[PARTITION_SRV].found) {
740 k = add_partition_mount(m->partitions + PARTITION_SRV, "srv", "/srv", "Server Data Partition");
73b80ec2
LP
741 if (k < 0)
742 r = k;
743 }
1a14a53c 744
d4dffb85
LP
745 if (m->partitions[PARTITION_VAR].found) {
746 k = add_partition_mount(m->partitions + PARTITION_VAR, "var", "/var", "Variable Data Partition");
747 if (k < 0)
748 r = k;
749 }
750
751 if (m->partitions[PARTITION_TMP].found) {
752 k = add_partition_mount(m->partitions + PARTITION_TMP, "var-tmp", "/var/tmp", "Temporary Data Partition");
753 if (k < 0)
754 r = k;
755 }
756
fd89051e
LP
757 if (m->partitions[PARTITION_ROOT].found) {
758 k = add_root_rw(m->partitions + PARTITION_ROOT);
759 if (k < 0)
760 r = k;
761 }
762
1a14a53c
LP
763 return r;
764}
765
9fe6f5cc 766static int add_mounts(void) {
b00651cf 767 _cleanup_free_ char *p = NULL;
9fe6f5cc 768 int r;
b00651cf 769 dev_t devno;
9fe6f5cc 770
b00651cf
KK
771 /* If the root mount has been replaced by some form of volatile file system (overlayfs), the
772 * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
773 * here. */
774 r = readlink_malloc("/run/systemd/volatile-root", &p);
775 if (r == -ENOENT) { /* volatile-root not found */
776 r = get_block_device_harder("/", &devno);
67f0ac8c 777 if (r == -EUCLEAN)
b00651cf 778 return btrfs_log_dev_root(LOG_ERR, r, "root file system");
9fe6f5cc 779 if (r < 0)
b00651cf 780 return log_error_errno(r, "Failed to determine block device of root file system: %m");
d5cb053c 781 if (r == 0) { /* Not backed by a single block device. (Could be NFS or so, or could be multi-device RAID or so) */
b00651cf
KK
782 r = get_block_device_harder("/usr", &devno);
783 if (r == -EUCLEAN)
784 return btrfs_log_dev_root(LOG_ERR, r, "/usr");
9fe6f5cc 785 if (r < 0)
d5cb053c
LP
786 return log_error_errno(r, "Failed to determine block device of /usr/ file system: %m");
787 if (r == 0) { /* /usr/ not backed by single block device, either. */
788 log_debug("Neither root nor /usr/ file system are on a (single) block device.");
789 return 0;
790 }
9fe6f5cc 791 }
b00651cf
KK
792 } else if (r < 0)
793 return log_error_errno(r, "Failed to read symlink /run/systemd/volatile-root: %m");
794 else {
795 mode_t m;
796 r = device_path_parse_major_minor(p, &m, &devno);
797 if (r < 0)
798 return log_error_errno(r, "Failed to parse major/minor device node: %m");
799 if (!S_ISBLK(m))
800 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Volatile root device is of wrong type.");
9fe6f5cc
ZJS
801 }
802
803 return enumerate_partitions(devno);
804}
805
96287a49 806static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
73b80ec2 807 int r;
1a14a53c 808
73b80ec2 809 assert(key);
1a14a53c 810
8a9c44ed
LP
811 if (proc_cmdline_key_streq(key, "systemd.gpt_auto") ||
812 proc_cmdline_key_streq(key, "rd.systemd.gpt_auto")) {
1a14a53c 813
1d84ad94 814 r = value ? parse_boolean(value) : 1;
73b80ec2 815 if (r < 0)
0a1b9449 816 log_warning_errno(r, "Failed to parse gpt-auto switch \"%s\", ignoring: %m", value);
8086ffac
ZJS
817 else
818 arg_enabled = r;
1a14a53c 819
8a9c44ed 820 } else if (proc_cmdline_key_streq(key, "root")) {
1d84ad94
LP
821
822 if (proc_cmdline_value_missing(key, value))
823 return 0;
73b80ec2
LP
824
825 /* Disable root disk logic if there's a root= value
826 * specified (unless it happens to be "gpt-auto") */
827
074cdb95
ZJS
828 if (!streq(value, "gpt-auto")) {
829 arg_root_enabled = false;
830 log_debug("Disabling root partition auto-detection, root= is defined.");
831 }
73b80ec2 832
8a9c44ed 833 } else if (proc_cmdline_key_streq(key, "roothash")) {
2f3dfc6f
LP
834
835 if (proc_cmdline_value_missing(key, value))
836 return 0;
837
838 /* Disable root disk logic if there's roothash= defined (i.e. verity enabled) */
839
840 arg_root_enabled = false;
841
8a9c44ed 842 } else if (proc_cmdline_key_streq(key, "rw") && !value)
73b80ec2 843 arg_root_rw = true;
8a9c44ed 844 else if (proc_cmdline_key_streq(key, "ro") && !value)
73b80ec2 845 arg_root_rw = false;
73b80ec2
LP
846
847 return 0;
848}
849
ec6e9597 850static int run(const char *dest, const char *dest_early, const char *dest_late) {
8f50e86a 851 int r, k;
73b80ec2 852
ec6e9597 853 assert_se(arg_dest = dest_late);
73b80ec2 854
75f86906 855 if (detect_container() > 0) {
73b80ec2 856 log_debug("In a container, exiting.");
ec6e9597 857 return 0;
1a14a53c 858 }
3db604b9 859
1d84ad94 860 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
b5884878 861 if (r < 0)
da927ba9 862 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
1a14a53c 863
73b80ec2
LP
864 if (!arg_enabled) {
865 log_debug("Disabled, exiting.");
ec6e9597 866 return 0;
73b80ec2
LP
867 }
868
869 if (arg_root_enabled)
870 r = add_root_mount();
871
872 if (!in_initrd()) {
73b80ec2 873 k = add_mounts();
ec6e9597 874 if (r >= 0)
73b80ec2
LP
875 r = k;
876 }
877
ec6e9597 878 return r;
1a14a53c 879}
ec6e9597
ZJS
880
881DEFINE_MAIN_GENERATOR_FUNCTION(run);