Clean up block device detection logic

This commit is contained in:
Kristopher James Kent (kjkent) 2025-03-22 22:06:29 +00:00
parent 2dafe75509
commit e13e4f7646
No known key found for this signature in database
GPG Key ID: D94D794E6BF77966
2 changed files with 72 additions and 48 deletions

View File

@ -312,21 +312,17 @@ 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"
vterr "######## mkexfatfs failed, exit ########"
exit 1
fi
else
vterr "${PART1} NOT exist"
vtinfo "mkexfatfs success"
fi
vtinfo "writing data to disk ..."

View File

@ -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
wait_for_blockdevs() {
attempts=3
delay=2
vtdebug "wait_for_blockdevs called with the following arguments:"
for dev in "$@"; do
vtdebug ' - '"$dev"
done
i=
while [ $((i+=1)) -le $attempts ]; do
all_ready=0
echo "Waiting for block devices..."
for dev in "$@"; do
if ! [ -b "$dev" ]; then
vtwarn "$dev not found."
all_ready=1
break
fi
else
echo "Wait for $vPART1 and $vPART2 ..."
sleep 1
done
if [ "$all_ready" -eq 0 ]; then
vtinfo "Block devices ready!"
return
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
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
partprobe
echo "Confirming partitions are now available..."
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
# Return if success
wait_for_blockdevs "$@" && return
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"
fi
else
echo "[FAIL] $vPART1/$vPART2 does not exist"
exit 1
fi
# RIP
vterr 'Cannot proceed -- please check your device. Try re-plugging or using a different USB port or device.'
exit 1
}