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