From 0ddc28312087bd711b49240361177309ed3641e7 Mon Sep 17 00:00:00 2001 From: Eric Hameleers Date: Tue, 19 Sep 2017 19:41:43 +0200 Subject: iso2usb.sh - add options to list or scan for removable devices Two new script options were added: -d|--devices List removable devices on this computer. -s|--scan Scan for insertion of new USB device instead of providing a devicename (using option '-o'). --- iso2usb.sh | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/iso2usb.sh b/iso2usb.sh index 7b386fc..dbf1a93 100644 --- a/iso2usb.sh +++ b/iso2usb.sh @@ -41,6 +41,12 @@ PERSISTENCE="${DEF_PERSISTENCE}" # Default persistence type is a directory: PERSISTTYPE="dir" +# Timeout when scanning for inserted USB device: +SCANWAIT=30 + +# Set to '1' if we are to scan for device insertion: +SCAN=0 + # Set to '1' if the script should not ask any questions: UNATTENDED=0 @@ -141,6 +147,7 @@ cat < Full path to the ISO image file. @@ -151,6 +158,8 @@ cat < Add seconds wait time to initialize USB. @@ -177,6 +186,30 @@ uncompressfs () { fi } +# Scan for insertion of a USB device: +scan_devices() { + local BD + # Inotifywatch does not trigger on symlink creation, + # so we can not watch /sys/block/ + BD=$(inotifywait -q -t ${SCANWAIT} -e create /dev 2>/dev/null |cut -d' ' -f3) + echo ${BD} +} # End of scan_devices() + +# Show a list of removable devices detected on this computer: +show_devices() { + local MYDATA="${*}" + if [ -z "${MYDATA}" ]; then + MYDATA="$(ls --indicator-style=none /sys/block/ |grep -Ev '(ram|loop|dm-)')" + fi + echo "# Removable devices detected on this computer:" + for BD in ${MYDATA} ; do + if [ $(cat /sys/block/${BD}/removable) -eq 1 ]; then + echo "# /dev/${BD} : $(cat /sys/block/${BD}/device/vendor) $(cat /sys/block/${BD}/device/model): $(( $(cat /sys/block/${BD}/size) / 2048)) MB" + fi + done + echo "#" +} # End of show_devices() + # Read configuration data from old initrd: read_initrd() { IMGFILE="$1" @@ -389,6 +422,10 @@ while [ ! -z "$1" ]; do REQTOOLS="${REQTOOLS} unsquashfs" shift 2 ;; + -d|--devices) + show_devices + exit + ;; -f|--force) FORCE=1 shift @@ -417,6 +454,10 @@ while [ ! -z "$1" ]; do REFRESH=1 shift ;; + -s|--scan) + SCAN=1 + shift + ;; -u|--unattended) UNATTENDED=1 shift @@ -453,8 +494,28 @@ if [ "$(id -u)" != "0" -a $FORCE -eq 0 ]; then fi # More sanity checks: -if [ -z "$TARGET" -o -z "$SLISO" ]; then - echo "*** You must specify both the Live ISO filename and the USB devicename!" +if [ -z "$SLISO" ]; then + echo "*** You must specify the Live ISO filename (option '-i')!" + exit 1 +fi + +# Either provide a block device, or else scan for a block device: +if [ -z "$TARGET" ]; then + if [ $SCAN -eq 1 ]; then + echo "-- Waiting for a USB stick to be inserted..." + TARGET=$(scan_devices) + if [ -z "$TARGET" ]; then + echo "*** No new USB device detected during $SCANWAIT seconds scan." + exit 1 + else + TARGET="/dev/${TARGET}" + fi + else + echo "*** You must specify the Live USB devicename (option '-o')!" + exit 1 + fi +elif [ $SCAN -eq 1 ]; then + echo "*** You can not use options '-o' and '-s' at the same time!" exit 1 fi @@ -465,9 +526,11 @@ fi if [ ! -b $TARGET -a $FORCE -eq 0 ]; then echo "*** Not a block device: '$TARGET' !" + show_devices exit 1 elif [ "$(echo ${TARGET%[0-9]})" != "$TARGET" -a $FORCE -eq 0 ]; then echo "*** You need to point to the USB device, not a partition ($TARGET)!" + show_devices exit 1 fi -- cgit v1.2.3