Skip to contentSkip to conversation

scripts: Create interactive menu for mkefidisk script

PATCH In review Bruno Bottazzini 2 participants 5 messages Sep 1, 2015 – Sep 4, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts: Create interactive menu for mkefidisk script

If it is passed only the image parameter, it will show some interactive options to help to conclude the script.

With this patch it will detect devices available in the machine, asking to choose what it is wanted to be flashed.

it will suggest the target device. If it is a SD card the suggestion is /dev/mmcblk0. If it is a pendrive it will suggest to use /dev/sda.

---
 scripts/contrib/mkefidisk.sh | 60 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 55 insertions(+), 5 deletions(-)
scripts/contrib/mkefidisk.sh +55 -5
index 55f72b0..7628de7 100755
@@ -152,8 +152,52 @@ unmount() {
 # Parse and validate arguments
 #
 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
-	usage
-	exit 1
+    if [ $# -eq 1 ]; then
+        AVAILABLE_DISK=`fdisk -l | grep "Disk /" | cut -f 1 -d ':' | cut -f 2 -d ' '`
+        if [ -z "$AVAILABLE_DISK" ]; then
+            die "Starting mkefidisk.sh as root is required"
+        fi
+        echo "Available disks are"
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            info "$X - $line"
+            X=`expr $X + 1`
+        done
+        read -p "Choose flashable device number: " DISK_NUMBER
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            if [ $DISK_NUMBER -eq $X ]; then
+                DISK_TO_BE_FLASHED=$line
+                break
+            else
+                X=`expr $X + 1`
+            fi
+        done
+        if [ -z "$DISK_TO_BE_FLASHED" ]; then
+            die "Option \"$DISK_NUMBER\" is invalid. Choose a valid option"
+        else
+            if [ -z `echo $DISK_TO_BE_FLASHED | grep "mmc"` ]; then
+                TARGET_TO_BE_BOOT="/dev/sda"
+            else
+                TARGET_TO_BE_BOOT="/dev/mmcblk0"
+            fi
+        fi
+        echo ""
+        echo "Choose a name of the device that will be boot from"
+        echo -n "Recommended name is: "
+        info "$TARGET_TO_BE_BOOT\n"
+        read -p "Is target device okay? [y/N]: " RESPONSE
+        if [ "$RESPONSE" != "y" ]; then
+            read -p "Choose target device name: " TARGET_TO_BE_BOOT
+        fi
+        echo ""
+        if [ -z "$TARGET_TO_BE_BOOT" ]; then
+            die "Error: choose a valid target name"
+        fi
+    else
+        usage
+	    exit 1
+    fi
 fi
 
 if [ "$1" = "-v" ]; then
@@ -162,9 +206,15 @@ if [ "$1" = "-v" ]; then
 	shift
 fi
 
-DEVICE=$1
-HDDIMG=$2
-TARGET_DEVICE=$3
+if [ -z "$AVAILABLE_DISK" ]; then
+    DEVICE=$1
+    HDDIMG=$2
+    TARGET_DEVICE=$3
+else
+    DEVICE=$DISK_TO_BE_FLASHED
+    HDDIMG=$1
+    TARGET_DEVICE=$TARGET_TO_BE_BOOT
+fi
 
 LINK=$(readlink $DEVICE)
 if [ $? -eq 0 ]; then
Signature · 1 line
2.5.0
#1 lore raw

Bump

Quoted 92 lines · Bruno Bottazzini

If it is passed only the image parameter, it will show some interactive options to help to conclude the script.

With this patch it will detect devices available in the machine, asking to choose what it is wanted to be flashed.

it will suggest the target device. If it is a SD card the suggestion is /dev/mmcblk0. If it is a pendrive it will suggest to use /dev/sda.

---
 scripts/contrib/mkefidisk.sh | 60 ++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 55 insertions(+), 5 deletions(-)
scripts/contrib/mkefidisk.sh +55 -5
index 55f72b0..7628de7 100755
@@ -152,8 +152,52 @@ unmount() {
 # Parse and validate arguments
 #
 if [ $# -lt 3 ] || [ $# -gt 4 ]; then
-	usage
-	exit 1
+    if [ $# -eq 1 ]; then
+        AVAILABLE_DISK=`fdisk -l | grep "Disk /" | cut -f 1 -d ':' | cut -f 2 -d ' '`
+        if [ -z "$AVAILABLE_DISK" ]; then
+            die "Starting mkefidisk.sh as root is required"
+        fi
+        echo "Available disks are"
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            info "$X - $line"
+            X=`expr $X + 1`
+        done
+        read -p "Choose flashable device number: " DISK_NUMBER
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            if [ $DISK_NUMBER -eq $X ]; then
+                DISK_TO_BE_FLASHED=$line
+                break
+            else
+                X=`expr $X + 1`
+            fi
+        done
+        if [ -z "$DISK_TO_BE_FLASHED" ]; then
+            die "Option \"$DISK_NUMBER\" is invalid. Choose a valid option"
+        else
+            if [ -z `echo $DISK_TO_BE_FLASHED | grep "mmc"` ]; then
+                TARGET_TO_BE_BOOT="/dev/sda"
+            else
+                TARGET_TO_BE_BOOT="/dev/mmcblk0"
+            fi
+        fi
+        echo ""
+        echo "Choose a name of the device that will be boot from"
+        echo -n "Recommended name is: "
+        info "$TARGET_TO_BE_BOOT\n"
+        read -p "Is target device okay? [y/N]: " RESPONSE
+        if [ "$RESPONSE" != "y" ]; then
+            read -p "Choose target device name: " TARGET_TO_BE_BOOT
+        fi
+        echo ""
+        if [ -z "$TARGET_TO_BE_BOOT" ]; then
+            die "Error: choose a valid target name"
+        fi
+    else
+        usage
+	    exit 1
+    fi
 fi
 
 if [ "$1" = "-v" ]; then
@@ -162,9 +206,15 @@ if [ "$1" = "-v" ]; then
 	shift
 fi
 
-DEVICE=$1
-HDDIMG=$2
-TARGET_DEVICE=$3
+if [ -z "$AVAILABLE_DISK" ]; then
+    DEVICE=$1
+    HDDIMG=$2
+    TARGET_DEVICE=$3
+else
+    DEVICE=$DISK_TO_BE_FLASHED
+    HDDIMG=$1
+    TARGET_DEVICE=$TARGET_TO_BE_BOOT
+fi
 
 LINK=$(readlink $DEVICE)
 if [ $? -eq 0 ]; then
#2 lore raw
In reply to Bruno Bottazzini
Quoted 9 lines · Bruno Bottazzini

If it is passed only the image parameter, it will show some interactive options to help to conclude the script.

With this patch it will detect devices available in the machine, asking to choose what it is wanted to be flashed.

it will suggest the target device. If it is a SD card the suggestion is /dev/mmcblk0. If it is a pendrive it will suggest to use /dev/sda.

I am little concerned by this patch since it allows the root disk to be selected, even if someone can type it on the command line, but offering it in list would make someone believe that it was OK.

Quoted 39 lines
---
  scripts/contrib/mkefidisk.sh | 60 ++++++++++++++++++++++++++++++++++++++++----
  1 file changed, 55 insertions(+), 5 deletions(-)
scripts/contrib/mkefidisk.sh +24 -2
index 55f72b0..7628de7 100755
@@ -152,8 +152,52 @@ unmount() {
  # Parse and validate arguments
  #
  if [ $# -lt 3 ] || [ $# -gt 4 ]; then
-	usage
-	exit 1
+    if [ $# -eq 1 ]; then
+        AVAILABLE_DISK=`fdisk -l | grep "Disk /" | cut -f 1 -d ':' | cut -f 2 -d ' '`
+        if [ -z "$AVAILABLE_DISK" ]; then
+            die "Starting mkefidisk.sh as root is required"
+        fi
+        echo "Available disks are"
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            info "$X - $line"
+            X=`expr $X + 1`
+        done
+        read -p "Choose flashable device number: " DISK_NUMBER
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            if [ $DISK_NUMBER -eq $X ]; then
+                DISK_TO_BE_FLASHED=$line
+                break
+            else
+                X=`expr $X + 1`
+            fi
+        done
+        if [ -z "$DISK_TO_BE_FLASHED" ]; then
+            die "Option \"$DISK_NUMBER\" is invalid. Choose a valid option"
+        else

scripts/contrib/mkefidisk.sh unmount() { · line 188

Quoted 10 lines
scripts/contrib/mkefidisk.sh +10 -0
+            if [ -z `echo $DISK_TO_BE_FLASHED | grep "mmc"` ]; then
+                TARGET_TO_BE_BOOT="/dev/sda"
+            else
+                TARGET_TO_BE_BOOT="/dev/mmcblk0"
+            fi
+        fi
+        echo ""
+        echo "Choose a name of the device that will be boot from"
+        echo -n "Recommended name is: "
+        info "$TARGET_TO_BE_BOOT\n"

The \n usage is probably wrong here.

Quoted 35 lines
scripts/contrib/mkefidisk.sh +21 -3
+        read -p "Is target device okay? [y/N]: " RESPONSE
+        if [ "$RESPONSE" != "y" ]; then
+            read -p "Choose target device name: " TARGET_TO_BE_BOOT
+        fi
+        echo ""
+        if [ -z "$TARGET_TO_BE_BOOT" ]; then
+            die "Error: choose a valid target name"
+        fi
+    else
+        usage
+	    exit 1
+    fi
  fi
 
  if [ "$1" = "-v" ]; then
@@ -162,9 +206,15 @@ if [ "$1" = "-v" ]; then
  	shift
  fi
 
-DEVICE=$1
-HDDIMG=$2
-TARGET_DEVICE=$3
+if [ -z "$AVAILABLE_DISK" ]; then
+    DEVICE=$1
+    HDDIMG=$2
+    TARGET_DEVICE=$3
+else
+    DEVICE=$DISK_TO_BE_FLASHED
+    HDDIMG=$1
+    TARGET_DEVICE=$TARGET_TO_BE_BOOT
+fi
 
  LINK=$(readlink $DEVICE)
  if [ $? -eq 0 ]; then
#3 lore raw
Quoted 7 lines · Saul Wold
Quoted 6 lines · Bruno Bottazzini

If it is passed only the image parameter, it will show some interactive options to help to conclude the script.

With this patch it will detect devices available in the machine, asking to choose what it is wanted to be flashed.

Quoted 7 lines
Quoted 3 lines

it will suggest the target device. If it is a SD card the suggestion is /dev/mmcblk0. If it is a pendrive it will suggest to use /dev/sda.

I am little concerned by this patch since it allows the root disk to be selected, even if someone can type it on the command line, but offering it in list would make someone believe that it was OK.

You are totally right.

However, the script already warn and handles this kind of situation. Also, this gives free will to the user. I don't see this as problem but something, besides as you have told the user may pass it as argument too. Maybe we can change the header message telling "all detect storage" or something like this?

I was wondering if the user wants to flash a H.D. when running a live CD and then maybe the root may be a right choice.

Since I have started using this patch, it got really faster when it comes flashing the device (I don't have to remember parameters every time). I think this will be a really good new feature for this script.

Quoted 88 lines
Quoted 41 lines
---
  scripts/contrib/mkefidisk.sh | 60 ++++++++++++++++++++++++++++++++++++++++----
  1 file changed, 55 insertions(+), 5 deletions(-)
scripts/contrib/mkefidisk.sh +24 -2
index 55f72b0..7628de7 100755
@@ -152,8 +152,52 @@ unmount() {
  # Parse and validate arguments
  #
  if [ $# -lt 3 ] || [ $# -gt 4 ]; then
-	usage
-	exit 1
+    if [ $# -eq 1 ]; then
+        AVAILABLE_DISK=`fdisk -l | grep "Disk /" | cut -f 1 -d ':' | cut -f 2 -d ' '`
+        if [ -z "$AVAILABLE_DISK" ]; then
+            die "Starting mkefidisk.sh as root is required"
+        fi
+        echo "Available disks are"
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            info "$X - $line"
+            X=`expr $X + 1`
+        done
+        read -p "Choose flashable device number: " DISK_NUMBER
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            if [ $DISK_NUMBER -eq $X ]; then
+                DISK_TO_BE_FLASHED=$line
+                break
+            else
+                X=`expr $X + 1`
+            fi
+        done
+        if [ -z "$DISK_TO_BE_FLASHED" ]; then
+            die "Option \"$DISK_NUMBER\" is invalid. Choose a valid option"
+        else

scripts/contrib/mkefidisk.sh unmount() { · line 188

Quoted 10 lines
scripts/contrib/mkefidisk.sh +10 -0
+            if [ -z `echo $DISK_TO_BE_FLASHED | grep "mmc"` ]; then
+                TARGET_TO_BE_BOOT="/dev/sda"
+            else
+                TARGET_TO_BE_BOOT="/dev/mmcblk0"
+            fi
+        fi
+        echo ""
+        echo "Choose a name of the device that will be boot from"
+        echo -n "Recommended name is: "
+        info "$TARGET_TO_BE_BOOT\n"

The \n usage is probably wrong here.

Quoted 35 lines
scripts/contrib/mkefidisk.sh +21 -3
+        read -p "Is target device okay? [y/N]: " RESPONSE
+        if [ "$RESPONSE" != "y" ]; then
+            read -p "Choose target device name: " TARGET_TO_BE_BOOT
+        fi
+        echo ""
+        if [ -z "$TARGET_TO_BE_BOOT" ]; then
+            die "Error: choose a valid target name"
+        fi
+    else
+        usage
+	    exit 1
+    fi
  fi
 
  if [ "$1" = "-v" ]; then
@@ -162,9 +206,15 @@ if [ "$1" = "-v" ]; then
  	shift
  fi
 
-DEVICE=$1
-HDDIMG=$2
-TARGET_DEVICE=$3
+if [ -z "$AVAILABLE_DISK" ]; then
+    DEVICE=$1
+    HDDIMG=$2
+    TARGET_DEVICE=$3
+else
+    DEVICE=$DISK_TO_BE_FLASHED
+    HDDIMG=$1
+    TARGET_DEVICE=$TARGET_TO_BE_BOOT
+fi
 
  LINK=$(readlink $DEVICE)
  if [ $? -eq 0 ]; then
#4 lore raw
Quoted 16 lines · Bruno
Quoted 7 lines · Saul Wold
Quoted 6 lines · Bruno Bottazzini

If it is passed only the image parameter, it will show some interactive options to help to conclude the script.

With this patch it will detect devices available in the machine, asking to choose what it is wanted to be flashed.

Quoted 7 lines
Quoted 3 lines

it will suggest the target device. If it is a SD card the suggestion is /dev/mmcblk0. If it is a pendrive it will suggest to use /dev/sda.

I am little concerned by this patch since it allows the root disk to be selected, even if someone can type it on the command line, but offering it in list would make someone believe that it was OK.

Quoted 5 lines

You are totally right.

However, the script already warn and handles this kind of situation. Also, this gives free will to the user. I don't see this as problem but something, besides as you have told the user may pass it as argument

Sorry correcting: s/I don't see this as problem but something/I don't see this as problem but as feature/

Quoted 100 lines

too. Maybe we can change the header message telling "all detect storage" or something like this?

I was wondering if the user wants to flash a H.D. when running a live CD and then maybe the root may be a right choice.

Since I have started using this patch, it got really faster when it comes flashing the device (I don't have to remember parameters every time). I think this will be a really good new feature for this script.

Quoted 88 lines
Quoted 41 lines
---
  scripts/contrib/mkefidisk.sh | 60 ++++++++++++++++++++++++++++++++++++++++----
  1 file changed, 55 insertions(+), 5 deletions(-)
scripts/contrib/mkefidisk.sh +24 -2
index 55f72b0..7628de7 100755
@@ -152,8 +152,52 @@ unmount() {
  # Parse and validate arguments
  #
  if [ $# -lt 3 ] || [ $# -gt 4 ]; then
-	usage
-	exit 1
+    if [ $# -eq 1 ]; then
+        AVAILABLE_DISK=`fdisk -l | grep "Disk /" | cut -f 1 -d ':' | cut -f 2 -d ' '`
+        if [ -z "$AVAILABLE_DISK" ]; then
+            die "Starting mkefidisk.sh as root is required"
+        fi
+        echo "Available disks are"
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            info "$X - $line"
+            X=`expr $X + 1`
+        done
+        read -p "Choose flashable device number: " DISK_NUMBER
+        X=0
+        for line in `echo $AVAILABLE_DISK`; do
+            if [ $DISK_NUMBER -eq $X ]; then
+                DISK_TO_BE_FLASHED=$line
+                break
+            else
+                X=`expr $X + 1`
+            fi
+        done
+        if [ -z "$DISK_TO_BE_FLASHED" ]; then
+            die "Option \"$DISK_NUMBER\" is invalid. Choose a valid option"
+        else

scripts/contrib/mkefidisk.sh unmount() { · line 188

Quoted 10 lines
scripts/contrib/mkefidisk.sh +10 -0
+            if [ -z `echo $DISK_TO_BE_FLASHED | grep "mmc"` ]; then
+                TARGET_TO_BE_BOOT="/dev/sda"
+            else
+                TARGET_TO_BE_BOOT="/dev/mmcblk0"
+            fi
+        fi
+        echo ""
+        echo "Choose a name of the device that will be boot from"
+        echo -n "Recommended name is: "
+        info "$TARGET_TO_BE_BOOT\n"

The \n usage is probably wrong here.

Quoted 35 lines
scripts/contrib/mkefidisk.sh +21 -3
+        read -p "Is target device okay? [y/N]: " RESPONSE
+        if [ "$RESPONSE" != "y" ]; then
+            read -p "Choose target device name: " TARGET_TO_BE_BOOT
+        fi
+        echo ""
+        if [ -z "$TARGET_TO_BE_BOOT" ]; then
+            die "Error: choose a valid target name"
+        fi
+    else
+        usage
+	    exit 1
+    fi
  fi
 
  if [ "$1" = "-v" ]; then
@@ -162,9 +206,15 @@ if [ "$1" = "-v" ]; then
  	shift
  fi
 
-DEVICE=$1
-HDDIMG=$2
-TARGET_DEVICE=$3
+if [ -z "$AVAILABLE_DISK" ]; then
+    DEVICE=$1
+    HDDIMG=$2
+    TARGET_DEVICE=$3
+else
+    DEVICE=$DISK_TO_BE_FLASHED
+    HDDIMG=$1
+    TARGET_DEVICE=$TARGET_TO_BE_BOOT
+fi
 
  LINK=$(readlink $DEVICE)
  if [ $? -eq 0 ]; then