Fixed some snapshot related debug messages.
[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 "${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            readlink /sys/block/${DEVICE}/device | grep -q usb # linux < 2.6.29
89         then
90                 return 0
91         fi
92
93         return 1
94 }
95
96 do_stop ()
97 {
98         if ! grep -qs nopersistent /proc/cmdline && grep -qs persistent /proc/cmdline
99         then
100                 # ROOTSNAP and HOMESNAP are defined in ${SNAPSHOT_CONF} file
101                 if [ ! -z "${ROOTSNAP}" ]
102                 then
103                         ${DO_SNAPSHOT} --resync-string="${ROOTSNAP}"
104                 fi
105
106                 if [ ! -z "${HOMESNAP}" ]
107                 then
108                         ${DO_SNAPSHOT} --resync-string="${HOMESNAP}"
109                 fi
110         fi
111
112         # check for netboot
113         if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline
114         then
115                 return 0
116         fi
117
118         # Don't prompt to eject the SD card on Babbage board, where we reuse it
119         # as a quasi-boot-floppy. Technically this uses a bit of ubiquity
120         # (archdetect), but since this is mostly only relevant for
121         # installations, who cares ...
122         if type archdetect >/dev/null 2>&1
123         then
124                 subarch="$(archdetect)"
125
126                 case $subarch in
127                         arm*/imx51)
128                                 return 0
129                                 ;;
130                 esac
131         fi
132
133         prompt=1
134         if grep -qs noprompt /proc/cmdline
135         then
136                 prompt=
137         fi
138
139         for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty) /bin/plymouth
140         do
141                 cache_path "${path}"
142         done
143
144         for x in $(cat /proc/cmdline)
145         do
146                 case ${x} in
147                         quickreboot)
148                                 QUICKREBOOT="Yes"
149                                 ;;
150                 esac
151         done
152
153         if [ -z ${QUICKREBOOT} ]
154         then
155
156                 # Exit if the system was booted from an ISO image rather than a physical CD
157                 grep -qs find_iso= /proc/cmdline && return 0
158                 # TODO: i18n
159                 BOOT_DEVICE="$(get_boot_device)"
160
161                 if device_is_USB_flash_drive ${BOOT_DEVICE}
162                 then
163                         # do NOT eject USB flash drives!
164                         # otherwise rebooting with most USB flash drives
165                         # failes because they actually remember the
166                         # "ejected" state even after reboot
167                         MESSAGE="Please remove the USB flash drive"
168                 else
169                         # ejecting is a very good idea here
170                         MESSAGE="Please remove the disc, close the the tray (if any)"
171
172                         if [ -x /usr/bin/eject ]
173                         then
174                                 eject -p -m /live/image >/dev/null 2>&1
175                         fi
176
177                 fi
178
179                 [ "$prompt" ] || return 0
180
181                 if [ -x /bin/plymouth ] && plymouth --ping
182                 then
183                         plymouth message --text="${MESSAGE} and press ENTER to continue:"
184                         plymouth watch-keystroke > /dev/null
185                 else
186                         stty sane < /dev/console
187
188                         printf "\n\n${MESSAGE} and press ENTER to continue:" > /dev/console
189
190                         read x < /dev/console
191                 fi
192         fi
193 }
194
195 case "${1}" in
196         restart|reload|force-reload|status)
197                 [ "${VERBOSE}" != no ] && log_end_msg 0
198                 ;;
199         start)
200                 log_begin_msg "${NAME} is configuring sendsigs..."
201                 if [ -f /live/root.pid ] ; then
202                         cat /live/root.pid >> /var/run/sendsigs.omit
203                 fi
204                 log_end_msg 0
205                 ;;
206
207         stop)
208                 log_begin_msg "${NAME} is resyncing snapshots and caching reboot files..."
209                 do_stop
210
211                 case "${?}" in
212                         0|1)
213                                 [ "${VERBOSE}" != no ] && log_end_msg 0
214                                 ;;
215
216                         2)
217                                 [ "${VERBOSE}" != no ] && log_end_msg 1
218                                 ;;
219                 esac
220
221                 mount -o remount,ro /live/cow
222                 ;;
223
224         *)
225                 log_success_msg "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
226                 exit 3
227                 ;;
228 esac