[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Patch for knoppix-halt



Hi all,

I noticed several problems with the knoppix-halt script:

1) I did some tests and people never seem to notice the final message "Please 
remove ...". They are sitting there staring at the screen waiting for 
something else to happen. I fixed this by using  a dialog message. The 
results were much better.

2) The exit message has "CD" hardcoded into it. I use Knoppix on a USB flash 
drive and want to see the correct medium in the final message.

3) Some USB flash drives actually understand the "eject" command. They 
stay "ejected" even after rebooting the system. Therefore rebooting Knoppix 
on a USB flash drive fails very often.

To detect if the boot medium is a USB flash drive, I just test if the device 
mounted to /cdrom starts with "sd" and is removable. This might be very 
simple and stupid but it had worked very well for me so far. I am sure that 
there is a more simple and more reliable method and I would be really glad to 
learn it. Klaus, can you help?

Anyway, attached is my current quick-and-dirty patch. Needs still some 
polish...

Greetings

Ronny
--- knoppix-halt_new	2008-06-30 12:10:23.000000000 +0200
+++ knoppix-halt_patched	2008-07-30 10:09:21.000000000 +0200
@@ -34,11 +34,17 @@
 ${YELLOW}KNOPPIX halted.${NORMAL}"
 	command="halt"
 	options="-p -d -i -f"
+	EXIT_LABEL="Shut down"
+	VERB1="shut down"
+	VERB2="$VERB1"
 	;;
   *reboot)
 	message="${GREEN}Preparing for reboot...${NORMAL}"
 	command="reboot"
 	options="-d -i -f"
+	EXIT_LABEL="Reboot"
+	VERB1="reboot"
+	VERB2="rebooted"
 	;;
   *)
 	echo "$0: call this script as \"halt\" or \"reboot\" please!"
@@ -330,12 +336,59 @@
 
 [ -e /cdrom/KNOPPIX/KNOPPIX ] || NOEJECT="true"
 
+get_boot_device() {
+  # search in /proc/mounts for the device that is mounted at /cdrom
+  while read DEVICE MOUNT REST; do
+    if [ "${MOUNT}" == "/cdrom" ]; then
+      echo "${DEVICE}"
+    fi
+  done < /proc/mounts
+}
+
+is_device_usb() {
+  # check that device starts with "sd"
+  DEVICE=$(expr substr $1 6 3)
+  [ "$(expr substr ${DEVICE} 1 2)" != "sd" ] && return 0
+  # check that device is removable
+  REMOVABLE=$(cat /sys/block/${DEVICE}/removable)
+  [ "${REMOVABLE}" == "1" ] && return 1
+  return 0
+}
+
+BOOT_DEVICE="$(get_boot_device)"
+is_device_usb ${BOOT_DEVICE}
+BOOT_DEVICE_IS_USB=$?
+
 if [ -z "$NOEJECT" ]; then
  # Preload "halt" command into memory before it is gone.
  $FINALCMD --help >/dev/null 2>&1
- eject -m -p /cdrom >/dev/null 2>&1 &
+ if [ ${BOOT_DEVICE_IS_USB} -eq 1 ]; then
+  MEDIUM="USB flash drive"
+ else
+  MEDIUM="DVD"
+  # TODO: detect CD
+ fi
+ MESSAGE="Please remove the Knoppix"
+ case "${MEDIUM}" in
+  "DVD")
+  MESSAGE="${MESSAGE} ${MEDIUM}, close the ${MEDIUM} drive"
+  WIDTH=55
+  HEIGHT=14
+  ;;
+  "USB flash drive")
+  MESSAGE="${MESSAGE} ${MEDIUM}"
+  WIDTH=55
+  HEIGHT=13
+  ;;
+ esac
+ MESSAGE="${MESSAGE} and press Enter or Return to ${VERB1} the system.\n\n(If you do nothing, the system will be automatically ${VERB2} when the timer runs out.)"
+
+ if [ "${MEDIUM}" == "DVD" ]; then
+  # dialog (below) needs a working system, therefore we wait for a second before calling eject
+  $(sleep 1; eject -m -p /cdrom >/dev/null 2>&1)&
+ fi
  if [ -z "$NOPROMPT" ]; then
-  read -s -p "${CYAN}Please remove CD, close cdrom drive and hit return [auto 2 minutes].${NORMAL}" -t 120 a </dev/console
+  dialog --colors --exit-label "$EXIT_LABEL" --pause "$MESSAGE" $HEIGHT $WIDTH 100
  fi
 fi
 

Reply to: