2 * probe disks for filesystems and partitions
4 * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
5 * Copyright (C) 2011 Karel Zak <kzak@redhat.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "alloc-util.h"
33 #include "blkid-util.h"
37 #include "string-util.h"
40 static void print_property(struct udev_device
*dev
, bool test
, const char *name
, const char *value
) {
45 if (streq(name
, "TYPE")) {
46 udev_builtin_add_property(dev
, test
, "ID_FS_TYPE", value
);
48 } else if (streq(name
, "USAGE")) {
49 udev_builtin_add_property(dev
, test
, "ID_FS_USAGE", value
);
51 } else if (streq(name
, "VERSION")) {
52 udev_builtin_add_property(dev
, test
, "ID_FS_VERSION", value
);
54 } else if (streq(name
, "UUID")) {
55 blkid_safe_string(value
, s
, sizeof(s
));
56 udev_builtin_add_property(dev
, test
, "ID_FS_UUID", s
);
57 blkid_encode_string(value
, s
, sizeof(s
));
58 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_ENC", s
);
60 } else if (streq(name
, "UUID_SUB")) {
61 blkid_safe_string(value
, s
, sizeof(s
));
62 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB", s
);
63 blkid_encode_string(value
, s
, sizeof(s
));
64 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB_ENC", s
);
66 } else if (streq(name
, "LABEL")) {
67 blkid_safe_string(value
, s
, sizeof(s
));
68 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL", s
);
69 blkid_encode_string(value
, s
, sizeof(s
));
70 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL_ENC", s
);
72 } else if (streq(name
, "PTTYPE")) {
73 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_TYPE", value
);
75 } else if (streq(name
, "PTUUID")) {
76 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_UUID", value
);
78 } else if (streq(name
, "PART_ENTRY_NAME")) {
79 blkid_encode_string(value
, s
, sizeof(s
));
80 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_NAME", s
);
82 } else if (streq(name
, "PART_ENTRY_TYPE")) {
83 blkid_encode_string(value
, s
, sizeof(s
));
84 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_TYPE", s
);
86 } else if (startswith(name
, "PART_ENTRY_")) {
87 strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
88 udev_builtin_add_property(dev
, test
, s
, value
);
90 } else if (streq(name
, "SYSTEM_ID")) {
91 blkid_encode_string(value
, s
, sizeof(s
));
92 udev_builtin_add_property(dev
, test
, "ID_FS_SYSTEM_ID", s
);
94 } else if (streq(name
, "PUBLISHER_ID")) {
95 blkid_encode_string(value
, s
, sizeof(s
));
96 udev_builtin_add_property(dev
, test
, "ID_FS_PUBLISHER_ID", s
);
98 } else if (streq(name
, "APPLICATION_ID")) {
99 blkid_encode_string(value
, s
, sizeof(s
));
100 udev_builtin_add_property(dev
, test
, "ID_FS_APPLICATION_ID", s
);
102 } else if (streq(name
, "BOOT_SYSTEM_ID")) {
103 blkid_encode_string(value
, s
, sizeof(s
));
104 udev_builtin_add_property(dev
, test
, "ID_FS_BOOT_SYSTEM_ID", s
);
108 static int find_gpt_root(struct udev_device
*dev
, blkid_probe pr
, bool test
) {
110 #if defined(GPT_ROOT_NATIVE) && defined(ENABLE_EFI)
112 _cleanup_free_
char *root_id
= NULL
;
113 bool found_esp
= false;
119 /* Iterate through the partitions on this disk, and see if the
120 * EFI ESP we booted from is on it. If so, find the first root
121 * disk, and add a property indicating its partition UUID. */
124 pl
= blkid_probe_get_partitions(pr
);
126 return -errno
?: -ENOMEM
;
128 nvals
= blkid_partlist_numof_partitions(pl
);
129 for (i
= 0; i
< nvals
; i
++) {
131 const char *stype
, *sid
;
134 pp
= blkid_partlist_get_partition(pl
, i
);
138 sid
= blkid_partition_get_uuid(pp
);
142 stype
= blkid_partition_get_type_string(pp
);
146 if (sd_id128_from_string(stype
, &type
) < 0)
149 if (sd_id128_equal(type
, GPT_ESP
)) {
152 /* We found an ESP, let's see if it matches
153 * the ESP we booted from. */
155 if (sd_id128_from_string(sid
, &id
) < 0)
158 r
= efi_loader_get_device_part_uuid(&esp
);
162 if (sd_id128_equal(id
, esp
))
165 } else if (sd_id128_equal(type
, GPT_ROOT_NATIVE
)) {
166 unsigned long long flags
;
168 flags
= blkid_partition_get_flags(pp
);
169 if (flags
& GPT_FLAG_NO_AUTO
)
172 /* We found a suitable root partition, let's
173 * remember the first one. */
176 root_id
= strdup(sid
);
183 /* We found the ESP on this disk, and also found a root
184 * partition, nice! Let's export its UUID */
185 if (found_esp
&& root_id
)
186 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT_UUID", root_id
);
192 static int probe_superblocks(blkid_probe pr
) {
196 if (fstat(blkid_probe_get_fd(pr
), &st
))
199 blkid_probe_enable_partitions(pr
, 1);
201 if (!S_ISCHR(st
.st_mode
) &&
202 blkid_probe_get_size(pr
) <= 1024 * 1440 &&
203 blkid_probe_is_wholedisk(pr
)) {
205 * check if the small disk is partitioned, if yes then
206 * don't probe for filesystems.
208 blkid_probe_enable_superblocks(pr
, 0);
210 rc
= blkid_do_fullprobe(pr
);
212 return rc
; /* -1 = error, 1 = nothing, 0 = success */
214 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
215 return 0; /* partition table detected */
218 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
219 blkid_probe_enable_superblocks(pr
, 1);
221 return blkid_do_safeprobe(pr
);
224 static int builtin_blkid(struct udev_device
*dev
, int argc
, char *argv
[], bool test
) {
225 const char *root_partition
;
228 _cleanup_close_
int fd
= -1;
229 _cleanup_blkid_free_probe_ blkid_probe pr
= NULL
;
237 static const struct option options
[] = {
238 { "offset", optional_argument
, NULL
, 'o' },
239 { "noraid", no_argument
, NULL
, 'R' },
246 option
= getopt_long(argc
, argv
, "oR", options
, NULL
);
252 offset
= strtoull(optarg
, NULL
, 0);
260 pr
= blkid_new_probe();
264 blkid_probe_set_superblocks_flags(pr
,
265 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
266 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
267 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
270 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
272 fd
= open(udev_device_get_devnode(dev
), O_RDONLY
|O_CLOEXEC
);
274 err
= log_debug_errno(errno
, "Failure opening block device %s: %m", udev_device_get_devnode(dev
));
278 err
= blkid_probe_set_device(pr
, fd
, offset
, 0);
282 log_debug("probe %s %sraid offset=%"PRIi64
,
283 udev_device_get_devnode(dev
),
284 noraid
? "no" : "", offset
);
286 err
= probe_superblocks(pr
);
290 /* If we are a partition then our parent passed on the root
291 * partition UUID to us */
292 root_partition
= udev_device_get_property_value(dev
, "ID_PART_GPT_AUTO_ROOT_UUID");
294 nvals
= blkid_probe_numof_values(pr
);
295 for (i
= 0; i
< nvals
; i
++) {
296 if (blkid_probe_get_value(pr
, i
, &name
, &data
, NULL
))
299 print_property(dev
, test
, name
, data
);
301 /* Is this a disk with GPT partition table? */
302 if (streq(name
, "PTTYPE") && streq(data
, "gpt"))
305 /* Is this a partition that matches the root partition
306 * property we inherited from our parent? */
307 if (root_partition
&& streq(name
, "PART_ENTRY_UUID") && streq(data
, root_partition
))
308 udev_builtin_add_property(dev
, test
, "ID_PART_GPT_AUTO_ROOT", "1");
312 find_gpt_root(dev
, pr
, test
);
321 const struct udev_builtin udev_builtin_blkid
= {
323 .cmd
= builtin_blkid
,
324 .help
= "Filesystem and partition probing",