1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * probe disks for filesystems and partitions
5 * Copyright © 2011 Karel Zak <kzak@redhat.com>
17 #include "alloc-util.h"
18 #include "blkid-util.h"
19 #include "device-util.h"
20 #include "efi-loader.h"
21 #include "errno-util.h"
24 #include "parse-util.h"
25 #include "string-util.h"
28 #include "udev-builtin.h"
30 static void print_property(sd_device
*dev
, bool test
, const char *name
, const char *value
) {
35 if (streq(name
, "TYPE")) {
36 udev_builtin_add_property(dev
, test
, "ID_FS_TYPE", value
);
38 } else if (streq(name
, "USAGE")) {
39 udev_builtin_add_property(dev
, test
, "ID_FS_USAGE", value
);
41 } else if (streq(name
, "VERSION")) {
42 udev_builtin_add_property(dev
, test
, "ID_FS_VERSION", value
);
44 } else if (streq(name
, "UUID")) {
45 blkid_safe_string(value
, s
, sizeof(s
));
46 udev_builtin_add_property(dev
, test
, "ID_FS_UUID", s
);
47 blkid_encode_string(value
, s
, sizeof(s
));
48 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_ENC", s
);
50 } else if (streq(name
, "UUID_SUB")) {
51 blkid_safe_string(value
, s
, sizeof(s
));
52 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB", s
);
53 blkid_encode_string(value
, s
, sizeof(s
));
54 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB_ENC", s
);
56 } else if (streq(name
, "LABEL")) {
57 blkid_safe_string(value
, s
, sizeof(s
));
58 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL", s
);
59 blkid_encode_string(value
, s
, sizeof(s
));
60 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL_ENC", s
);
62 } else if (STR_IN_SET(name
, "FSSIZE", "FSLASTBLOCK", "FSBLOCKSIZE")) {
63 strscpyl(s
, sizeof(s
), "ID_FS_", name
+ 2, NULL
);
64 udev_builtin_add_property(dev
, test
, s
, value
);
66 } else if (streq(name
, "PTTYPE")) {
67 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_TYPE", value
);
69 } else if (streq(name
, "PTUUID")) {
70 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_UUID", value
);
72 } else if (streq(name
, "PART_ENTRY_NAME")) {
73 blkid_encode_string(value
, s
, sizeof(s
));
74 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_NAME", s
);
76 } else if (streq(name
, "PART_ENTRY_TYPE")) {
77 blkid_encode_string(value
, s
, sizeof(s
));
78 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_TYPE", s
);
80 } else if (startswith(name
, "PART_ENTRY_")) {
81 strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
82 udev_builtin_add_property(dev
, test
, s
, value
);
84 } else if (streq(name
, "SYSTEM_ID")) {
85 blkid_encode_string(value
, s
, sizeof(s
));
86 udev_builtin_add_property(dev
, test
, "ID_FS_SYSTEM_ID", s
);
88 } else if (streq(name
, "PUBLISHER_ID")) {
89 blkid_encode_string(value
, s
, sizeof(s
));
90 udev_builtin_add_property(dev
, test
, "ID_FS_PUBLISHER_ID", s
);
92 } else if (streq(name
, "APPLICATION_ID")) {
93 blkid_encode_string(value
, s
, sizeof(s
));
94 udev_builtin_add_property(dev
, test
, "ID_FS_APPLICATION_ID", s
);
96 } else if (streq(name
, "BOOT_SYSTEM_ID")) {
97 blkid_encode_string(value
, s
, sizeof(s
));
98 udev_builtin_add_property(dev
, test
, "ID_FS_BOOT_SYSTEM_ID", s
);
100 } else if (streq(name
, "VOLUME_ID")) {
101 blkid_encode_string(value
, s
, sizeof(s
));
102 udev_builtin_add_property(dev
, test
, "ID_FS_VOLUME_ID", s
);
104 } else if (streq(name
, "LOGICAL_VOLUME_ID")) {
105 blkid_encode_string(value
, s
, sizeof(s
));
106 udev_builtin_add_property(dev
, test
, "ID_FS_LOGICAL_VOLUME_ID", s
);
108 } else if (streq(name
, "VOLUME_SET_ID")) {
109 blkid_encode_string(value
, s
, sizeof(s
));
110 udev_builtin_add_property(dev
, test
, "ID_FS_VOLUME_SET_ID", s
);
112 } else if (streq(name
, "DATA_PREPARER_ID")) {
113 blkid_encode_string(value
, s
, sizeof(s
));
114 udev_builtin_add_property(dev
, test
, "ID_FS_DATA_PREPARER_ID", s
);
118 static int find_gpt_root(sd_device
*dev
, blkid_probe pr
, bool test
) {
120 #if defined(SD_GPT_ROOT_NATIVE) && ENABLE_EFI
122 _cleanup_free_
char *root_label
= NULL
;
123 bool found_esp_or_xbootldr
= false;
124 sd_id128_t root_id
= SD_ID128_NULL
;
129 /* Iterate through the partitions on this disk, and see if the UEFI ESP or XBOOTLDR partition we
130 * booted from is on it. If so, find the first root disk, and add a property indicating its partition
134 blkid_partlist pl
= blkid_probe_get_partitions(pr
);
136 return errno_or_else(ENOMEM
);
138 int nvals
= blkid_partlist_numof_partitions(pl
);
139 for (int i
= 0; i
< nvals
; i
++) {
144 pp
= blkid_partlist_get_partition(pl
, i
);
148 r
= blkid_partition_get_uuid_id128(pp
, &id
);
150 log_debug_errno(r
, "Failed to get partition UUID, ignoring: %m");
154 r
= blkid_partition_get_type_id128(pp
, &type
);
156 log_debug_errno(r
, "Failed to get partition type UUID, ignoring: %m");
160 label
= blkid_partition_get_name(pp
); /* returns NULL if empty */
162 if (sd_id128_in_set(type
, SD_GPT_ESP
, SD_GPT_XBOOTLDR
)) {
163 sd_id128_t esp_or_xbootldr
;
165 /* We found an ESP or XBOOTLDR, let's see if it matches the ESP/XBOOTLDR we booted from. */
167 r
= efi_loader_get_device_part_uuid(&esp_or_xbootldr
);
171 if (sd_id128_equal(id
, esp_or_xbootldr
))
172 found_esp_or_xbootldr
= true;
174 } else if (sd_id128_equal(type
, SD_GPT_ROOT_NATIVE
)) {
175 unsigned long long flags
;
177 flags
= blkid_partition_get_flags(pp
);
178 if (flags
& SD_GPT_FLAG_NO_AUTO
)
181 /* We found a suitable root partition, let's remember the first one, or the one with
182 * the newest version, as determined by comparing the partition labels. */
184 if (sd_id128_is_null(root_id
) || strverscmp_improved(label
, root_label
) > 0) {
187 r
= free_and_strdup(&root_label
, label
);
194 /* We found the ESP/XBOOTLDR on this disk, and also found a root partition, nice! Let's export its
196 if (found_esp_or_xbootldr
&& !sd_id128_is_null(root_id
))
197 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT_UUID", SD_ID128_TO_UUID_STRING(root_id
));
203 static int probe_superblocks(blkid_probe pr
) {
207 /* TODO: Return negative errno. */
209 if (fstat(blkid_probe_get_fd(pr
), &st
))
212 blkid_probe_enable_partitions(pr
, 1);
214 if (!S_ISCHR(st
.st_mode
) &&
215 blkid_probe_get_size(pr
) <= 1024 * 1440 &&
216 blkid_probe_is_wholedisk(pr
)) {
218 * check if the small disk is partitioned, if yes then
219 * don't probe for filesystems.
221 blkid_probe_enable_superblocks(pr
, 0);
223 rc
= blkid_do_fullprobe(pr
);
225 return rc
; /* -1 = error, 1 = nothing, 0 = success */
227 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
228 return 0; /* partition table detected */
231 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
232 blkid_probe_enable_superblocks(pr
, 1);
234 return blkid_do_safeprobe(pr
);
237 static int builtin_blkid(sd_device
*dev
, sd_netlink
**rtnl
, int argc
, char *argv
[], bool test
) {
238 const char *devnode
, *root_partition
= NULL
, *data
, *name
;
239 _cleanup_(blkid_free_probep
) blkid_probe pr
= NULL
;
240 bool noraid
= false, is_gpt
= false;
241 _cleanup_close_
int fd
= -EBADF
;
245 static const struct option options
[] = {
246 { "offset", required_argument
, NULL
, 'o' },
247 { "hint", required_argument
, NULL
, 'H' },
248 { "noraid", no_argument
, NULL
, 'R' },
253 pr
= blkid_new_probe();
255 return log_device_debug_errno(dev
, errno_or_else(ENOMEM
), "Failed to create blkid prober: %m");
260 option
= getopt_long(argc
, argv
, "o:H:R", options
, NULL
);
266 #if HAVE_BLKID_PROBE_SET_HINT
268 r
= blkid_probe_set_hint(pr
, optarg
, 0);
270 return log_device_error_errno(dev
, errno_or_else(ENOMEM
), "Failed to use '%s' probing hint: %m", optarg
);
273 /* Use the hint <name>=<offset> as probing offset for old versions */
274 optarg
= strchr(optarg
, '=');
276 /* no value means 0, do nothing for old versions */
282 r
= safe_atoi64(optarg
, &offset
);
284 return log_device_error_errno(dev
, r
, "Failed to parse '%s' as an integer: %m", optarg
);
286 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
), "Invalid offset %"PRIi64
": %m", offset
);
294 blkid_probe_set_superblocks_flags(pr
,
295 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
296 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
297 #ifdef BLKID_SUBLKS_FSINFO
298 BLKID_SUBLKS_FSINFO
|
300 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
303 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
305 r
= sd_device_get_devname(dev
, &devnode
);
307 return log_device_debug_errno(dev
, r
, "Failed to get device name: %m");
309 fd
= sd_device_open(dev
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
311 bool ignore
= ERRNO_IS_DEVICE_ABSENT(fd
);
312 log_device_debug_errno(dev
, fd
, "Failed to open block device %s%s: %m",
313 devnode
, ignore
? ", ignoring" : "");
314 return ignore
? 0 : fd
;
318 r
= blkid_probe_set_device(pr
, fd
, offset
, 0);
320 return log_device_debug_errno(dev
, errno_or_else(ENOMEM
), "Failed to set device to blkid prober: %m");
322 log_device_debug(dev
, "Probe %s with %sraid and offset=%"PRIi64
, devnode
, noraid
? "no" : "", offset
);
324 r
= probe_superblocks(pr
);
326 return log_device_debug_errno(dev
, r
, "Failed to probe superblocks: %m");
328 /* If the device is a partition then its parent passed the root partition UUID to the device */
329 (void) sd_device_get_property_value(dev
, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition
);
332 int nvals
= blkid_probe_numof_values(pr
);
334 return log_device_debug_errno(dev
, errno_or_else(ENOMEM
), "Failed to get number of probed values: %m");
336 for (int i
= 0; i
< nvals
; i
++) {
337 if (blkid_probe_get_value(pr
, i
, &name
, &data
, NULL
) < 0)
340 print_property(dev
, test
, name
, data
);
342 /* Is this a disk with GPT partition table? */
343 if (streq(name
, "PTTYPE") && streq(data
, "gpt"))
346 /* Is this a partition that matches the root partition
347 * property inherited from the parent? */
348 if (root_partition
&& streq(name
, "PART_ENTRY_UUID") && streq(data
, root_partition
))
349 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT", "1");
353 find_gpt_root(dev
, pr
, test
);
358 const UdevBuiltin udev_builtin_blkid
= {
360 .cmd
= builtin_blkid
,
361 .help
= "Filesystem and partition probing",