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