149314bef0afb35888d704ed1b48ecaa7ae04694
[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 do_stop ()
60 {
61     if [ ! -z "${ROOTSNAP}" ]; then
62         ${DO_SNAPSHOT} --resync-string="${ROOTSNAP}"
63     fi
64
65     if [ ! -z "${HOMESNAP}" ]; then
66         ${DO_SNAPSHOT} --resync-string="${HOMESNAP}"
67     fi
68
69     # check for netboot
70     if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline ; then
71         return 0
72     fi
73
74     prompt=1
75     if grep -qs noprompt /proc/cmdline; then
76        prompt=
77     fi
78
79     for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty); do
80         cache_path "${path}"
81     done
82
83     for x in $(cat /proc/cmdline); do
84         case ${x} in
85             quickreboot)
86                 QUICKREBOOT="Yes"
87             ;;
88         esac
89     done
90
91     if [ -z ${QUICKREBOOT} ]; then
92         if [ -x /usr/bin/eject ]
93         then
94                 eject -p -m /live/image >/dev/null 2>&1
95
96                 [ "$prompt" ] || return 0
97         fi
98
99         stty sane < /dev/console
100
101         # XXX - i18n
102         echo "Please remove the disc and close the tray (if any) then press ENTER: " > /dev/console
103         if [ -x /sbin/usplash_write ]; then
104             /sbin/usplash_write "TIMEOUT 86400"
105             /sbin/usplash_write "TEXT-URGENT Please remove the disc, close the tray (if any)"
106             /sbin/usplash_write "TEXT-URGENT and press ENTER to continue"
107         fi
108
109         read x < /dev/console
110     fi
111 }
112
113 case "${1}" in
114     start|restart|reload|force-reload|status)
115         [ "${VERBOSE}" != no ] && log_end_msg 0
116         ;;
117     stop)
118         log_begin_msg "${NAME} is resyncing snapshots and caching reboot files..."
119         do_stop
120         case "${?}" in
121             0|1) [ "${VERBOSE}" != no ] && log_end_msg 0 ;;
122             2) [ "${VERBOSE}" != no ] && log_end_msg 1 ;;
123         esac
124         ;;
125     *)
126         log_success_msg "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
127         exit 3
128         ;;
129 esac