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