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