update changelog
[live-boot-grml.git] / debian / live-boot-grml.init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:             live-boot
5 # Required-Start:       $syslog bootmisc
6 # Required-Stop:
7 # Should-Start:         $local_fs
8 # Should-Stop:          halt reboot
9 # X-Stop-After:         umountroot
10 # Default-Start:        S
11 # Default-Stop:         0 6
12 # Short-Description:    live-boot 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-boot
22 SCRIPTNAME=/etc/init.d/${NAME}
23 DO_SNAPSHOT=/sbin/live-snapshot
24 SNAPSHOT_CONF="/etc/live/boot.d/snapshot.conf"
25
26 # Exit if system was not booted by live-boot
27 grep -qs boot=live /proc/cmdline || exit 0
28
29 # Read snapshot configuration variables
30 [ -r ${SNAPSHOT_CONF} ] && . ${SNAPSHOT_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
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 {
43         path="${1}"
44
45         if [ -d "${path}" ]
46         then
47                 find "${path}" -type f | xargs cat > /dev/null 2>&1
48         elif [ -f "${path}" ]
49         then
50                 if file -L "${path}" | grep -q 'dynamically linked'
51                 then
52                         # ldd output can be of three forms:
53                         # 1. linux-vdso.so.1 =>  (0x00007fffe3fb4000)
54                         #    This is a virtual, kernel shared library and we want to skip it
55                         # 2. libc.so.6 => /lib/libc.so.6 (0x00007f5e9dc0c000)
56                         #    We want to cache the third word.
57                         # 3. /lib64/ld-linux-x86-64.so.2 (0x00007f5e9df8b000)
58                         #    We want to cache the first word.
59                         ldd "${path}" | while read line
60                         do
61                                 if echo "$line" | grep -qs ' =>  '
62                                 then
63                                         continue
64                                 elif echo "$line" | grep -qs ' => '
65                                 then
66                                         lib=$(echo "${line}" | awk '{ print $3 }')
67                                 else
68                                         lib=$(echo "${line}" | awk '{ print $1 }')
69                                 fi
70                                 cache_path "${lib}"
71                         done
72                 fi
73
74                 cat "${path}" >/dev/null 2>&1
75         fi
76 }
77
78 get_boot_device()
79 {
80         # search in /proc/mounts for the device that is mounted at /live/image
81         while read DEVICE MOUNT REST
82         do
83                 if [ "${MOUNT}" = "/live/image" ]
84                 then
85                         echo "${DEVICE}"
86                         exit 0
87                 fi
88         done < /proc/mounts
89 }
90
91 device_is_USB_flash_drive()
92 {
93         # remove leading "/dev/" and all trailing numbers from input
94         DEVICE=$(expr substr ${1} 6 3)
95
96         # check that device starts with "sd"
97         [ "$(expr substr ${DEVICE} 1 2)" != "sd" ] && return 1
98
99         # check that the device is an USB device
100         if readlink /sys/block/${DEVICE} | grep -q usb
101         then
102                 return 0
103         fi
104
105         return 1
106 }
107
108 do_stop ()
109 {
110         if ! grep -qs nopersistent /proc/cmdline && grep -qs persistent /proc/cmdline
111         then
112                 # ROOTSNAP and HOMESNAP are defined in ${SNAPSHOT_CONF} file
113                 if [ ! -z "${ROOTSNAP}" ]
114                 then
115                         ${DO_SNAPSHOT} --resync-string="${ROOTSNAP}"
116                 fi
117
118                 if [ ! -z "${HOMESNAP}" ]
119                 then
120                         ${DO_SNAPSHOT} --resync-string="${HOMESNAP}"
121                 fi
122         fi
123
124         # check for netboot
125         if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline
126         then
127                 return 0
128         fi
129
130         # check for toram
131         if grep -qs toram /proc/cmdline
132         then
133                 return 0
134         fi
135
136         # Don't prompt to eject the SD card on Babbage board, where we reuse it
137         # as a quasi-boot-floppy. Technically this uses a bit of ubiquity
138         # (archdetect), but since this is mostly only relevant for
139         # installations, who cares ...
140         if type archdetect >/dev/null 2>&1
141         then
142                 subarch="$(archdetect)"
143
144                 case $subarch in
145                         arm*/imx51)
146                                 return 0
147                                 ;;
148                 esac
149         fi
150
151         prompt=1
152         if [ "${NOPROMPT}" = "Yes" ]
153         then
154                 prompt=
155         fi
156
157         for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty) /bin/plymouth
158         do
159                 cache_path "${path}"
160         done
161
162         for x in $(cat /proc/cmdline)
163         do
164                 case ${x} in
165                         quickreboot)
166                                 QUICKREBOOT="Yes"
167                                 ;;
168                 esac
169         done
170
171         mount -o remount,ro /live/cow
172
173         if [ -z ${QUICKREBOOT} ]
174         then
175
176                 # Exit if the system was booted from an ISO image rather than a physical CD
177                 grep -qs find_iso= /proc/cmdline && return 0
178                 # TODO: i18n
179                 BOOT_DEVICE="$(get_boot_device)"
180
181                 if device_is_USB_flash_drive ${BOOT_DEVICE}
182                 then
183                         # do NOT eject USB flash drives!
184                         # otherwise rebooting with most USB flash drives
185                         # failes because they actually remember the
186                         # "ejected" state even after reboot
187                         MESSAGE="Please remove the USB flash drive"
188
189                         if [ "${NOPROMPT}" = "usb" ]
190                         then
191                                 prompt=
192                         fi
193
194                 else
195                         # ejecting is a very good idea here
196                         MESSAGE="Please remove the disc, close the tray (if any)"
197
198                         if [ -x /usr/bin/eject ]
199                         then
200                                 eject -p -m /live/image >/dev/null 2>&1
201                         fi
202
203                         if [ "${NOPROMPT}" = "cd" ]
204                         then
205                                 prompt=
206                         fi
207
208                 fi
209
210                 [ "$prompt" ] || return 0
211
212                 if [ -x /bin/plymouth ] && plymouth --ping
213                 then
214                         plymouth message --text="${MESSAGE} and press ENTER to continue:"
215                         plymouth watch-keystroke > /dev/null
216                 else
217                         stty sane < /dev/console
218
219                         printf "\n\n${MESSAGE} and press ENTER to continue:" > /dev/console
220
221                         read x < /dev/console
222                 fi
223         fi
224 }
225
226 case "${1}" in
227         start|restart|reload|force-reload|status)
228                 [ "${VERBOSE}" != no ] && log_end_msg 0
229                 ;;
230
231         stop)
232                 log_begin_msg "${NAME} is resyncing snapshots and caching reboot files..."
233                 do_stop
234
235                 case "${?}" in
236                         0|1)
237                                 [ "${VERBOSE}" != no ] && log_end_msg 0
238                                 ;;
239
240                         2)
241                                 [ "${VERBOSE}" != no ] && log_end_msg 1
242                                 ;;
243                 esac
244                 ;;
245
246         *)
247                 log_success_msg "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
248                 exit 3
249                 ;;
250 esac