10_support_ethdevice.dpatch: support setting NIC name via ethdevice
[live-initramfs-grml.git] / debian / live-initramfs.init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:             live-initramfs
5 # Required-Start:       $syslog
6 # Required-Stop:        $syslog
7 # Should-Start:         $local_fs
8 # Should-Stop:          $local_fs
9 # Default-Start:        1 2 3 4 5
10 # Default-Stop:         0 6
11 # Short-Description:    live-initramfs init script
12 # Description:          Resyncs snapshots, evantually caches files in order to
13 #                       let remove the media.
14 ### END INIT INFO
15
16 # Authors: Tollef Fog Heen <tfheen@canonical.com>
17 #          Marco Amadori <marco.amadori@gmail.com>
18
19 PATH=/usr/sbin:/usr/bin:/sbin:/bin
20 NAME=live-initramfs
21 SCRIPTNAME=/etc/init.d/${NAME}
22 DO_SNAPSHOT=/sbin/live-snapshot
23
24 # Exit if system was not booted by live-initramfs
25 grep -qs boot=live /proc/cmdline || exit 0
26
27 # Exit if the system was booted from an ISO image rather than a physical CD
28 grep -qs find_iso= /proc/cmdline && exit 0
29
30 # Read configuration variable file if it is present
31 [ -r /etc/live.conf ] && . /etc/live.conf
32
33 # Load the VERBOSE setting and other rcS variables
34 [ -f /etc/default/rcS ] && . /etc/default/rcS
35
36 # Define LSB log_* functions.
37 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
38 . /lib/lsb/init-functions
39
40 # Try to cache everything we're likely to need after ejecting.  This
41 # is fragile and simple-minded, but our options are limited.
42 cache_path()
43 {
44         path="${1}"
45
46         if [ -d "${path}" ]
47         then
48                 find "${path}" -type f | xargs cat > /dev/null 2>&1
49         elif [ -f "${path}" ]
50         then
51                 if [ -x "${path}" ]
52                 then
53                         if file "${path}" | grep -q 'dynamically linked'
54                         then
55                                 for lib in $(ldd "${path}" | awk '{ print $3 }')
56                                 do
57                                         cache_path "${lib}"
58                                 done
59                         fi
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 /live/image
69         while read DEVICE MOUNT REST
70         do
71                 if [ "${MOUNT}" = "/live/image" ]
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}/device | grep -q usb
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                 if [ ! -z "${ROOTSNAP}" ]
101                 then
102                         ${DO_SNAPSHOT} --resync-string="${ROOTSNAP}"
103                 fi
104
105                 if [ ! -z "${HOMESNAP}" ]
106                 then
107                         ${DO_SNAPSHOT} --resync-string="${HOMESNAP}"
108                 fi
109         else
110                 return 0
111         fi
112
113         # check for netboot
114         if [ ! -z "${NETBOOT}" ] || grep -qs netboot /proc/cmdline || grep -qsi root=/dev/nfs /proc/cmdline  || grep -qsi root=/dev/cifs /proc/cmdline
115         then
116                 return 0
117         fi
118
119         prompt=1
120         if grep -qs noprompt /proc/cmdline
121         then
122                 prompt=
123         fi
124
125         for path in $(which halt) $(which reboot) /etc/rc?.d /etc/default $(which stty)
126         do
127                 cache_path "${path}"
128         done
129
130         for x in $(cat /proc/cmdline)
131         do
132                 case ${x} in
133                         quickreboot)
134                                 QUICKREBOOT="Yes"
135                                 ;;
136                 esac
137         done
138
139         if [ -z ${QUICKREBOOT} ]
140         then
141                 # TODO: i18n
142                 BOOT_DEVICE="$(get_boot_device)"
143
144                 if device_is_USB_flash_drive ${BOOT_DEVICE}
145                 then
146                         # do NOT eject USB flash drives!
147                         # otherwise rebooting with most USB flash drives
148                         # failes because they actually remember the
149                         # "ejected" state even after reboot
150                         MESSAGE="Please remove the USB flash drive"
151                 else
152                         # ejecting is a very good idea here
153                         MESSAGE="Please remove the disc, close the the tray (if any)"
154
155                         if [ -x /usr/bin/eject ]
156                         then
157                                 eject -p -m /live/image >/dev/null 2>&1
158                         fi
159
160                         [ "$prompt" ] || return 0
161                 fi
162
163                 stty sane < /dev/console
164
165                 echo -n -e "\n\n${MESSAGE} and press ENTER to continue:" > /dev/console
166
167                 if [ -x /sbin/usplash_write ]
168                 then
169                         /sbin/usplash_write "TIMEOUT 86400"
170                         /sbin/usplash_write "TEXT-URGENT ${MESSAGE}"
171                         /sbin/usplash_write "TEXT-URGENT and press ENTER to continue"
172                 fi
173
174                 read x < /dev/console
175         fi
176 }
177
178 case "${1}" in
179         start|restart|reload|force-reload|status)
180                 [ "${VERBOSE}" != no ] && log_end_msg 0
181                 ;;
182
183         stop)
184                 log_begin_msg "${NAME} is resyncing snapshots and caching reboot files..."
185                 do_stop
186
187                 case "${?}" in
188                         0|1)
189                                 [ "${VERBOSE}" != no ] && log_end_msg 0
190                                 ;;
191
192                         2)
193                                 [ "${VERBOSE}" != no ] && log_end_msg 1
194                                 ;;
195                 esac
196                 ;;
197
198         *)
199                 log_success_msg "Usage: ${SCRIPTNAME} {start|stop|restart|force-reload}" >&2
200                 exit 3
201                 ;;
202 esac