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