Fixing cache_path: cache non-executable libs dependencies as well.
[live-boot-grml.git] / debian / live-boot.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                         for lib in $(ldd "${path}" | awk '{ print $3 }')
53                         do
54                                 cache_path "${lib}"
55                         done
56                 fi
57
58                 cat "${path}" >/dev/null 2>&1
59         fi
60 }
61
62 get_boot_device()
63 {
64         # search in /proc/mounts for the device that is mounted at /live/image
65         while read DEVICE MOUNT REST
66         do
67                 if [ "${MOUNT}" = "/live/image" ]
68                 then
69                         echo "${DEVICE}"
70                         exit 0
71                 fi
72         done < /proc/mounts
73 }
74
75 device_is_USB_flash_drive()
76 {
77         # remove leading "/dev/" and all trailing numbers from input
78         DEVICE=$(expr substr ${1} 6 3)
79
80         # check that device starts with "sd"
81         [ "$(expr substr ${DEVICE} 1 2)" != "sd" ] && return 1
82
83         # check that the device is an USB device
84         if readlink /sys/block/${DEVICE} | grep -q usb
85         then
86                 return 0
87         fi
88
89         return 1
90 }
91
92 do_stop ()
93 {
94         if ! grep -qs nopersistent /proc/cmdline && grep -qs persistent /proc/cmdline
95         then
96                 # ROOTSNAP and HOMESNAP are defined in ${SNAPSHOT_CONF} file
97                 if [ ! -z "${ROOTSNAP}" ]
98                 then
99                         ${DO_SNAPSHOT} --resync-string="${ROOTSNAP}"
100                 fi
101
102                 if [ ! -z "${HOMESNAP}" ]
103                 then
104                         ${DO_SNAPSHOT} --resync-string="${HOMESNAP}"
105                 fi
106         fi
107
108         # check for netboot
109         if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline
110         then
111                 return 0
112         fi
113
114         # check for toram
115         if grep -qs toram /proc/cmdline
116         then
117                 return 0
118         fi
119
120         # Don't prompt to eject the SD card on Babbage board, where we reuse it
121         # as a quasi-boot-floppy. Technically this uses a bit of ubiquity
122         # (archdetect), but since this is mostly only relevant for
123         # installations, who cares ...
124         if type archdetect >/dev/null 2>&1
125         then
126                 subarch="$(archdetect)"
127
128                 case $subarch in
129                         arm*/imx51)
130                                 return 0
131                                 ;;
132                 esac
133         fi
134
135         prompt=1
136         if [ "${NOPROMPT}" = "Yes" ]
137         then
138                 prompt=
139         fi
140
141         for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty) /bin/plymouth
142         do
143                 cache_path "${path}"
144         done
145
146         for x in $(cat /proc/cmdline)
147         do
148                 case ${x} in
149                         quickreboot)
150                                 QUICKREBOOT="Yes"
151                                 ;;
152                 esac
153         done
154
155         mount -o remount,ro /live/cow
156
157         if [ -z ${QUICKREBOOT} ]
158         then
159
160                 # Exit if the system was booted from an ISO image rather than a physical CD
161                 grep -qs find_iso= /proc/cmdline && return 0
162                 # TODO: i18n
163                 BOOT_DEVICE="$(get_boot_device)"
164
165                 if device_is_USB_flash_drive ${BOOT_DEVICE}
166                 then
167                         # do NOT eject USB flash drives!
168                         # otherwise rebooting with most USB flash drives
169                         # failes because they actually remember the
170                         # "ejected" state even after reboot
171                         MESSAGE="Please remove the USB flash drive"
172
173                         if [ "${NOPROMPT}" = "usb" ]
174                         then
175                                 prompt=
176                         fi
177
178                 else
179                         # ejecting is a very good idea here
180                         MESSAGE="Please remove the disc, close the tray (if any)"
181
182                         if [ -x /usr/bin/eject ]
183                         then
184                                 eject -p -m /live/image >/dev/null 2>&1
185                         fi
186
187                         if [ ${NOPROMPT} = "cd" ]
188                         then
189                                 prompt=
190                         fi
191
192                 fi
193
194                 [ "$prompt" ] || return 0
195
196                 if [ -x /bin/plymouth ] && plymouth --ping
197                 then
198                         plymouth message --text="${MESSAGE} and press ENTER to continue:"
199                         plymouth watch-keystroke > /dev/null
200                 else
201                         stty sane < /dev/console
202
203                         printf "\n\n${MESSAGE} and press ENTER to continue:" > /dev/console
204
205                         read x < /dev/console
206                 fi
207         fi
208 }
209
210 case "${1}" in
211         start|restart|reload|force-reload|status)
212                 [ "${VERBOSE}" != no ] && log_end_msg 0
213                 ;;
214
215         stop)
216                 log_begin_msg "${NAME} is resyncing snapshots and caching reboot files..."
217                 do_stop
218
219                 case "${?}" in
220                         0|1)
221                                 [ "${VERBOSE}" != no ] && log_end_msg 0
222                                 ;;
223
224                         2)
225                                 [ "${VERBOSE}" != no ] && log_end_msg 1
226                                 ;;
227                 esac
228                 ;;
229
230         *)
231                 log_success_msg "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
232                 exit 3
233                 ;;
234 esac