1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * probe disks for filesystems and partitions
5 * Copyright © 2011 Karel Zak <kzak@redhat.com>
19 #include "alloc-util.h"
20 #include "blkid-util.h"
21 #include "device-util.h"
25 #include "parse-util.h"
26 #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 (streq(name
, "PTTYPE")) {
63 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_TYPE", value
);
65 } else if (streq(name
, "PTUUID")) {
66 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_UUID", value
);
68 } else if (streq(name
, "PART_ENTRY_NAME")) {
69 blkid_encode_string(value
, s
, sizeof(s
));
70 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_NAME", s
);
72 } else if (streq(name
, "PART_ENTRY_TYPE")) {
73 blkid_encode_string(value
, s
, sizeof(s
));
74 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_TYPE", s
);
76 } else if (startswith(name
, "PART_ENTRY_")) {
77 strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
78 udev_builtin_add_property(dev
, test
, s
, value
);
80 } else if (streq(name
, "SYSTEM_ID")) {
81 blkid_encode_string(value
, s
, sizeof(s
));
82 udev_builtin_add_property(dev
, test
, "ID_FS_SYSTEM_ID", s
);
84 } else if (streq(name
, "PUBLISHER_ID")) {
85 blkid_encode_string(value
, s
, sizeof(s
));
86 udev_builtin_add_property(dev
, test
, "ID_FS_PUBLISHER_ID", s
);
88 } else if (streq(name
, "APPLICATION_ID")) {
89 blkid_encode_string(value
, s
, sizeof(s
));
90 udev_builtin_add_property(dev
, test
, "ID_FS_APPLICATION_ID", s
);
92 } else if (streq(name
, "BOOT_SYSTEM_ID")) {
93 blkid_encode_string(value
, s
, sizeof(s
));
94 udev_builtin_add_property(dev
, test
, "ID_FS_BOOT_SYSTEM_ID", s
);
98 static int find_gpt_root(sd_device
*dev
, blkid_probe pr
, bool test
) {
100 #if defined(GPT_ROOT_NATIVE) && ENABLE_EFI
102 _cleanup_free_
char *root_id
= NULL
;
103 bool found_esp
= false;
109 /* Iterate through the partitions on this disk, and see if the
110 * EFI ESP we booted from is on it. If so, find the first root
111 * disk, and add a property indicating its partition UUID. */
114 pl
= blkid_probe_get_partitions(pr
);
116 return -errno
?: -ENOMEM
;
118 nvals
= blkid_partlist_numof_partitions(pl
);
119 for (i
= 0; i
< nvals
; i
++) {
121 const char *stype
, *sid
;
124 pp
= blkid_partlist_get_partition(pl
, i
);
128 sid
= blkid_partition_get_uuid(pp
);
132 stype
= blkid_partition_get_type_string(pp
);
136 if (sd_id128_from_string(stype
, &type
) < 0)
139 if (sd_id128_equal(type
, GPT_ESP
)) {
142 /* We found an ESP, let's see if it matches
143 * the ESP we booted from. */
145 if (sd_id128_from_string(sid
, &id
) < 0)
148 r
= efi_loader_get_device_part_uuid(&esp
);
152 if (sd_id128_equal(id
, esp
))
155 } else if (sd_id128_equal(type
, GPT_ROOT_NATIVE
)) {
156 unsigned long long flags
;
158 flags
= blkid_partition_get_flags(pp
);
159 if (flags
& GPT_FLAG_NO_AUTO
)
162 /* We found a suitable root partition, let's
163 * remember the first one. */
166 root_id
= strdup(sid
);
173 /* We found the ESP on this disk, and also found a root
174 * partition, nice! Let's export its UUID */
175 if (found_esp
&& root_id
)
176 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT_UUID", root_id
);
182 static int probe_superblocks(blkid_probe pr
) {
186 /* TODO: Return negative errno. */
188 if (fstat(blkid_probe_get_fd(pr
), &st
))
191 blkid_probe_enable_partitions(pr
, 1);
193 if (!S_ISCHR(st
.st_mode
) &&
194 blkid_probe_get_size(pr
) <= 1024 * 1440 &&
195 blkid_probe_is_wholedisk(pr
)) {
197 * check if the small disk is partitioned, if yes then
198 * don't probe for filesystems.
200 blkid_probe_enable_superblocks(pr
, 0);
202 rc
= blkid_do_fullprobe(pr
);
204 return rc
; /* -1 = error, 1 = nothing, 0 = success */
206 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
207 return 0; /* partition table detected */
210 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
211 blkid_probe_enable_superblocks(pr
, 1);
213 return blkid_do_safeprobe(pr
);
216 static int builtin_blkid(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
217 const char *devnode
, *root_partition
= NULL
, *data
, *name
;
218 _cleanup_(blkid_free_probep
) blkid_probe pr
= NULL
;
219 bool noraid
= false, is_gpt
= false;
220 _cleanup_close_
int fd
= -1;
224 static const struct option options
[] = {
225 { "offset", required_argument
, NULL
, 'o' },
226 { "noraid", no_argument
, NULL
, 'R' },
233 option
= getopt_long(argc
, argv
, "o:R", options
, NULL
);
239 r
= safe_atoi64(optarg
, &offset
);
241 return log_device_error_errno(dev
, r
, "Failed to parse '%s' as an integer: %m", optarg
);
243 return log_device_error_errno(dev
, -ERANGE
, "Invalid offset %"PRIi64
": %m", offset
);
252 pr
= blkid_new_probe();
254 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to create blkid prober: %m");
256 blkid_probe_set_superblocks_flags(pr
,
257 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
258 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
259 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
262 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
264 r
= sd_device_get_devname(dev
, &devnode
);
266 return log_device_debug_errno(dev
, r
, "Failed to get device name: %m");
268 fd
= open(devnode
, O_RDONLY
|O_CLOEXEC
);
270 return log_device_debug_errno(dev
, errno
, "Failed to open block device %s: %m", devnode
);
273 r
= blkid_probe_set_device(pr
, fd
, offset
, 0);
275 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to set device to blkid prober: %m");
277 log_device_debug(dev
, "Probe %s with %sraid and offset=%"PRIi64
, devnode
, noraid
? "no" : "", offset
);
279 r
= probe_superblocks(pr
);
281 return log_device_debug_errno(dev
, r
, "Failed to probe superblocks: %m");
283 /* If the device is a partition then its parent passed the root partition UUID to the device */
284 (void) sd_device_get_property_value(dev
, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition
);
287 nvals
= blkid_probe_numof_values(pr
);
289 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to get number of probed values: %m");
291 for (i
= 0; i
< nvals
; i
++) {
292 if (blkid_probe_get_value(pr
, i
, &name
, &data
, NULL
) < 0)
295 print_property(dev
, test
, name
, data
);
297 /* Is this a disk with GPT partition table? */
298 if (streq(name
, "PTTYPE") && streq(data
, "gpt"))
301 /* Is this a partition that matches the root partition
302 * property inherited from the parent? */
303 if (root_partition
&& streq(name
, "PART_ENTRY_UUID") && streq(data
, root_partition
))
304 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT", "1");
308 find_gpt_root(dev
, pr
, test
);
313 const struct udev_builtin udev_builtin_blkid
= {
315 .cmd
= builtin_blkid
,
316 .help
= "Filesystem and partition probing",