1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * probe disks for filesystems and partitions
5 * Copyright © 2011 Karel Zak <kzak@redhat.com>
8 #if HAVE_VALGRIND_MEMCHECK_H
9 #include <valgrind/memcheck.h>
15 #include <linux/loop.h>
18 #include <sys/ioctl.h>
23 #include "alloc-util.h"
24 #include "blkid-util.h"
25 #include "device-util.h"
26 #include "devnum-util.h"
27 #include "efi-loader.h"
28 #include "errno-util.h"
31 #include "parse-util.h"
32 #include "string-util.h"
35 #include "udev-builtin.h"
37 static void print_property(sd_device
*dev
, EventMode mode
, const char *name
, const char *value
) {
42 if (streq(name
, "TYPE")) {
43 udev_builtin_add_property(dev
, mode
, "ID_FS_TYPE", value
);
45 } else if (streq(name
, "USAGE")) {
46 udev_builtin_add_property(dev
, mode
, "ID_FS_USAGE", value
);
48 } else if (streq(name
, "VERSION")) {
49 udev_builtin_add_property(dev
, mode
, "ID_FS_VERSION", value
);
51 } else if (streq(name
, "UUID")) {
52 blkid_safe_string(value
, s
, sizeof(s
));
53 udev_builtin_add_property(dev
, mode
, "ID_FS_UUID", s
);
54 blkid_encode_string(value
, s
, sizeof(s
));
55 udev_builtin_add_property(dev
, mode
, "ID_FS_UUID_ENC", s
);
57 } else if (streq(name
, "UUID_SUB")) {
58 blkid_safe_string(value
, s
, sizeof(s
));
59 udev_builtin_add_property(dev
, mode
, "ID_FS_UUID_SUB", s
);
60 blkid_encode_string(value
, s
, sizeof(s
));
61 udev_builtin_add_property(dev
, mode
, "ID_FS_UUID_SUB_ENC", s
);
63 } else if (streq(name
, "LABEL")) {
64 blkid_safe_string(value
, s
, sizeof(s
));
65 udev_builtin_add_property(dev
, mode
, "ID_FS_LABEL", s
);
66 blkid_encode_string(value
, s
, sizeof(s
));
67 udev_builtin_add_property(dev
, mode
, "ID_FS_LABEL_ENC", s
);
69 } else if (STR_IN_SET(name
, "FSSIZE", "FSLASTBLOCK", "FSBLOCKSIZE")) {
70 strscpyl(s
, sizeof(s
), "ID_FS_", name
+ 2, NULL
);
71 udev_builtin_add_property(dev
, mode
, s
, value
);
73 } else if (streq(name
, "PTTYPE")) {
74 udev_builtin_add_property(dev
, mode
, "ID_PART_TABLE_TYPE", value
);
76 } else if (streq(name
, "PTUUID")) {
77 udev_builtin_add_property(dev
, mode
, "ID_PART_TABLE_UUID", value
);
79 } else if (streq(name
, "PART_ENTRY_NAME")) {
80 blkid_encode_string(value
, s
, sizeof(s
));
81 udev_builtin_add_property(dev
, mode
, "ID_PART_ENTRY_NAME", s
);
83 } else if (streq(name
, "PART_ENTRY_TYPE")) {
84 blkid_encode_string(value
, s
, sizeof(s
));
85 udev_builtin_add_property(dev
, mode
, "ID_PART_ENTRY_TYPE", s
);
87 } else if (startswith(name
, "PART_ENTRY_")) {
88 strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
89 udev_builtin_add_property(dev
, mode
, s
, value
);
91 } else if (streq(name
, "SYSTEM_ID")) {
92 blkid_encode_string(value
, s
, sizeof(s
));
93 udev_builtin_add_property(dev
, mode
, "ID_FS_SYSTEM_ID", s
);
95 } else if (streq(name
, "PUBLISHER_ID")) {
96 blkid_encode_string(value
, s
, sizeof(s
));
97 udev_builtin_add_property(dev
, mode
, "ID_FS_PUBLISHER_ID", s
);
99 } else if (streq(name
, "APPLICATION_ID")) {
100 blkid_encode_string(value
, s
, sizeof(s
));
101 udev_builtin_add_property(dev
, mode
, "ID_FS_APPLICATION_ID", s
);
103 } else if (streq(name
, "BOOT_SYSTEM_ID")) {
104 blkid_encode_string(value
, s
, sizeof(s
));
105 udev_builtin_add_property(dev
, mode
, "ID_FS_BOOT_SYSTEM_ID", s
);
107 } else if (streq(name
, "VOLUME_ID")) {
108 blkid_encode_string(value
, s
, sizeof(s
));
109 udev_builtin_add_property(dev
, mode
, "ID_FS_VOLUME_ID", s
);
111 } else if (streq(name
, "LOGICAL_VOLUME_ID")) {
112 blkid_encode_string(value
, s
, sizeof(s
));
113 udev_builtin_add_property(dev
, mode
, "ID_FS_LOGICAL_VOLUME_ID", s
);
115 } else if (streq(name
, "VOLUME_SET_ID")) {
116 blkid_encode_string(value
, s
, sizeof(s
));
117 udev_builtin_add_property(dev
, mode
, "ID_FS_VOLUME_SET_ID", s
);
119 } else if (streq(name
, "DATA_PREPARER_ID")) {
120 blkid_encode_string(value
, s
, sizeof(s
));
121 udev_builtin_add_property(dev
, mode
, "ID_FS_DATA_PREPARER_ID", s
);
125 static int find_gpt_root(sd_device
*dev
, blkid_probe pr
, EventMode mode
) {
127 #if defined(SD_GPT_ROOT_NATIVE) && ENABLE_EFI
129 _cleanup_free_
char *root_label
= NULL
;
130 bool found_esp_or_xbootldr
= false;
131 sd_id128_t root_id
= SD_ID128_NULL
;
136 /* Iterate through the partitions on this disk, and see if the UEFI ESP or XBOOTLDR partition we
137 * booted from is on it. If so, find the first root disk, and add a property indicating its partition
141 blkid_partlist pl
= blkid_probe_get_partitions(pr
);
143 return errno_or_else(ENOMEM
);
145 int nvals
= blkid_partlist_numof_partitions(pl
);
146 for (int i
= 0; i
< nvals
; i
++) {
151 pp
= blkid_partlist_get_partition(pl
, i
);
155 r
= blkid_partition_get_uuid_id128(pp
, &id
);
157 log_debug_errno(r
, "Failed to get partition UUID, ignoring: %m");
161 r
= blkid_partition_get_type_id128(pp
, &type
);
163 log_debug_errno(r
, "Failed to get partition type UUID, ignoring: %m");
167 label
= blkid_partition_get_name(pp
); /* returns NULL if empty */
169 if (sd_id128_in_set(type
, SD_GPT_ESP
, SD_GPT_XBOOTLDR
)) {
170 sd_id128_t esp_or_xbootldr
;
172 /* We found an ESP or XBOOTLDR, let's see if it matches the ESP/XBOOTLDR we booted from. */
174 r
= efi_loader_get_device_part_uuid(&esp_or_xbootldr
);
178 if (sd_id128_equal(id
, esp_or_xbootldr
))
179 found_esp_or_xbootldr
= true;
181 } else if (sd_id128_equal(type
, SD_GPT_ROOT_NATIVE
)) {
182 unsigned long long flags
;
184 flags
= blkid_partition_get_flags(pp
);
185 if (flags
& SD_GPT_FLAG_NO_AUTO
)
188 /* We found a suitable root partition, let's remember the first one, or the one with
189 * the newest version, as determined by comparing the partition labels. */
191 if (sd_id128_is_null(root_id
) || strverscmp_improved(label
, root_label
) > 0) {
194 r
= free_and_strdup(&root_label
, label
);
201 /* We found the ESP/XBOOTLDR on this disk, and also found a root partition, nice! Let's export its
203 if (found_esp_or_xbootldr
&& !sd_id128_is_null(root_id
))
204 udev_builtin_add_property(dev
, mode
, "ID_PART_GPT_AUTO_ROOT_UUID", SD_ID128_TO_UUID_STRING(root_id
));
210 static int probe_superblocks(blkid_probe pr
) {
214 /* TODO: Return negative errno. */
216 if (fstat(blkid_probe_get_fd(pr
), &st
))
219 blkid_probe_enable_partitions(pr
, 1);
221 if (!S_ISCHR(st
.st_mode
) &&
222 blkid_probe_get_size(pr
) <= 1024 * 1440 &&
223 blkid_probe_is_wholedisk(pr
)) {
225 * check if the small disk is partitioned, if yes then
226 * don't probe for filesystems.
228 blkid_probe_enable_superblocks(pr
, 0);
230 rc
= blkid_do_fullprobe(pr
);
232 return rc
; /* -1 = error, 1 = nothing, 0 = success */
234 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
235 return 0; /* partition table detected */
238 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
239 blkid_probe_enable_superblocks(pr
, 1);
241 return blkid_do_safeprobe(pr
);
244 static int read_loopback_backing_inode(
251 _cleanup_free_
char *fn
= NULL
;
252 struct loop_info64 info
;
262 /* Retrieves various fields of the current loopback device backing file, so that we can ultimately
263 * use it to create stable symlinks to loopback block devices, based on what they are backed by. We
264 * pick up inode/device as well as file name field. Note that we pick up the "lo_file_name" field
265 * here, which is an arbitrary free-form string provided by userspace. We do not return the sysfs
266 * attribute loop/backing_file here, because that is directly accessible from udev rules anyway. And
267 * sometimes, depending on context, it's a good thing to return the string userspace can freely pick
268 * over the string automatically generated by the kernel. */
270 r
= sd_device_get_sysname(dev
, &name
);
274 if (!startswith(name
, "loop"))
277 if (ioctl(fd
, LOOP_GET_STATUS64
, &info
) < 0) {
278 if (ERRNO_IS_NOT_SUPPORTED(errno
))
284 #if HAVE_VALGRIND_MEMCHECK_H
285 VALGRIND_MAKE_MEM_DEFINED(&info
, sizeof(info
));
288 if (isempty((char*) info
.lo_file_name
) ||
289 strnlen((char*) info
.lo_file_name
, sizeof(info
.lo_file_name
)-1) == sizeof(info
.lo_file_name
)-1)
290 /* Don't pick up file name if it is unset or possibly truncated. (Note: the kernel silently
291 * truncates the string passed from userspace by LOOP_SET_STATUS64 ioctl. See
292 * loop_set_status_from_info() in drivers/block/loop.c. Hence, we can't really know the file
293 * name is truncated if it uses sizeof(info.lo_file_name)-1 as length; it could also mean the
294 * string is just that long and wasn't truncated — but the fact is simply that we cannot know
295 * in that case if it was truncated or not. Thus, we assume the worst and suppress — at least
296 * for now. For shorter strings we know for sure it wasn't truncated, hence that's always
300 fn
= memdup_suffix0(info
.lo_file_name
, sizeof(info
.lo_file_name
));
305 *ret_inode
= info
.lo_inode
;
306 *ret_devno
= info
.lo_device
;
307 *ret_fname
= TAKE_PTR(fn
);
318 static int builtin_blkid(UdevEvent
*event
, int argc
, char *argv
[]) {
319 sd_device
*dev
= ASSERT_PTR(ASSERT_PTR(event
)->dev
);
320 const char *devnode
, *root_partition
= NULL
, *data
, *name
;
321 _cleanup_(blkid_free_probep
) blkid_probe pr
= NULL
;
322 _cleanup_free_
char *backing_fname
= NULL
;
323 bool noraid
= false, is_gpt
= false;
324 _cleanup_close_
int fd
= -EBADF
;
325 ino_t backing_inode
= 0;
326 dev_t backing_devno
= 0;
330 static const struct option options
[] = {
331 { "offset", required_argument
, NULL
, 'o' },
332 { "hint", required_argument
, NULL
, 'H' },
333 { "noraid", no_argument
, NULL
, 'R' },
338 pr
= blkid_new_probe();
340 return log_device_debug_errno(dev
, errno_or_else(ENOMEM
), "Failed to create blkid prober: %m");
345 option
= getopt_long(argc
, argv
, "o:H:R", options
, NULL
);
351 #if HAVE_BLKID_PROBE_SET_HINT
353 r
= blkid_probe_set_hint(pr
, optarg
, 0);
355 return log_device_error_errno(dev
, errno_or_else(ENOMEM
), "Failed to use '%s' probing hint: %m", optarg
);
358 /* Use the hint <name>=<offset> as probing offset for old versions */
359 optarg
= strchr(optarg
, '=');
361 /* no value means 0, do nothing for old versions */
367 r
= safe_atoi64(optarg
, &offset
);
369 return log_device_error_errno(dev
, r
, "Failed to parse '%s' as an integer: %m", optarg
);
371 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
), "Invalid offset %"PRIi64
".", offset
);
379 blkid_probe_set_superblocks_flags(pr
,
380 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
381 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
382 #ifdef BLKID_SUBLKS_FSINFO
383 BLKID_SUBLKS_FSINFO
|
385 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
388 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
390 r
= sd_device_get_devname(dev
, &devnode
);
392 return log_device_debug_errno(dev
, r
, "Failed to get device name: %m");
394 fd
= sd_device_open(dev
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
396 bool ignore
= ERRNO_IS_DEVICE_ABSENT(fd
);
397 log_device_debug_errno(dev
, fd
, "Failed to open block device %s%s: %m",
398 devnode
, ignore
? ", ignoring" : "");
399 return ignore
? 0 : fd
;
403 r
= blkid_probe_set_device(pr
, fd
, offset
, 0);
405 return log_device_debug_errno(dev
, errno_or_else(ENOMEM
), "Failed to set device to blkid prober: %m");
407 log_device_debug(dev
, "Probe %s with %sraid and offset=%"PRIi64
, devnode
, noraid
? "no" : "", offset
);
409 r
= probe_superblocks(pr
);
411 return log_device_debug_errno(dev
, r
, "Failed to probe superblocks: %m");
413 /* If the device is a partition then its parent passed the root partition UUID to the device */
414 (void) sd_device_get_property_value(dev
, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition
);
417 int nvals
= blkid_probe_numof_values(pr
);
419 return log_device_debug_errno(dev
, errno_or_else(ENOMEM
), "Failed to get number of probed values: %m");
421 for (int i
= 0; i
< nvals
; i
++) {
422 if (blkid_probe_get_value(pr
, i
, &name
, &data
, NULL
) < 0)
425 print_property(dev
, event
->event_mode
, name
, data
);
427 /* Is this a disk with GPT partition table? */
428 if (streq(name
, "PTTYPE") && streq(data
, "gpt"))
431 /* Is this a partition that matches the root partition
432 * property inherited from the parent? */
433 if (root_partition
&& streq(name
, "PART_ENTRY_UUID") && streq(data
, root_partition
))
434 udev_builtin_add_property(dev
, event
->event_mode
, "ID_PART_GPT_AUTO_ROOT", "1");
438 find_gpt_root(dev
, pr
, event
->event_mode
);
440 r
= read_loopback_backing_inode(
447 log_device_debug_errno(dev
, r
, "Failed to read loopback backing inode, ignoring: %m");
449 udev_builtin_add_propertyf(dev
, event
->event_mode
, "ID_LOOP_BACKING_DEVICE", DEVNUM_FORMAT_STR
, DEVNUM_FORMAT_VAL(backing_devno
));
450 udev_builtin_add_propertyf(dev
, event
->event_mode
, "ID_LOOP_BACKING_INODE", "%" PRIu64
, (uint64_t) backing_inode
);
453 /* In the worst case blkid_encode_string() will blow up to 4x the string
454 * length. Hence size the buffer to 4x of the longest string
455 * read_loopback_backing_inode() might return */
456 char encoded
[sizeof_field(struct loop_info64
, lo_file_name
) * 4 + 1];
458 assert(strlen(backing_fname
) < ELEMENTSOF(encoded
) / 4);
459 blkid_encode_string(backing_fname
, encoded
, ELEMENTSOF(encoded
));
461 udev_builtin_add_property(dev
, event
->event_mode
, "ID_LOOP_BACKING_FILENAME", backing_fname
);
462 udev_builtin_add_property(dev
, event
->event_mode
, "ID_LOOP_BACKING_FILENAME_ENC", encoded
);
469 const UdevBuiltin udev_builtin_blkid
= {
471 .cmd
= builtin_blkid
,
472 .help
= "Filesystem and partition probing",