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