From e13e4f764643b18e6d8256a0fade47e7df0019c0 Mon Sep 17 00:00:00 2001 From: "Kristopher James Kent (kjkent)" Date: Sat, 22 Mar 2025 22:06:29 +0000 Subject: [PATCH] Clean up block device detection logic --- INSTALL/tool/VentoyWorker.sh | 22 ++++---- INSTALL/tool/ventoy_lib.sh | 98 +++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 48 deletions(-) diff --git a/INSTALL/tool/VentoyWorker.sh b/INSTALL/tool/VentoyWorker.sh index 7844b922..39a81a84 100644 --- a/INSTALL/tool/VentoyWorker.sh +++ b/INSTALL/tool/VentoyWorker.sh @@ -312,22 +312,18 @@ if [ "$MODE" = "install" -a -z "$NONDESTRUCTIVE" ]; then #format part1 wait_and_create_part ${PART1} ${PART2} - if [ -b ${PART1} ]; then - vtinfo "Format partition 1 ${PART1} ..." + vtinfo "Format partition 1 ${PART1}..." + mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1} + if [ $? -ne 0 ]; then + vtwarn "mkexfatfs failed; retrying..." mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1} if [ $? -ne 0 ]; then - echo "mkexfatfs failed, now retry..." - mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1} - if [ $? -ne 0 ]; then - echo "######### mkexfatfs failed, exit ########" - exit 1 - fi - else - echo "mkexfatfs success" - fi + vterr "######## mkexfatfs failed, exit ########" + exit 1 + fi else - vterr "${PART1} NOT exist" - fi + vtinfo "mkexfatfs success" + fi vtinfo "writing data to disk ..." dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=446 diff --git a/INSTALL/tool/ventoy_lib.sh b/INSTALL/tool/ventoy_lib.sh index 854881d7..6c164bba 100644 --- a/INSTALL/tool/ventoy_lib.sh +++ b/INSTALL/tool/ventoy_lib.sh @@ -210,45 +210,73 @@ get_disk_ventoy_version() { ventoy_false } -wait_and_create_part() { - vPART1=$1 - vPART2=$2 - echo 'Wait for partitions $vPART1 and $vPART2 ...' - for i in 0 1 2 3 4 5 6 7 8 9; do - if ls -l $vPART1 2>/dev/null | grep -q '^b'; then - if ls -l $vPART2 2>/dev/null | grep -q '^b'; then - break - fi - else - echo "Wait for $vPART1 and $vPART2 ..." - sleep 1 - fi +wait_for_blockdevs() { + attempts=3 + delay=2 + + vtdebug "wait_for_blockdevs called with the following arguments:" + for dev in "$@"; do + vtdebug ' - '"$dev" done - if ls -l $vPART1 2>/dev/null | grep -q '^b'; then - echo "$vPART1 exist OK" - else - MajorMinor=$(sed "s/:/ /" /sys/class/block/${vPART1#/dev/}/dev) - echo "mknod -m 0660 $vPART1 b $MajorMinor ..." - mknod -m 0660 $vPART1 b $MajorMinor - fi - - if ls -l $vPART2 2>/dev/null | grep -q '^b'; then - echo "$vPART2 exist OK" - else - MajorMinor=$(sed "s/:/ /" /sys/class/block/${vPART2#/dev/}/dev) - echo "mknod -m 0660 $vPART2 b $MajorMinor ..." - mknod -m 0660 $vPART2 b $MajorMinor - fi + i= + while [ $((i+=1)) -le $attempts ]; do + all_ready=0 - if ls -l $vPART1 2>/dev/null | grep -q '^b'; then - if ls -l $vPART2 2>/dev/null | grep -q '^b'; then - echo "partition exist OK" + echo "Waiting for block devices..." + for dev in "$@"; do + if ! [ -b "$dev" ]; then + vtwarn "$dev not found." + all_ready=1 + break + fi + done + + if [ "$all_ready" -eq 0 ]; then + vtinfo "Block devices ready!" + return fi - else - echo "[FAIL] $vPART1/$vPART2 does not exist" - exit 1 - fi + + echo 'Refreshing kernel partition table' + partprobe + + sleep "$delay" + done + + vterr "Required block devices not ready." + return 1 +} + +wait_and_create_part() { + # Return immediately if devices found + wait_for_blockdevs "$@" && return + + # Fallback: manually create fs nodes + echo "Attempting to create device nodes in /dev/" + + for part in "$@"; do + # Skip if device found + [ -b $part ] && continue + + MajorMinor=$(sed "s/:/ /" /sys/class/block/${part#/dev/}/dev) + echo ">> mknod -m 0660 $part b $MajorMinor..." + mknod -m 0660 $part b $MajorMinor + + if [ "$?" -ne 0 ]; then + vterr 'Creating device node failed' + exit 1 + fi + done + + partprobe + echo "Confirming partitions are now available..." + + # Return if success + wait_for_blockdevs "$@" && return + + # RIP + vterr 'Cannot proceed -- please check your device. Try re-plugging or using a different USB port or device.' + exit 1 }