Debian Codegraph @

src/udev/udev-builtin-blkid.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * probe disks for filesystems and partitions
4 *
5 * Copyright © 2011 Karel Zak <[email protected]>
6 */
7
8 #if HAVE_VALGRIND_MEMCHECK_H
9 #include <valgrind/memcheck.h>
10 #endif
11
12 #include <fcntl.h>
13 #include <linux/loop.h>
14 #include <sys/ioctl.h>
15 #include <sys/stat.h>
16
17 #include "alloc-util.h"
18 #include "blkid-util.h"
19 #include "blockdev-util.h"
20 #include "device-util.h"
21 #include "devnum-util.h"
22 #include "efi-api.h"
23 #include "efi-loader.h"
24 #include "errno-util.h"
25 #include "fd-util.h"
26 #include "initrd-util.h"
27 #include "gpt.h"
28 #include "options.h"
29 #include "parse-util.h"
30 #include "string-util.h"
31 #include "strv.h"
32 #include "strxcpyx.h"
33 #include "udev-builtin.h"
34
35 static void print_property(UdevEvent *event, const char *name, const char *value) {
36 char s[256];
37
38 assert(event);
39 assert(name);
40
41 s[0] = '\0';
42
43 if (streq(name, "TYPE")) {
44 udev_builtin_add_property(event, "ID_FS_TYPE", value);
45
46 } else if (streq(name, "USAGE")) {
47 udev_builtin_add_property(event, "ID_FS_USAGE", value);
48
49 } else if (streq(name, "VERSION")) {
50 udev_builtin_add_property(event, "ID_FS_VERSION", value);
51
52 } else if (streq(name, "UUID")) {
53 sym_blkid_safe_string(value, s, sizeof(s));
54 udev_builtin_add_property(event, "ID_FS_UUID", s);
55 sym_blkid_encode_string(value, s, sizeof(s));
56 udev_builtin_add_property(event, "ID_FS_UUID_ENC", s);
57
58 } else if (streq(name, "UUID_SUB")) {
59 sym_blkid_safe_string(value, s, sizeof(s));
60 udev_builtin_add_property(event, "ID_FS_UUID_SUB", s);
61 sym_blkid_encode_string(value, s, sizeof(s));
62 udev_builtin_add_property(event, "ID_FS_UUID_SUB_ENC", s);
63
64 } else if (streq(name, "LABEL")) {
65 sym_blkid_safe_string(value, s, sizeof(s));
66 udev_builtin_add_property(event, "ID_FS_LABEL", s);
67 sym_blkid_encode_string(value, s, sizeof(s));
68 udev_builtin_add_property(event, "ID_FS_LABEL_ENC", s);
69
70 } else if (STR_IN_SET(name, "FSSIZE", "FSLASTBLOCK", "FSBLOCKSIZE")) {
71 strscpyl(s, sizeof(s), "ID_FS_", name + 2, NULL);
72 udev_builtin_add_property(event, s, value);
73
74 } else if (streq(name, "PTTYPE")) {
75 udev_builtin_add_property(event, "ID_PART_TABLE_TYPE", value);
76
77 } else if (streq(name, "PTUUID")) {
78 udev_builtin_add_property(event, "ID_PART_TABLE_UUID", value);
79
80 } else if (streq(name, "PART_ENTRY_NAME")) {
81 sym_blkid_encode_string(value, s, sizeof(s));
82 udev_builtin_add_property(event, "ID_PART_ENTRY_NAME", s);
83
84 } else if (streq(name, "PART_ENTRY_TYPE")) {
85 sym_blkid_encode_string(value, s, sizeof(s));
86 udev_builtin_add_property(event, "ID_PART_ENTRY_TYPE", s);
87
88 } else if (startswith(name, "PART_ENTRY_")) {
89 strscpyl(s, sizeof(s), "ID_", name, NULL);
90 udev_builtin_add_property(event, s, value);
91
92 } else if (streq(name, "SYSTEM_ID")) {
93 sym_blkid_encode_string(value, s, sizeof(s));
94 udev_builtin_add_property(event, "ID_FS_SYSTEM_ID", s);
95
96 } else if (streq(name, "PUBLISHER_ID")) {
97 sym_blkid_encode_string(value, s, sizeof(s));
98 udev_builtin_add_property(event, "ID_FS_PUBLISHER_ID", s);
99
100 } else if (streq(name, "APPLICATION_ID")) {
101 sym_blkid_encode_string(value, s, sizeof(s));
102 udev_builtin_add_property(event, "ID_FS_APPLICATION_ID", s);
103
104 } else if (streq(name, "BOOT_SYSTEM_ID")) {
105 sym_blkid_encode_string(value, s, sizeof(s));
106 udev_builtin_add_property(event, "ID_FS_BOOT_SYSTEM_ID", s);
107
108 } else if (streq(name, "VOLUME_ID")) {
109 sym_blkid_encode_string(value, s, sizeof(s));
110 udev_builtin_add_property(event, "ID_FS_VOLUME_ID", s);
111
112 } else if (streq(name, "LOGICAL_VOLUME_ID")) {
113 sym_blkid_encode_string(value, s, sizeof(s));
114 udev_builtin_add_property(event, "ID_FS_LOGICAL_VOLUME_ID", s);
115
116 } else if (streq(name, "VOLUME_SET_ID")) {
117 sym_blkid_encode_string(value, s, sizeof(s));
118 udev_builtin_add_property(event, "ID_FS_VOLUME_SET_ID", s);
119
120 } else if (streq(name, "DATA_PREPARER_ID")) {
121 sym_blkid_encode_string(value, s, sizeof(s));
122 udev_builtin_add_property(event, "ID_FS_DATA_PREPARER_ID", s);
123 }
124 }
125
126 static int find_gpt_root(UdevEvent *event, blkid_probe pr, const char *loop_backing_fname) {
127
128 #if defined(SD_GPT_ROOT_NATIVE) && ENABLE_EFI
129 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
130 int r;
131
132 assert(event);
133 assert(pr);
134
135 /* In the initrd: Iterate through the partitions on this disk, and see if the UEFI ESP or XBOOTLDR
136 * partition we booted from is on it. If so, find the newest root partition, and add a property
137 * indicating its partition UUID. We also do this if we are dealing with a loopback block device
138 * whose "backing filename" field is set to the string "root". In the latter case we do not search
139 * for ESP or XBOOTLDR.
140 *
141 * After the initrd→host transition: look at the current block device mounted at / and set the same
142 * properties to its whole block device. */
143
144 const char *devnode;
145 r = sd_device_get_devname(dev, &devnode);
146 if (r < 0)
147 return log_device_debug_errno(dev, r, "Failed to get device node: %m");
148
149 r = block_device_is_whole_disk(dev);
150 if (r < 0)
151 return log_device_debug_errno(dev, r, "Unable to determine if device '%s' is a whole-block device: %m", devnode);
152 if (r == 0) {
153 log_device_debug(dev, "Invoked on device '%s' which is not a whole-disk block device, ignoring.", devnode);
154 return 0;
155 }
156
157 r = blockdev_partscan_enabled(dev);
158 if (r < 0)
159 return log_device_debug_errno(dev, r, "Failed to determine if block device '%s' supports partitions: %m", devnode);
160 if (r == 0) {
161 log_device_debug(dev, "Invoked on block device '%s' that lacks partition scanning, ignoring.", devnode);
162 return 0;
163 }
164
165 sd_id128_t esp_or_xbootldr = SD_ID128_NULL;
166 bool need_esp_or_xbootldr;
167 dev_t root_devno = 0;
168 if (in_initrd()) {
169 /* In the initrd look at the boot loader provided data (and loopback backing fname) to find
170 * our *future* root */
171
172 r = efi_loader_get_device_part_uuid(&esp_or_xbootldr);
173 if (r < 0) {
174 if (r != -ENOENT && !ERRNO_IS_NEG_NOT_SUPPORTED(r))
175 return log_debug_errno(r, "Unable to determine loader partition UUID: %m");
176
177 log_device_debug(dev, "No loader partition UUID EFI variable set, not using partition data to search for default root block device.");
178
179 /* NB: if an ESP/xbootldr field is set, we always use that. We do this in order to guarantee
180 * systematic behaviour. */
181 if (!STRPTR_IN_SET(loop_backing_fname, "rootdisk", "rootdisk.raw")) {
182 log_device_debug(dev, "Device is not a loopback block device with reference string 'root', not considering block device as default root block device.");
183 return 0;
184 }
185
186 /* OK, we have now sufficiently identified this device as the right root "whole" device,
187 * hence no need to bother with searching for ESP/XBOOTLDR */
188 need_esp_or_xbootldr = false;
189 } else
190 /* We now know the the ESP/xbootldr UUID, but we cannot be sure yet it's on this block
191 * device, hence look for it among partitions now */
192 need_esp_or_xbootldr = true;
193 } else {
194 /* On the main system look at the *current* root instead */
195
196 r = blockdev_get_root(LOG_DEBUG, &root_devno);
197 if (r < 0) {
198 log_device_debug_errno(dev, r, "Unable to determine current root block device, skipping gpt-auto probing: %m");
199 return 0;
200 }
201 if (r == 0) {
202 log_device_debug(dev, "Root block device not backed by a (single) whole block device, skipping gpt-auto probing.");
203 return 0;
204 }
205
206 dev_t whole_devno;
207 r = block_get_whole_disk(root_devno, &whole_devno);
208 if (r < 0)
209 return log_device_debug_errno(dev, r, "Failed to find whole block device for root block device: %m");
210
211 dev_t this_devno;
212 r = sd_device_get_devnum(dev, &this_devno);
213 if (r < 0)
214 return log_device_debug_errno(dev, r, "Failed to get device major/minor of device: %m");
215
216 if (whole_devno != this_devno) {
217 log_device_debug(dev, "This device is not the current root block device.");
218 return 0;
219 }
220
221 /* We don't need to check ESP/XBOOTLDR UUID, we *know* what our root disk is */
222 need_esp_or_xbootldr = false;
223 }
224
225 errno = 0;
226 blkid_partlist pl = sym_blkid_probe_get_partitions(pr);
227 if (!pl)
228 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to probe partitions: %m");
229
230 sd_id128_t root_id = SD_ID128_NULL;
231 bool found_esp_or_xbootldr = false;
232
233 if (root_devno != 0) {
234 /* If we already know the root partition, let's verify its type ID and then directly query
235 * its ID */
236
237 blkid_partition root_partition = sym_blkid_partlist_devno_to_partition(pl, root_devno);
238 if (root_partition) {
239 sd_id128_t type;
240 r = blkid_partition_get_type_id128(root_partition, &type);
241 if (r < 0)
242 log_device_debug_errno(dev, r, "Failed to get root partition type UUID, ignoring: %m");
243 else if (sd_id128_equal(type, SD_GPT_ROOT_NATIVE)) {
244 r = blkid_partition_get_uuid_id128(root_partition, &root_id);
245 if (r < 0)
246 log_device_debug_errno(dev, r, "Failed to get partition UUID, ignoring: %m");
247 }
248 }
249 } else {
250 /* We do not know the root partition, let's search for it. */
251
252 _cleanup_free_ char *root_label = NULL;
253 int nvals = sym_blkid_partlist_numof_partitions(pl);
254 for (int i = 0; i < nvals; i++) {
255 blkid_partition pp;
256 const char *label;
257 sd_id128_t type, id;
258
259 pp = sym_blkid_partlist_get_partition(pl, i);
260 if (!pp)
261 continue;
262
263 r = blkid_partition_get_uuid_id128(pp, &id);
264 if (r < 0) {
265 log_device_debug_errno(dev, r, "Failed to get partition UUID, ignoring: %m");
266 continue;
267 }
268
269 r = blkid_partition_get_type_id128(pp, &type);
270 if (r < 0) {
271 log_device_debug_errno(dev, r, "Failed to get partition type UUID, ignoring: %m");
272 continue;
273 }
274
275 label = sym_blkid_partition_get_name(pp); /* returns NULL if empty */
276
277 if (need_esp_or_xbootldr && sd_id128_in_set(type, SD_GPT_ESP, SD_GPT_XBOOTLDR)) {
278
279 /* We found an ESP or XBOOTLDR, let's see if it matches the ESP/XBOOTLDR we booted from. */
280 if (sd_id128_equal(id, esp_or_xbootldr))
281 found_esp_or_xbootldr = true;
282
283 } else if (sd_id128_equal(type, SD_GPT_ROOT_NATIVE)) {
284 unsigned long long flags;
285
286 flags = sym_blkid_partition_get_flags(pp);
287 if (flags & SD_GPT_FLAG_NO_AUTO)
288 continue;
289
290 /* systemd-sysupdate expects empty partitions to be marked with an "_empty" label, hence ignore them here. */
291 if (streq_ptr(label, "_empty"))
292 continue;
293
294 /* We found a suitable root partition, let's remember the first one, or the one with
295 * the newest version, as determined by comparing the partition labels. */
296
297 if (sd_id128_is_null(root_id) || strverscmp_improved(label, root_label) > 0) {
298 root_id = id;
299
300 if (free_and_strdup(&root_label, label) < 0)
301 return log_oom_debug();
302 }
303 }
304 }
305 }
306
307 if (!need_esp_or_xbootldr || found_esp_or_xbootldr) {
308 /* We found the ESP/XBOOTLDR on this disk (or we didn't need it) */
309 udev_builtin_add_property(event, "ID_PART_GPT_AUTO_ROOT_DISK", "1");
310
311 /* We found a root partition, nice! Let's export its UUID. */
312 if (!sd_id128_is_null(root_id))
313 udev_builtin_add_property(event, "ID_PART_GPT_AUTO_ROOT_UUID", SD_ID128_TO_UUID_STRING(root_id));
314 }
315 #endif
316
317 return 0;
318 }
319
320 static int probe_superblocks(blkid_probe pr) {
321 struct stat st;
322 int rc;
323
324 /* TODO: Return negative errno. */
325
326 if (fstat(sym_blkid_probe_get_fd(pr), &st))
327 return -errno;
328
329 sym_blkid_probe_enable_partitions(pr, 1);
330
331 if (!S_ISCHR(st.st_mode) &&
332 sym_blkid_probe_get_size(pr) <= 1024 * 1440 &&
333 sym_blkid_probe_is_wholedisk(pr)) {
334 /*
335 * check if the small disk is partitioned, if yes then
336 * don't probe for filesystems.
337 */
338 sym_blkid_probe_enable_superblocks(pr, 0);
339
340 rc = sym_blkid_do_fullprobe(pr);
341 if (rc < 0)
342 return rc; /* -1 = error, 1 = nothing, 0 = success */
343
344 if (sym_blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
345 return 0; /* partition table detected */
346 }
347
348 sym_blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
349 sym_blkid_probe_enable_superblocks(pr, 1);
350
351 return sym_blkid_do_safeprobe(pr);
352 }
353
354 static int read_loopback_backing_inode(
355 sd_device *dev,
356 int fd,
357 dev_t *ret_devno,
358 ino_t *ret_inode,
359 char **ret_fname) {
360
361 _cleanup_free_ char *fn = NULL;
362 struct loop_info64 info;
363 int r;
364
365 assert(dev);
366 assert(fd >= 0);
367 assert(ret_devno);
368 assert(ret_inode);
369 assert(ret_fname);
370
371 /* Retrieves various fields of the current loopback device backing file, so that we can ultimately
372 * use it to create stable symlinks to loopback block devices, based on what they are backed by. We
373 * pick up inode/device as well as file name field. Note that we pick up the "lo_file_name" field
374 * here, which is an arbitrary free-form string provided by userspace. We do not return the sysfs
375 * attribute loop/backing_file here, because that is directly accessible from udev rules anyway. And
376 * sometimes, depending on context, it's a good thing to return the string userspace can freely pick
377 * over the string automatically generated by the kernel. */
378
379 r = device_sysname_startswith(dev, "loop");
380 if (r < 0)
381 return r;
382 if (r == 0)
383 goto notloop;
384
385 if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
386 if (ERRNO_IS_NOT_SUPPORTED(errno))
387 goto notloop;
388
389 return -errno;
390 }
391
392 #if HAVE_VALGRIND_MEMCHECK_H
393 VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
394 #endif
395
396 if (isempty((char*) info.lo_file_name) ||
397 strnlen((char*) info.lo_file_name, sizeof(info.lo_file_name)-1) == sizeof(info.lo_file_name)-1)
398 /* Don't pick up file name if it is unset or possibly truncated. (Note: the kernel silently
399 * truncates the string passed from userspace by LOOP_SET_STATUS64 ioctl. See
400 * loop_set_status_from_info() in drivers/block/loop.c. Hence, we can't really know the file
401 * name is truncated if it uses sizeof(info.lo_file_name)-1 as length; it could also mean the
402 * string is just that long and wasn't truncated — but the fact is simply that we cannot know
403 * in that case if it was truncated or not. Thus, we assume the worst and suppress — at least
404 * for now. For shorter strings we know for sure it wasn't truncated, hence that's always
405 * safe.) */
406 fn = NULL;
407 else {
408 fn = memdup_suffix0(info.lo_file_name, sizeof(info.lo_file_name));
409 if (!fn)
410 return -ENOMEM;
411 }
412
413 *ret_inode = info.lo_inode;
414 *ret_devno = info.lo_device;
415 *ret_fname = TAKE_PTR(fn);
416 return 1;
417
418 notloop:
419 *ret_devno = 0;
420 *ret_inode = 0;
421 *ret_fname = NULL;
422 return 0;
423 }
424
425 static int probe_gpt_boot_disk_needs_loop(UdevEvent *event, int fd) {
426 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
427 int r;
428
429 /* Probe the GPT sector size. For devices booted via El Torito, the GPT may use a different
430 * sector size than the device (e.g. a 512-byte GPT on a device with 2048-byte blocks). If
431 * there's a mismatch, check if this is the boot disk by comparing GPT partition UUIDs with the
432 * ESP/XBOOTLDR UUID exported by the boot loader. If it matches, set a property so that udev
433 * rules can set up a loop device with the correct sector size — the kernel can't parse the
434 * partition table itself in this case.
435 *
436 * Even if the sector sizes match, if the device does not support partition scanning (e.g. some
437 * CD-ROM drives), the kernel still can't parse the partition table. In that case, if the disk
438 * contains the ESP we booted from, we still need a loop device to expose the partitions. */
439
440 _cleanup_free_ void *entries = NULL;
441 uint32_t n_entries, entry_size;
442 ssize_t gpt_ssz = gpt_probe(fd, /* ret_header= */ NULL, &entries, &n_entries, &entry_size);
443 if (gpt_ssz < 0)
444 return log_device_debug_errno(dev, gpt_ssz, "Failed to probe GPT: %m");
445 if (gpt_ssz == 0)
446 return 0;
447
448 uint32_t device_ssz;
449 r = blockdev_get_sector_size(fd, &device_ssz);
450 if (r < 0)
451 return log_device_debug_errno(dev, r, "Failed to get device sector size: %m");
452
453 bool sector_size_mismatch = (uint32_t) gpt_ssz != device_ssz;
454
455 if (!sector_size_mismatch) {
456 r = blockdev_partscan_enabled(dev);
457 if (r < 0)
458 return log_device_debug_errno(dev, r, "Failed to check if partition scanning is enabled: %m");
459 if (r > 0)
460 return 0;
461 }
462
463 if (sector_size_mismatch)
464 log_device_debug(dev, "GPT sector size %zi does not match device sector size %" PRIu32 ".",
465 gpt_ssz, device_ssz);
466 else
467 log_device_debug(dev, "Device does not support partition scanning.");
468
469 sd_id128_t loader_part_uuid;
470 r = efi_loader_get_device_part_uuid(&loader_part_uuid);
471 if (r < 0) {
472 if (r != -ENOENT && !ERRNO_IS_NEG_NOT_SUPPORTED(r))
473 return log_device_debug_errno(dev, r, "Failed to get loader partition UUID: %m");
474
475 return 0;
476 }
477
478 for (uint32_t i = 0; i < n_entries; i++) {
479 const GptPartitionEntry *entry = (const GptPartitionEntry *) ((const uint8_t *) entries + (size_t) entry_size * i);
480
481 sd_id128_t type = efi_guid_to_id128(entry->partition_type_guid);
482 if (!sd_id128_in_set(type, SD_GPT_ESP, SD_GPT_XBOOTLDR))
483 continue;
484
485 if (!sd_id128_equal(efi_guid_to_id128(entry->unique_partition_guid), loader_part_uuid))
486 continue;
487
488 log_device_debug(dev, "Found boot partition (ESP/XBOOTLDR) on disk where kernel cannot scan partitions.");
489 udev_builtin_add_property(event, "ID_PART_GPT_AUTO_ROOT_DISK_NEEDS_LOOP", "1");
490 udev_builtin_add_propertyf(event, "ID_PART_GPT_SECTOR_SIZE", "%zi", gpt_ssz);
491 return 1; /* boot disk needs loop device */
492 }
493
494 return 0;
495 }
496
497 static int builtin_blkid(UdevEvent *event, int argc, char *argv[]) {
498 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
499 const char *devnode, *root_partition = NULL, *data, *name;
500 _cleanup_(blkid_free_probep) blkid_probe pr = NULL;
501 _cleanup_free_ char *backing_fname = NULL;
502 bool noraid = false, is_gpt = false;
503 _cleanup_close_ int fd = -EBADF;
504 ino_t backing_inode = 0;
505 dev_t backing_devno = 0;
506 int64_t offset = 0;
507 int r;
508
509 r = DLOPEN_LIBBLKID(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED);
510 if (r < 0)
511 return log_device_debug_errno(dev, r, "blkid not available: %m");
512
513 errno = 0;
514 pr = sym_blkid_new_probe();
515 if (!pr)
516 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to create blkid prober: %m");
517
518 OptionParser opts = { argc, argv, .namespace = "udev-builtin-blkid" };
519
520 FOREACH_OPTION_OR_RETURN(c, &opts)
521 switch (c) {
522
523 OPTION_NAMESPACE("udev-builtin-blkid"): {}
524
525 OPTION('H', "hint", "HINT", NULL):
526 errno = 0;
527 r = sym_blkid_probe_set_hint(pr, opts.arg, 0);
528 if (r < 0)
529 return log_device_error_errno(dev, errno_or_else(ENOMEM), "Failed to use '%s' probing hint: %m", opts.arg);
530 break;
531
532 OPTION('o', "offset", "OFFSET", NULL):
533 r = safe_atoi64(opts.arg, &offset);
534 if (r < 0)
535 return log_device_error_errno(dev, r, "Failed to parse '%s' as an integer: %m", opts.arg);
536 if (offset < 0)
537 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid offset %"PRIi64".", offset);
538 break;
539
540 OPTION('R', "noraid", NULL, NULL):
541 noraid = true;
542 break;
543 }
544
545 r = sd_device_get_devname(dev, &devnode);
546 if (r < 0)
547 return log_device_debug_errno(dev, r, "Failed to get device name: %m");
548
549 fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
550 if (fd < 0) {
551 bool ignore = ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(fd);
552 log_device_debug_errno(dev, fd, "Failed to open block device %s%s: %m",
553 devnode, ignore ? ", ignoring" : "");
554 return ignore ? 0 : fd;
555 }
556
557 if (offset == 0) {
558 r = probe_gpt_boot_disk_needs_loop(event, fd);
559 if (r > 0)
560 return 0;
561 }
562
563 sym_blkid_probe_set_superblocks_flags(pr,
564 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
565 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
566 #ifdef BLKID_SUBLKS_FSINFO /* since util-linux 2.39 */
567 BLKID_SUBLKS_FSINFO |
568 #endif
569 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
570
571 if (noraid)
572 sym_blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
573
574 errno = 0;
575 r = sym_blkid_probe_set_device(pr, fd, offset, 0);
576 if (r < 0)
577 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to set device to blkid prober: %m");
578
579 log_device_debug(dev, "Probe %s with %sraid and offset=%"PRIi64, devnode, noraid ? "no" : "", offset);
580
581 r = probe_superblocks(pr);
582 if (r < 0)
583 return log_device_debug_errno(dev, r, "Failed to probe superblocks: %m");
584
585 /* If the device is a partition then its parent passed the root partition UUID to the device */
586 (void) sd_device_get_property_value(dev, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition);
587
588 errno = 0;
589 int nvals = sym_blkid_probe_numof_values(pr);
590 if (nvals < 0)
591 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to get number of probed values: %m");
592
593 for (int i = 0; i < nvals; i++) {
594 if (sym_blkid_probe_get_value(pr, i, &name, &data, NULL) < 0)
595 continue;
596
597 print_property(event, name, data);
598
599 /* Is this a disk with GPT partition table? */
600 if (streq(name, "PTTYPE") && streq(data, "gpt"))
601 is_gpt = true;
602
603 /* Is this a partition that matches the root partition
604 * property inherited from the parent? */
605 if (root_partition && streq(name, "PART_ENTRY_UUID") && streq(data, root_partition))
606 udev_builtin_add_property(event, "ID_PART_GPT_AUTO_ROOT", "1");
607 }
608
609 r = read_loopback_backing_inode(
610 dev,
611 fd,
612 &backing_devno,
613 &backing_inode,
614 &backing_fname);
615 if (r < 0)
616 log_device_debug_errno(dev, r, "Failed to read loopback backing inode, ignoring: %m");
617 else if (r > 0) {
618 udev_builtin_add_propertyf(event, "ID_LOOP_BACKING_DEVICE", DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(backing_devno));
619 udev_builtin_add_propertyf(event, "ID_LOOP_BACKING_INODE", "%" PRIu64, (uint64_t) backing_inode);
620
621 if (backing_fname) {
622 /* In the worst case blkid_encode_string() will blow up to 4x the string
623 * length. Hence size the buffer to 4x of the longest string
624 * read_loopback_backing_inode() might return */
625 char encoded[sizeof_field(struct loop_info64, lo_file_name) * 4 + 1];
626
627 assert(strlen(backing_fname) < ELEMENTSOF(encoded) / 4);
628 sym_blkid_encode_string(backing_fname, encoded, ELEMENTSOF(encoded));
629
630 udev_builtin_add_property(event, "ID_LOOP_BACKING_FILENAME", backing_fname);
631 udev_builtin_add_property(event, "ID_LOOP_BACKING_FILENAME_ENC", encoded);
632 }
633 }
634
635 if (is_gpt)
636 find_gpt_root(event, pr, backing_fname);
637
638 return 0;
639 }
640
641 const UdevBuiltin udev_builtin_blkid = {
642 .name = "blkid",
643 .cmd = builtin_blkid,
644 .help = "Filesystem and partition probing",
645 .run_once = true,
646 };