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"
23 #include "errno-util.h"
26 #include "parse-util.h"
27 #include "string-util.h"
29 #include "udev-builtin.h"
31 static void print_property(sd_device
*dev
, bool test
, const char *name
, const char *value
) {
36 if (streq(name
, "TYPE")) {
37 udev_builtin_add_property(dev
, test
, "ID_FS_TYPE", value
);
39 } else if (streq(name
, "USAGE")) {
40 udev_builtin_add_property(dev
, test
, "ID_FS_USAGE", value
);
42 } else if (streq(name
, "VERSION")) {
43 udev_builtin_add_property(dev
, test
, "ID_FS_VERSION", value
);
45 } else if (streq(name
, "UUID")) {
46 blkid_safe_string(value
, s
, sizeof(s
));
47 udev_builtin_add_property(dev
, test
, "ID_FS_UUID", s
);
48 blkid_encode_string(value
, s
, sizeof(s
));
49 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_ENC", s
);
51 } else if (streq(name
, "UUID_SUB")) {
52 blkid_safe_string(value
, s
, sizeof(s
));
53 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB", s
);
54 blkid_encode_string(value
, s
, sizeof(s
));
55 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB_ENC", s
);
57 } else if (streq(name
, "LABEL")) {
58 blkid_safe_string(value
, s
, sizeof(s
));
59 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL", s
);
60 blkid_encode_string(value
, s
, sizeof(s
));
61 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL_ENC", s
);
63 } else if (streq(name
, "PTTYPE")) {
64 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_TYPE", value
);
66 } else if (streq(name
, "PTUUID")) {
67 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_UUID", value
);
69 } else if (streq(name
, "PART_ENTRY_NAME")) {
70 blkid_encode_string(value
, s
, sizeof(s
));
71 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_NAME", s
);
73 } else if (streq(name
, "PART_ENTRY_TYPE")) {
74 blkid_encode_string(value
, s
, sizeof(s
));
75 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_TYPE", s
);
77 } else if (startswith(name
, "PART_ENTRY_")) {
78 strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
79 udev_builtin_add_property(dev
, test
, s
, value
);
81 } else if (streq(name
, "SYSTEM_ID")) {
82 blkid_encode_string(value
, s
, sizeof(s
));
83 udev_builtin_add_property(dev
, test
, "ID_FS_SYSTEM_ID", s
);
85 } else if (streq(name
, "PUBLISHER_ID")) {
86 blkid_encode_string(value
, s
, sizeof(s
));
87 udev_builtin_add_property(dev
, test
, "ID_FS_PUBLISHER_ID", s
);
89 } else if (streq(name
, "APPLICATION_ID")) {
90 blkid_encode_string(value
, s
, sizeof(s
));
91 udev_builtin_add_property(dev
, test
, "ID_FS_APPLICATION_ID", s
);
93 } else if (streq(name
, "BOOT_SYSTEM_ID")) {
94 blkid_encode_string(value
, s
, sizeof(s
));
95 udev_builtin_add_property(dev
, test
, "ID_FS_BOOT_SYSTEM_ID", s
);
99 static int find_gpt_root(sd_device
*dev
, blkid_probe pr
, bool test
) {
101 #if defined(GPT_ROOT_NATIVE) && ENABLE_EFI
103 _cleanup_free_
char *root_id
= NULL
;
104 bool found_esp
= false;
110 /* Iterate through the partitions on this disk, and see if the
111 * EFI ESP we booted from is on it. If so, find the first root
112 * disk, and add a property indicating its partition UUID. */
115 pl
= blkid_probe_get_partitions(pr
);
117 return errno_or_else(ENOMEM
);
119 nvals
= blkid_partlist_numof_partitions(pl
);
120 for (i
= 0; i
< nvals
; i
++) {
122 const char *stype
, *sid
;
125 pp
= blkid_partlist_get_partition(pl
, i
);
129 sid
= blkid_partition_get_uuid(pp
);
133 stype
= blkid_partition_get_type_string(pp
);
137 if (sd_id128_from_string(stype
, &type
) < 0)
140 if (sd_id128_equal(type
, GPT_ESP
)) {
143 /* We found an ESP, let's see if it matches
144 * the ESP we booted from. */
146 if (sd_id128_from_string(sid
, &id
) < 0)
149 r
= efi_loader_get_device_part_uuid(&esp
);
153 if (sd_id128_equal(id
, esp
))
156 } else if (sd_id128_equal(type
, GPT_ROOT_NATIVE
)) {
157 unsigned long long flags
;
159 flags
= blkid_partition_get_flags(pp
);
160 if (flags
& GPT_FLAG_NO_AUTO
)
163 /* We found a suitable root partition, let's
164 * remember the first one. */
167 root_id
= strdup(sid
);
174 /* We found the ESP on this disk, and also found a root
175 * partition, nice! Let's export its UUID */
176 if (found_esp
&& root_id
)
177 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT_UUID", root_id
);
183 static int probe_superblocks(blkid_probe pr
) {
187 /* TODO: Return negative errno. */
189 if (fstat(blkid_probe_get_fd(pr
), &st
))
192 blkid_probe_enable_partitions(pr
, 1);
194 if (!S_ISCHR(st
.st_mode
) &&
195 blkid_probe_get_size(pr
) <= 1024 * 1440 &&
196 blkid_probe_is_wholedisk(pr
)) {
198 * check if the small disk is partitioned, if yes then
199 * don't probe for filesystems.
201 blkid_probe_enable_superblocks(pr
, 0);
203 rc
= blkid_do_fullprobe(pr
);
205 return rc
; /* -1 = error, 1 = nothing, 0 = success */
207 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
208 return 0; /* partition table detected */
211 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
212 blkid_probe_enable_superblocks(pr
, 1);
214 return blkid_do_safeprobe(pr
);
217 static int builtin_blkid(sd_device
*dev
, int argc
, char *argv
[], bool test
) {
218 const char *devnode
, *root_partition
= NULL
, *data
, *name
;
219 _cleanup_(blkid_free_probep
) blkid_probe pr
= NULL
;
220 bool noraid
= false, is_gpt
= false;
221 _cleanup_close_
int fd
= -1;
225 static const struct option options
[] = {
226 { "offset", required_argument
, NULL
, 'o' },
227 { "noraid", no_argument
, NULL
, 'R' },
234 option
= getopt_long(argc
, argv
, "o:R", options
, NULL
);
240 r
= safe_atoi64(optarg
, &offset
);
242 return log_device_error_errno(dev
, r
, "Failed to parse '%s' as an integer: %m", optarg
);
244 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(ERANGE
), "Invalid offset %"PRIi64
": %m", offset
);
253 pr
= blkid_new_probe();
255 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to create blkid prober: %m");
257 blkid_probe_set_superblocks_flags(pr
,
258 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
259 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
260 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
263 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
265 r
= sd_device_get_devname(dev
, &devnode
);
267 return log_device_debug_errno(dev
, r
, "Failed to get device name: %m");
269 fd
= open(devnode
, O_RDONLY
|O_CLOEXEC
);
271 return log_device_debug_errno(dev
, errno
, "Failed to open block device %s: %m", devnode
);
274 r
= blkid_probe_set_device(pr
, fd
, offset
, 0);
276 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to set device to blkid prober: %m");
278 log_device_debug(dev
, "Probe %s with %sraid and offset=%"PRIi64
, devnode
, noraid
? "no" : "", offset
);
280 r
= probe_superblocks(pr
);
282 return log_device_debug_errno(dev
, r
, "Failed to probe superblocks: %m");
284 /* If the device is a partition then its parent passed the root partition UUID to the device */
285 (void) sd_device_get_property_value(dev
, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition
);
288 nvals
= blkid_probe_numof_values(pr
);
290 return log_device_debug_errno(dev
, errno
> 0 ? errno
: ENOMEM
, "Failed to get number of probed values: %m");
292 for (i
= 0; i
< nvals
; i
++) {
293 if (blkid_probe_get_value(pr
, i
, &name
, &data
, NULL
) < 0)
296 print_property(dev
, test
, name
, data
);
298 /* Is this a disk with GPT partition table? */
299 if (streq(name
, "PTTYPE") && streq(data
, "gpt"))
302 /* Is this a partition that matches the root partition
303 * property inherited from the parent? */
304 if (root_partition
&& streq(name
, "PART_ENTRY_UUID") && streq(data
, root_partition
))
305 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT", "1");
309 find_gpt_root(dev
, pr
, test
);
314 const UdevBuiltin udev_builtin_blkid
= {
316 .cmd
= builtin_blkid
,
317 .help
= "Filesystem and partition probing",