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