~ubuntu-core-dev/casper/trunk

249 by Tollef Fog Heen
Redo how the init script works and require it to be installed in the
1
#! /bin/sh
344.1.1 by Tollef Fog Heen
Merge from Debian
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
249 by Tollef Fog Heen
Redo how the init script works and require it to be installed in the
24
grep -qs boot=casper /proc/cmdline || exit 0
1 by Matt Zimmerman
tag of matt.zimmerman@canonical.com--2004/casper--main--0--patch-26
25
455 by Colin Watson
* Avoid ejecting the CD if booting from an ISO image rather than from a
26
# Exit if the system was booted from an ISO image rather than a physical CD
27
grep -qs find_iso= /proc/cmdline && exit 0
28
344.1.1 by Tollef Fog Heen
Merge from Debian
29
# Read configuration variable file if it is present
30
[ -r /etc/$NAME.conf ] && . /etc/$NAME.conf
31
32
# Load the VERBOSE setting and other rcS variables
33
[ -f /etc/default/rcS ] && . /etc/default/rcS
34
35
# Define LSB log_* functions.
36
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
37
. /lib/lsb/init-functions
38
1 by Matt Zimmerman
tag of matt.zimmerman@canonical.com--2004/casper--main--0--patch-26
39
# Try to cache everything we're likely to need after ejecting.  This
40
# is fragile and simple-minded, but our options are limited.
41
cache_path() {
42
    path="$1"
43
44
    if [ -d "$path" ]; then
45
        find "$path" -type f | xargs cat > /dev/null 2>&1
46
    elif [ -f "$path" ]; then
47
        if [ -x "$path" ]; then
48
            if file "$path" | grep -q 'dynamically linked'; then
49
                for lib in $(ldd "$path" | awk '{ print $3 }'); do
50
                    cache_path "$lib"
51
                done
52
            fi
53
        fi
54
        cat "$path" >/dev/null 2>&1
55
    fi
56
}
57
344.1.1 by Tollef Fog Heen
Merge from Debian
58
do_stop ()
59
{
60
    if [ ! -z "${ROOTSNAP}" ]; then
61
        $DO_SNAPSHOT --resync-string="${ROOTSNAP}"
62
    fi
63
64
    if [ ! -z "${HOMESNAP}" ]; then
65
        $DO_SNAPSHOT --resync-string="${HOMESNAP}"
66
    fi
67
68
    # check for netboot
69
    if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline ; then
70
        return 0
71
    fi
72
446 by Colin Watson
* Skip the CD eject prompt if 'noprompt' is on the kernel command line
73
    prompt=1
74
    if grep -qs noprompt /proc/cmdline; then
75
	prompt=
76
    fi
77
445 by Colin Watson
* Cache the stty binary before ejecting the CD.
78
    for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty); do
344.1.1 by Tollef Fog Heen
Merge from Debian
79
        cache_path "$path"
80
    done
81
353 by Colin Watson
* Fix more leftover /live_media wreckage (LP: #84592).
82
    eject -p -m /cdrom >/dev/null 2>&1
344.1.1 by Tollef Fog Heen
Merge from Debian
83
446 by Colin Watson
* Skip the CD eject prompt if 'noprompt' is on the kernel command line
84
    [ "$prompt" ] || return 0
85
440 by Colin Watson
* Write the please-remove-CD message to /dev/console so that it works even
86
    stty sane < /dev/console
87
344.1.1 by Tollef Fog Heen
Merge from Debian
88
    # XXX - i18n
440 by Colin Watson
* Write the please-remove-CD message to /dev/console so that it works even
89
    echo "Please remove the disc and close the tray (if any) then press ENTER: " > /dev/console
344.1.1 by Tollef Fog Heen
Merge from Debian
90
    if [ -x /sbin/usplash_write ]; then
91
        /sbin/usplash_write "TIMEOUT 86400"
92
        /sbin/usplash_write "TEXT-URGENT Please remove the disc, close the tray (if any)"
93
        /sbin/usplash_write "TEXT-URGENT and press ENTER to continue"
94
    fi
95
96
    read x < /dev/console
97
}
98
99
case "$1" in
100
    start|restart|reload|force-reload|status)
101
        [ "$VERBOSE" != no ] && log_end_msg 0
102
        ;;
103
    stop)
104
        log_begin_msg "${NAME} is resyncing snapshots and caching reboot files..."
105
        do_stop
106
        case "$?" in
107
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
108
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
109
        esac
110
        ;;
111
    *)
112
        log_success_msg "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
113
        exit 3
114
        ;;
115
esac