]> git.ipfire.org Git - thirdparty/systemd.git/commit - src/shared/dissect-image.c
dissect: rework how we wait for partition block devices
authorLennart Poettering <lennart@poettering.net>
Wed, 6 Apr 2022 09:50:01 +0000 (11:50 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 10 Apr 2022 13:52:29 +0000 (22:52 +0900)
commit1b010ae7c96d953f11a9925d4c5aef29befc86d1
treefc55d4d27b30397cd5c96f84a8d56e411d96a711
parent1196b6a2e61075c029cb959f1e866af04e01fbe4
dissect: rework how we wait for partition block devices

This revisits the mess around waiting for partition block devices in
the image dissection code. It implements a nice little trick:

Instead of waiting for the kernel to probe the partition table for us
and generate the block devices from it, we'll just do that ourselves.
How can we do it? Via the BLKPG_ADD_PARTITION ioctl, that the kernel has
supported for a while. This ioctl allows creating partition block
devices off "whole" block devices from userspace, without the partitions
necessarily being present in the partition table at all.

So, whenever we want a partition to be there, we'll just issue
BLKPG_ADD_PARTITION. This can either work, in which case we know the
partition is there, and can use it. Yay. Or it can fail with EBUSY,
which the kernel returns if a partition by the selected partition index
already exists (or if an existing partition overlaps with the new one).
But if that's the case, then that's also OK, because the partition will
already exist.

So, regardless if we win or the kernel wins, for us the outcome is the
same: the partition block device will exist after invoking the ioctl.
Yay.

Net effect: we are not dependent on asynchronous uevent messages to wait
for the devices. Instead we synchronously get what we need. This makes
us independent of the (apparently less than reliable) netlink transport,
and should almost always be quicker.

Hopefully addresses #17469 even on older kernels.

Fixes: #17469
src/gpt-auto-generator/gpt-auto-generator.c
src/shared/dissect-image.c
src/shared/dissect-image.h