1 /* SPDX-License-Identifier: GPL-2.0+ */
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"
27 #include "udev-builtin.h"
29 static void print_property(sd_device
*dev
, bool test
, const char *name
, const char *value
) {
34 if (streq(name
, "TYPE")) {
35 udev_builtin_add_property(dev
, test
, "ID_FS_TYPE", value
);
37 } else if (streq(name
, "USAGE")) {
38 udev_builtin_add_property(dev
, test
, "ID_FS_USAGE", value
);
40 } else if (streq(name
, "VERSION")) {
41 udev_builtin_add_property(dev
, test
, "ID_FS_VERSION", value
);
43 } else if (streq(name
, "UUID")) {
44 blkid_safe_string(value
, s
, sizeof(s
));
45 udev_builtin_add_property(dev
, test
, "ID_FS_UUID", s
);
46 blkid_encode_string(value
, s
, sizeof(s
));
47 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_ENC", s
);
49 } else if (streq(name
, "UUID_SUB")) {
50 blkid_safe_string(value
, s
, sizeof(s
));
51 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB", s
);
52 blkid_encode_string(value
, s
, sizeof(s
));
53 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB_ENC", s
);
55 } else if (streq(name
, "LABEL")) {
56 blkid_safe_string(value
, s
, sizeof(s
));
57 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL", s
);
58 blkid_encode_string(value
, s
, sizeof(s
));
59 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL_ENC", s
);
61 } else if (streq(name
, "PTTYPE")) {
62 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_TYPE", value
);
64 } else if (streq(name
, "PTUUID")) {
65 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_UUID", value
);
67 } else if (streq(name
, "PART_ENTRY_NAME")) {
68 blkid_encode_string(value
, s
, sizeof(s
));
69 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_NAME", s
);
71 } else if (streq(name
, "PART_ENTRY_TYPE")) {
72 blkid_encode_string(value
, s
, sizeof(s
));
73 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_TYPE", s
);
75 } else if (startswith(name
, "PART_ENTRY_")) {
76 strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
77 udev_builtin_add_property(dev
, test
, s
, value
);
79 } else if (streq(name
, "SYSTEM_ID")) {
80 blkid_encode_string(value
, s
, sizeof(s
));
81 udev_builtin_add_property(dev
, test
, "ID_FS_SYSTEM_ID", s
);
83 } else if (streq(name
, "PUBLISHER_ID")) {
84 blkid_encode_string(value
, s
, sizeof(s
));
85 udev_builtin_add_property(dev
, test
, "ID_FS_PUBLISHER_ID", s
);
87 } else if (streq(name
, "APPLICATION_ID")) {
88 blkid_encode_string(value
, s
, sizeof(s
));
89 udev_builtin_add_property(dev
, test
, "ID_FS_APPLICATION_ID", s
);
91 } else if (streq(name
, "BOOT_SYSTEM_ID")) {
92 blkid_encode_string(value
, s
, sizeof(s
));
93 udev_builtin_add_property(dev
, test
, "ID_FS_BOOT_SYSTEM_ID", s
);
97 static int find_gpt_root(sd_device
*dev
, blkid_probe pr
, bool test
) {
99 #if defined(GPT_ROOT_NATIVE) && ENABLE_EFI
101 _cleanup_free_
char *root_id
= NULL
;
102 bool found_esp
= false;
108 /* Iterate through the partitions on this disk, and see if the
109 * EFI ESP we booted from is on it. If so, find the first root
110 * disk, and add a property indicating its partition UUID. */
113 pl
= blkid_probe_get_partitions(pr
);
115 return errno_or_else(ENOMEM
);
117 nvals
= blkid_partlist_numof_partitions(pl
);
118 for (i
= 0; i
< nvals
; i
++) {
120 const char *stype
, *sid
;
123 pp
= blkid_partlist_get_partition(pl
, i
);
127 sid
= blkid_partition_get_uuid(pp
);
131 stype
= blkid_partition_get_type_string(pp
);
135 if (sd_id128_from_string(stype
, &type
) < 0)
138 if (sd_id128_equal(type
, GPT_ESP
)) {
141 /* We found an ESP, let's see if it matches
142 * the ESP we booted from. */
144 if (sd_id128_from_string(sid
, &id
) < 0)
147 r
= efi_loader_get_device_part_uuid(&esp
);
151 if (sd_id128_equal(id
, esp
))
154 } else if (sd_id128_equal(type
, GPT_ROOT_NATIVE
)) {
155 unsigned long long flags
;
157 flags
= blkid_partition_get_flags(pp
);
158 if (flags
& GPT_FLAG_NO_AUTO
)
161 /* We found a suitable root partition, let's
162 * remember the first one. */
165 root_id
= strdup(sid
);
172 /* We found the ESP on this disk, and also found a root
173 * partition, nice! Let's export its UUID */
174 if (found_esp
&& root_id
)
175 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT_UUID", root_id
);
181 static int probe_superblocks(blkid_probe pr
) {
185 /* TODO: Return negative errno. */
187 if (fstat(blkid_probe_get_fd(pr
), &st
))
190 blkid_probe_enable_partitions(pr
, 1);
192 if (!S_ISCHR(st
.st_mode
) &&
193 blkid_probe_get_size(pr
) <= 1024 * 1440 &&
194 blkid_probe_is_wholedisk(pr
)) {
196 * check if the small disk is partitioned, if yes then
197 * don't probe for filesystems.
199 blkid_probe_enable_superblocks(pr
, 0);
201 rc
= blkid_do_fullprobe(pr
);
203 return rc
; /* -1 = error, 1 = nothing, 0 = success */
205 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
206 return 0; /* partition table detected */
209 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
210 blkid_probe_enable_superblocks(pr
, 1);
212 return blkid_do_safeprobe(pr
);
215 static int builtin_blkid(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
216 const char *devnode
, *root_partition
= NULL
, *data
, *name
;
217 _cleanup_(blkid_free_probep
) blkid_probe pr
= NULL
;
218 bool noraid
= false, is_gpt
= false;
219 _cleanup_close_
int fd
= -1;
223 static const struct option options
[] = {
224 { "offset", required_argument
, NULL
, 'o' },
225 { "noraid", no_argument
, NULL
, 'R' },
232 option
= getopt_long(argc
, argv
, "o:R", options
, NULL
);
238 r
= safe_atoi64(optarg
, &offset
);
240 return log_device_error_errno(dev
, r
, "Failed to parse '%s' as an integer: %m", optarg
);
242 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(ERANGE
), "Invalid offset %"PRIi64
": %m", offset
);
251 pr
= blkid_new_probe();
253 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to create blkid prober: %m");
255 blkid_probe_set_superblocks_flags(pr
,
256 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
257 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
258 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
261 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
263 r
= sd_device_get_devname(dev
, &devnode
);
265 return log_device_debug_errno(dev
, r
, "Failed to get device name: %m");
267 fd
= open(devnode
, O_RDONLY
|O_CLOEXEC
);
269 return log_device_debug_errno(dev
, errno
, "Failed to open block device %s: %m", devnode
);
272 r
= blkid_probe_set_device(pr
, fd
, offset
, 0);
274 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to set device to blkid prober: %m");
276 log_device_debug(dev
, "Probe %s with %sraid and offset=%"PRIi64
, devnode
, noraid
? "no" : "", offset
);
278 r
= probe_superblocks(pr
);
280 return log_device_debug_errno(dev
, r
, "Failed to probe superblocks: %m");
282 /* If the device is a partition then its parent passed the root partition UUID to the device */
283 (void) sd_device_get_property_value(dev
, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition
);
286 nvals
= blkid_probe_numof_values(pr
);
288 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to get number of probed values: %m");
290 for (i
= 0; i
< nvals
; i
++) {
291 if (blkid_probe_get_value(pr
, i
, &name
, &data
, NULL
) < 0)
294 print_property(dev
, test
, name
, data
);
296 /* Is this a disk with GPT partition table? */
297 if (streq(name
, "PTTYPE") && streq(data
, "gpt"))
300 /* Is this a partition that matches the root partition
301 * property inherited from the parent? */
302 if (root_partition
&& streq(name
, "PART_ENTRY_UUID") && streq(data
, root_partition
))
303 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT", "1");
307 find_gpt_root(dev
, pr
, test
);
312 const UdevBuiltin udev_builtin_blkid
= {
314 .cmd
= builtin_blkid
,
315 .help
= "Filesystem and partition probing",