1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 * probe disks for filesystems and partitions
6 * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
7 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <blkid/blkid.h>
34 #include "alloc-util.h"
38 #include "string-util.h"
41 static void print_property(struct udev_device
*dev
, bool test
, const char *name
, const char *value
) {
46 if (streq(name
, "TYPE")) {
47 udev_builtin_add_property(dev
, test
, "ID_FS_TYPE", value
);
49 } else if (streq(name
, "USAGE")) {
50 udev_builtin_add_property(dev
, test
, "ID_FS_USAGE", value
);
52 } else if (streq(name
, "VERSION")) {
53 udev_builtin_add_property(dev
, test
, "ID_FS_VERSION", value
);
55 } else if (streq(name
, "UUID")) {
56 blkid_safe_string(value
, s
, sizeof(s
));
57 udev_builtin_add_property(dev
, test
, "ID_FS_UUID", s
);
58 blkid_encode_string(value
, s
, sizeof(s
));
59 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_ENC", s
);
61 } else if (streq(name
, "UUID_SUB")) {
62 blkid_safe_string(value
, s
, sizeof(s
));
63 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB", s
);
64 blkid_encode_string(value
, s
, sizeof(s
));
65 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB_ENC", s
);
67 } else if (streq(name
, "LABEL")) {
68 blkid_safe_string(value
, s
, sizeof(s
));
69 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL", s
);
70 blkid_encode_string(value
, s
, sizeof(s
));
71 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL_ENC", s
);
73 } else if (streq(name
, "PTTYPE")) {
74 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_TYPE", value
);
76 } else if (streq(name
, "PTUUID")) {
77 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_UUID", value
);
79 } else if (streq(name
, "PART_ENTRY_NAME")) {
80 blkid_encode_string(value
, s
, sizeof(s
));
81 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_NAME", s
);
83 } else if (streq(name
, "PART_ENTRY_TYPE")) {
84 blkid_encode_string(value
, s
, sizeof(s
));
85 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_TYPE", s
);
87 } else if (startswith(name
, "PART_ENTRY_")) {
88 strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
89 udev_builtin_add_property(dev
, test
, s
, value
);
91 } else if (streq(name
, "SYSTEM_ID")) {
92 blkid_encode_string(value
, s
, sizeof(s
));
93 udev_builtin_add_property(dev
, test
, "ID_FS_SYSTEM_ID", s
);
95 } else if (streq(name
, "PUBLISHER_ID")) {
96 blkid_encode_string(value
, s
, sizeof(s
));
97 udev_builtin_add_property(dev
, test
, "ID_FS_PUBLISHER_ID", s
);
99 } else if (streq(name
, "APPLICATION_ID")) {
100 blkid_encode_string(value
, s
, sizeof(s
));
101 udev_builtin_add_property(dev
, test
, "ID_FS_APPLICATION_ID", s
);
103 } else if (streq(name
, "BOOT_SYSTEM_ID")) {
104 blkid_encode_string(value
, s
, sizeof(s
));
105 udev_builtin_add_property(dev
, test
, "ID_FS_BOOT_SYSTEM_ID", s
);
109 static int find_gpt_root(struct udev_device
*dev
, blkid_probe pr
, bool test
) {
111 #if defined(GPT_ROOT_NATIVE) && defined(ENABLE_EFI)
113 _cleanup_free_
char *root_id
= NULL
;
114 bool found_esp
= false;
120 /* Iterate through the partitions on this disk, and see if the
121 * EFI ESP we booted from is on it. If so, find the first root
122 * disk, and add a property indicating its partition UUID. */
125 pl
= blkid_probe_get_partitions(pr
);
127 return errno
> 0 ? -errno
: -ENOMEM
;
129 nvals
= blkid_partlist_numof_partitions(pl
);
130 for (i
= 0; i
< nvals
; i
++) {
132 const char *stype
, *sid
;
135 pp
= blkid_partlist_get_partition(pl
, i
);
139 sid
= blkid_partition_get_uuid(pp
);
143 stype
= blkid_partition_get_type_string(pp
);
147 if (sd_id128_from_string(stype
, &type
) < 0)
150 if (sd_id128_equal(type
, GPT_ESP
)) {
152 unsigned long long flags
;
154 flags
= blkid_partition_get_flags(pp
);
155 if (flags
& GPT_FLAG_NO_AUTO
)
158 /* We found an ESP, let's see if it matches
159 * the ESP we booted from. */
161 if (sd_id128_from_string(sid
, &id
) < 0)
164 r
= efi_loader_get_device_part_uuid(&esp
);
168 if (sd_id128_equal(id
, esp
))
171 } else if (sd_id128_equal(type
, GPT_ROOT_NATIVE
)) {
173 /* We found a suitable root partition, let's
174 * remember the first one. */
177 root_id
= strdup(sid
);
184 /* We found the ESP on this disk, and also found a root
185 * partition, nice! Let's export its UUID */
186 if (found_esp
&& root_id
)
187 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT_UUID", root_id
);
193 static int probe_superblocks(blkid_probe pr
) {
197 if (fstat(blkid_probe_get_fd(pr
), &st
))
200 blkid_probe_enable_partitions(pr
, 1);
202 if (!S_ISCHR(st
.st_mode
) &&
203 blkid_probe_get_size(pr
) <= 1024 * 1440 &&
204 blkid_probe_is_wholedisk(pr
)) {
206 * check if the small disk is partitioned, if yes then
207 * don't probe for filesystems.
209 blkid_probe_enable_superblocks(pr
, 0);
211 rc
= blkid_do_fullprobe(pr
);
213 return rc
; /* -1 = error, 1 = nothing, 0 = success */
215 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
216 return 0; /* partition table detected */
219 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
220 blkid_probe_enable_superblocks(pr
, 1);
222 return blkid_do_safeprobe(pr
);
225 static int builtin_blkid(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
226 const char *root_partition
;
229 _cleanup_close_
int fd
= -1;
233 const char *prtype
= NULL
;
239 static const struct option options
[] = {
240 { "offset", optional_argument
, NULL
, 'o' },
241 { "noraid", no_argument
, NULL
, 'R' },
248 option
= getopt_long(argc
, argv
, "oR", options
, NULL
);
254 offset
= strtoull(optarg
, NULL
, 0);
262 pr
= blkid_new_probe();
266 blkid_probe_set_superblocks_flags(pr
,
267 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
268 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
269 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
|
270 BLKID_SUBLKS_BADCSUM
);
273 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
275 fd
= open(udev_device_get_devnode(dev
), O_RDONLY
|O_CLOEXEC
);
277 err
= log_debug_errno(errno
, "Failure opening block device %s: %m", udev_device_get_devnode(dev
));
281 err
= blkid_probe_set_device(pr
, fd
, offset
, 0);
285 log_debug("probe %s %sraid offset=%"PRIi64
,
286 udev_device_get_devnode(dev
),
287 noraid
? "no" : "", offset
);
289 err
= probe_superblocks(pr
);
292 if (blkid_probe_has_value(pr
, "SBBADCSUM")) {
293 if (!blkid_probe_lookup_value(pr
, "TYPE", &prtype
, NULL
))
294 log_warning("incorrect %s checksum on %s",
295 prtype
, udev_device_get_devnode(dev
));
297 log_warning("incorrect checksum on %s",
298 udev_device_get_devnode(dev
));
302 /* If we are a partition then our parent passed on the root
303 * partition UUID to us */
304 root_partition
= udev_device_get_property_value(dev
, "ID_PART_GPT_AUTO_ROOT_UUID");
306 nvals
= blkid_probe_numof_values(pr
);
307 for (i
= 0; i
< nvals
; i
++) {
308 if (blkid_probe_get_value(pr
, i
, &name
, &data
, NULL
))
311 print_property(dev
, test
, name
, data
);
313 /* Is this a disk with GPT partition table? */
314 if (streq(name
, "PTTYPE") && streq(data
, "gpt"))
317 /* Is this a partition that matches the root partition
318 * property we inherited from our parent? */
319 if (root_partition
&& streq(name
, "PART_ENTRY_UUID") && streq(data
, root_partition
))
320 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT", "1");
324 find_gpt_root(dev
, pr
, test
);
326 blkid_free_probe(pr
);
334 const struct udev_builtin udev_builtin_blkid
= {
336 .cmd
= builtin_blkid
,
337 .help
= "Filesystem and partition probing",