Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partitioning: fix race condition on loop device allocation #6729

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions lib/functions/image/partitioning.sh
Original file line number Diff line number Diff line change
Expand Up @@ -216,25 +216,9 @@ function prepare_partitions() {
flock -x $FD

declare -g LOOP
# replace losetup --find with own function and do 10 cycles
# losetup always return 1st free loop device and in parallel build,
# it often happens that same is found which resoults in:
# "failed to set up loop device: Device or resource busy"
# If we seek random way, chanches of allocating the same are significantly smaller.
FIND_LOOP_CYCLES=1
while : ; do
LOOP=$(find /dev/loop* | grep -Po "(\/dev\/loop\d|\/dev\/loop\d\d)$" | sort -R | head -1)
LOOP_COMPARE=$(losetup -l --noheadings --raw --output=NAME | grep $LOOP || true)
[[ -z $LOOP_COMPARE ]] && break
[[ $FIND_LOOP_CYCLES -gt 10 ]] && exit_with_error "Unable to find free loop device"
FIND_LOOP_CYCLES=$(( FIND_LOOP_CYCLES + 1 ))
done
display_alert "Allocated loop device" "LOOP=${LOOP} in cycle $FIND_LOOP_CYCLES"

CHECK_LOOP_FOR_SIZE="no" check_loop_device "$LOOP" # initially loop is zero sized, ignore it.

#--partscan is using to force the kernel for scaning partition table in preventing of partprobe errors
run_host_command_logged losetup --partscan "${LOOP}" "${SDCARD}".raw
#--partscan is using to force the kernel for scaning partition table in preventing of partprobe errors
LOOP=$(losetup --show --partscan --find "${SDCARD}".raw) || exit_with_error "Unable to find free loop device"
display_alert "Allocated loop device" "LOOP=${LOOP}"

# loop device was grabbed here, unlock
flock -u $FD
Expand Down
Loading