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