1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * probe disks for filesystems and partitions
5 * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
6 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "alloc-util.h"
34 #include "blkid-util.h"
38 #include "parse-util.h"
39 #include "string-util.h"
42 static void print_property(struct udev_device
*dev
, bool test
, const char *name
, const char *value
) {
47 if (streq(name
, "TYPE")) {
48 udev_builtin_add_property(dev
, test
, "ID_FS_TYPE", value
);
50 } else if (streq(name
, "USAGE")) {
51 udev_builtin_add_property(dev
, test
, "ID_FS_USAGE", value
);
53 } else if (streq(name
, "VERSION")) {
54 udev_builtin_add_property(dev
, test
, "ID_FS_VERSION", value
);
56 } else if (streq(name
, "UUID")) {
57 blkid_safe_string(value
, s
, sizeof(s
));
58 udev_builtin_add_property(dev
, test
, "ID_FS_UUID", s
);
59 blkid_encode_string(value
, s
, sizeof(s
));
60 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_ENC", s
);
62 } else if (streq(name
, "UUID_SUB")) {
63 blkid_safe_string(value
, s
, sizeof(s
));
64 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB", s
);
65 blkid_encode_string(value
, s
, sizeof(s
));
66 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB_ENC", s
);
68 } else if (streq(name
, "LABEL")) {
69 blkid_safe_string(value
, s
, sizeof(s
));
70 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL", s
);
71 blkid_encode_string(value
, s
, sizeof(s
));
72 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL_ENC", s
);
74 } else if (streq(name
, "PTTYPE")) {
75 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_TYPE", value
);
77 } else if (streq(name
, "PTUUID")) {
78 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_UUID", value
);
80 } else if (streq(name
, "PART_ENTRY_NAME")) {
81 blkid_encode_string(value
, s
, sizeof(s
));
82 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_NAME", s
);
84 } else if (streq(name
, "PART_ENTRY_TYPE")) {
85 blkid_encode_string(value
, s
, sizeof(s
));
86 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_TYPE", s
);
88 } else if (startswith(name
, "PART_ENTRY_")) {
89 strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
90 udev_builtin_add_property(dev
, test
, s
, value
);
92 } else if (streq(name
, "SYSTEM_ID")) {
93 blkid_encode_string(value
, s
, sizeof(s
));
94 udev_builtin_add_property(dev
, test
, "ID_FS_SYSTEM_ID", s
);
96 } else if (streq(name
, "PUBLISHER_ID")) {
97 blkid_encode_string(value
, s
, sizeof(s
));
98 udev_builtin_add_property(dev
, test
, "ID_FS_PUBLISHER_ID", s
);
100 } else if (streq(name
, "APPLICATION_ID")) {
101 blkid_encode_string(value
, s
, sizeof(s
));
102 udev_builtin_add_property(dev
, test
, "ID_FS_APPLICATION_ID", s
);
104 } else if (streq(name
, "BOOT_SYSTEM_ID")) {
105 blkid_encode_string(value
, s
, sizeof(s
));
106 udev_builtin_add_property(dev
, test
, "ID_FS_BOOT_SYSTEM_ID", s
);
110 static int find_gpt_root(struct udev_device
*dev
, blkid_probe pr
, bool test
) {
112 #if defined(GPT_ROOT_NATIVE) && ENABLE_EFI
114 _cleanup_free_
char *root_id
= NULL
;
115 bool found_esp
= false;
121 /* Iterate through the partitions on this disk, and see if the
122 * EFI ESP we booted from is on it. If so, find the first root
123 * disk, and add a property indicating its partition UUID. */
126 pl
= blkid_probe_get_partitions(pr
);
128 return -errno
?: -ENOMEM
;
130 nvals
= blkid_partlist_numof_partitions(pl
);
131 for (i
= 0; i
< nvals
; i
++) {
133 const char *stype
, *sid
;
136 pp
= blkid_partlist_get_partition(pl
, i
);
140 sid
= blkid_partition_get_uuid(pp
);
144 stype
= blkid_partition_get_type_string(pp
);
148 if (sd_id128_from_string(stype
, &type
) < 0)
151 if (sd_id128_equal(type
, GPT_ESP
)) {
154 /* We found an ESP, let's see if it matches
155 * the ESP we booted from. */
157 if (sd_id128_from_string(sid
, &id
) < 0)
160 r
= efi_loader_get_device_part_uuid(&esp
);
164 if (sd_id128_equal(id
, esp
))
167 } else if (sd_id128_equal(type
, GPT_ROOT_NATIVE
)) {
168 unsigned long long flags
;
170 flags
= blkid_partition_get_flags(pp
);
171 if (flags
& GPT_FLAG_NO_AUTO
)
174 /* We found a suitable root partition, let's
175 * remember the first one. */
178 root_id
= strdup(sid
);
185 /* We found the ESP on this disk, and also found a root
186 * partition, nice! Let's export its UUID */
187 if (found_esp
&& root_id
)
188 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT_UUID", root_id
);
194 static int probe_superblocks(blkid_probe pr
) {
198 if (fstat(blkid_probe_get_fd(pr
), &st
))
201 blkid_probe_enable_partitions(pr
, 1);
203 if (!S_ISCHR(st
.st_mode
) &&
204 blkid_probe_get_size(pr
) <= 1024 * 1440 &&
205 blkid_probe_is_wholedisk(pr
)) {
207 * check if the small disk is partitioned, if yes then
208 * don't probe for filesystems.
210 blkid_probe_enable_superblocks(pr
, 0);
212 rc
= blkid_do_fullprobe(pr
);
214 return rc
; /* -1 = error, 1 = nothing, 0 = success */
216 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
217 return 0; /* partition table detected */
220 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
221 blkid_probe_enable_superblocks(pr
, 1);
223 return blkid_do_safeprobe(pr
);
226 static int builtin_blkid(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
227 const char *root_partition
;
230 _cleanup_close_
int fd
= -1;
231 _cleanup_(blkid_free_probep
) blkid_probe pr
= NULL
;
239 static const struct option options
[] = {
240 { "offset", required_argument
, NULL
, 'o' },
241 { "noraid", no_argument
, NULL
, 'R' },
248 option
= getopt_long(argc
, argv
, "o:R", options
, NULL
);
254 err
= safe_atoi64(optarg
, &offset
);
268 pr
= blkid_new_probe();
272 blkid_probe_set_superblocks_flags(pr
,
273 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
274 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
275 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
278 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
280 fd
= open(udev_device_get_devnode(dev
), O_RDONLY
|O_CLOEXEC
);
282 err
= log_debug_errno(errno
, "Failure opening block device %s: %m", udev_device_get_devnode(dev
));
286 err
= blkid_probe_set_device(pr
, fd
, offset
, 0);
290 log_debug("probe %s %sraid offset=%"PRIi64
,
291 udev_device_get_devnode(dev
),
292 noraid
? "no" : "", offset
);
294 err
= probe_superblocks(pr
);
298 /* If we are a partition then our parent passed on the root
299 * partition UUID to us */
300 root_partition
= udev_device_get_property_value(dev
, "ID_PART_GPT_AUTO_ROOT_UUID");
302 nvals
= blkid_probe_numof_values(pr
);
303 for (i
= 0; i
< nvals
; i
++) {
304 if (blkid_probe_get_value(pr
, i
, &name
, &data
, NULL
))
307 print_property(dev
, test
, name
, data
);
309 /* Is this a disk with GPT partition table? */
310 if (streq(name
, "PTTYPE") && streq(data
, "gpt"))
313 /* Is this a partition that matches the root partition
314 * property we inherited from our parent? */
315 if (root_partition
&& streq(name
, "PART_ENTRY_UUID") && streq(data
, root_partition
))
316 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT", "1");
320 find_gpt_root(dev
, pr
, test
);
329 const struct udev_builtin udev_builtin_blkid
= {
331 .cmd
= builtin_blkid
,
332 .help
= "Filesystem and partition probing",