]>
Commit | Line | Data |
---|---|---|
81dadce5 KS |
1 | /* |
2 | * probe disks for filesystems and partitions | |
3 | * | |
1298001e | 4 | * Copyright (C) 2011 Kay Sievers <kay@vrfy.org> |
81dadce5 KS |
5 | * Copyright (C) 2011 Karel Zak <kzak@redhat.com> |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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/>. | |
19 | */ | |
20 | ||
07630cea | 21 | #include <blkid/blkid.h> |
81dadce5 KS |
22 | #include <errno.h> |
23 | #include <fcntl.h> | |
c38a141b | 24 | #include <getopt.h> |
07630cea LP |
25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
81dadce5 | 28 | #include <sys/stat.h> |
81dadce5 | 29 | |
cbd353ce | 30 | #include "sd-id128.h" |
07630cea | 31 | |
cbd353ce | 32 | #include "efivars.h" |
3ffd4af2 | 33 | #include "fd-util.h" |
07630cea LP |
34 | #include "gpt.h" |
35 | #include "string-util.h" | |
81dadce5 KS |
36 | #include "udev.h" |
37 | ||
9ec6e95b | 38 | static void print_property(struct udev_device *dev, bool test, const char *name, const char *value) { |
d13394a8 | 39 | char s[256]; |
81dadce5 | 40 | |
912541b0 | 41 | s[0] = '\0'; |
81dadce5 | 42 | |
33502ffe | 43 | if (streq(name, "TYPE")) { |
912541b0 | 44 | udev_builtin_add_property(dev, test, "ID_FS_TYPE", value); |
81dadce5 | 45 | |
33502ffe | 46 | } else if (streq(name, "USAGE")) { |
912541b0 | 47 | udev_builtin_add_property(dev, test, "ID_FS_USAGE", value); |
e5d8ce91 | 48 | |
33502ffe | 49 | } else if (streq(name, "VERSION")) { |
912541b0 | 50 | udev_builtin_add_property(dev, test, "ID_FS_VERSION", value); |
81dadce5 | 51 | |
33502ffe | 52 | } else if (streq(name, "UUID")) { |
912541b0 KS |
53 | blkid_safe_string(value, s, sizeof(s)); |
54 | udev_builtin_add_property(dev, test, "ID_FS_UUID", s); | |
55 | blkid_encode_string(value, s, sizeof(s)); | |
56 | udev_builtin_add_property(dev, test, "ID_FS_UUID_ENC", s); | |
81dadce5 | 57 | |
33502ffe | 58 | } else if (streq(name, "UUID_SUB")) { |
912541b0 KS |
59 | blkid_safe_string(value, s, sizeof(s)); |
60 | udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB", s); | |
61 | blkid_encode_string(value, s, sizeof(s)); | |
62 | udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB_ENC", s); | |
81dadce5 | 63 | |
33502ffe | 64 | } else if (streq(name, "LABEL")) { |
912541b0 KS |
65 | blkid_safe_string(value, s, sizeof(s)); |
66 | udev_builtin_add_property(dev, test, "ID_FS_LABEL", s); | |
67 | blkid_encode_string(value, s, sizeof(s)); | |
68 | udev_builtin_add_property(dev, test, "ID_FS_LABEL_ENC", s); | |
81dadce5 | 69 | |
33502ffe | 70 | } else if (streq(name, "PTTYPE")) { |
912541b0 | 71 | udev_builtin_add_property(dev, test, "ID_PART_TABLE_TYPE", value); |
81dadce5 | 72 | |
894a156d DR |
73 | } else if (streq(name, "PTUUID")) { |
74 | udev_builtin_add_property(dev, test, "ID_PART_TABLE_UUID", value); | |
75 | ||
33502ffe | 76 | } else if (streq(name, "PART_ENTRY_NAME")) { |
912541b0 | 77 | blkid_encode_string(value, s, sizeof(s)); |
1b9e13e2 | 78 | udev_builtin_add_property(dev, test, "ID_PART_ENTRY_NAME", s); |
81dadce5 | 79 | |
33502ffe | 80 | } else if (streq(name, "PART_ENTRY_TYPE")) { |
912541b0 | 81 | blkid_encode_string(value, s, sizeof(s)); |
1b9e13e2 | 82 | udev_builtin_add_property(dev, test, "ID_PART_ENTRY_TYPE", s); |
81dadce5 | 83 | |
33502ffe | 84 | } else if (startswith(name, "PART_ENTRY_")) { |
d5a89d7d | 85 | strscpyl(s, sizeof(s), "ID_", name, NULL); |
a3642381 | 86 | udev_builtin_add_property(dev, test, s, value); |
ddb5bee1 ZAK |
87 | |
88 | } else if (streq(name, "SYSTEM_ID")) { | |
89 | blkid_encode_string(value, s, sizeof(s)); | |
90 | udev_builtin_add_property(dev, test, "ID_FS_SYSTEM_ID", s); | |
91 | ||
92 | } else if (streq(name, "PUBLISHER_ID")) { | |
93 | blkid_encode_string(value, s, sizeof(s)); | |
94 | udev_builtin_add_property(dev, test, "ID_FS_PUBLISHER_ID", s); | |
95 | ||
96 | } else if (streq(name, "APPLICATION_ID")) { | |
97 | blkid_encode_string(value, s, sizeof(s)); | |
98 | udev_builtin_add_property(dev, test, "ID_FS_APPLICATION_ID", s); | |
99 | ||
100 | } else if (streq(name, "BOOT_SYSTEM_ID")) { | |
101 | blkid_encode_string(value, s, sizeof(s)); | |
102 | udev_builtin_add_property(dev, test, "ID_FS_BOOT_SYSTEM_ID", s); | |
912541b0 | 103 | } |
81dadce5 KS |
104 | } |
105 | ||
cbd353ce LP |
106 | static int find_gpt_root(struct udev_device *dev, blkid_probe pr, bool test) { |
107 | ||
108 | #if defined(GPT_ROOT_NATIVE) && defined(ENABLE_EFI) | |
109 | ||
110 | _cleanup_free_ char *root_id = NULL; | |
111 | bool found_esp = false; | |
112 | blkid_partlist pl; | |
113 | int i, nvals, r; | |
114 | ||
115 | assert(pr); | |
116 | ||
117 | /* Iterate through the partitions on this disk, and see if the | |
118 | * EFI ESP we booted from is on it. If so, find the first root | |
119 | * disk, and add a property indicating its partition UUID. */ | |
120 | ||
121 | errno = 0; | |
122 | pl = blkid_probe_get_partitions(pr); | |
123 | if (!pl) | |
124 | return errno ? -errno : -ENOMEM; | |
125 | ||
126 | nvals = blkid_partlist_numof_partitions(pl); | |
127 | for (i = 0; i < nvals; i++) { | |
128 | blkid_partition pp; | |
129 | const char *stype, *sid; | |
130 | sd_id128_t type; | |
131 | ||
132 | pp = blkid_partlist_get_partition(pl, i); | |
133 | if (!pp) | |
134 | continue; | |
135 | ||
136 | sid = blkid_partition_get_uuid(pp); | |
137 | if (!sid) | |
138 | continue; | |
139 | ||
140 | stype = blkid_partition_get_type_string(pp); | |
141 | if (!stype) | |
142 | continue; | |
143 | ||
144 | if (sd_id128_from_string(stype, &type) < 0) | |
145 | continue; | |
146 | ||
147 | if (sd_id128_equal(type, GPT_ESP)) { | |
148 | sd_id128_t id, esp; | |
0238d4c6 KS |
149 | unsigned long long flags; |
150 | ||
151 | flags = blkid_partition_get_flags(pp); | |
152 | if (flags & GPT_FLAG_NO_AUTO) | |
153 | continue; | |
cbd353ce LP |
154 | |
155 | /* We found an ESP, let's see if it matches | |
156 | * the ESP we booted from. */ | |
157 | ||
158 | if (sd_id128_from_string(sid, &id) < 0) | |
159 | continue; | |
160 | ||
161 | r = efi_loader_get_device_part_uuid(&esp); | |
162 | if (r < 0) | |
163 | return r; | |
164 | ||
165 | if (sd_id128_equal(id, esp)) | |
166 | found_esp = true; | |
167 | ||
168 | } else if (sd_id128_equal(type, GPT_ROOT_NATIVE)) { | |
169 | ||
170 | /* We found a suitable root partition, let's | |
171 | * remember the first one. */ | |
172 | ||
173 | if (!root_id) { | |
174 | root_id = strdup(sid); | |
175 | if (!root_id) | |
176 | return -ENOMEM; | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | /* We found the ESP on this disk, and also found a root | |
329f7803 | 182 | * partition, nice! Let's export its UUID */ |
cbd353ce | 183 | if (found_esp && root_id) |
329f7803 | 184 | udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT_UUID", root_id); |
cbd353ce LP |
185 | #endif |
186 | ||
187 | return 0; | |
188 | } | |
189 | ||
9ec6e95b | 190 | static int probe_superblocks(blkid_probe pr) { |
912541b0 KS |
191 | struct stat st; |
192 | int rc; | |
81dadce5 | 193 | |
912541b0 KS |
194 | if (fstat(blkid_probe_get_fd(pr), &st)) |
195 | return -1; | |
81dadce5 | 196 | |
912541b0 | 197 | blkid_probe_enable_partitions(pr, 1); |
81dadce5 | 198 | |
d13394a8 LP |
199 | if (!S_ISCHR(st.st_mode) && |
200 | blkid_probe_get_size(pr) <= 1024 * 1440 && | |
912541b0 KS |
201 | blkid_probe_is_wholedisk(pr)) { |
202 | /* | |
203 | * check if the small disk is partitioned, if yes then | |
204 | * don't probe for filesystems. | |
205 | */ | |
206 | blkid_probe_enable_superblocks(pr, 0); | |
81dadce5 | 207 | |
912541b0 KS |
208 | rc = blkid_do_fullprobe(pr); |
209 | if (rc < 0) | |
d13394a8 | 210 | return rc; /* -1 = error, 1 = nothing, 0 = success */ |
81dadce5 | 211 | |
912541b0 KS |
212 | if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0) |
213 | return 0; /* partition table detected */ | |
214 | } | |
81dadce5 | 215 | |
912541b0 KS |
216 | blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS); |
217 | blkid_probe_enable_superblocks(pr, 1); | |
81dadce5 | 218 | |
912541b0 | 219 | return blkid_do_safeprobe(pr); |
81dadce5 KS |
220 | } |
221 | ||
9ec6e95b | 222 | static int builtin_blkid(struct udev_device *dev, int argc, char *argv[], bool test) { |
329f7803 | 223 | const char *root_partition; |
912541b0 KS |
224 | int64_t offset = 0; |
225 | bool noraid = false; | |
d13394a8 | 226 | _cleanup_close_ int fd = -1; |
912541b0 KS |
227 | blkid_probe pr; |
228 | const char *data; | |
229 | const char *name; | |
d47f6ca5 | 230 | const char *prtype = NULL; |
912541b0 KS |
231 | int nvals; |
232 | int i; | |
912541b0 | 233 | int err = 0; |
cbd353ce | 234 | bool is_gpt = false; |
912541b0 KS |
235 | |
236 | static const struct option options[] = { | |
237 | { "offset", optional_argument, NULL, 'o' }, | |
238 | { "noraid", no_argument, NULL, 'R' }, | |
239 | {} | |
240 | }; | |
241 | ||
242 | for (;;) { | |
243 | int option; | |
244 | ||
245 | option = getopt_long(argc, argv, "oR", options, NULL); | |
246 | if (option == -1) | |
247 | break; | |
248 | ||
249 | switch (option) { | |
250 | case 'o': | |
251 | offset = strtoull(optarg, NULL, 0); | |
252 | break; | |
253 | case 'R': | |
254 | noraid = true; | |
255 | break; | |
256 | } | |
257 | } | |
258 | ||
259 | pr = blkid_new_probe(); | |
b49d9b50 | 260 | if (!pr) |
912541b0 | 261 | return EXIT_FAILURE; |
912541b0 KS |
262 | |
263 | blkid_probe_set_superblocks_flags(pr, | |
264 | BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | | |
265 | BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE | | |
d47f6ca5 GP |
266 | BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION | |
267 | BLKID_SUBLKS_BADCSUM); | |
912541b0 KS |
268 | |
269 | if (noraid) | |
270 | blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID); | |
271 | ||
272 | fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC); | |
273 | if (fd < 0) { | |
8d8ce9e2 | 274 | err = log_debug_errno(errno, "Failure opening block device %s: %m", udev_device_get_devnode(dev)); |
912541b0 KS |
275 | goto out; |
276 | } | |
277 | ||
278 | err = blkid_probe_set_device(pr, fd, offset, 0); | |
279 | if (err < 0) | |
280 | goto out; | |
281 | ||
1fa2f38f | 282 | log_debug("probe %s %sraid offset=%"PRIi64, |
baa30fbc | 283 | udev_device_get_devnode(dev), |
de0671ee | 284 | noraid ? "no" : "", offset); |
912541b0 KS |
285 | |
286 | err = probe_superblocks(pr); | |
287 | if (err < 0) | |
288 | goto out; | |
d47f6ca5 GP |
289 | if (blkid_probe_has_value(pr, "SBBADCSUM")) { |
290 | if (!blkid_probe_lookup_value(pr, "TYPE", &prtype, NULL)) | |
291 | log_warning("incorrect %s checksum on %s", | |
292 | prtype, udev_device_get_devnode(dev)); | |
293 | else | |
294 | log_warning("incorrect checksum on %s", | |
295 | udev_device_get_devnode(dev)); | |
296 | goto out; | |
297 | } | |
912541b0 | 298 | |
329f7803 LP |
299 | /* If we are a partition then our parent passed on the root |
300 | * partition UUID to us */ | |
301 | root_partition = udev_device_get_property_value(dev, "ID_PART_GPT_AUTO_ROOT_UUID"); | |
302 | ||
912541b0 KS |
303 | nvals = blkid_probe_numof_values(pr); |
304 | for (i = 0; i < nvals; i++) { | |
8a39439e | 305 | if (blkid_probe_get_value(pr, i, &name, &data, NULL)) |
912541b0 | 306 | continue; |
cbd353ce | 307 | |
8a39439e | 308 | print_property(dev, test, name, data); |
cbd353ce | 309 | |
329f7803 | 310 | /* Is this a disk with GPT partition table? */ |
cbd353ce LP |
311 | if (streq(name, "PTTYPE") && streq(data, "gpt")) |
312 | is_gpt = true; | |
329f7803 LP |
313 | |
314 | /* Is this a partition that matches the root partition | |
315 | * property we inherited from our parent? */ | |
316 | if (root_partition && streq(name, "PART_ENTRY_UUID") && streq(data, root_partition)) | |
317 | udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT", "1"); | |
912541b0 KS |
318 | } |
319 | ||
cbd353ce LP |
320 | if (is_gpt) |
321 | find_gpt_root(dev, pr, test); | |
322 | ||
912541b0 | 323 | blkid_free_probe(pr); |
81dadce5 | 324 | out: |
912541b0 KS |
325 | if (err < 0) |
326 | return EXIT_FAILURE; | |
d13394a8 | 327 | |
912541b0 | 328 | return EXIT_SUCCESS; |
81dadce5 KS |
329 | } |
330 | ||
331 | const struct udev_builtin udev_builtin_blkid = { | |
912541b0 KS |
332 | .name = "blkid", |
333 | .cmd = builtin_blkid, | |
5ac0162c | 334 | .help = "Filesystem and partition probing", |
912541b0 | 335 | .run_once = true, |
81dadce5 | 336 | }; |