source: pkg/main/initscripts-parsix-live/trunk/debian/live-reboot.init @ 2640

Revision 2640, 9.2 KB checked in by alanbach-guest, 5 years ago (diff)
  • Updated live-reboot
    • disabled kernel messages after CD eject message
    • do-stop anyway
    • script name is fll-reboot
Line 
1#!/bin/bash
2
3### BEGIN INIT INFO
4# Provides:          live-reboot
5# Required-Start:   
6# Required-Stop:     
7# Default-Start:     
8# Default-Stop:      0 6
9# Short-Description: shutdown and eject live media
10# Description:       This script provides the equivalent of umountfs,
11#                    umountroot and halt on a stadard debian system. It umounts
12#                    live mounts and ejects the live media at shutdown/reboot.
13### END INIT INFO
14
15###
16# F.U.L.L.S.T.O.R.Y init script
17#
18# Copyright: (C) 2007 F.U.L.L.S.T.O.R.Y Project
19# Copyright: (C) 2007 Kel Modderman <kel@otaku42.de>
20# Copyright: (C) 2007 Alan Baghumian <alan@technotux.org>
21# License:   GPLv2
22#
23# F.U.L.L.S.T.O.R.Y Project Homepage:
24# http://developer.berlios.de/projects/fullstory
25###
26
27PATH=/sbin:/usr/sbin:/bin:/usr/bin
28NAME="live-reboot"
29
30###
31# source distro-defaults, no-op unless in live mode
32###
33FLL_DISTRO_MODE="installed"
34
35if [ -s /etc/default/distro ]; then
36        . /etc/default/distro
37fi
38
39if [ "${FLL_DISTRO_MODE}" != "live" ]; then
40        exit 0
41fi
42
43###
44# VERBOSE setting and other rcS variables
45###
46. /lib/init/vars.sh
47
48###
49# source lsb functions
50###
51. /lib/lsb/init-functions
52
53NORMAL="[0;39m"
54RED="[1;31m"
55GREEN="[1;32m"
56YELLOW="[1;33m"
57BLUE="[1;34m"
58MAGENTA="[1;35m"
59CYAN="[1;36m"
60WHITE="[1;37m"
61
62###
63# define this for copy_exec
64###
65DESTDIR="/live-hack"
66
67if [ -f /proc/cmdline ]; then
68        for param in $(cat /proc/cmdline); do
69                case "${param}" in
70                        flldebug=*)
71                                [ "${param#flldebug=}" = "${NAME#fll-}" ] && set -x
72                                ;;
73                        noeject)
74                                NOEJECT="1"
75                                ;;
76                        noprompt)
77                                NOPROMPT="1"
78                                ;;
79                esac
80        done
81fi
82
83###############################################################################
84# Adapted from initscripts: /etc/init.d/umountfs (2.86.ds1-38)
85###############################################################################
86# Print in order of decreasing length
87#
88# Algorithm: Find and print longest argument, then call self
89# to print remaining arguments in order of decreasing length
90#
91# This function runs at one tenth the speed of the sort program
92# but we use the function because we don't want to rely on any
93# programs in /usr/.
94#
95# N.B.: Arguments must not be null and must not contain whitespace
96#
97pioodl() {
98        [ "$1" ] || return 0
99       
100        ARGNUM=1
101        ARGNUM_LONGEST=0
102        ARGLENGTH_LONGEST=0
103        for ARG in "$@"
104        do
105                ARGLENGTH="${#ARG}"
106                if [ "$ARGLENGTH" -gt "$ARGLENGTH_LONGEST" ]
107                then
108                        ARGLENGTH_LONGEST="$ARGLENGTH"
109                        ARGNUM_LONGEST="$ARGNUM"
110                fi
111                ARGNUM=$(($ARGNUM + 1))
112        done
113       
114        # The method of passing prevargs assumes that args can be
115        # delimited with spaces
116        ARGNUM=1
117        PREVARGS=""
118        while [ "$ARGNUM" -lt "$ARGNUM_LONGEST" ]
119        do
120                PREVARGS="$PREVARGS $1"
121                shift
122                ARGNUM=$(($ARGNUM + 1))
123        done
124        echo "$1"
125        shift
126       
127        pioodl $PREVARGS "$@"
128}
129
130do_umount() {
131        exec 9<&0 < /proc/mounts
132
133        REG_MTPTS=""
134        TMPFS_MTPTS=""
135        while read DEV MTPT FSTYPE OPTS REST
136        do
137                case "$MTPT" in
138                        #
139                        # live hack
140                        #
141                        /fll/*)
142                                case "$FSTYPE" in
143                                        iso9660)
144                                                if [ "$MTPT" = /fll/fromiso ]; then
145                                                        LIVE_MTPTS="$LIVE_MTPTS $MTPT"
146                                                else
147                                                        LIVE_CDROM="$DEV"
148                                                fi
149                                                ;;
150                                        *)
151                                                LIVE_MTPTS="$LIVE_MTPTS $MTPT"
152                                                ;;
153                                esac
154                                continue
155                                ;;
156                        #
157                        # live hack
158                        #
159                        /|/proc|/dev|/.dev|/dev/pts|/dev/shm|/dev/.static/dev|/proc/*|/sys|/lib/init/rw)
160                                continue
161                                ;;
162                        /var/run)
163                                if [ yes = "$RAMRUN" ] ; then
164                                        continue
165                                fi
166                                ;;
167                        /var/lock)
168                                if [ yes = "$RAMLOCK" ] ; then
169                                        continue
170                                fi
171                                ;;
172                esac
173                case "$FSTYPE" in
174                        proc|procfs|linprocfs|devfs|sysfs|usbfs|usbdevfs|devpts)
175                                continue
176                                ;;
177                        #
178                        # live hack
179                        #
180                        aufs|unionfs)
181                                UNION_MTPTS="$UNION_MTPTS $MTPT"
182                                ;;
183                        squashfs)
184                                SQUSH_MTPTS="$SQUSH_MTPTS $MTPT"
185                                ;;
186                        #
187                        # live hack
188                        #
189                        tmpfs)
190                                TMPFS_MTPTS="$TMPFS_MTPTS $MTPT"
191                                ;;
192                        *)
193                                REG_MTPTS="$REG_MTPTS $MTPT"
194                                ;;
195                esac
196        done
197
198        exec 0<&9 9<&-
199       
200        #
201        # Make sure tmpfs file systems are umounted before turning off
202        # swap, to avoid running out of memory if the tmpfs filesystems
203        # use a lot of space.
204        #
205        if [ "$TMPFS_MTPTS" ]
206        then
207                TMPFS_MTPTS="$(pioodl $TMPFS_MTPTS)"
208                if [ "$VERBOSE" = no ]
209                then
210                        log_action_begin_msg "Unmounting temporary filesystems"
211                        umount $TMPFS_MTPTS
212                        log_action_end_msg $?
213                else
214                        log_daemon_msg "Will now unmount temporary filesystems"
215                        umount -v $TMPFS_MTPTS
216                        log_end_msg $?
217                fi
218        fi
219
220        #
221        # Deactivate swap
222        #
223        if [ "$VERBOSE" = no ]
224        then
225                log_action_begin_msg "Deactivating swap"
226                swapoff -a >/dev/null
227                log_action_end_msg $?
228        else
229                log_daemon_msg "Will now deactivate swap"
230                swapoff -a -v
231                log_end_msg $?
232        fi
233
234        #
235        # Unmount local filesystems
236        #
237        if [ "$REG_MTPTS" ]
238        then
239                REG_MTPTS="$(pioodl $REG_MTPTS)"
240                if [ "$VERBOSE" = no ]
241                then
242                        log_action_begin_msg "Unmounting local filesystems"
243                        umount -n -f -r -d $REG_MTPTS
244                        log_action_end_msg $?
245                else
246                        log_daemon_msg "Will now unmount local filesystems"
247                        umount -n -f -v -r -d $REG_MTPTS
248                        log_end_msg $?
249                fi
250        fi
251
252        #
253        # live hack: any command after this point must be prefixed with ${BINDIR}
254        #
255        if [ "$UNION_MTPTS" ]
256        then
257                UNION_MTPTS="$(pioodl $UNION_MTPTS)"
258                if [ "$VERBOSE" = no ]
259                then
260                        log_action_begin_msg "Unmounting union filesystems"
261                        ${BINDIR}umount -l -n -f -d $UNION_MTPTS
262                        log_action_end_msg $?
263                else
264                        log_daemon_msg "Will now unmount union filesystems"
265                        ${BINDIR}umount -l -n -f -v -d $UNION_MTPTS
266                        log_end_msg $?
267                fi
268        fi
269
270        if [ "$SQSH_MTPTS" ]
271        then
272                SQSH_MTPTS="$(pioodl $SQSH_MTPTS)"
273                if [ "$VERBOSE" = no ]
274                then
275                        log_action_begin_msg "Unmounting squashfs filesystems"
276                        ${BINDIR}umount -l -n -f -d $SQSH_MTPTS
277                        log_action_end_msg $?
278                else
279                        log_daemon_msg "Will now unmount squashfs filesystems"
280                        ${BINDIR}umount -l -n -f -v -d $SQSH_MTPTS
281                        log_end_msg $?
282                fi
283        fi
284
285        if [ "$LIVE_MTPTS" ]
286        then
287                LIVE_MTPTS="$(pioodl $LIVE_MTPTS)"
288                if [ "$VERBOSE" = no ]
289                then
290                        log_action_begin_msg "Unmounting live filesystems"
291                        ${BINDIR}umount -l -n -f -d $LIVE_MTPTS
292                        log_action_end_msg $?
293                else
294                        log_daemon_msg "Will now unmount live filesystems"
295                        ${BINDIR}umount -l -n -f -v -d $LIVE_MTPTS
296                        log_end_msg $?
297                fi
298        fi
299
300        if [ "$LIVE_CDROM" ]; then
301                export LIVE_CDROM
302        fi
303}
304###############################################################################
305# Adapted from initramfs-tools hook-functions (0.87b)
306###############################################################################
307# $1 is source
308# $2 is relative destination
309copy_exec() {
310        local verbose="${VERBOSE}"
311        local final_destination=${DESTDIR}/${2}/`basename ${1}`
312       
313        if [ -L "$final_destination" ]; then
314                if ! [ `readlink ${final_destination}` = "${1}" ]; then
315                        return
316                fi
317        else
318                cp ${1} ${DESTDIR}/${2}
319                if [ -n "${verbose}" ] && [ "${verbose}" = "y" ]; then
320                        echo "Adding binary ${1}"
321                fi
322        fi
323
324        # Copy the dependant libraries
325        for x in $(ldd ${1} 2>/dev/null | sed -e '
326            /\//!d;
327            /linux-gate/d;
328            /=>/ {s/.*=>[[:blank:]]*\([^[:blank:]]*\).*/\1/};
329            s/[[:blank:]]*\([^[:blank:]]*\) (.*)/\1/' 2>/dev/null); do
330
331                # Try to use non-optimised libraries where possible.
332                # We assume that all HWCAP libraries will be in tls.
333                nonoptlib=$(echo ${x} | sed -e 's#/lib/tls.*/\(lib.*\)#/lib/\1#')
334
335                if [ -e ${nonoptlib} ]; then
336                        x=${nonoptlib}
337                fi
338
339                libname=$(basename ${x})
340                dirname=$(dirname ${x})
341
342                mkdir -p ${DESTDIR}/${dirname}
343                if [ ! -e ${DESTDIR}/${dirname}/${libname} ]; then
344                        cp ${x} ${DESTDIR}/${dirname}
345                        if [ -n "${verbose}" ] && [ "${verbose}" = "y" ]; then
346                                echo "Adding library ${x}"
347                        fi
348                fi
349        done
350}
351###############################################################################
352# End adapted functions
353###############################################################################
354
355live_hack() {
356        mkdir -p ${DESTDIR}/bin
357        copy_exec /bin/umount           /bin
358        copy_exec /sbin/halt            /bin
359        copy_exec /sbin/reboot          /bin
360        copy_exec /usr/bin/eject        /bin
361       
362        LD_LIBRARY_PATH="${DESTDIR}/lib"
363        if [ -e "${DESTDIR}/lib64" ]; then
364                LD_LIBRARY_PATH="${DESTDIR}/lib64:${LD_LIBRARY_PATH}"
365        fi
366        export LD_LIBRARY_PATH
367
368        LD_LINUX="${DESTDIR}/lib/ld-linux.so.2"
369        if [ -e "${DESTDIR}/lib64/ld-linux-x86-64.so.2" ]; then
370                LD_LINUX="${DESTDIR}/lib64/ld-linux-x86-64.so.2"
371        fi
372        export LD_LINUX
373
374        PATH="${DESTDIR}/bin:${PATH}"
375        export PATH
376
377        BINDIR="${LD_LINUX} ${DESTDIR}/bin/"
378        export BINDIR
379}
380
381pop_live_cd()
382{
383        if [ -z "${LIVE_CDROM}" ] || [ "${NOEJECT}" ]; then
384                return 0
385        fi
386
387        #
388        # disable kernel messages while ejecting cdrom (libata noise)
389        #
390        echo "0" > /proc/sys/kernel/printk
391       
392        if [ -b "${LIVE_CDROM}" ]; then
393                ${BINDIR}eject -m -p ${LIVE_CDROM}
394        fi
395
396        if [ -z "${NOPROMPT}" ]; then
397                echo "${CYAN}Please remove CD, close cdrom drive and hit return.${NORMAL}" > /dev/console
398                read
399        fi
400}
401
402do_stop() {
403        local command message options
404
405        # prepare binaries for post-umount of live media
406        live_hack
407        # umount the live media and other mount points
408        do_umount
409
410        case "${0}" in
411                *halt)
412                        message="${GREEN}${FLL_DISTRO_SNAME} halted.${NORMAL}"
413                        command="halt"
414                        options="-h -n -p -i -f"
415                        ;;
416                *reboot)
417                        message="${GREEN}Preparing for reboot...${NORMAL}"
418                        command="reboot"
419                        options="-n -i -f"
420                        ;;
421                *)
422                        echo "${0}: call this script as \"halt\" or \"reboot\" please!"
423                        exit 1
424                        ;;
425        esac
426
427        echo "${message}" > /dev/console
428
429        pop_live_cd
430       
431        exec ${BINDIR}${command} ${options} > /dev/console 2>&1 < /dev/console
432}
433
434case "${1}" in
435        *)
436                # live-hack, umount and eject
437                do_stop
438                ;;
439esac
Note: See TracBrowser for help on using the repository browser.