Releasing debian version 3.0.23-1+deb7u1.
[live-config.git] / bin / boot-init.sh
1 #!/bin/sh
2
3 ## live-config(7) - System Configuration Scripts
4 ## Copyright (C) 2006-2012 Daniel Baumann <daniel@debian.org>
5 ##
6 ## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
7 ## This is free software, and you are welcome to redistribute it
8 ## under certain conditions; see COPYING for details.
9
10
11 set -e
12
13 # Exit if system is not a live system
14 if ! grep -qs "boot=live" /proc/cmdline || \
15 # Exit if system is netboot
16 grep -qs "netboot" /proc/cmdline || \
17 grep -qs "root=/dev/nfs" /proc/cmdline || \
18 grep -qs "root=/dev/cifs" /proc/cmdline || \
19 # Exit if system is toram
20 grep -qs "toram" /proc/cmdline
21 then
22 exit 0
23 fi
24
25
26
27 # Try to cache everything we're likely to need after ejecting. This
28 # is fragile and simple-minded, but our options are limited.
29 cache_path()
30 {
31 path="${1}"
32
33 if [ -d "${path}" ]
34 then
35 find "${path}" -type f | xargs cat > /dev/null 2>&1
36 elif [ -f "${path}" ]
37 then
38 if file -L "${path}" | grep -q 'dynamically linked'
39 then
40 # ldd output can be of three forms:
41 # 1. linux-vdso.so.1 => (0x00007fffe3fb4000)
42 # This is a virtual, kernel shared library and we want to skip it
43 # 2. libc.so.6 => /lib/libc.so.6 (0x00007f5e9dc0c000)
44 # We want to cache the third word.
45 # 3. /lib64/ld-linux-x86-64.so.2 (0x00007f5e9df8b000)
46 # We want to cache the first word.
47 ldd "${path}" | while read line
48 do
49 if echo "$line" | grep -qs ' => '
50 then
51 continue
52 elif echo "$line" | grep -qs ' => '
53 then
54 lib=$(echo "${line}" | awk '{ print $3 }')
55 else
56 lib=$(echo "${line}" | awk '{ print $1 }')
57 fi
58 cache_path "${lib}"
59 done
60 fi
61
62 cat "${path}" >/dev/null 2>&1
63 fi
64 }
65
66 get_boot_device()
67 {
68 # search in /proc/mounts for the device that is mounted at /lib/live/mount/medium
69 while read DEVICE MOUNT REST
70 do
71 if [ "${MOUNT}" = "/lib/live/mount/medium" ]
72 then
73 echo "${DEVICE}"
74 exit 0
75 fi
76 done < /proc/mounts
77 }
78
79 device_is_USB_flash_drive()
80 {
81 # remove leading "/dev/" and all trailing numbers from input
82 DEVICE=$(expr substr ${1} 6 3)
83
84 # check that device starts with "sd"
85 [ "$(expr substr ${DEVICE} 1 2)" != "sd" ] && return 1
86
87 # check that the device is an USB device
88 if readlink /sys/block/${DEVICE} | grep -q usb
89 then
90 return 0
91 fi
92
93 return 1
94 }
95
96 Eject ()
97 {
98 # TODO: i18n
99 BOOT_DEVICE="$(get_boot_device)"
100
101 if device_is_USB_flash_drive ${BOOT_DEVICE}
102 then
103 # do NOT eject USB flash drives!
104 # otherwise rebooting with most USB flash drives
105 # failes because they actually remember the
106 # "ejected" state even after reboot
107 MESSAGE="Please remove the USB flash drive"
108
109 if [ "${NOPROMPT}" = "usb" ]
110 then
111 prompt=
112 fi
113
114 else
115 # ejecting is a very good idea here
116 MESSAGE="Please remove the disc, close the tray (if any)"
117
118 if [ -x /usr/bin/eject ]
119 then
120 eject -p -m /lib/live/mount/medium >/dev/null 2>&1
121 fi
122
123 if [ "${NOPROMPT}" = "cd" ]
124 then
125 prompt=
126 fi
127 fi
128
129 [ "$prompt" ] || return 0
130
131 if [ -x /bin/plymouth ] && plymouth --ping
132 then
133 plymouth message --text="${MESSAGE} and press ENTER to continue:"
134 plymouth watch-keystroke > /dev/null
135 else
136 stty sane < /dev/console
137
138 printf "\n\n${MESSAGE} and press ENTER to continue:" > /dev/console
139
140 read x < /dev/console
141 fi
142 }
143
144 echo "live-boot: caching reboot files..."
145
146 prompt=1
147 if [ "${NOPROMPT}" = "Yes" ]
148 then
149 prompt=
150 fi
151
152 for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty) /bin/plymouth
153 do
154 cache_path "${path}"
155 done
156
157 mount -o remount,ro /lib/live/mount/overlay > /dev/null 2>&1
158
159 # Remounting any persistence devices read-only
160 for _MOUNT in $(awk '/\/lib\/live\/mount\/persistence/ { print $2 }' /proc/mounts)
161 do
162 mount -o remount,ro ${_MOUNT} > /dev/null 2>&1
163 done
164
165 # Flush filesystem buffers
166 sync
167
168 # Check if we need to eject the drive
169 if grep -qs "cdrom-detect/eject=false" /proc/cmdline || \
170 grep -qs "noeject" /proc/cmdline || \
171 grep -qs "find_iso" /proc/cmdline
172 then
173 return
174 else
175 Eject
176 fi