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/>.
30 #include <blkid/blkid.h>
34 static void print_property(struct udev_device
*dev
, bool test
, const char *name
, const char *value
)
40 if (streq(name
, "TYPE")) {
41 udev_builtin_add_property(dev
, test
, "ID_FS_TYPE", value
);
43 } else if (streq(name
, "USAGE")) {
44 udev_builtin_add_property(dev
, test
, "ID_FS_USAGE", value
);
46 } else if (streq(name
, "VERSION")) {
47 udev_builtin_add_property(dev
, test
, "ID_FS_VERSION", value
);
49 } else if (streq(name
, "UUID")) {
50 blkid_safe_string(value
, s
, sizeof(s
));
51 udev_builtin_add_property(dev
, test
, "ID_FS_UUID", s
);
52 blkid_encode_string(value
, s
, sizeof(s
));
53 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_ENC", s
);
55 } else if (streq(name
, "UUID_SUB")) {
56 blkid_safe_string(value
, s
, sizeof(s
));
57 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB", s
);
58 blkid_encode_string(value
, s
, sizeof(s
));
59 udev_builtin_add_property(dev
, test
, "ID_FS_UUID_SUB_ENC", s
);
61 } else if (streq(name
, "LABEL")) {
62 blkid_safe_string(value
, s
, sizeof(s
));
63 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL", s
);
64 blkid_encode_string(value
, s
, sizeof(s
));
65 udev_builtin_add_property(dev
, test
, "ID_FS_LABEL_ENC", s
);
67 } else if (streq(name
, "PTTYPE")) {
68 udev_builtin_add_property(dev
, test
, "ID_PART_TABLE_TYPE", value
);
70 } else if (streq(name
, "PART_ENTRY_NAME")) {
71 blkid_encode_string(value
, s
, sizeof(s
));
72 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_NAME", s
);
74 } else if (streq(name
, "PART_ENTRY_TYPE")) {
75 blkid_encode_string(value
, s
, sizeof(s
));
76 udev_builtin_add_property(dev
, test
, "ID_PART_ENTRY_TYPE", s
);
78 } else if (startswith(name
, "PART_ENTRY_")) {
79 util_strscpyl(s
, sizeof(s
), "ID_", name
, NULL
);
80 udev_builtin_add_property(dev
, test
, s
, value
);
84 static int probe_superblocks(blkid_probe pr
)
89 if (fstat(blkid_probe_get_fd(pr
), &st
))
92 blkid_probe_enable_partitions(pr
, 1);
94 if (!S_ISCHR(st
.st_mode
) && blkid_probe_get_size(pr
) <= 1024 * 1440 &&
95 blkid_probe_is_wholedisk(pr
)) {
97 * check if the small disk is partitioned, if yes then
98 * don't probe for filesystems.
100 blkid_probe_enable_superblocks(pr
, 0);
102 rc
= blkid_do_fullprobe(pr
);
104 return rc
; /* -1 = error, 1 = nothing, 0 = succes */
106 if (blkid_probe_lookup_value(pr
, "PTTYPE", NULL
, NULL
) == 0)
107 return 0; /* partition table detected */
110 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_ENTRY_DETAILS
);
111 blkid_probe_enable_superblocks(pr
, 1);
113 return blkid_do_safeprobe(pr
);
116 static int builtin_blkid(struct udev_device
*dev
, int argc
, char *argv
[], bool test
)
129 static const struct option options
[] = {
130 { "offset", optional_argument
, NULL
, 'o' },
131 { "noraid", no_argument
, NULL
, 'R' },
138 option
= getopt_long(argc
, argv
, "oR", options
, NULL
);
144 offset
= strtoull(optarg
, NULL
, 0);
152 pr
= blkid_new_probe();
156 blkid_probe_set_superblocks_flags(pr
,
157 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
158 BLKID_SUBLKS_TYPE
| BLKID_SUBLKS_SECTYPE
|
159 BLKID_SUBLKS_USAGE
| BLKID_SUBLKS_VERSION
);
162 blkid_probe_filter_superblocks_usage(pr
, BLKID_FLTR_NOTIN
, BLKID_USAGE_RAID
);
164 fd
= open(udev_device_get_devnode(dev
), O_RDONLY
|O_CLOEXEC
);
166 fprintf(stderr
, "error: %s: %m\n", udev_device_get_devnode(dev
));
170 err
= blkid_probe_set_device(pr
, fd
, offset
, 0);
174 log_debug("probe %s %sraid offset=%llu\n",
175 udev_device_get_devnode(dev
),
176 noraid
? "no" : "", (unsigned long long) offset
);
178 err
= probe_superblocks(pr
);
182 nvals
= blkid_probe_numof_values(pr
);
183 for (i
= 0; i
< nvals
; i
++) {
184 if (blkid_probe_get_value(pr
, i
, &name
, &data
, &len
))
186 len
= strnlen((char *) data
, len
);
187 print_property(dev
, test
, name
, (char *) data
);
190 blkid_free_probe(pr
);
199 const struct udev_builtin udev_builtin_blkid
= {
201 .cmd
= builtin_blkid
,
202 .help
= "filesystem and partition probing",