80066d0c5cab1daec8d0aab443fd7238c6241474
[live-boot-grml.git] / debian / casper.init
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          casper
4 # Required-Start:    $syslog
5 # Required-Stop:     $syslog
6 # Should-Start:      $local_fs
7 # Should-Stop:       $local_fs
8 # Default-Start:     1 2 3 4 5
9 # Default-Stop:      0 6
10 # Short-Description: Casper init script
11 # Description:       Resyncs snapshots, evantually caches files in order
12 #                    to let remove the media.
13 ### END INIT INFO
14
15 # Author: Tollef Fog Heen <tfheen@canonical.com>
16 #         Marco Amadori <marco.amadori@gmail.com>
17 #
18 PATH=/usr/sbin:/usr/bin:/sbin:/bin
19 NAME=casper
20 SCRIPTNAME=/etc/init.d/${NAME}
21 DO_SNAPSHOT=/sbin/${NAME}-snapshot
22
23 # Exit if system was not booted by casper
24 grep -qs boot=casper /proc/cmdline || exit 0
25
26 # Read configuration variable file if it is present
27 [ -r /etc/$NAME.conf ] && . /etc/$NAME.conf
28
29 # Load the VERBOSE setting and other rcS variables
30 [ -f /etc/default/rcS ] && . /etc/default/rcS
31
32 # Define LSB log_* functions.
33 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
34 . /lib/lsb/init-functions
35
36 # Try to cache everything we're likely to need after ejecting.  This
37 # is fragile and simple-minded, but our options are limited.
38 cache_path() {
39     path="$1"
40
41     if [ -d "$path" ]; then
42         find "$path" -type f | xargs cat > /dev/null 2>&1
43     elif [ -f "$path" ]; then
44         if [ -x "$path" ]; then
45             if file "$path" | grep -q 'dynamically linked'; then
46                 for lib in $(ldd "$path" | awk '{ print $3 }'); do
47                     cache_path "$lib"
48                 done
49             fi
50         fi
51         cat "$path" >/dev/null 2>&1
52     fi
53 }
54
55 do_stop ()
56 {
57     if [ ! -z "${ROOTSNAP}" ]; then
58         $DO_SNAPSHOT --resync-string="${ROOTSNAP}"
59     fi
60
61     if [ ! -z "${HOMESNAP}" ]; then
62         $DO_SNAPSHOT --resync-string="${HOMESNAP}"
63     fi
64
65     # check for netboot
66     if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline ; then
67         return 0
68     fi
69
70     for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default; do
71         cache_path "$path"
72     done
73
74     for x in $(cat /proc/cmdline); do
75         case $x in
76             quickreboot)
77                 QUICKREBOOT="Yes"
78             ;;
79         esac
80     done
81
82     if [ -z ${QUICKREBOOT} ]; then
83         eject -p -m /live_media >/dev/null 2>&1
84
85         # XXX - i18n
86         echo "Please remove the disc and close the tray (if any) then press ENTER: "
87         if [ -x /sbin/usplash_write ]; then
88             /sbin/usplash_write "TIMEOUT 86400"
89             /sbin/usplash_write "TEXT-URGENT Please remove the disc, close the tray (if any)"
90             /sbin/usplash_write "TEXT-URGENT and press ENTER to continue"
91         fi
92
93         read x < /dev/console
94     fi
95 }
96
97 case "$1" in
98     start|restart|reload|force-reload|status)
99         [ "$VERBOSE" != no ] && log_end_msg 0
100         ;;
101     stop)
102         log_begin_msg "${NAME} is resyncing snapshots and caching reboot files..."
103         do_stop
104         case "$?" in
105             0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
106             2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
107         esac
108         ;;
109     *)
110         log_success_msg "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
111         exit 3
112         ;;
113 esac