main/postmarketos-initramfs: fix bug with booting from usb

This fixes a weird race that I observed when booting from usb, where
mount_subpartitons was called and:

1) rootfs on USB wasn't available so the first find_root_partition found nothing

2) the loop was entered to search for subpartitions

3) after a few iterations, the USB block device showed up along with its
partitions and the for loop was started

4) kpartx -afs created mappings for partitions on the disk (e.g.
/dev/sda), the rootfs was found by find_root_partition but the mappings
were never removed

5) Later when things like e2fsck or mount tried to operate on /dev/sda2,
they would fail with "in use" or "resource busy" errors

The fix for this is two parts:

* Don't try to look for subpartitions on whole disks (e.g /dev/sda),
it tests for this by assuming that partitions have a `partition` thing
under sysfs

* Running find_root_partition on each iteration of the while loop just
in case the real partition showed up so we can bail out of the while
loop entirely. This is janky af, but since we have no idea if we're in
mount_subpartitions because we need to actually search for subpartitions
OR if we're here because of a slow-to-init block device, I'm not sure we
have any other choice.

Signed-off-by: Clayton Craft <craftyguy@postmarketos.org>
Part-of: <https://gitlab.postmarketos.org/postmarketOS/pmaports/-/merge_requests/7246>
This commit is contained in:
Clayton Craft 2025-10-24 16:26:41 -07:00 committed by The Friendly Merge Bot
parent bde521a2aa
commit 0268336968
No known key found for this signature in database
2 changed files with 6 additions and 2 deletions

View file

@ -1,7 +1,7 @@
# Maintainer: Casey Connolly <kcxt@postmarketos.org>
# Co-Maintainer: Clayton Craft <clayton@craftyguy.net>
pkgname=postmarketos-initramfs
pkgver=3.8.7
pkgver=3.8.8
pkgrel=0
pkgdesc="Base files for the postmarketOS initramfs / initramfs-extra"
url="https://postmarketos.org"
@ -112,7 +112,7 @@ sha512sums="
e268094cbd25e063fdab2226f8cf93885bd8b90455991a53296231a3401a03669440333025ca44b2a8459df98787a5725f7b227ccb9750f8fcad17ef58ba21a1 00-initramfs-base.files
d0f35562365756d93066ce45924d17fb347b54095179c3262daa2f073e12743505cd5d9372ad30485915ca754d95a8edba9a74314e7949e0b3cf6978a87d03a5 00-initramfs-extra-base.files
e90596540805662211bd2a2e427ce7cc38f6a30023267f902e23f2bae136b6cd8f4f4716b8fb611be9d671a964edd2ecce03fa32bc8024e29b8375b0097fde0c init.sh
65b4a7fae6f54cb98dfc505572eba3e73dccafc20316661197ed5b355971136c3ab078dc351099754a8e7165c1eda5c006d22186e84d9be53e2f6aa046107bec init_functions.sh
c8aab3e0d407a413fa35070b800a6fa733ccb691327f9fef9df1d604cd2a019504ad7c447637df218da113199ff25998ca66a192fcabd5ae62b9873a985db1c1 init_functions.sh
6b4881f9b43c90ebcc70d7b7a192d26a2e3ce01c43b8536fe900c2ffaf74ef035803b9aa083e7a9d5c49c75be8ba6ff22997a037ae4f399e6ee5e1978c4b3f9c init_2nd.sh
d7d003e906ee017dacd1e994729ce1c94ac628ea4a36636bc78eedd16125e3f2cf500ecddac144cc11d69cc741fc5f4512510209236c2fda8a893d506773b965 init_functions_2nd.sh
675e7d5bee39b2df7d322117f8dcaccc274d61beaf4d50ead19bbf2109446d64b1c0aa0c5b4f9846eb6c1c403418f28f6364eff4537ba41120fbfcbc484b7da7 mdev.conf

View file

@ -360,6 +360,8 @@ mount_subpartitions() {
partitions="$android_parts $(grep -v "loop\|ram" < /proc/diskstats |\
sed 's/\(\s\+[0-9]\+\)\+\s\+//;s/ .*//;s/^/\/dev\//')"
for partition in $partitions; do
# Skip whole disks - only check partitions for subpartitions
[ -e "/sys/class/block/$(basename "$partition")/partition" ] || continue
case "$(kpartx -l "$partition" 2>/dev/null | wc -l)" in
2)
echo "Mount subpartitions of $partition"
@ -386,6 +388,8 @@ mount_subpartitions() {
return;
fi
sleep 0.1;
# Check if partition appeared without needing subpartitions
find_root_partition
done
}