heads/initrd/bin/mount-usb
Jonathon Hall a8a843ecc8
mount-usb: Improve reliability with partitioned disks
Extract exclusion for unpartitioned block device of partitioned media
to gui_functions, and exclude them even if kernel hasn't listed the
partitions yet.  (Fixes flash/USB boot prompts incorrectly trying to
use the whole device for partitioned media the first time.)

Ignore block devices of size 0, like empty USB SD card readers.

Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm>
2022-11-03 18:22:03 -04:00

99 lines
2.6 KiB
Bash
Executable File

#!/bin/sh
# Mount a USB device
. /etc/functions
enable_usb
if ! lsmod | grep -q usb_storage; then
timeout=0
echo "Scanning for USB storage devices..."
insmod /lib/modules/usb-storage.ko >/dev/null 2>&1 \
|| die "usb_storage: module load failed"
while [[ $(list_usb_storage | wc -l) -eq 0 ]]; do
[[ $timeout -ge 8 ]] && break
sleep 1
timeout=$(($timeout+1))
done
fi
if [ ! -d /media ]; then
mkdir /media
fi
list_usb_storage > /tmp/usb_block_devices
if [ -z `cat /tmp/usb_block_devices` ]; then
if [ -x /bin/whiptail ]; then
whiptail $BG_COLOR --title 'USB Drive Missing' \
--msgbox "Insert your USB drive and press Enter to continue." 16 60
else
echo "+++ USB Drive Missing! Insert your USB drive and press Enter to continue."
read
fi
sleep 1
list_usb_storage > /tmp/usb_block_devices
if [ -z `cat /tmp/usb_block_devices` ]; then
if [ -x /bin/whiptail ]; then
whiptail $BG_COLOR_ERROR --title 'ERROR: USB Drive Missing' \
--msgbox "USB Drive Missing! Aborting mount attempt.\n\nPress Enter to continue." 16 60
else
echo "!!! ERROR: USB Drive Missing! Aborting mount. Press Enter to continue."
fi
exit 1
fi
fi
USB_MOUNT_DEVICE=""
# Check for the common case: a single USB disk with one partition
if [ `cat /tmp/usb_block_devices | wc -l` -eq 1 ]; then
USB_MOUNT_DEVICE=`cat /tmp/usb_block_devices`
fi
# otherwise, let the user pick
if [ -z ${USB_MOUNT_DEVICE} ]; then
> /tmp/usb_disk_list
for i in `cat /tmp/usb_block_devices`; do
echo $i $(blkid | grep $i | grep -o 'LABEL=".*"' | cut -f2 -d '"') >> /tmp/usb_disk_list
done
if [ -x /bin/whiptail ]; then
MENU_OPTIONS=""
n=0
while read option
do
n=`expr $n + 1`
option=$(echo $option | tr " " "_")
MENU_OPTIONS="$MENU_OPTIONS $n ${option}"
done < /tmp/usb_disk_list
MENU_OPTIONS="$MENU_OPTIONS a Abort"
whiptail $BG_COLOR --clear --title "Select your USB disk" \
--menu "Choose your USB disk [1-$n, a to abort]:" 20 120 8 \
-- $MENU_OPTIONS \
2>/tmp/whiptail
option_index=$(cat /tmp/whiptail)
else
echo "+++ Select your USB disk:"
n=0
while read option
do
n=`expr $n + 1`
echo "$n. $option"
done < /tmp/usb_disk_list
read \
-p "Choose your USB disk [1-$n, a to abort]: " \
option_index
fi
if [ "$option_index" = "a" ]; then
exit 5
fi
USB_MOUNT_DEVICE=`head -n $option_index /tmp/usb_disk_list | tail -1 | sed 's/\ .*$//'`
fi
if [ "$1" = "rw" ]; then
mount -o rw $USB_MOUNT_DEVICE /media
else
mount -o ro $USB_MOUNT_DEVICE /media
fi