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