Adding live-media-eject, previously included in live-config.
authorDaniel Baumann <mail@daniel-baumann.ch>
Mon, 24 Jun 2013 18:04:00 +0000 (20:04 +0200)
committerDaniel Baumann <mail@daniel-baumann.ch>
Mon, 24 Jun 2013 18:04:00 +0000 (20:04 +0200)
bin/live-media-eject [new file with mode: 0755]
debian/live-tools.init [new file with mode: 0644]
debian/rules

diff --git a/bin/live-media-eject b/bin/live-media-eject
new file mode 100755 (executable)
index 0000000..c688cee
--- /dev/null
@@ -0,0 +1,187 @@
+#!/bin/sh
+
+## live-tools(7) - System Support Scripts
+## Copyright (C) 2006-2013 Daniel Baumann <mail@daniel-baumann.ch>
+##
+## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
+## This is free software, and you are welcome to redistribute it
+## under certain conditions; see COPYING for details.
+
+
+set -e
+
+# Exit if system is not a live system
+if ! grep -qs "boot=live" /proc/cmdline || \
+# Exit if system is netboot
+   grep -qs "netboot" /proc/cmdline || \
+   grep -qs "root=/dev/nfs" /proc/cmdline || \
+   grep -qs "root=/dev/cifs" /proc/cmdline || \
+# Exit if system is toram
+   grep -qs "toram" /proc/cmdline
+then
+       exit 0
+fi
+
+
+
+# Try to cache everything we're likely to need after ejecting.  This
+# is fragile and simple-minded, but our options are limited.
+cache_path()
+{
+       path="${1}"
+
+       if [ -d "${path}" ]
+       then
+               find "${path}" -type f | xargs cat > /dev/null 2>&1
+       elif [ -f "${path}" ]
+       then
+               if [ -x /usr/bin/file ] && file -L "${path}" | grep -q 'dynamically linked'
+               then
+                       # ldd output can be of three forms:
+                       # 1. linux-vdso.so.1 =>  (0x00007fffe3fb4000)
+                       #    This is a virtual, kernel shared library and we want to skip it
+                       # 2. libc.so.6 => /lib/libc.so.6 (0x00007f5e9dc0c000)
+                       #    We want to cache the third word.
+                       # 3. /lib64/ld-linux-x86-64.so.2 (0x00007f5e9df8b000)
+                       #    We want to cache the first word.
+                       ldd "${path}" | while read line
+                       do
+                               if echo "$line" | grep -qs ' =>  '
+                               then
+                                       continue
+                               elif echo "$line" | grep -qs ' => '
+                               then
+                                       lib=$(echo "${line}" | awk '{ print $3 }')
+                               else
+                                       lib=$(echo "${line}" | awk '{ print $1 }')
+                               fi
+                               cache_path "${lib}"
+                       done
+               fi
+
+               cat "${path}" >/dev/null 2>&1
+       fi
+}
+
+get_boot_device()
+{
+       # search in /proc/mounts for the device that is mounted at /lib/live/mount/medium
+       while read DEVICE MOUNT REST
+       do
+               case "${MOUNT}" in
+                       /lib/live/mount/medium)
+                               echo "${DEVICE}"
+                               exit 0
+                               ;;
+               esac
+       done < /proc/mounts
+}
+
+device_is_USB_flash_drive()
+{
+       # remove leading "/dev/" and all trailing numbers from input
+       DEVICE=$(echo ${1} | sed -e 's|/dev/||' -e 's|[0-9].*$||')
+
+       # check that device starts with "sd"
+       case "${DEVICE}" in
+               sd*)
+                       ;;
+
+               *)
+                       return 1
+                       ;;
+       esac
+
+       # check that the device is an USB device
+       if readlink /sys/block/${DEVICE} | grep -q usb
+       then
+               return 0
+       fi
+
+       return 1
+}
+
+Eject ()
+{
+       # TODO: i18n
+       BOOT_DEVICE="$(get_boot_device)"
+
+       if device_is_USB_flash_drive ${BOOT_DEVICE}
+       then
+               # do NOT eject USB flash drives!
+               # otherwise rebooting with most USB flash drives
+               # failes because they actually remember the
+               # "ejected" state even after reboot
+               MESSAGE="Please remove the USB flash drive"
+
+               case "${NOPROMPT}" in
+                       usb)
+                               prompt=
+                               ;;
+               esac
+       else
+               # ejecting is a very good idea here
+               MESSAGE="Please remove the disc, close the tray (if any)"
+
+               if [ -x /usr/bin/eject ]
+               then
+                       eject -p -m /lib/live/mount/medium >/dev/null 2>&1
+               fi
+
+               case "${NOPROMPT}" in
+                       cd)
+                               prompt=
+                               ;;
+               esac
+       fi
+
+       [ "$prompt" ] || return 0
+
+       if [ -x /bin/plymouth ] && plymouth --ping
+       then
+               plymouth message --text="${MESSAGE} and press ENTER to continue:"
+               plymouth watch-keystroke > /dev/null
+       else
+               stty sane < /dev/console
+
+               printf "\n\n${MESSAGE} and press ENTER to continue:" > /dev/console
+
+               read x < /dev/console
+       fi
+}
+
+echo "live-tools: caching reboot files..."
+
+prompt=1
+
+case "${NOPROMPT}" in
+       yes)
+               prompt=
+               ;;
+esac
+
+for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty) /bin/plymouth
+do
+       cache_path "${path}"
+done
+
+mount -o remount,ro /lib/live/mount/overlay > /dev/null 2>&1
+
+# Remounting any persistence devices read-only
+for _MOUNT in $(awk '/\/lib\/live\/mount\/persistence/ { print $2 }' /proc/mounts)
+do
+       mount -o remount,ro ${_MOUNT} > /dev/null 2>&1
+done
+
+# Flush filesystem buffers
+sync
+
+# Check if we need to eject the drive
+if grep -qs "cdrom-detect/eject=false" /proc/cmdline || \
+   grep -qs "noeject" /proc/cmdline || \
+   grep -qs "find_iso" /proc/cmdline
+then
+       return
+else
+       Eject
+fi
diff --git a/debian/live-tools.init b/debian/live-tools.init
new file mode 100644 (file)
index 0000000..f4bc962
--- /dev/null
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+## live-tools contains additional support scripts for live systems.
+##
+## This is the sysvinit initscript for live-tools.
+
+
+### BEGIN INIT INFO
+# Provides:            live-tools
+# Required-Start:      $syslog bootmisc
+# Required-Stop:
+# Should-Start:                $local_fs
+# Should-Stop:         halt reboot
+# Default-Start:       S
+# Default-Stop:                0 6
+# Short-Description:   live-tools - System Support Scripts
+# Description:         live-tools contains additional support scripts for live
+#                      systems.
+# X-Start-Before:
+# X-Stop-After:                umountroot
+# X-Interactive:       true
+### END INIT INFO
+
+case "${1}" in
+       stop)
+               if [ -e /bin/live-media-eject ]
+               then
+                       /bin/live-media-eject ${@}
+               fi
+               ;;
+
+       start|force-reload|restart)
+
+               ;;
+esac
index 08f25ae..eb05034 100755 (executable)
@@ -11,3 +11,6 @@ override_dh_auto_install:
 
 override_dh_builddeb:
        dh_builddeb -- -Zxz
+
+override_dh_installinit:
+       dh_installinit --no-restart-on-upgrade --no-start --update-rcd-params='start 89 0 6 .'