| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* probe disks for filesystems and partitions |
| 4 |
* |
| 5 |
* Copyright © 2011 Karel Zak <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#if HAVE_VALGRIND_MEMCHECK_H |
| 9 |
#include <valgrind/memcheck.h> |
| 10 |
#endif |
| 11 |
|
| 12 |
#include <fcntl.h> |
| 13 |
#include <linux/loop.h> |
| 14 |
#include <sys/ioctl.h> |
| 15 |
#include <sys/stat.h> |
| 16 |
|
| 17 |
#include "alloc-util.h" |
| 18 |
#include "blkid-util.h" |
| 19 |
#include "blockdev-util.h" |
| 20 |
#include "device-util.h" |
| 21 |
#include "devnum-util.h" |
| 22 |
#include "efi-api.h" |
| 23 |
#include "efi-loader.h" |
| 24 |
#include "errno-util.h" |
| 25 |
#include "fd-util.h" |
| 26 |
#include "initrd-util.h" |
| 27 |
#include "gpt.h" |
| 28 |
#include "options.h" |
| 29 |
#include "parse-util.h" |
| 30 |
#include "string-util.h" |
| 31 |
#include "strv.h" |
| 32 |
#include "strxcpyx.h" |
| 33 |
#include "udev-builtin.h" |
| 34 |
|
| 35 |
static void print_property(UdevEvent *event, const char *name, const char *value) { |
| 36 |
char s[256]; |
| 37 |
|
| 38 |
assert(event); |
| 39 |
assert(name); |
| 40 |
|
| 41 |
s[0] = '\0'; |
| 42 |
|
| 43 |
if (streq(name, "TYPE")) { |
| 44 |
udev_builtin_add_property(event, "ID_FS_TYPE", value); |
| 45 |
|
| 46 |
} else if (streq(name, "USAGE")) { |
| 47 |
udev_builtin_add_property(event, "ID_FS_USAGE", value); |
| 48 |
|
| 49 |
} else if (streq(name, "VERSION")) { |
| 50 |
udev_builtin_add_property(event, "ID_FS_VERSION", value); |
| 51 |
|
| 52 |
} else if (streq(name, "UUID")) { |
| 53 |
sym_blkid_safe_string(value, s, sizeof(s)); |
| 54 |
udev_builtin_add_property(event, "ID_FS_UUID", s); |
| 55 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 56 |
udev_builtin_add_property(event, "ID_FS_UUID_ENC", s); |
| 57 |
|
| 58 |
} else if (streq(name, "UUID_SUB")) { |
| 59 |
sym_blkid_safe_string(value, s, sizeof(s)); |
| 60 |
udev_builtin_add_property(event, "ID_FS_UUID_SUB", s); |
| 61 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 62 |
udev_builtin_add_property(event, "ID_FS_UUID_SUB_ENC", s); |
| 63 |
|
| 64 |
} else if (streq(name, "LABEL")) { |
| 65 |
sym_blkid_safe_string(value, s, sizeof(s)); |
| 66 |
udev_builtin_add_property(event, "ID_FS_LABEL", s); |
| 67 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 68 |
udev_builtin_add_property(event, "ID_FS_LABEL_ENC", s); |
| 69 |
|
| 70 |
} else if (STR_IN_SET(name, "FSSIZE", "FSLASTBLOCK", "FSBLOCKSIZE")) { |
| 71 |
strscpyl(s, sizeof(s), "ID_FS_", name + 2, NULL); |
| 72 |
udev_builtin_add_property(event, s, value); |
| 73 |
|
| 74 |
} else if (streq(name, "PTTYPE")) { |
| 75 |
udev_builtin_add_property(event, "ID_PART_TABLE_TYPE", value); |
| 76 |
|
| 77 |
} else if (streq(name, "PTUUID")) { |
| 78 |
udev_builtin_add_property(event, "ID_PART_TABLE_UUID", value); |
| 79 |
|
| 80 |
} else if (streq(name, "PART_ENTRY_NAME")) { |
| 81 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 82 |
udev_builtin_add_property(event, "ID_PART_ENTRY_NAME", s); |
| 83 |
|
| 84 |
} else if (streq(name, "PART_ENTRY_TYPE")) { |
| 85 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 86 |
udev_builtin_add_property(event, "ID_PART_ENTRY_TYPE", s); |
| 87 |
|
| 88 |
} else if (startswith(name, "PART_ENTRY_")) { |
| 89 |
strscpyl(s, sizeof(s), "ID_", name, NULL); |
| 90 |
udev_builtin_add_property(event, s, value); |
| 91 |
|
| 92 |
} else if (streq(name, "SYSTEM_ID")) { |
| 93 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 94 |
udev_builtin_add_property(event, "ID_FS_SYSTEM_ID", s); |
| 95 |
|
| 96 |
} else if (streq(name, "PUBLISHER_ID")) { |
| 97 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 98 |
udev_builtin_add_property(event, "ID_FS_PUBLISHER_ID", s); |
| 99 |
|
| 100 |
} else if (streq(name, "APPLICATION_ID")) { |
| 101 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 102 |
udev_builtin_add_property(event, "ID_FS_APPLICATION_ID", s); |
| 103 |
|
| 104 |
} else if (streq(name, "BOOT_SYSTEM_ID")) { |
| 105 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 106 |
udev_builtin_add_property(event, "ID_FS_BOOT_SYSTEM_ID", s); |
| 107 |
|
| 108 |
} else if (streq(name, "VOLUME_ID")) { |
| 109 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 110 |
udev_builtin_add_property(event, "ID_FS_VOLUME_ID", s); |
| 111 |
|
| 112 |
} else if (streq(name, "LOGICAL_VOLUME_ID")) { |
| 113 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 114 |
udev_builtin_add_property(event, "ID_FS_LOGICAL_VOLUME_ID", s); |
| 115 |
|
| 116 |
} else if (streq(name, "VOLUME_SET_ID")) { |
| 117 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 118 |
udev_builtin_add_property(event, "ID_FS_VOLUME_SET_ID", s); |
| 119 |
|
| 120 |
} else if (streq(name, "DATA_PREPARER_ID")) { |
| 121 |
sym_blkid_encode_string(value, s, sizeof(s)); |
| 122 |
udev_builtin_add_property(event, "ID_FS_DATA_PREPARER_ID", s); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
static int find_gpt_root(UdevEvent *event, blkid_probe pr, const char *loop_backing_fname) { |
| 127 |
|
| 128 |
#if defined(SD_GPT_ROOT_NATIVE) && ENABLE_EFI |
| 129 |
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); |
| 130 |
int r; |
| 131 |
|
| 132 |
assert(event); |
| 133 |
assert(pr); |
| 134 |
|
| 135 |
/* In the initrd: Iterate through the partitions on this disk, and see if the UEFI ESP or XBOOTLDR |
| 136 |
* partition we booted from is on it. If so, find the newest root partition, and add a property |
| 137 |
* indicating its partition UUID. We also do this if we are dealing with a loopback block device |
| 138 |
* whose "backing filename" field is set to the string "root". In the latter case we do not search |
| 139 |
* for ESP or XBOOTLDR. |
| 140 |
* |
| 141 |
* After the initrd→host transition: look at the current block device mounted at / and set the same |
| 142 |
* properties to its whole block device. */ |
| 143 |
|
| 144 |
const char *devnode; |
| 145 |
r = sd_device_get_devname(dev, &devnode); |
| 146 |
if (r < 0) |
| 147 |
return log_device_debug_errno(dev, r, "Failed to get device node: %m"); |
| 148 |
|
| 149 |
r = block_device_is_whole_disk(dev); |
| 150 |
if (r < 0) |
| 151 |
return log_device_debug_errno(dev, r, "Unable to determine if device '%s' is a whole-block device: %m", devnode); |
| 152 |
if (r == 0) { |
| 153 |
log_device_debug(dev, "Invoked on device '%s' which is not a whole-disk block device, ignoring.", devnode); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
r = blockdev_partscan_enabled(dev); |
| 158 |
if (r < 0) |
| 159 |
return log_device_debug_errno(dev, r, "Failed to determine if block device '%s' supports partitions: %m", devnode); |
| 160 |
if (r == 0) { |
| 161 |
log_device_debug(dev, "Invoked on block device '%s' that lacks partition scanning, ignoring.", devnode); |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
sd_id128_t esp_or_xbootldr = SD_ID128_NULL; |
| 166 |
bool need_esp_or_xbootldr; |
| 167 |
dev_t root_devno = 0; |
| 168 |
if (in_initrd()) { |
| 169 |
/* In the initrd look at the boot loader provided data (and loopback backing fname) to find |
| 170 |
* our *future* root */ |
| 171 |
|
| 172 |
r = efi_loader_get_device_part_uuid(&esp_or_xbootldr); |
| 173 |
if (r < 0) { |
| 174 |
if (r != -ENOENT && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 175 |
return log_debug_errno(r, "Unable to determine loader partition UUID: %m"); |
| 176 |
|
| 177 |
log_device_debug(dev, "No loader partition UUID EFI variable set, not using partition data to search for default root block device."); |
| 178 |
|
| 179 |
/* NB: if an ESP/xbootldr field is set, we always use that. We do this in order to guarantee |
| 180 |
* systematic behaviour. */ |
| 181 |
if (!STRPTR_IN_SET(loop_backing_fname, "rootdisk", "rootdisk.raw")) { |
| 182 |
log_device_debug(dev, "Device is not a loopback block device with reference string 'root', not considering block device as default root block device."); |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
/* OK, we have now sufficiently identified this device as the right root "whole" device, |
| 187 |
* hence no need to bother with searching for ESP/XBOOTLDR */ |
| 188 |
need_esp_or_xbootldr = false; |
| 189 |
} else |
| 190 |
/* We now know the the ESP/xbootldr UUID, but we cannot be sure yet it's on this block |
| 191 |
* device, hence look for it among partitions now */ |
| 192 |
need_esp_or_xbootldr = true; |
| 193 |
} else { |
| 194 |
/* On the main system look at the *current* root instead */ |
| 195 |
|
| 196 |
r = blockdev_get_root(LOG_DEBUG, &root_devno); |
| 197 |
if (r < 0) { |
| 198 |
log_device_debug_errno(dev, r, "Unable to determine current root block device, skipping gpt-auto probing: %m"); |
| 199 |
return 0; |
| 200 |
} |
| 201 |
if (r == 0) { |
| 202 |
log_device_debug(dev, "Root block device not backed by a (single) whole block device, skipping gpt-auto probing."); |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
dev_t whole_devno; |
| 207 |
r = block_get_whole_disk(root_devno, &whole_devno); |
| 208 |
if (r < 0) |
| 209 |
return log_device_debug_errno(dev, r, "Failed to find whole block device for root block device: %m"); |
| 210 |
|
| 211 |
dev_t this_devno; |
| 212 |
r = sd_device_get_devnum(dev, &this_devno); |
| 213 |
if (r < 0) |
| 214 |
return log_device_debug_errno(dev, r, "Failed to get device major/minor of device: %m"); |
| 215 |
|
| 216 |
if (whole_devno != this_devno) { |
| 217 |
log_device_debug(dev, "This device is not the current root block device."); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
/* We don't need to check ESP/XBOOTLDR UUID, we *know* what our root disk is */ |
| 222 |
need_esp_or_xbootldr = false; |
| 223 |
} |
| 224 |
|
| 225 |
errno = 0; |
| 226 |
blkid_partlist pl = sym_blkid_probe_get_partitions(pr); |
| 227 |
if (!pl) |
| 228 |
return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to probe partitions: %m"); |
| 229 |
|
| 230 |
sd_id128_t root_id = SD_ID128_NULL; |
| 231 |
bool found_esp_or_xbootldr = false; |
| 232 |
|
| 233 |
if (root_devno != 0) { |
| 234 |
/* If we already know the root partition, let's verify its type ID and then directly query |
| 235 |
* its ID */ |
| 236 |
|
| 237 |
blkid_partition root_partition = sym_blkid_partlist_devno_to_partition(pl, root_devno); |
| 238 |
if (root_partition) { |
| 239 |
sd_id128_t type; |
| 240 |
r = blkid_partition_get_type_id128(root_partition, &type); |
| 241 |
if (r < 0) |
| 242 |
log_device_debug_errno(dev, r, "Failed to get root partition type UUID, ignoring: %m"); |
| 243 |
else if (sd_id128_equal(type, SD_GPT_ROOT_NATIVE)) { |
| 244 |
r = blkid_partition_get_uuid_id128(root_partition, &root_id); |
| 245 |
if (r < 0) |
| 246 |
log_device_debug_errno(dev, r, "Failed to get partition UUID, ignoring: %m"); |
| 247 |
} |
| 248 |
} |
| 249 |
} else { |
| 250 |
/* We do not know the root partition, let's search for it. */ |
| 251 |
|
| 252 |
_cleanup_free_ char *root_label = NULL; |
| 253 |
int nvals = sym_blkid_partlist_numof_partitions(pl); |
| 254 |
for (int i = 0; i < nvals; i++) { |
| 255 |
blkid_partition pp; |
| 256 |
const char *label; |
| 257 |
sd_id128_t type, id; |
| 258 |
|
| 259 |
pp = sym_blkid_partlist_get_partition(pl, i); |
| 260 |
if (!pp) |
| 261 |
continue; |
| 262 |
|
| 263 |
r = blkid_partition_get_uuid_id128(pp, &id); |
| 264 |
if (r < 0) { |
| 265 |
log_device_debug_errno(dev, r, "Failed to get partition UUID, ignoring: %m"); |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
r = blkid_partition_get_type_id128(pp, &type); |
| 270 |
if (r < 0) { |
| 271 |
log_device_debug_errno(dev, r, "Failed to get partition type UUID, ignoring: %m"); |
| 272 |
continue; |
| 273 |
} |
| 274 |
|
| 275 |
label = sym_blkid_partition_get_name(pp); /* returns NULL if empty */ |
| 276 |
|
| 277 |
if (need_esp_or_xbootldr && sd_id128_in_set(type, SD_GPT_ESP, SD_GPT_XBOOTLDR)) { |
| 278 |
|
| 279 |
/* We found an ESP or XBOOTLDR, let's see if it matches the ESP/XBOOTLDR we booted from. */ |
| 280 |
if (sd_id128_equal(id, esp_or_xbootldr)) |
| 281 |
found_esp_or_xbootldr = true; |
| 282 |
|
| 283 |
} else if (sd_id128_equal(type, SD_GPT_ROOT_NATIVE)) { |
| 284 |
unsigned long long flags; |
| 285 |
|
| 286 |
flags = sym_blkid_partition_get_flags(pp); |
| 287 |
if (flags & SD_GPT_FLAG_NO_AUTO) |
| 288 |
continue; |
| 289 |
|
| 290 |
/* systemd-sysupdate expects empty partitions to be marked with an "_empty" label, hence ignore them here. */ |
| 291 |
if (streq_ptr(label, "_empty")) |
| 292 |
continue; |
| 293 |
|
| 294 |
/* We found a suitable root partition, let's remember the first one, or the one with |
| 295 |
* the newest version, as determined by comparing the partition labels. */ |
| 296 |
|
| 297 |
if (sd_id128_is_null(root_id) || strverscmp_improved(label, root_label) > 0) { |
| 298 |
root_id = id; |
| 299 |
|
| 300 |
if (free_and_strdup(&root_label, label) < 0) |
| 301 |
return log_oom_debug(); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
if (!need_esp_or_xbootldr || found_esp_or_xbootldr) { |
| 308 |
/* We found the ESP/XBOOTLDR on this disk (or we didn't need it) */ |
| 309 |
udev_builtin_add_property(event, "ID_PART_GPT_AUTO_ROOT_DISK", "1"); |
| 310 |
|
| 311 |
/* We found a root partition, nice! Let's export its UUID. */ |
| 312 |
if (!sd_id128_is_null(root_id)) |
| 313 |
udev_builtin_add_property(event, "ID_PART_GPT_AUTO_ROOT_UUID", SD_ID128_TO_UUID_STRING(root_id)); |
| 314 |
} |
| 315 |
#endif |
| 316 |
|
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
static int probe_superblocks(blkid_probe pr) { |
| 321 |
struct stat st; |
| 322 |
int rc; |
| 323 |
|
| 324 |
/* TODO: Return negative errno. */ |
| 325 |
|
| 326 |
if (fstat(sym_blkid_probe_get_fd(pr), &st)) |
| 327 |
return -errno; |
| 328 |
|
| 329 |
sym_blkid_probe_enable_partitions(pr, 1); |
| 330 |
|
| 331 |
if (!S_ISCHR(st.st_mode) && |
| 332 |
sym_blkid_probe_get_size(pr) <= 1024 * 1440 && |
| 333 |
sym_blkid_probe_is_wholedisk(pr)) { |
| 334 |
/* |
| 335 |
* check if the small disk is partitioned, if yes then |
| 336 |
* don't probe for filesystems. |
| 337 |
*/ |
| 338 |
sym_blkid_probe_enable_superblocks(pr, 0); |
| 339 |
|
| 340 |
rc = sym_blkid_do_fullprobe(pr); |
| 341 |
if (rc < 0) |
| 342 |
return rc; /* -1 = error, 1 = nothing, 0 = success */ |
| 343 |
|
| 344 |
if (sym_blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0) |
| 345 |
return 0; /* partition table detected */ |
| 346 |
} |
| 347 |
|
| 348 |
sym_blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS); |
| 349 |
sym_blkid_probe_enable_superblocks(pr, 1); |
| 350 |
|
| 351 |
return sym_blkid_do_safeprobe(pr); |
| 352 |
} |
| 353 |
|
| 354 |
static int read_loopback_backing_inode( |
| 355 |
sd_device *dev, |
| 356 |
int fd, |
| 357 |
dev_t *ret_devno, |
| 358 |
ino_t *ret_inode, |
| 359 |
char **ret_fname) { |
| 360 |
|
| 361 |
_cleanup_free_ char *fn = NULL; |
| 362 |
struct loop_info64 info; |
| 363 |
int r; |
| 364 |
|
| 365 |
assert(dev); |
| 366 |
assert(fd >= 0); |
| 367 |
assert(ret_devno); |
| 368 |
assert(ret_inode); |
| 369 |
assert(ret_fname); |
| 370 |
|
| 371 |
/* Retrieves various fields of the current loopback device backing file, so that we can ultimately |
| 372 |
* use it to create stable symlinks to loopback block devices, based on what they are backed by. We |
| 373 |
* pick up inode/device as well as file name field. Note that we pick up the "lo_file_name" field |
| 374 |
* here, which is an arbitrary free-form string provided by userspace. We do not return the sysfs |
| 375 |
* attribute loop/backing_file here, because that is directly accessible from udev rules anyway. And |
| 376 |
* sometimes, depending on context, it's a good thing to return the string userspace can freely pick |
| 377 |
* over the string automatically generated by the kernel. */ |
| 378 |
|
| 379 |
r = device_sysname_startswith(dev, "loop"); |
| 380 |
if (r < 0) |
| 381 |
return r; |
| 382 |
if (r == 0) |
| 383 |
goto notloop; |
| 384 |
|
| 385 |
if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) { |
| 386 |
if (ERRNO_IS_NOT_SUPPORTED(errno)) |
| 387 |
goto notloop; |
| 388 |
|
| 389 |
return -errno; |
| 390 |
} |
| 391 |
|
| 392 |
#if HAVE_VALGRIND_MEMCHECK_H |
| 393 |
VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info)); |
| 394 |
#endif |
| 395 |
|
| 396 |
if (isempty((char*) info.lo_file_name) || |
| 397 |
strnlen((char*) info.lo_file_name, sizeof(info.lo_file_name)-1) == sizeof(info.lo_file_name)-1) |
| 398 |
/* Don't pick up file name if it is unset or possibly truncated. (Note: the kernel silently |
| 399 |
* truncates the string passed from userspace by LOOP_SET_STATUS64 ioctl. See |
| 400 |
* loop_set_status_from_info() in drivers/block/loop.c. Hence, we can't really know the file |
| 401 |
* name is truncated if it uses sizeof(info.lo_file_name)-1 as length; it could also mean the |
| 402 |
* string is just that long and wasn't truncated — but the fact is simply that we cannot know |
| 403 |
* in that case if it was truncated or not. Thus, we assume the worst and suppress — at least |
| 404 |
* for now. For shorter strings we know for sure it wasn't truncated, hence that's always |
| 405 |
* safe.) */ |
| 406 |
fn = NULL; |
| 407 |
else { |
| 408 |
fn = memdup_suffix0(info.lo_file_name, sizeof(info.lo_file_name)); |
| 409 |
if (!fn) |
| 410 |
return -ENOMEM; |
| 411 |
} |
| 412 |
|
| 413 |
*ret_inode = info.lo_inode; |
| 414 |
*ret_devno = info.lo_device; |
| 415 |
*ret_fname = TAKE_PTR(fn); |
| 416 |
return 1; |
| 417 |
|
| 418 |
notloop: |
| 419 |
*ret_devno = 0; |
| 420 |
*ret_inode = 0; |
| 421 |
*ret_fname = NULL; |
| 422 |
return 0; |
| 423 |
} |
| 424 |
|
| 425 |
static int gpt_boot_disk_needs_loop(sd_device *dev, int fd, ssize_t *ret_sector_size) { |
| 426 |
int r; |
| 427 |
|
| 428 |
assert(dev); |
| 429 |
assert(fd >= 0); |
| 430 |
assert(ret_sector_size); |
| 431 |
|
| 432 |
/* Determine whether the boot disk needs a loop device to expose its partitions. For devices |
| 433 |
* booted via El Torito, the GPT may use a different sector size than the device (e.g. a 512-byte |
| 434 |
* GPT on a device with 2048-byte blocks). If there's a mismatch, check whether this is the boot |
| 435 |
* disk by comparing GPT partition UUIDs with the ESP/XBOOTLDR UUID exported by the boot loader. |
| 436 |
* The kernel can't parse the partition table itself in this case, so the caller sets up a loop |
| 437 |
* device with the returned sector size. |
| 438 |
* |
| 439 |
* Even if the sector sizes match, if the device does not support partition scanning (e.g. some |
| 440 |
* CD-ROM drives), the kernel still can't parse the partition table. In that case too, if the |
| 441 |
* disk contains the ESP we booted from, a loop device is needed to expose the partitions. |
| 442 |
* |
| 443 |
* Returns 1 if the boot disk needs a loop device, 0 if not. On success the GPT sector size is |
| 444 |
* returned in *ret_sector_size (0 if the disk has no GPT), regardless of whether a loop device |
| 445 |
* is needed. */ |
| 446 |
|
| 447 |
_cleanup_free_ void *entries = NULL; |
| 448 |
uint32_t n_entries, entry_size; |
| 449 |
ssize_t gpt_ssz = gpt_probe(fd, /* ret_header= */ NULL, &entries, &n_entries, &entry_size); |
| 450 |
if (gpt_ssz < 0) |
| 451 |
return log_device_debug_errno(dev, gpt_ssz, "Failed to probe GPT: %m"); |
| 452 |
if (gpt_ssz == 0) { |
| 453 |
*ret_sector_size = 0; |
| 454 |
return 0; |
| 455 |
} |
| 456 |
|
| 457 |
uint32_t device_ssz; |
| 458 |
r = blockdev_get_sector_size(fd, &device_ssz); |
| 459 |
if (r < 0) |
| 460 |
return log_device_debug_errno(dev, r, "Failed to get device sector size: %m"); |
| 461 |
|
| 462 |
bool sector_size_mismatch = (uint32_t) gpt_ssz != device_ssz; |
| 463 |
|
| 464 |
if (!sector_size_mismatch) { |
| 465 |
r = blockdev_partscan_enabled(dev); |
| 466 |
if (r < 0) |
| 467 |
return log_device_debug_errno(dev, r, "Failed to check if partition scanning is enabled: %m"); |
| 468 |
if (r > 0) { |
| 469 |
/* The kernel can parse the partition table itself; no loop device needed. */ |
| 470 |
*ret_sector_size = gpt_ssz; |
| 471 |
return 0; |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
if (sector_size_mismatch) |
| 476 |
log_device_debug(dev, "GPT sector size %zi does not match device sector size %" PRIu32 ".", |
| 477 |
gpt_ssz, device_ssz); |
| 478 |
else |
| 479 |
log_device_debug(dev, "Device does not support partition scanning."); |
| 480 |
|
| 481 |
sd_id128_t loader_part_uuid; |
| 482 |
r = efi_loader_get_device_part_uuid(&loader_part_uuid); |
| 483 |
if (r < 0) { |
| 484 |
if (r != -ENOENT && !ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 485 |
return log_device_debug_errno(dev, r, "Failed to get loader partition UUID: %m"); |
| 486 |
|
| 487 |
/* No loader partition UUID set, can't tell whether this is the boot disk. */ |
| 488 |
*ret_sector_size = gpt_ssz; |
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
for (uint32_t i = 0; i < n_entries; i++) { |
| 493 |
const GptPartitionEntry *entry = (const GptPartitionEntry *) ((const uint8_t *) entries + (size_t) entry_size * i); |
| 494 |
|
| 495 |
sd_id128_t type = efi_guid_to_id128(entry->partition_type_guid); |
| 496 |
if (!sd_id128_in_set(type, SD_GPT_ESP, SD_GPT_XBOOTLDR)) |
| 497 |
continue; |
| 498 |
|
| 499 |
if (!sd_id128_equal(efi_guid_to_id128(entry->unique_partition_guid), loader_part_uuid)) |
| 500 |
continue; |
| 501 |
|
| 502 |
log_device_debug(dev, "Found boot partition (ESP/XBOOTLDR) on disk where kernel cannot scan partitions."); |
| 503 |
*ret_sector_size = gpt_ssz; |
| 504 |
return 1; /* boot disk needs a loop device */ |
| 505 |
} |
| 506 |
|
| 507 |
*ret_sector_size = gpt_ssz; |
| 508 |
return 0; |
| 509 |
} |
| 510 |
|
| 511 |
static int builtin_blkid(UdevEvent *event, int argc, char *argv[]) { |
| 512 |
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev); |
| 513 |
const char *devnode, *root_partition = NULL, *data, *name; |
| 514 |
_cleanup_(blkid_free_probep) blkid_probe pr = NULL; |
| 515 |
_cleanup_free_ char *backing_fname = NULL; |
| 516 |
bool noraid = false, is_gpt = false; |
| 517 |
_cleanup_close_ int fd = -EBADF; |
| 518 |
ino_t backing_inode = 0; |
| 519 |
dev_t backing_devno = 0; |
| 520 |
int64_t offset = 0; |
| 521 |
int r; |
| 522 |
|
| 523 |
r = DLOPEN_LIBBLKID(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 524 |
if (r < 0) |
| 525 |
return log_device_debug_errno(dev, r, "blkid not available: %m"); |
| 526 |
|
| 527 |
errno = 0; |
| 528 |
pr = sym_blkid_new_probe(); |
| 529 |
if (!pr) |
| 530 |
return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to create blkid prober: %m"); |
| 531 |
|
| 532 |
OptionParser opts = { argc, argv, .namespace = "udev-builtin-blkid" }; |
| 533 |
|
| 534 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 535 |
switch (c) { |
| 536 |
|
| 537 |
OPTION_NAMESPACE("udev-builtin-blkid"): {} |
| 538 |
|
| 539 |
OPTION('H', "hint", "HINT", NULL): |
| 540 |
errno = 0; |
| 541 |
r = sym_blkid_probe_set_hint(pr, opts.arg, 0); |
| 542 |
if (r < 0) |
| 543 |
return log_device_error_errno(dev, errno_or_else(ENOMEM), "Failed to use '%s' probing hint: %m", opts.arg); |
| 544 |
break; |
| 545 |
|
| 546 |
OPTION('o', "offset", "OFFSET", NULL): |
| 547 |
r = safe_atoi64(opts.arg, &offset); |
| 548 |
if (r < 0) |
| 549 |
return log_device_error_errno(dev, r, "Failed to parse '%s' as an integer: %m", opts.arg); |
| 550 |
if (offset < 0) |
| 551 |
return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "Invalid offset %"PRIi64".", offset); |
| 552 |
break; |
| 553 |
|
| 554 |
OPTION('R', "noraid", NULL, NULL): |
| 555 |
noraid = true; |
| 556 |
break; |
| 557 |
} |
| 558 |
|
| 559 |
r = sd_device_get_devname(dev, &devnode); |
| 560 |
if (r < 0) |
| 561 |
return log_device_debug_errno(dev, r, "Failed to get device name: %m"); |
| 562 |
|
| 563 |
fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 564 |
if (fd < 0) { |
| 565 |
bool ignore = ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(fd); |
| 566 |
log_device_debug_errno(dev, fd, "Failed to open block device %s%s: %m", |
| 567 |
devnode, ignore ? ", ignoring" : ""); |
| 568 |
return ignore ? 0 : fd; |
| 569 |
} |
| 570 |
|
| 571 |
/* If the kernel can't parse the boot disk's partition table, set properties so a udev rule sets |
| 572 |
* up a loop device (with the correct sector size) to expose the partitions. We still probe the |
| 573 |
* device itself below for its whole-disk properties (filesystem type, partition table UUID, and |
| 574 |
* so on); partition and root discovery happen on the loop device instead. We don't need to |
| 575 |
* suppress the latter here: blkid probes at the device's own logical sector size, so a GPT |
| 576 |
* written for a different sector size is simply not detected, and both PART_ENTRY_* and |
| 577 |
* find_gpt_root() only ever act on partitions the kernel actually registered — which, for a disk |
| 578 |
* that needs a loop device, is none. */ |
| 579 |
if (offset == 0) { |
| 580 |
ssize_t gpt_ssz = 0; |
| 581 |
|
| 582 |
r = gpt_boot_disk_needs_loop(dev, fd, &gpt_ssz); |
| 583 |
if (gpt_ssz > 0) |
| 584 |
udev_builtin_add_propertyf(event, "ID_PART_GPT_SECTOR_SIZE", "%zi", gpt_ssz); |
| 585 |
if (r > 0) |
| 586 |
udev_builtin_add_property(event, "ID_PART_GPT_AUTO_ROOT_DISK_NEEDS_LOOP", "1"); |
| 587 |
} |
| 588 |
|
| 589 |
sym_blkid_probe_set_superblocks_flags(pr, |
| 590 |
BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | |
| 591 |
BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE | |
| 592 |
#ifdef BLKID_SUBLKS_FSINFO /* since util-linux 2.39 */ |
| 593 |
BLKID_SUBLKS_FSINFO | |
| 594 |
#endif |
| 595 |
BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION); |
| 596 |
|
| 597 |
if (noraid) |
| 598 |
sym_blkid_probe_filter_superblocks_usage(pr, BLKID_FLTR_NOTIN, BLKID_USAGE_RAID); |
| 599 |
|
| 600 |
errno = 0; |
| 601 |
r = sym_blkid_probe_set_device(pr, fd, offset, 0); |
| 602 |
if (r < 0) |
| 603 |
return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to set device to blkid prober: %m"); |
| 604 |
|
| 605 |
log_device_debug(dev, "Probe %s with %sraid and offset=%"PRIi64, devnode, noraid ? "no" : "", offset); |
| 606 |
|
| 607 |
r = probe_superblocks(pr); |
| 608 |
if (r < 0) |
| 609 |
return log_device_debug_errno(dev, r, "Failed to probe superblocks: %m"); |
| 610 |
|
| 611 |
/* If the device is a partition then its parent passed the root partition UUID to the device */ |
| 612 |
(void) sd_device_get_property_value(dev, "ID_PART_GPT_AUTO_ROOT_UUID", &root_partition); |
| 613 |
|
| 614 |
errno = 0; |
| 615 |
int nvals = sym_blkid_probe_numof_values(pr); |
| 616 |
if (nvals < 0) |
| 617 |
return log_device_debug_errno(dev, errno_or_else(ENOMEM), "Failed to get number of probed values: %m"); |
| 618 |
|
| 619 |
for (int i = 0; i < nvals; i++) { |
| 620 |
if (sym_blkid_probe_get_value(pr, i, &name, &data, NULL) < 0) |
| 621 |
continue; |
| 622 |
|
| 623 |
print_property(event, name, data); |
| 624 |
|
| 625 |
/* Is this a disk with GPT partition table? */ |
| 626 |
if (streq(name, "PTTYPE") && streq(data, "gpt")) |
| 627 |
is_gpt = true; |
| 628 |
|
| 629 |
/* Is this a partition that matches the root partition |
| 630 |
* property inherited from the parent? */ |
| 631 |
if (root_partition && streq(name, "PART_ENTRY_UUID") && streq(data, root_partition)) |
| 632 |
udev_builtin_add_property(event, "ID_PART_GPT_AUTO_ROOT", "1"); |
| 633 |
} |
| 634 |
|
| 635 |
r = read_loopback_backing_inode( |
| 636 |
dev, |
| 637 |
fd, |
| 638 |
&backing_devno, |
| 639 |
&backing_inode, |
| 640 |
&backing_fname); |
| 641 |
if (r < 0) |
| 642 |
log_device_debug_errno(dev, r, "Failed to read loopback backing inode, ignoring: %m"); |
| 643 |
else if (r > 0) { |
| 644 |
udev_builtin_add_propertyf(event, "ID_LOOP_BACKING_DEVICE", DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(backing_devno)); |
| 645 |
udev_builtin_add_propertyf(event, "ID_LOOP_BACKING_INODE", "%" PRIu64, (uint64_t) backing_inode); |
| 646 |
|
| 647 |
if (backing_fname) { |
| 648 |
/* In the worst case blkid_encode_string() will blow up to 4x the string |
| 649 |
* length. Hence size the buffer to 4x of the longest string |
| 650 |
* read_loopback_backing_inode() might return */ |
| 651 |
char encoded[sizeof_field(struct loop_info64, lo_file_name) * 4 + 1]; |
| 652 |
|
| 653 |
assert(strlen(backing_fname) < ELEMENTSOF(encoded) / 4); |
| 654 |
sym_blkid_encode_string(backing_fname, encoded, ELEMENTSOF(encoded)); |
| 655 |
|
| 656 |
udev_builtin_add_property(event, "ID_LOOP_BACKING_FILENAME", backing_fname); |
| 657 |
udev_builtin_add_property(event, "ID_LOOP_BACKING_FILENAME_ENC", encoded); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
if (is_gpt) |
| 662 |
find_gpt_root(event, pr, backing_fname); |
| 663 |
|
| 664 |
return 0; |
| 665 |
} |
| 666 |
|
| 667 |
const UdevBuiltin udev_builtin_blkid = { |
| 668 |
.name = "blkid", |
| 669 |
.cmd = builtin_blkid, |
| 670 |
.help = "Filesystem and partition probing", |
| 671 |
.run_once = true, |
| 672 |
}; |