]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-builtin-blkid.c
Merge pull request #32755 from yuwata/test-network-cleanups
[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 #if HAVE_VALGRIND_MEMCHECK_H
9 #include <valgrind/memcheck.h>
10 #endif
11
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <getopt.h>
15 #include <linux/loop.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sys/ioctl.h>
19 #include <sys/stat.h>
20
21 #include "sd-id128.h"
22
23 #include "alloc-util.h"
24 #include "blkid-util.h"
25 #include "device-util.h"
26 #include "devnum-util.h"
27 #include "efi-loader.h"
28 #include "errno-util.h"
29 #include "fd-util.h"
30 #include "gpt.h"
31 #include "parse-util.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "strxcpyx.h"
35 #include "udev-builtin.h"
36
37 static void print_property(sd_device *dev, EventMode mode, 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, mode, "ID_FS_TYPE", value);
44
45 } else if (streq(name, "USAGE")) {
46 udev_builtin_add_property(dev, mode, "ID_FS_USAGE", value);
47
48 } else if (streq(name, "VERSION")) {
49 udev_builtin_add_property(dev, mode, "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, mode, "ID_FS_UUID", s);
54 blkid_encode_string(value, s, sizeof(s));
55 udev_builtin_add_property(dev, mode, "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, mode, "ID_FS_UUID_SUB", s);
60 blkid_encode_string(value, s, sizeof(s));
61 udev_builtin_add_property(dev, mode, "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, mode, "ID_FS_LABEL", s);
66 blkid_encode_string(value, s, sizeof(s));
67 udev_builtin_add_property(dev, mode, "ID_FS_LABEL_ENC", s);
68
69 } else if (STR_IN_SET(name, "FSSIZE", "FSLASTBLOCK", "FSBLOCKSIZE")) {
70 strscpyl(s, sizeof(s), "ID_FS_", name + 2, NULL);
71 udev_builtin_add_property(dev, mode, s, value);
72
73 } else if (streq(name, "PTTYPE")) {
74 udev_builtin_add_property(dev, mode, "ID_PART_TABLE_TYPE", value);
75
76 } else if (streq(name, "PTUUID")) {
77 udev_builtin_add_property(dev, mode, "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, mode, "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, mode, "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, mode, s, value);
90
91 } else if (streq(name, "SYSTEM_ID")) {
92 blkid_encode_string(value, s, sizeof(s));
93 udev_builtin_add_property(dev, mode, "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, mode, "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, mode, "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, mode, "ID_FS_BOOT_SYSTEM_ID", s);
106
107 } else if (streq(name, "VOLUME_ID")) {
108 blkid_encode_string(value, s, sizeof(s));
109 udev_builtin_add_property(dev, mode, "ID_FS_VOLUME_ID", s);
110
111 } else if (streq(name, "LOGICAL_VOLUME_ID")) {
112 blkid_encode_string(value, s, sizeof(s));
113 udev_builtin_add_property(dev, mode, "ID_FS_LOGICAL_VOLUME_ID", s);
114
115 } else if (streq(name, "VOLUME_SET_ID")) {
116 blkid_encode_string(value, s, sizeof(s));
117 udev_builtin_add_property(dev, mode, "ID_FS_VOLUME_SET_ID", s);
118
119 } else if (streq(name, "DATA_PREPARER_ID")) {
120 blkid_encode_string(value, s, sizeof(s));
121 udev_builtin_add_property(dev, mode, "ID_FS_DATA_PREPARER_ID", s);
122 }
123 }
124
125 static int find_gpt_root(sd_device *dev, blkid_probe pr, EventMode mode) {
126
127 #if defined(SD_GPT_ROOT_NATIVE) && ENABLE_EFI
128
129 _cleanup_free_ char *root_label = NULL;
130 bool found_esp_or_xbootldr = false;
131 sd_id128_t root_id = SD_ID128_NULL;
132 int r;
133
134 assert(pr);
135
136 /* Iterate through the partitions on this disk, and see if the UEFI ESP or XBOOTLDR partition we
137 * booted from is on it. If so, find the first root disk, and add a property indicating its partition
138 * UUID. */
139
140 errno = 0;
141 blkid_partlist pl = blkid_probe_get_partitions(pr);
142 if (!pl)
143 return errno_or_else(ENOMEM);
144
145 int nvals = blkid_partlist_numof_partitions(pl);
146 for (int i = 0; i < nvals; i++) {
147 blkid_partition pp;
148 const char *label;
149 sd_id128_t type, id;
150
151 pp = blkid_partlist_get_partition(pl, i);
152 if (!pp)
153 continue;
154
155 r = blkid_partition_get_uuid_id128(pp, &id);
156 if (r < 0) {
157 log_debug_errno(r, "Failed to get partition UUID, ignoring: %m");
158 continue;
159 }
160
161 r = blkid_partition_get_type_id128(pp, &type);
162 if (r < 0) {
163 log_debug_errno(r, "Failed to get partition type UUID, ignoring: %m");
164 continue;
165 }
166
167 label = blkid_partition_get_name(pp); /* returns NULL if empty */
168
169 if (sd_id128_in_set(type, SD_GPT_ESP, SD_GPT_XBOOTLDR)) {
170 sd_id128_t esp_or_xbootldr;
171
172 /* We found an ESP or XBOOTLDR, let's see if it matches the ESP/XBOOTLDR we booted from. */
173
174 r = efi_loader_get_device_part_uuid(&esp_or_xbootldr);
175 if (r < 0)
176 return r;
177
178 if (sd_id128_equal(id, esp_or_xbootldr))
179 found_esp_or_xbootldr = true;
180
181 } else if (sd_id128_equal(type, SD_GPT_ROOT_NATIVE)) {
182 unsigned long long flags;
183
184 flags = blkid_partition_get_flags(pp);
185 if (flags & SD_GPT_FLAG_NO_AUTO)
186 continue;
187
188 /* We found a suitable root partition, let's remember the first one, or the one with
189 * the newest version, as determined by comparing the partition labels. */
190
191 if (sd_id128_is_null(root_id) || strverscmp_improved(label, root_label) > 0) {
192 root_id = id;
193
194 r = free_and_strdup(&root_label, label);
195 if (r < 0)
196 return r;
197 }
198 }
199 }
200
201 /* We found the ESP/XBOOTLDR on this disk, and also found a root partition, nice! Let's export its
202 * UUID */
203 if (found_esp_or_xbootldr && !sd_id128_is_null(root_id))
204 udev_builtin_add_property(dev, mode, "ID_PART_GPT_AUTO_ROOT_UUID", SD_ID128_TO_UUID_STRING(root_id));
205 #endif
206
207 return 0;
208 }
209
210 static int probe_superblocks(blkid_probe pr) {
211 struct stat st;
212 int rc;
213
214 /* TODO: Return negative errno. */
215
216 if (fstat(blkid_probe_get_fd(pr), &st))
217 return -errno;
218
219 blkid_probe_enable_partitions(pr, 1);
220
221 if (!S_ISCHR(st.st_mode) &&
222 blkid_probe_get_size(pr) <= 1024 * 1440 &&
223 blkid_probe_is_wholedisk(pr)) {
224 /*
225 * check if the small disk is partitioned, if yes then
226 * don't probe for filesystems.
227 */
228 blkid_probe_enable_superblocks(pr, 0);
229
230 rc = blkid_do_fullprobe(pr);
231 if (rc < 0)
232 return rc; /* -1 = error, 1 = nothing, 0 = success */
233
234 if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0)
235 return 0; /* partition table detected */
236 }
237
238 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS);
239 blkid_probe_enable_superblocks(pr, 1);
240
241 return blkid_do_safeprobe(pr);
242 }
243
244 static int read_loopback_backing_inode(
245 sd_device *dev,
246 int fd,
247 dev_t *ret_devno,
248 ino_t *ret_inode,
249 char **ret_fname) {
250
251 _cleanup_free_ char *fn = NULL;
252 struct loop_info64 info;
253 const char *name;
254 int r;
255
256 assert(dev);
257 assert(fd >= 0);
258 assert(ret_devno);
259 assert(ret_inode);
260 assert(ret_fname);
261
262 /* Retrieves various fields of the current loopback device backing file, so that we can ultimately
263 * use it to create stable symlinks to loopback block devices, based on what they are backed by. We
264 * pick up inode/device as well as file name field. Note that we pick up the "lo_file_name" field
265 * here, which is an arbitrary free-form string provided by userspace. We do not return the sysfs
266 * attribute loop/backing_file here, because that is directly accessible from udev rules anyway. And
267 * sometimes, depending on context, it's a good thing to return the string userspace can freely pick
268 * over the string automatically generated by the kernel. */
269
270 r = sd_device_get_sysname(dev, &name);
271 if (r < 0)
272 return r;
273
274 if (!startswith(name, "loop"))
275 goto notloop;
276
277 if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
278 if (ERRNO_IS_NOT_SUPPORTED(errno))
279 goto notloop;
280
281 return -errno;
282 }
283
284 #if HAVE_VALGRIND_MEMCHECK_H
285 VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
286 #endif
287
288 if (isempty((char*) info.lo_file_name) ||
289 strnlen((char*) info.lo_file_name, sizeof(info.lo_file_name)-1) == sizeof(info.lo_file_name)-1)
290 /* Don't pick up file name if it is unset or possibly truncated. (Note: the kernel silently
291 * truncates the string passed from userspace by LOOP_SET_STATUS64 ioctl. See
292 * loop_set_status_from_info() in drivers/block/loop.c. Hence, we can't really know the file
293 * name is truncated if it uses sizeof(info.lo_file_name)-1 as length; it could also mean the
294 * string is just that long and wasn't truncated — but the fact is simply that we cannot know
295 * in that case if it was truncated or not. Thus, we assume the worst and suppress — at least
296 * for now. For shorter strings we know for sure it wasn't truncated, hence that's always
297 * safe.) */
298 fn = NULL;
299 else {
300 fn = memdup_suffix0(info.lo_file_name, sizeof(info.lo_file_name));
301 if (!fn)
302 return -ENOMEM;
303 }
304
305 *ret_inode = info.lo_inode;
306 *ret_devno = info.lo_device;
307 *ret_fname = TAKE_PTR(fn);
308 return 1;
309
310
311 notloop:
312 *ret_devno = 0;
313 *ret_inode = 0;
314 *ret_fname = NULL;
315 return 0;
316 }
317
318 static int builtin_blkid(UdevEvent *event, int argc, char *argv[]) {
319 sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
320 const char *devnode, *root_partition = NULL, *data, *name;
321 _cleanup_(blkid_free_probep) blkid_probe pr = NULL;
322 _cleanup_free_ char *backing_fname = NULL;
323 bool noraid = false, is_gpt = false;
324 _cleanup_close_ int fd = -EBADF;
325 ino_t backing_inode = 0;
326 dev_t backing_devno = 0;
327 int64_t offset = 0;
328 int r;
329
330 static const struct option options[] = {
331 { "offset", required_argument, NULL, 'o' },
332 { "hint", required_argument, NULL, 'H' },
333 { "noraid", no_argument, NULL, 'R' },
334 {}
335 };
336
337 errno = 0;
338 pr = blkid_new_probe();
339 if (!pr)
340 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to create blkid prober: %m");
341
342 for (;;) {
343 int option;
344
345 option = getopt_long(argc, argv, "o:H:R", options, NULL);
346 if (option == -1)
347 break;
348
349 switch (option) {
350 case 'H':
351 #if HAVE_BLKID_PROBE_SET_HINT
352 errno = 0;
353 r = blkid_probe_set_hint(pr, optarg, 0);
354 if (r < 0)
355 return log_device_error_errno(dev, errno_or_else(ENOMEM), "Failed to use '%s' probing hint: %m", optarg);
356 break;
357 #else
358 /* Use the hint <name>=<offset> as probing offset for old versions */
359 optarg = strchr(optarg, '=');
360 if (!optarg)
361 /* no value means 0, do nothing for old versions */
362 break;
363 ++optarg;
364 _fallthrough_;
365 #endif
366 case 'o':
367 r = safe_atoi64(optarg, &offset);
368 if (r < 0)
369 return log_device_error_errno(dev, r, "Failed to parse '%s' as an integer: %m", optarg);
370 if (offset < 0)
371 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid offset %"PRIi64".", offset);
372 break;
373 case 'R':
374 noraid = true;
375 break;
376 }
377 }
378
379 blkid_probe_set_superblocks_flags(pr,
380 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
381 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE |
382 #ifdef BLKID_SUBLKS_FSINFO
383 BLKID_SUBLKS_FSINFO |
384 #endif
385 BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION);
386
387 if (noraid)
388 blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID);
389
390 r = sd_device_get_devname(dev, &devnode);
391 if (r < 0)
392 return log_device_debug_errno(dev, r, "Failed to get device name: %m");
393
394 fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
395 if (fd < 0) {
396 bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
397 log_device_debug_errno(dev, fd, "Failed to open block device %s%s: %m",
398 devnode, ignore ? ", ignoring" : "");
399 return ignore ? 0 : fd;
400 }
401
402 errno = 0;
403 r = blkid_probe_set_device(pr, fd, offset, 0);
404 if (r < 0)
405 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to set device to blkid prober: %m");
406
407 log_device_debug(dev, "Probe %s with %sraid and offset=%"PRIi64, devnode, noraid ? "no" : "", offset);
408
409 r = probe_superblocks(pr);
410 if (r < 0)
411 return log_device_debug_errno(dev, r, "Failed to probe superblocks: %m");
412
413 /* If the device is a partition then its parent passed the root partition UUID to the device */
414 (void) sd_device_get_property_value(dev, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition);
415
416 errno = 0;
417 int nvals = blkid_probe_numof_values(pr);
418 if (nvals < 0)
419 return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to get number of probed values: %m");
420
421 for (int i = 0; i < nvals; i++) {
422 if (blkid_probe_get_value(pr, i, &name, &data, NULL) < 0)
423 continue;
424
425 print_property(dev, event->event_mode, name, data);
426
427 /* Is this a disk with GPT partition table? */
428 if (streq(name, "PTTYPE") && streq(data, "gpt"))
429 is_gpt = true;
430
431 /* Is this a partition that matches the root partition
432 * property inherited from the parent? */
433 if (root_partition && streq(name, "PART_ENTRY_UUID") && streq(data, root_partition))
434 udev_builtin_add_property(dev, event->event_mode, "ID_PART_GPT_AUTO_ROOT", "1");
435 }
436
437 if (is_gpt)
438 find_gpt_root(dev, pr, event->event_mode);
439
440 r = read_loopback_backing_inode(
441 dev,
442 fd,
443 &backing_devno,
444 &backing_inode,
445 &backing_fname);
446 if (r < 0)
447 log_device_debug_errno(dev, r, "Failed to read loopback backing inode, ignoring: %m");
448 else if (r > 0) {
449 udev_builtin_add_propertyf(dev, event->event_mode, "ID_LOOP_BACKING_DEVICE", DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(backing_devno));
450 udev_builtin_add_propertyf(dev, event->event_mode, "ID_LOOP_BACKING_INODE", "%" PRIu64, (uint64_t) backing_inode);
451
452 if (backing_fname) {
453 /* In the worst case blkid_encode_string() will blow up to 4x the string
454 * length. Hence size the buffer to 4x of the longest string
455 * read_loopback_backing_inode() might return */
456 char encoded[sizeof_field(struct loop_info64, lo_file_name) * 4 + 1];
457
458 assert(strlen(backing_fname) < ELEMENTSOF(encoded) / 4);
459 blkid_encode_string(backing_fname, encoded, ELEMENTSOF(encoded));
460
461 udev_builtin_add_property(dev, event->event_mode, "ID_LOOP_BACKING_FILENAME", backing_fname);
462 udev_builtin_add_property(dev, event->event_mode, "ID_LOOP_BACKING_FILENAME_ENC", encoded);
463 }
464 }
465
466 return 0;
467 }
468
469 const UdevBuiltin udev_builtin_blkid = {
470 .name = "blkid",
471 .cmd = builtin_blkid,
472 .help = "Filesystem and partition probing",
473 .run_once = true,
474 };