1dd72cce75b42688c962b114a97fd4baaabce7e9
[live-boot-grml.git] / debian / init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:             live-initramfs
5 # Required-Start:       $syslog
6 # Required-Stop:        $syslog
7 # Should-Start:         $local_fs
8 # Should-Stop:          $local_fs
9 # Default-Start:        1 2 3 4 5
10 # Default-Stop:         0 6
11 # Short-Description:    live-initramfs init script
12 # Description:          Resyncs snapshots, evantually caches files in order to
13 #                       let remove the media.
14 ### END INIT INFO
15
16 # Authors: Tollef Fog Heen <tfheen@canonical.com>
17 #          Marco Amadori <marco.amadori@gmail.com>
18
19 PATH=/usr/sbin:/usr/bin:/sbin:/bin
20 NAME=live-initramfs
21 SCRIPTNAME=/etc/init.d/${NAME}
22 DO_SNAPSHOT=/sbin/live-snapshot
23
24 # Exit if system was not booted by live-initramfs
25 grep -qs boot=live /proc/cmdline || exit 0
26
27 # Exit if the system was booted from an ISO image rather than a physical CD
28 grep -qs find_iso= /proc/cmdline && exit 0
29
30 # Read configuration variable file if it is present
31 [ -r /etc/live.conf ] && . /etc/live.conf
32
33 # Load the VERBOSE setting and other rcS variables
34 [ -f /etc/default/rcS ] && . /etc/default/rcS
35
36 # Define LSB log_* functions.
37 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
38 . /lib/lsb/init-functions
39
40 # Try to cache everything we're likely to need after ejecting.  This
41 # is fragile and simple-minded, but our options are limited.
42 cache_path() {
43     path="${1}"
44
45     if [ -d "${path}" ]; then
46         find "${path}" -type f | xargs cat > /dev/null 2>&1
47     elif [ -f "${path}" ]; then
48         if [ -x "${path}" ]; then
49             if file "${path}" | grep -q 'dynamically linked'; then
50                 for lib in $(ldd "${path}" | awk '{ print $3 }'); do
51                     cache_path "${lib}"
52                 done
53             fi
54         fi
55         cat "${path}" >/dev/null 2>&1
56     fi
57 }
58
59 get_boot_device() {
60     # search in /proc/mounts for the device that is mounted at /live/image
61     while read DEVICE MOUNT REST; do
62         if [ "${MOUNT}" == "/live/image" ]; then
63             echo "${DEVICE}"
64             exit 0
65         fi
66     done < /proc/mounts
67 }
68
69 device_is_USB_flash_drive()
70 {
71     # remove leading "/dev/" and all trailing numbers from input
72     DEVICE=$(expr substr ${1} 6 3)
73     # check that device starts with "sd"
74     [ "$(expr substr ${DEVICE} 1 2)" != "sd" ] && return 1
75     # check that the device is an USB device
76     if readlink /sys/block/${DEVICE}/device | grep -q usb ; then
77         return 0
78     fi
79     return 1
80 }
81
82 do_stop ()
83 {
84     if [ ! -z "${ROOTSNAP}" ]; then
85         ${DO_SNAPSHOT} --resync-string="${ROOTSNAP}"
86     fi
87
88     if [ ! -z "${HOMESNAP}" ]; then
89         ${DO_SNAPSHOT} --resync-string="${HOMESNAP}"
90     fi
91
92     # check for netboot
93     if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline ; then
94         return 0
95     fi
96
97     prompt=1
98     if grep -qs noprompt /proc/cmdline; then
99        prompt=
100     fi
101
102     for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty); do
103         cache_path "${path}"
104     done
105
106     for x in $(cat /proc/cmdline); do
107         case ${x} in
108             quickreboot)
109                 QUICKREBOOT="Yes"
110             ;;
111         esac
112     done
113
114     if [ -z ${QUICKREBOOT} ]; then
115         # TODO: i18n, dialog
116         if [ -x /usr/bin/eject ]
117         then
118                 BOOT_DEVICE="$(get_boot_device)"
119                 if device_is_USB_flash_drive ${BOOT_DEVICE}; then
120                         # do NOT eject USB flash drives!
121                         # otherwise rebooting with most USB flash drives failes because
122                         # they actually remember the "ejected" state even after reboot
123                         MESSAGE="Please remove the USB flash drive"
124                 else
125                         # ejecting is a very good idea here
126                         eject -p -m /live/image >/dev/null 2>&1
127                         # TODO: detect CD
128                         MEDIUM="DVD"
129                         MESSAGE="Please remove the ${MEDIUM}, close the ${MEDIUM} tray (if any)"
130                 fi
131                 MESSAGE="${MESSAGE} and press ENTER:"
132
133                 [ "$prompt" ] || return 0
134         fi
135
136         stty sane < /dev/console
137
138         echo -n -e "\n\n${MESSAGE}" > /dev/console
139         if [ -x /sbin/usplash_write ]; then
140             /sbin/usplash_write "TIMEOUT 86400"
141             /sbin/usplash_write "TEXT-URGENT Please remove the disc, close the tray (if any)"
142             /sbin/usplash_write "TEXT-URGENT and press ENTER to continue"
143         fi
144
145         read x < /dev/console
146     fi
147 }
148
149 case "${1}" in
150     start|restart|reload|force-reload|status)
151         [ "${VERBOSE}" != no ] && log_end_msg 0
152         ;;
153     stop)
154         log_begin_msg "${NAME} is resyncing snapshots and caching reboot files..."
155         do_stop
156         case "${?}" in
157             0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
158             2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
159         esac
160         ;;
161     *)
162         log_success_msg "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
163         exit 3
164         ;;
165 esac