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