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