]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-blkid.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / udev / udev-builtin-blkid.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * probe disks for filesystems and partitions
4 *
5 * Copyright © 2011 Karel Zak <kzak@redhat.com>
6 */
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <getopt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <sys/stat.h>
14
15 #include "sd-id128.h"
16
17 #include "alloc-util.h"
18 #include "blkid-util.h"
19 #include "device-util.h"
20 #include "efi-loader.h"
21 #include "errno-util.h"
22 #include "fd-util.h"
23 #include "gpt.h"
24 #include "parse-util.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "strxcpyx.h"
28 #include "udev-builtin.h"
29
30 static void print_property(sd_device *dev, bool test, const char *name, const char *value) {
31 char s[256];
32
33 s[0] = '\0';
34
35 if (streq(name, "TYPE")) {
36 udev_builtin_add_property(dev, test, "ID_FS_TYPE", value);
37
38 } else if (streq(name, "USAGE")) {
39 udev_builtin_add_property(dev, test, "ID_FS_USAGE", value);
40
41 } else if (streq(name, "VERSION")) {
42 udev_builtin_add_property(dev, test, "ID_FS_VERSION", value);
43
44 } else if (streq(name, "UUID")) {
45 blkid_safe_string(value, s, sizeof(s));
46 udev_builtin_add_property(dev, test, "ID_FS_UUID", s);
47 blkid_encode_string(value, s, sizeof(s));
48 udev_builtin_add_property(dev, test, "ID_FS_UUID_ENC", s);
49
50 } else if (streq(name, "UUID_SUB")) {
51 blkid_safe_string(value, s, sizeof(s));
52 udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB", s);
53 blkid_encode_string(value, s, sizeof(s));
54 udev_builtin_add_property(dev, test, "ID_FS_UUID_SUB_ENC", s);
55
56 } else if (streq(name, "LABEL")) {
57 blkid_safe_string(value, s, sizeof(s));
58 udev_builtin_add_property(dev, test, "ID_FS_LABEL", s);
59 blkid_encode_string(value, s, sizeof(s));
60 udev_builtin_add_property(dev, test, "ID_FS_LABEL_ENC", s);
61
62 } else if (STR_IN_SET(name, "FSSIZE", "FSLASTBLOCK", "FSBLOCKSIZE")) {
63 strscpyl(s, sizeof(s), "ID_FS_", name + 2, NULL);
64 udev_builtin_add_property(dev, test, s, value);
65
66 } else if (streq(name, "PTTYPE")) {
67 udev_builtin_add_property(dev, test, "ID_PART_TABLE_TYPE", value);
68
69 } else if (streq(name, "PTUUID")) {
70 udev_builtin_add_property(dev, test, "ID_PART_TABLE_UUID", value);
71
72 } else if (streq(name, "PART_ENTRY_NAME")) {
73 blkid_encode_string(value, s, sizeof(s));
74 udev_builtin_add_property(dev, test, "ID_PART_ENTRY_NAME", s);
75
76 } else if (streq(name, "PART_ENTRY_TYPE")) {
77 blkid_encode_string(value, s, sizeof(s));
78 udev_builtin_add_property(dev, test, "ID_PART_ENTRY_TYPE", s);
79
80 } else if (startswith(name, "PART_ENTRY_")) {
81 strscpyl(s, sizeof(s), "ID_", name, NULL);
82 udev_builtin_add_property(dev, test, s, value);
83
84 } else if (streq(name, "SYSTEM_ID")) {
85 blkid_encode_string(value, s, sizeof(s));
86 udev_builtin_add_property(dev, test, "ID_FS_SYSTEM_ID", s);
87
88 } else if (streq(name, "PUBLISHER_ID")) {
89 blkid_encode_string(value, s, sizeof(s));
90 udev_builtin_add_property(dev, test, "ID_FS_PUBLISHER_ID", s);
91
92 } else if (streq(name, "APPLICATION_ID")) {
93 blkid_encode_string(value, s, sizeof(s));
94 udev_builtin_add_property(dev, test, "ID_FS_APPLICATION_ID", s);
95
96 } else if (streq(name, "BOOT_SYSTEM_ID")) {
97 blkid_encode_string(value, s, sizeof(s));
98 udev_builtin_add_property(dev, test, "ID_FS_BOOT_SYSTEM_ID", s);
99
100 } else if (streq(name, "VOLUME_ID")) {
101 blkid_encode_string(value, s, sizeof(s));
102 udev_builtin_add_property(dev, test, "ID_FS_VOLUME_ID", s);
103
104 } else if (streq(name, "LOGICAL_VOLUME_ID")) {
105 blkid_encode_string(value, s, sizeof(s));
106 udev_builtin_add_property(dev, test, "ID_FS_LOGICAL_VOLUME_ID", s);
107
108 } else if (streq(name, "VOLUME_SET_ID")) {
109 blkid_encode_string(value, s, sizeof(s));
110 udev_builtin_add_property(dev, test, "ID_FS_VOLUME_SET_ID", s);
111
112 } else if (streq(name, "DATA_PREPARER_ID")) {
113 blkid_encode_string(value, s, sizeof(s));
114 udev_builtin_add_property(dev, test, "ID_FS_DATA_PREPARER_ID", s);
115 }
116 }
117
118 static int find_gpt_root(sd_device *dev, blkid_probe pr, bool test) {
119
120 #if defined(SD_GPT_ROOT_NATIVE) && ENABLE_EFI
121
122 _cleanup_free_ char *root_label = NULL;
123 bool found_esp_or_xbootldr = false;
124 sd_id128_t root_id = SD_ID128_NULL;
125 int r;
126
127 assert(pr);
128
129 /* Iterate through the partitions on this disk, and see if the UEFI ESP or XBOOTLDR partition we
130 * booted from is on it. If so, find the first root disk, and add a property indicating its partition
131 * UUID. */
132
133 errno = 0;
134 blkid_partlist pl = blkid_probe_get_partitions(pr);
135 if (!pl)
136 return errno_or_else(ENOMEM);
137
138 int nvals = blkid_partlist_numof_partitions(pl);
139 for (int i = 0; i < nvals; i++) {
140 blkid_partition pp;
141 const char *label;
142 sd_id128_t type, id;
143
144 pp = blkid_partlist_get_partition(pl, i);
145 if (!pp)
146 continue;
147
148 r = blkid_partition_get_uuid_id128(pp, &id);
149 if (r < 0) {
150 log_debug_errno(r, "Failed to get partition UUID, ignoring: %m");
151 continue;
152 }
153
154 r = blkid_partition_get_type_id128(pp, &type);
155 if (r < 0) {
156 log_debug_errno(r, "Failed to get partition type UUID, ignoring: %m");
157 continue;
158 }
159
160 label = blkid_partition_get_name(pp); /* returns NULL if empty */
161
162 if (sd_id128_in_set(type, SD_GPT_ESP, SD_GPT_XBOOTLDR)) {
163 sd_id128_t esp_or_xbootldr;
164
165 /* We found an ESP or XBOOTLDR, let's see if it matches the ESP/XBOOTLDR we booted from. */
166
167 r = efi_loader_get_device_part_uuid(&esp_or_xbootldr);
168 if (r < 0)
169 return r;
170
171 if (sd_id128_equal(id, esp_or_xbootldr))
172 found_esp_or_xbootldr = true;
173
174 } else if (sd_id128_equal(type, SD_GPT_ROOT_NATIVE)) {
175 unsigned long long flags;
176
177 flags = blkid_partition_get_flags(pp);
178 if (flags & SD_GPT_FLAG_NO_AUTO)
179 continue;
180
181 /* We found a suitable root partition, let's remember the first one, or the one with
182 * the newest version, as determined by comparing the partition labels. */
183
184 if (sd_id128_is_null(root_id) || strverscmp_improved(label, root_label) > 0) {
185 root_id = id;
186
187 r = free_and_strdup(&root_label, label);
188 if (r < 0)
189 return r;
190 }
191 }
192 }
193
194 /* We found the ESP/XBOOTLDR on this disk, and also found a root partition, nice! Let's export its
195 * UUID */
196 if (found_esp_or_xbootldr && !sd_id128_is_null(root_id))
197 udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT_UUID", SD_ID128_TO_UUID_STRING(root_id));
198 #endif
199
200 return 0;
201 }
202
203 static int probe_superblocks(blkid_probe pr) {
204 struct stat st;
205 int rc;
206
207 /* TODO: Return negative errno. */
208
209 if (fstat(blkid_probe_get_fd(pr), &st))
210 return -errno;
211
212 blkid_probe_enable_partitions(pr, 1);
213
214 if (!S_ISCHR(st.st_mode) &&
215 blkid_probe_get_size(pr) <= 1024 * 1440 &&
216 blkid_probe_is_wholedisk(pr)) {
217 /*
218 * check if the small disk is partitioned, if yes then
219 * don't probe for filesystems.
220 */
221 blkid_probe_enable_superblocks(pr, 0);
222
223 rc = blkid_do_fullprobe(pr);
224 if (rc < 0)
225 return rc; /* -1 = error, 1 = nothing, 0 = success */
226
227 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
228 return 0; /* partition table detected */
229 }
230
231 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
232 blkid_probe_enable_superblocks(pr, 1);
233
234 return blkid_do_safeprobe(pr);
235 }
236
237 static int builtin_blkid(sd_device *dev, sd_netlink **rtnl, int argc, char *argv[], bool test) {
238 const char *devnode, *root_partition = NULL, *data, *name;
239 _cleanup_(blkid_free_probep) blkid_probe pr = NULL;
240 bool noraid = false, is_gpt = false;
241 _cleanup_close_ int fd = -EBADF;
242 int64_t offset = 0;
243 int r;
244
245 static const struct option options[] = {
246 { "offset", required_argument, NULL, 'o' },
247 { "hint", required_argument, NULL, 'H' },
248 { "noraid", no_argument, NULL, 'R' },
249 {}
250 };
251
252 errno = 0;
253 pr = blkid_new_probe();
254 if (!pr)
255 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to create blkid prober: %m");
256
257 for (;;) {
258 int option;
259
260 option = getopt_long(argc, argv, "o:H:R", options, NULL);
261 if (option == -1)
262 break;
263
264 switch (option) {
265 case 'H':
266 #if HAVE_BLKID_PROBE_SET_HINT
267 errno = 0;
268 r = blkid_probe_set_hint(pr, optarg, 0);
269 if (r < 0)
270 return log_device_error_errno(dev, errno_or_else(ENOMEM), "Failed to use '%s' probing hint: %m", optarg);
271 break;
272 #else
273 /* Use the hint <name>=<offset> as probing offset for old versions */
274 optarg = strchr(optarg, '=');
275 if (!optarg)
276 /* no value means 0, do nothing for old versions */
277 break;
278 ++optarg;
279 _fallthrough_;
280 #endif
281 case 'o':
282 r = safe_atoi64(optarg, &offset);
283 if (r < 0)
284 return log_device_error_errno(dev, r, "Failed to parse '%s' as an integer: %m", optarg);
285 if (offset < 0)
286 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid offset %"PRIi64": %m", offset);
287 break;
288 case 'R':
289 noraid = true;
290 break;
291 }
292 }
293
294 blkid_probe_set_superblocks_flags(pr,
295 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
296 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
297 #ifdef BLKID_SUBLKS_FSINFO
298 BLKID_SUBLKS_FSINFO |
299 #endif
300 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
301
302 if (noraid)
303 blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
304
305 r = sd_device_get_devname(dev, &devnode);
306 if (r < 0)
307 return log_device_debug_errno(dev, r, "Failed to get device name: %m");
308
309 fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
310 if (fd < 0) {
311 bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
312 log_device_debug_errno(dev, fd, "Failed to open block device %s%s: %m",
313 devnode, ignore ? ", ignoring" : "");
314 return ignore ? 0 : fd;
315 }
316
317 errno = 0;
318 r = blkid_probe_set_device(pr, fd, offset, 0);
319 if (r < 0)
320 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to set device to blkid prober: %m");
321
322 log_device_debug(dev, "Probe %s with %sraid and offset=%"PRIi64, devnode, noraid ? "no" : "", offset);
323
324 r = probe_superblocks(pr);
325 if (r < 0)
326 return log_device_debug_errno(dev, r, "Failed to probe superblocks: %m");
327
328 /* If the device is a partition then its parent passed the root partition UUID to the device */
329 (void) sd_device_get_property_value(dev, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition);
330
331 errno = 0;
332 int nvals = blkid_probe_numof_values(pr);
333 if (nvals < 0)
334 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to get number of probed values: %m");
335
336 for (int i = 0; i < nvals; i++) {
337 if (blkid_probe_get_value(pr, i, &name, &data, NULL) < 0)
338 continue;
339
340 print_property(dev, test, name, data);
341
342 /* Is this a disk with GPT partition table? */
343 if (streq(name, "PTTYPE") && streq(data, "gpt"))
344 is_gpt = true;
345
346 /* Is this a partition that matches the root partition
347 * property inherited from the parent? */
348 if (root_partition && streq(name, "PART_ENTRY_UUID") && streq(data, root_partition))
349 udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT", "1");
350 }
351
352 if (is_gpt)
353 find_gpt_root(dev, pr, test);
354
355 return 0;
356 }
357
358 const UdevBuiltin udev_builtin_blkid = {
359 .name = "blkid",
360 .cmd = builtin_blkid,
361 .help = "Filesystem and partition probing",
362 .run_once = true,
363 };