source: branches/3.3/mindi/rootfs/etc/init.d/rcS @ 3697

Last change on this file since 3697 was 3697, checked in by bruno, 15 months ago

Remove usage of groovy word to clarify what is done

  • Property svn:keywords set to Id
File size: 32.9 KB
Line 
1#!/bin/bash
2#
3# $Id: rcS 3697 2018-01-07 13:52:31Z bruno $
4#
5# init script launched during the restore process
6#------------------------------------------------------------
7
8CaughtSoftReset() {
9    trap SIGTERM
10
11    reboot
12    umount -a -d -r 2> /dev/null
13    sync
14    # If this is not sufficient, then continue killing what is runnig
15    kill -TERM 1 
16    sleep 1
17    kill -KILL 1 
18    sleep 1
19    # Finally force reboot at kernel level if everything else failed
20    echo s > /proc/sysrq-trigger
21    echo u > /proc/sysrq-trigger
22    echo b > /proc/sysrq-trigger
23}
24
25
26ConfigureLoggingDaemons() {
27    echo -en "Running klogd..."
28    klogd -c 2 > /dev/null 2> /dev/null
29    echo -en "Done.\nRunning syslogd..."
30    syslogd > /dev/null 2> /dev/null
31    echo "Done."
32    LogIt "klogd and syslogd have been started."
33}
34
35
36
37CopyBootDevEntry() {
38    local outfile devfile
39    devfile=/dev/boot_device
40    outfile=`cat /BOOTLOADER.DEVICE 2> /dev/null`
41    [ ! "$outfile" ] && return 0
42    echo -en "Copying boot device to $outfile..."
43    if [ -e "$outfile" ] ; then
44        echo "not needed."
45        return 0
46    fi
47    mkdir -p $outfile
48    rmdir $outfile
49    cp -pRdf $devfile $outfile
50    if [ "$?" -ne "0" ] ; then
51        echo "Failed."
52        return 1
53    else
54        echo "OK."
55        return 0
56    fi
57}
58
59
60GetShell() {
61    echo -en "Type 'exit' to reboot the machine\n"
62    umount -d /mnt/cdrom 2> /dev/null
63    mount / -o rw,remount > /dev/null 2> /dev/null
64    for i in `cat /proc/cmdline`; do
65        echo $i | grep -qi console= && console=`echo $i | cut -d= -f2`
66        echo $i | grep -qi serial= && serial=`echo $i | cut -d= -f2`
67    done
68    LogIt "Launching Shell"
69    if [[ $serial != "" && -e $serial ]]; then
70        setsid sh -c "exec sh <$serial >$serial 2>&1"
71    elif [[ $console != "" && -e /dev/$console ]]; then
72        setsid sh -c "exec sh </dev/$console >/dev/$console 2>&1"
73    else
74        setsid sh -c "exec sh </dev/tty1 >/dev/tty1 2>&1"
75    fi
76}
77
78
79Die() {
80    LogIt "Fatal error! $1" 1
81    GetShell
82}
83
84
85ExtractDevTarballs() {
86    cd /
87    if [ -x /usr/lib/systemd/systemd-udevd ] || [ -x /lib/systemd/systemd-udevd ]; then
88        # Just use vc dev files. The other are useless and create huge delays with LVM
89        tar -zxf /vc.tgz || LogIt "Error occurred while extracting /vc.tgz"
90    else
91        for fname in ataraid.tgz ida.tgz i2o.tgz rd.tgz raw.tgz cciss.tgz nst.tgz dm.tgz vc.tgz ; do
92            if [ ! -e "/$fname" ] ; then
93                LogIt "/$fname not found; cannot extract to /." 1
94            else
95                echo -en "\rExtracting /$fname...     "
96                tar -zxf /$fname || LogIt "Error occurred while extracting /$fname"
97            fi
98        done
99    fi
100    echo -en "\r"
101    LogIt "Extracted additional /dev entries OK.     " 1
102}
103
104
105
106LaunchTerminals() {
107    # Depending on busybox version, syntax of openvt is different
108    /bin/busybox --help 2>&1 | head -1 | grep -Eq "1\.7\.3|1\.2\."
109    if [ "$?" -eq "0" ] ; then
110        opt=""
111    else
112        # 1.18.3 requires -c
113        opt="-c"
114    fi
115    echo "Launching terminals on other ttys - Use ALT+F[2-6] to use them"
116    openvt $opt 2 /bin/sh
117    openvt $opt 3 /bin/sh
118    openvt $opt 4 /bin/sh
119    openvt $opt 5 /bin/sh
120    openvt $opt 6 /bin/sh
121    openvt $opt 7 /bin/sh /sbin/wait-for-petris
122    openvt $opt 8 /usr/bin/tail -f $LOGFILE
123    if [ -f /var/log/messages ]; then
124        openvt $opt 9 /usr/bin/tail -f /var/log/messages
125    fi
126    # May avoid shell error messages
127    chmod 666 /dev/tty* /dev/console
128    # By default first serial line is configured as tty
129    # Required to have a correct serial console support (MP on ia64 or VSP with iLO e.g.)
130    for i in `cat /proc/cmdline` ; do
131        echo $i | grep -qi serial= && serial=`echo $i | cut -d= -f2`
132    done
133    # Doing that is only valid when using a real serial line
134    if [[ $serial != "" && -e $serial ]]; then
135        LogIt "Redirecting serial $serial to /dev/tty" 1
136        ln -s -f $serial /dev/tty
137    fi
138}
139
140
141LoadKeymap() {
142    local fname
143    fname=`grep keymap-lives-here /tmp/mondorestore.cfg | cut -f2 -d' ' 2> /dev/null`
144    [ "$fname" = "" ] && return
145    if which loadkeys > /dev/null 2> /dev/null ; then
146        loadkeys $fname
147        LogIt "Using $fname keyboard map." 1
148    else
149        LogIt "Using default US keyboard map." 1
150    fi
151}
152
153
154UntarTapeStuff() {
155    local old_pwd res
156    old_pwd=`pwd`
157    cd $EXTRACTDIR
158    [ "$1" != "" ] && tapedev=$1
159    [ ! "$tapedev" ] && tapedev=`grep media-dev /tmp/mondorestore.cfg 2>/dev/null | tr -s ' ' ' ' | cut -d' ' -f2`
160    mt -f $tapedev rewind
161    if [ $? -ne 0 ]; then
162        LogIt "WARNING: $tapedev failed to rewind, either not a tape or still in OBDR mode." 1
163        LogIt "         Trying to find the corresponding SCSI generic device." 1
164        c=0
165        for d in `ls /sys/class/scsi_device/*`; do
166            devtype=`cat $d/device/type`
167            if [ "$devtype" -eq "1" ]; then
168                LogIt "NOTE: Device /dev/sg$c is a Sequential device." 1
169                if [ -x /usr/bin/sg_map ]; then
170                    dev=`/usr/bin/sg_map | grep -E "^/dev/sg$c" | awk '{print $2}'`
171                    LogIt "      Retry using device $dev" 1
172                fi
173            elif [ "$devtype" -eq "0" ]; then
174                # Skipping a HDD
175                /bin/true
176            elif [ "$devtype" -eq "5" ]; then
177                # Checking whether it's a CD or a tape in OBDR mode
178                grep -q -i -E 'dat|ultrium|tape' $d/device/model
179                if [ $? -eq 0 ]; then
180                    # Is probably a Tape drive left in OBDR.
181                    LogIt "Found that /dev/sg$c is a tape drive (`cat $d/device/model`)." 1
182                    grep -q -i -w 'hp' $d/device/vendor
183                    if [ $? -eq 0 ]; then
184                        LogIt "This is an HP tape drive, even better :-)." 1
185                    fi
186                    if [ -x /usr/bin/sg_reset ]; then
187                        OBDR_MODS="st mptsas mptspi aic7xxx aic94xx mpt2sas "
188                        if [ "`grep -i obdrmods /proc/cmdline`" ]; then
189                            OBDR_MODS="$OBDR_MODS `cat /proc/cmdline | sed 's~.*obdrmods=\"\(.*\)\".*~\1~'`"
190                        fi
191                        LogIt "Removing modules $OBDR_MODS." 1
192                        for m in $OBDR_MODS; do
193                            rmmod $i
194                        done
195                        LogIt "Trying to force device /dev/sg$c to be in sequential mode." 1
196                        /usr/bin/sg_reset -d /dev/sg$c
197                        LogIt "Inserting modules `rev $OBDR_MODS`." 1
198                        for m in `rev $OBDR_MODS`; do
199                            modprobe $i
200                        done
201                        LogIt "Retry again as the device has been set back into sequential mode." 1
202                        mt -f $tapedev fsf 2
203                        res=$?
204                        # exit the loop to continue and return if that previous mt failed.
205                        # If it succeeded we transparently continue !!
206                        break
207                    else
208                        LogIt "WARNING: Unable to reset /dev/sg$c as the sg_reset command is not availble." 1
209                    fi
210                fi
211            else 
212                LogIt "Device /dev/sg$c has an unknown type $devtype." 1
213            fi
214            c=`expr $c + 1`
215        done
216
217        if [ $res -ne 0 ]; then
218            cd $old_pwd
219            return $res
220        fi
221    fi
222
223    mt -f $tapedev fsf 2
224    # 32k is DEFAULT_INTERNAL_TAPE_BLOCK_SIZE
225    mt -f $tapedev setblk 32768
226    dd if=$tapedev bs=32k count=1024 | tar -zx
227    res=$?
228    if [ "$res" -eq "0" ] ; then
229        # Store the dev name in case we changed it interactively
230        if [ -f "/tmp/mondorestore.cfg" ]; then
231            sed -i "s/^media-dev .*$/media-dev  $tapedev/" /tmp/mondorestore.cfg
232        fi
233    fi
234    cd $old_pwd
235    return $res
236}
237
238HandleTape() {
239    local res tapedev
240    tapedev="" ; # will be set by UntarTapeStuff()
241
242    # Here we can add the USB storage module, it's working even with OBDR !
243    modprobe -q usb-storage 2> /dev/null
244    # Tape takes some time to appear
245    sleep 10
246    cd $EXTRACTDIR
247    UntarTapeStuff $tapedev
248    res=$?
249    tapedev="default tape device"
250    while [ "$res" -ne "0" ] ; do
251        LogIt "$tapedev failed to act as extended data disk for booting." 1
252        LogIt "Please specify an alternate non-rewinding tape device, such as /dev/nst0" 1
253        LogIt "or hit <Enter> to boot from another media." 1
254        echo -en "---> "
255        read tapedev
256        if [ "$tapedev" ] ; then
257            LogIt "User specified $tapedev instead"
258            UntarTapeStuff $tapedev
259            res=$?
260        else
261            LogIt "User opted not to specify an alternate tapedev"
262            res=1
263        break
264        fi
265    done
266    # For post-init
267    export TAPEDEV=$tapedev
268
269    if [ "$res" -ne "0" ] ; then
270        cd /
271        LogIt "Failed to use tape as extended datadisk. Reverting to another media." 1
272        HandleCDROM
273        res=$?
274    else
275        LogIt "Using tape as extended datadisk. Good." 3
276        echo "Using tape as extd ddisk." > /tmp/TAPEDEV-HAS-DATA-DISKS
277        res=0
278        CD_MOUNTED_OK=yes
279    fi
280    return $res
281}
282
283
284
285HandleCDROM() {
286    # Just in case we have an iLO ensure we will map it correctly
287    echo "$DENY_MODS" | grep -q "usb-storage "
288    if [ $? -eq 0 ]; then
289        return
290    fi
291    echo "Activating a potential USB Storage device"
292    modprobe -q usb-storage 2> /dev/null
293    for i in 1 2 3 4 5 6 7 8 9 10 ; do
294        sleep 1
295        echo -en "."
296    done
297
298    find-and-mount-cdrom
299    res=$?
300    if [ "$res" -ne "0" ] ; then
301        LogIt "First call to find-and-mount-cdrom failed." 1
302        LogIt "Sleeping for 3 seconds and trying again." 1
303        sleep 3
304        find-and-mount-cdrom
305        res=$?
306    fi
307    if [ "$res" -eq "0" ] ; then
308        LogIt "OK, I am running on a CD-ROM. Good." 1
309        CD_MOUNTED_OK=yes
310    else
311        LogIt "You probably not have the right drivers" 1
312        LogIt "to support the hardware on which we are running" 1
313        LogIt "Your archives are probably fine but" 1
314        LogIt "your tape streamer and/or CD-ROM drive are unsupported at that point." 1
315        CD_MOUNTED_OK=""
316    fi
317    return 0
318}
319
320
321PauseForRaids() {
322    if [ "`dmesg | grep -i "RAID Controller"`" != "" ] || [ "`dmesg | grep -i "Vendor: 3ware"`" != "" ] ; then
323        LogIt "RAID controller(s) detected. Pausing 10 seconds to let them warm up." 1
324        echo -en "Pausing..."
325        for i in 1 2 3 4 5 6 7 8 9 10 ; do
326            sleep 1
327            echo -en "$(($i*10))%..."
328        done
329        echo "Done."
330    fi
331}
332
333
334# 06/13/2002 --- DuckX's & Hugo's new routine
335# 10/11/2003 --- added a simple devfsd.conf file
336# 01/24/2005 --- cater for arbitrary locations of devfsd.conf by using the
337#                config file path compiled into devfsd
338RunDevfsd() {
339    loc=`which devfsd 2> /dev/null`
340    if [ "$loc" != "" ] ; then
341    LogIt "Devfs found. Testing kernel support..."
342    if [ ! -e "/dev/.devfsd" ] ; then
343        mount -t devfs devfs /dev 2>> $LOGFILE
344        if [ "$?" -ne "0" ] ; then
345            LogIt "Error while trying to mount devfs"
346        else
347            LogIt "Devfs mounted OK"
348        fi
349    fi
350    #check if the kernel supports devfs
351    if [ -e "/dev/.devfsd" ] ; then
352        [ -d "/proc/1" ] || mount -n /proc
353        LogIt "Kernel support found. Creating config file and starting devfsd"
354        conffile=`strings $loc | grep -E "devfsd.conf$"`
355        [ "$conffile" ] || conffile="/etc/devfsd.conf"
356        confpath=`echo $conffile | sed "s/\/devfsd\.conf$//"`
357        [ -d "$confpath" ] || mkdir -p $confpath
358        echo -en "REGISTER .* MKOLDCOMPAT\nUNREGISTER .* RMOLDCOMPAT\n" > $conffile
359        devfsd /dev &
360        sleep 5
361    else
362        LogIt "No devfs kernel support."
363    fi
364    fi
365}
366
367# Taken from udev management in Mandriva 2008.0. Kudos guys
368MakeExtraNodes () {
369    # there are a few things that sysfs does not export for us.
370    # these things are listed in /etc/udev/links.conf
371    grep '^[^#]' /etc/udev-links.conf | \
372    while read type name arg1; do
373        [ "$type" -a "$name" -a ! -e "/dev/$name" -a ! -L "/dev/$name" ] ||continue
374        case "$type" in
375            L) ln -s $arg1 /dev/$name ;;
376            D) mkdir -p /dev/$name ;;
377            M) mknod /dev/$name $arg1 && chmod 600 /dev/$name;;
378            *) echo "udev-links.conf: unparseable line (%s %s %s)\n" "$type" "$name" "$arg1" ;;
379        esac
380    done
381}
382
383CreateDevMakedev() {
384    if [ -e /sbin/MAKEDEV ]; then
385        ln -sf /sbin/MAKEDEV /dev/MAKEDEV
386    else
387        ln -sf /bin/true /dev/MAKEDEV
388    fi
389}
390
391RunUdevd() {
392    # Initial Inspiration from Mandriva 2008.0 startup script
393    LogIt "Preparing udev environment..." 1
394    mv /dev /dev.static
395    mkdir /dev
396    # Contributed during LinuxCon Japan !
397    if [ -x /usr/lib/systemd/systemd-udevd ] || [ -x /lib/systemd/systemd-udevd ]; then
398        mount -n -t devtmpfs none /dev
399    else
400        mount -n -o size=5M,mode=0755 -t tmpfs none /dev
401        MakeExtraNodes
402        mount -n -t devpts -o mode=620 none /dev/pts
403    fi
404    mkdir -p /dev/shm
405    mount -n -t tmpfs  none /dev/shm
406    if [ -e /proc/sys/kernel/hotplug ]; then
407        echo > /proc/sys/kernel/hotplug
408    fi
409    PKLVL=`cut -f1 /proc/sys/kernel/printk`
410    echo 0 > /proc/sys/kernel/printk
411    # Many possibilities depending on udev versions
412    if [ -x /sbin/startudev ]; then
413        /sbin/startudev
414    fi
415    if [ -x /sbin/start_udev ]; then
416        /sbin/start_udev
417    fi
418    # Depending on udevd version it gives back the hand or not :-(
419    ps | grep -v grep |grep udevd 2> /dev/null 1> /dev/null
420    if [ $? -ne 0 ]; then
421        echo "Waiting for udev to start..."
422        if [ -x /sbin/udevd ]; then
423            /sbin/udevd --daemon &
424        elif [ -x /usr/bin/udevd ]; then
425            /usr/bin/udevd --daemon &
426        elif [ -x /usr/lib/systemd/systemd-udevd ] || [ -x /lib/systemd/systemd-udevd ]; then
427            # from https://github.com/hut/minirc/blob/master/rc
428            echo "INFO: This is the systemd version of udev"
429            # Contributed during LinuxCon Japan
430            #mount -n -t tmpfs tmpfs /sys/fs/cgroup
431            #mount -n -t cgroup cgroup /sys/fs/cgroup/systemd
432            mkdir -p /run/udev
433            mkdir -p /run/systemd/journal
434            if [ -x /usr/lib/systemd/systemd-udevd ]; then
435                /usr/lib/systemd/systemd-udevd --daemon
436            else
437                /lib/systemd/systemd-udevd --daemon
438            fi
439            udevadm trigger --action=add --type=subsystems
440            udevadm trigger --action=add --type=devices
441        fi
442        sleep 5
443        LogIt "udev started manually" 1
444    fi
445    mkdir -p /dev/.udev/queue/
446    if [ -x /sbin/udevtrigger ]; then
447        /sbin/udevtrigger
448    fi
449    echo "Waiting for udev to discover..."
450    CreateDevMakedev
451    # Newer version use udevadm for that
452    if [ -x /sbin/udevsettle ]; then
453        /sbin/udevsettle --timeout=10
454        LogIt "Discovering with udevsettle"
455    elif [ -x /sbin/udevadm ] || [ -x /usr/bin/udevadm ]; then
456        ua=`UdevadmTrigger`
457        $ua settle --timeout=10
458        LogIt "Discovering with udevadm"
459    fi
460    # It seems we need to have more static devs on some distro were
461    # udev as some other requirements to be covered later on.
462    # So in the mean time:
463    for d in `ls /dev.static`; do
464        if [ ! -e /dev/$d ]; then
465            mv /dev.static/$d /dev
466        fi
467    done
468    echo $PKLVL > /proc/sys/kernel/printk
469}
470
471RstHW() {
472
473    # Restore the HW configuration if available (NOT by default)
474    answer="NO"
475
476    grep -q nohw /proc/cmdline
477    if [ "$?" -eq 0 ]; then
478        return
479    fi
480    if [ -x ./mindi-rsthw ]; then
481        grep -q RESTORE /proc/cmdline
482        if [ "$?" -ne 0 ]; then
483            #clear
484            echo "*********************************************************************"
485            echo "Do you want to restore the HW configuration of the original machine ?"
486            echo "(This may dammage your hardware so be sure to check twice before saying yes)"
487            echo "*********************************************************************"
488            echo "Please confirm by typing YES exactly as written here (NO by default)"
489            echo -n "--> "
490            read answer
491        else
492            answer="YES"
493        fi
494        if [ "$answer" = "YES" ] ; then
495            ./mindi-rsthw
496        fi
497    fi
498}
499
500
501StartUSBKbd() {
502# Prepare minimal USB env in case we have USB kbd such as with iLO
503[ -d /proc/bus/usb ] && ! grep -q /proc/bus/usb /proc/mounts && mount -t usbfs none /proc/bus/usb
504echo "$DENY_MODS" | grep -Eq 'uhcd_hci |usbhid |usbcore '
505if [ $? -eq 0 ]; then
506    return
507fi
508modprobe -q uhcd_hci 2> /dev/null
509modprobe -q usbhid 2> /dev/null
510}
511
512
513
514StartLvms() {
515    if [ "`grep -i nolvm /proc/cmdline`" ]; then
516        return;
517    fi
518    if [ -e "/tmp/i-want-my-lvm" ] ; then
519        # Recent LVM need this caching daemon to work
520        LVMETAD=""
521        if [ -x /sbin/lvmetad ]; then
522            LVMETAD="/sbin/lvmetad"
523        fi
524        if [ -x /usr/sbin/lvmetad ]; then
525            LVMETAD="/usr/sbin/lvmetad"
526        fi
527        if [ -x $LVMETAD ]; then
528            mkdir -p /run/lvm
529            $LVMETAD
530        fi
531        LogIt "Scanning LVM's..." 1
532        if which lvm ; then
533            modprobe -q dm-mod 2> /dev/null
534            modprobe -q dm_mod 2> /dev/null
535            lvm vgscan --mknodes
536        else
537            vgscan
538        fi
539
540        # Exclude devices we may not want
541        rm -f /tmp/restorevgs
542        for d in $MINDI_EXCLUDE_DEVS ; do
543            echo "LVM exclusion == $d"
544            # LVM: $4 to be checked if changed
545            EXCLUDE_VGS=`grep " $d" /tmp/i-want-my-lvm | grep vgcreate | awk '{print $4}'`
546            vg=`echo $EXCLUDE_VGS | sed "s/ /|/g"`
547            if [ "$vg" != "" ]; then
548                re=" $d|$vg"
549            else 
550                re=" $d"
551            fi
552            # Remove VGs from i-want-my-lvm
553            grep -Ev "$re" /tmp/i-want-my-lvm > /tmp/i-want-my-lvm.new
554            mv /tmp/i-want-my-lvm.new /tmp/i-want-my-lvm
555            # Prepare  script to restore the VG exluded here if needed
556            for v in $EXCLUDE_VGS; do
557                echo "vgcfgrestore $v" >> /tmp/restorevgs
558                # Remove LVs from mountlist
559                # LVM: sed to be checked if changed
560                EXCLUDE_LVS=`grep " $v" /tmp/i-want-my-lvm | grep lvcreate | sed "s/^.*-n \([^ ][^ ]*\) .*$/$1/"`
561                for l in $EXCLUDE_LVS; do
562                    # FIXME: Should search for all possible device names here
563                    if [ -f "/tmp/mountlist.txt" ]; then
564                        grep -Ev "/dev/$v/$l" /tmp/mountlist.txt > /tmp/mountlist.txt.new
565                        grep -Ev "/dev/mapper/${v}-$l" /tmp/mountlist.txt.new > /tmp/mountlist.txt
566                    fi
567                done
568            done
569        done
570
571        # LVM: remove #
572        grep -E "^#.*vgchange" /tmp/i-want-my-lvm | sed "s/^#[ ]*//" > /tmp/start-lvm
573        chmod +x /tmp/start-lvm
574        echo -en "Starting LVM's..."
575        /tmp/start-lvm &
576        for i in 1 2 3 4 5 ; do
577            echo -en "."
578            sleep 1
579        done
580        echo "Done."
581    fi
582# If necessary, cannibalize 'analyze-my-lvm'; copy some of its code here,
583# pipe vgscan's output, strip it, run 'vgchange' on its output, etc.etc.
584    LogIt "LVM's have been started."
585}
586
587StartPowerPath() {
588
589    # Taken from the init script of EMC PowerPath on RHEL
590    if [ -f /etc/emcp_devicesDB.dat ]; then 
591        if [ -f /etc/emcp_devicesDB.idx ]; then
592            /etc/opt/emcpower/emcpmgr map -p > /dev/null 2>&1
593        fi
594    fi
595
596    /sbin/powermt config > /dev/null 2>&1
597    #   Wait for udev to finish creating emcpower devices
598    #
599    pdfound=1
600    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
601        pdfound=1
602        for pd in `/bin/ls -d /sys/block/emcpower* 2> /dev/null`; do
603            bpd=`basename $pd`
604            if [ ! -e /dev/$bpd ]; then
605                pdfound=0
606                sleep 2
607                break;
608            fi
609        done
610        if [ "$pdfound" -eq 1 ]; then
611            break
612        fi
613    done
614    if [ "$pdfound" -eq 0 ]; then
615        echo "Unable to start PowerPath"
616    fi
617
618    /sbin/powermt load      > /dev/null 2>&1
619    /etc/opt/emcpower/emcpmgr map   > /dev/null 2>&1
620    /etc/opt/emcpower/powercf -C    > /dev/null 2>&1
621    if [ ! -e /etc/powermt.custom ]; then 
622        /sbin/powermt save      > /dev/null 2>&1
623    fi 
624    /sbin/powermt register  > /dev/null 2>&1
625    /sbin/powermig transition -startup -noprompt    > /dev/null 2>&1
626}
627
628StartMpath() {
629    if [ "`grep -i nompath /proc/cmdline`" ]; then
630        return;
631    fi
632    if [ -f "/tmp/mountlist.txt" ] && [ "`grep mpath /tmp/mountlist.txt`" ]; then
633        if [ -x /sbin/multipath ]; then
634            echo "Starting Mpath..."
635            mkdir -p /var/lib/multipath
636            cat > /etc/multipath.conf << EOF
637defaults {
638        user_friendly_names yes
639}
640EOF
641            /sbin/multipath -v 0
642            if [ -x /sbin/kpartx ]; then
643                /sbin/dmsetup ls --target multipath --exec "/sbin/kpartx -a -p p"
644            fi
645            LogIt "Multipath started"
646        fi
647    fi
648}
649
650StartDRBD() {
651    if [ "`grep -i nodrbd /proc/cmdline`" ]; then
652        return;
653    fi
654    if [ "`grep -i drbd /proc/cmdline`" ]; then
655        echo "Starting DRBD..."
656        mkdir -p /var/run/drbd
657        drbdadm adjust-with-progress all
658        drbdadm wait-con-int
659        # Become primary
660        drbdadm sh-b-pri
661        drbdadm sh-status
662        drbdsetup all show
663        LogIt "DRBD started"
664    fi
665}
666
667StartRaids() {
668    local raid_devices i
669
670    if [ "`grep -i noraid /proc/cmdline`" ]; then
671        return;
672    fi
673
674    if [ -f "/tmp/mountlist.txt" ]; then
675        raid_devices=`grep /dev/md /tmp/mountlist.txt | cut -d' ' -f1`
676    else
677        raid_devices=""
678    fi
679
680    if which raidstart > /dev/null 2> /dev/null ; then
681        for i in $raid_devices ; do
682            if grep `basename $i` /proc/mdstat > /dev/null 2> /dev/null ; then
683                LogIt "$i is started already; no need to run 'raidstart $i'" 1
684            else
685                LogIt "Running 'raidstart $i'" 1
686                raidstart $i
687            fi
688        done
689    elif which mdrun > /dev/null 2> /dev/null ; then
690        if [ "`grep -i nomd /proc/cmdline`" ]; then
691            return;
692        fi
693        LogIt "Running 'mdrun'" 1
694        mdrun
695    elif which mdadm > /dev/null 2> /dev/null ; then
696        if [ "`grep -i nomd /proc/cmdline`" ]; then
697            return;
698        fi
699        LogIt "Running 'mdadm'" 1
700        for i in $raid_devices ; do
701            if grep `basename $i` /proc/mdstat > /dev/null 2> /dev/null ; then
702                LogIt "$i is started already; no need to run 'mdadm $i'" 1
703            else
704                if [ -f /etc/mdadm.conf ] ; then
705                    LogIt "Running 'mdadm $i' with user supplied /etc/mdadm.conf" 1
706                    mdadm -A $i -c /etc/mdadm.conf
707                elif [ -f /etc/mdadm/mdadm.conf ] ; then
708                    LogIt "Running 'mdadm $i' with user supplied /etc/mdadm/mdadm.conf" 1
709                    mdadm -A $i -c /etc/mdadm/mdadm.conf
710                else
711                    LogIt "Running 'mdadm $i'" 1
712                    mdadm -Ac partitions -m dev $i
713                fi
714            fi
715        done
716    elif which dmraid > /dev/null 2> /dev/null ; then
717        if [ "`grep -i nodmraid /proc/cmdline`" ]; then
718            return;
719        fi
720        for x in $(/sbin/dmraid -ay -i -p -t 2>/dev/null | grep -E  -iv "^no " |awk -F ':' '{ print $1 }') ; do
721            echo "Please send the result of /sbin/dmraid -ay -i -p -t to the devteam !!!"
722            return
723            #dmname=$(resolve_dm_name $x)
724            #[ -z "$dmname" ] && continue
725            #/sbin/dmraid  -ay -i -p "$dmname" >/dev/null 2>&1
726            #/sbin/kpartx -a -p p "/dev/mapper/$dmname"
727        done
728    else
729        LogIt "Warning: Neither 'raidstart' nor 'mdrun''found. RAID devices may not have started." 1
730    fi
731
732}
733
734
735TryAgainToFindCD() {
736    local res
737    mount | grep /mnt/cdrom && return 0
738    [ "`grep "backup_media_type" /tmp/mondorestore.cfg 2> /dev/null | grep "cdstream"`" ] && return
739    LogIt "Trying to mount CD-ROM a 2nd time..."
740    find-and-mount-cdrom --second-try
741    res=$?
742    if [ "$res" -eq "0" ] ; then
743        CD_MOUNTED_OK=yes
744        LogIt "CD-ROM drive mounted successfully." 1
745    else
746        LogIt "I still cannot find or mount the CD-ROM drive, by the way."
747    fi
748}
749
750
751
752UseTmpfs()
753{
754    local mount_cmd
755    echo -en "Mounting /tmp/tmpfs..."
756    mkdir -p /tmp/tmpfs
757# For technical reasons, some sets are as large as 16MB.
758# I am allowing 32MB because selective restore occupies a lot of space.
759    for size in 256m 128m 64m 48m 40m 32m ; do
760        mount_cmd="mount /dev/shm -t tmpfs -o size=$size" ; # was 34m until 04/2003
761        LogIt "Trying '$mount_cmd'"
762        $mount_cmd /tmp/tmpfs 2>> $LOGFILE
763        res=$?
764        [ "$res" -eq "0" ] && break
765    done
766    if [ "$res" -ne "0" ] ; then
767        LogIt "Failed. I could not run '$mount_cmd /tmp/tmpfs'. Your kernel is BROKEN or you do not have enough RAM." 1
768        umount /tmp/tmpfs > /dev/null 2> /dev/null
769        rmdir /tmp/tmpfs
770        ln -sf /mnt/RESTORING/tmp /tmp/tmpfs; # used by mondorestore
771        LogIt "Failed to mount /tmp/tmpfs; using ugly softlink instead"
772    else
773        LogIt "Great. Pivot succeeded w/ size=$size" 1
774        echo -en "Pivoting /tmp..."
775        umount /tmp/tmpfs
776        mkdir -p /tmp.old
777        mv /tmp/* /tmp.old/
778        # Try to Deal with a busybox bug on inexistant links
779        cp /tmp/* /tmp.old/ 2> /dev/null
780        rm -f /tmp/*
781        $mount_cmd /tmp
782        mv /tmp.old/* /tmp/ 2> /dev/null
783        # Try to Deal with a busybox bug on inexistant links
784        cp /tmp.old/* /tmp/ 2> /dev/null
785        rm -rf /tmp.old
786        mkdir -p /tmp/tmpfs
787        mkdir -p $EXTRACTDIR
788    echo "Done."
789    LogIt "Successfully mounted dynamic /tmp ramdisk"
790#   mkdir -p /tmp/tmpfs/var
791#        mv -f /var/log /tmp/tmpfs/var/log
792#        ln -sf /tmp/tmpfs/var/log /var/log
793    fi
794}
795
796
797WelcomeMessage()
798{
799    echo "********************************************************************"
800    echo "MINDI-LINUX by Mondo Dev Team - web site: http://www.mondorescue.org"
801    echo "Boot disk based on AlfaLinux & Trinux. BusyBox by Erik Andersen."
802which petris > /dev/null 2> /dev/null && echo "Petris was written by Peter Seidler <p.seidler@mail1.stofanet.dk>."
803    echo "Executables and source code are covered by the GNU GPL. No warranty."
804    echo "running on $ARCH"
805    echo "********************************************************************"
806}
807
808EnableCcissIfAppropriate() {
809    local f
810
811    # In some cases, the OBDR tape remains in CD-ROM mode. This branch solves that issue (#498)
812    if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2>/dev/null`" ] && [ -x /usr/bin/hpsa_obdr_mode ]; then
813        for lun in `/usr/bin/hpsa_obdr_mode -m query /dev/cciss/* 2>&1 | grep "is an OBDR device" | cut -d' ' -f4`; do
814            /usr/bin/hpsa_obdr_mode -m query -l $lun /dev/cciss/* 2>&1 | grep -q "is in CD-ROM mode"
815            if [ $? -eq 0 ]; then
816                # This tape/lun should be brought back in Sequential mode
817                LogIt "Putting back tape $lun in Sequential mode..."
818                /usr/bin/hpsa_obdr_mode -m tape -l $lun /dev/cciss/* 2>&1 > /dev/null
819            fi
820        done
821    fi
822
823    # Now engage all Smart Array controllers to detect attached devices
824    if [ -d  "/proc/driver/cciss" ]; then
825        for f in /proc/driver/cciss/cciss* ; do
826            LogIt "Engaging $f"
827            echo "engage scsi" > $f
828            LogIt "...result=$?"
829        done
830    fi
831}
832
833ModprobeAllModules() {
834
835    echo "Searching for modules to install..."
836    if [ ! -e /tmp/modules ]; then
837        LogIt "Unable to find the modules list. Computing one. This may take some time..."
838        lismod=`find /lib/modules -name '*\.ko*' -o -name '*\.o*'`
839        for m in $lismod; do
840            k=`basename $m | sed 's/\.ko.*$//'`
841            j=`basename $k | sed 's/\.o.*$//'`
842            echo "$j" >> /tmp/modules
843        done
844    fi
845    # loading forced modules first
846    for j in `cat /tmp/modules`; do
847        echo "$FORCE_MODS" | grep -q "$j "
848        if [ $? -eq 0 ]; then
849            echo "Forcing first $j..."
850            modprobe -q $j 2> /dev/null
851        fi
852    done
853    for j in `cat /tmp/modules`; do
854        echo "$DENY_MODS" | grep -q "$j "
855        if [ $? -eq 0 ]; then
856            echo "Denying $j..."
857            continue
858        fi
859        echo "$FORCE_MODS" | grep -q "$j "
860        if [ $? -eq 0 ]; then
861            continue
862        fi
863        echo "Probing $j..."
864        modprobe -q $j 2> /dev/null
865    done
866}
867
868UdevadmTrigger() {
869
870# This function should just echo the return value $ua
871
872if [ -x /sbin/udevadm ]; then
873    ua="/sbin/udevadm"
874elif [ -x /usr/bin/udevadm ]; then
875    ua="/usr/bin/udevadm"
876else
877    LogIt "WARNING: Unable to find udevadm. Not triggering udev"
878    ua="/bin/true"
879fi
880
881v="`$ua --version`"
882
883if [ -x "$v" ]; then
884    LogIt "Triggering udev again..."
885    if [ "$v" -ge "146" -a "$v" -lt "174" ]; then
886        $ua trigger --type=failed
887    elif [ "$v" -lt "146" ]; then
888        $ua trigger --retry-failed
889    else
890        LogIt "... not needed"
891    fi
892fi
893echo $ua
894}
895
896ExtractDataDisksAndLoadModules() {
897    LogIt "Installing additional tools ..." 1
898    # TODO: test return value
899    install-additional-tools
900
901    # Retry failed udev events now that local filesystems are mounted read-write
902    # (useful for rules creating network ifcfg files)
903    if [ -e "/tmp/USE-UDEV" ] ; then
904        ua=`UdevadmTrigger`
905    fi
906}
907
908# ------------------------ main -----------------------
909
910MINDI_VER=PBVER
911MINDI_REV=PBREV
912trap CaughtSoftReset SIGTERM
913trap GetShell SIGINT
914LOGFILE=/var/log/mondorestore.log
915PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/mondo:/usr/games
916EXTRACTDIR=/tmp/extract
917USER=root
918ARCH=`uname -m`
919
920export PATH EXTRACTDIR USER LOGFILE ARCH
921
922mount -o remount,rw /
923rm -f $LOGFILE
924
925mkdir /proc 2> /dev/null
926mount /proc /proc -t proc
927
928# Creates log file
929LogIt ""
930# Backup fd
931exec 4>&2 5>&1
932# Redirect every message to the LOGFILE
933exec > >(tee -a ${LOGFILE})
934exec 2> >(tee -a ${LOGFILE} >&2)
935
936echo "Welcome to init (from mindi ${MINDI_VER}-r${MINDI_REV})"
937
938mkdir /sys 2> /dev/null
939mount /sys /sys -t sysfs
940
941for i in $EXTRACTDIR /mnt/cdrom /mnt/isodir /var/local/petris /tmp/isodir; do
942    mkdir -p $i
943done
944# For ESX 3
945[ -d /proc/vmware ] && ! grep -q /vmfs /proc/mounts && mount -t vmfs /vmfs /vmfs
946
947if [ "`grep -i denymods /proc/cmdline`" ]; then
948    export DENY_MODS="`cat /proc/cmdline | sed 's~.*denymods=\"\(.*\)\".*~\1~'` mondonone"
949else
950    export DENY_MODS=" "
951fi
952if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2> /dev/null`" ]; then
953    # Do not try to load usb storage when dealing with OBDR it makes the modprobe hang :-(
954    export DENY_MODS="usb-storage $DENY_MODS"
955fi
956if [ "`grep -i forcemods /proc/cmdline`" ]; then
957    export FORCE_MODS="`cat /proc/cmdline | sed 's~.*forcemods=\"\(.*\)\".*~\1~'` mondonone"
958else
959    export FORCE_MODS=" "
960fi
961echo "Activating a potential USB keyboard/mouse"
962StartUSBKbd
963
964if [ -f /proc/sys/kernel/exec-shield ]; then
965    echo 0 > /proc/sys/kernel/exec-shield 2>> /dev/null
966fi
967
968if [ -f /proc/modules ]; then
969    echo "/sbin/modprobe" > /proc/sys/kernel/modprobe
970fi
971
972if [ -e "/tmp/USE-UDEV" ] ; then
973    RunUdevd
974fi
975ExtractDevTarballs
976LaunchTerminals
977
978# Keeping kernel silent for module insertion
979PKLVL=`cut -f1 /proc/sys/kernel/printk`
980echo 0 > /proc/sys/kernel/printk
981ModprobeAllModules
982echo $PKLVL > /proc/sys/kernel/printk
983
984EnableCcissIfAppropriate
985#-------------------------------
986#WHOLIVESINAPINEAPPLEUNDERTHESEA#;# --- don't touch this :)
987#-------------------------------
988UseTmpfs
989if [ ! -e "/tmp/mondorestore.cfg" ] ; then
990    LogIt "WARNING: /tmp/mondorestore.cfg not found - forging an empty one"
991    LogIt "         Hopefully this is a Mindi media"
992    touch /tmp/mondorestore.cfg
993fi
994if [ "`grep -i pxe /proc/cmdline`" ] || [ "`grep -i net /proc/cmdline`" ] || [ "`grep 'netfs-proto ' /tmp/mondorestore.cfg`" ] || [ "`grep -i drbd /proc/cmdline`" ]; then
995    # We need to get here exported variables from start-netfs
996    . /usr/sbin/start-netfs
997fi
998
999if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2>/dev/null`" ] || [ "`grep -i ' udev' /tmp/mondorestore.cfg 2>/dev/null`" ]; then
1000    HandleTape
1001    ExtractDataDisksAndLoadModules
1002elif [ "`grep -i pxe /proc/cmdline`" ]; then
1003    # Simulate a local CD
1004    echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
1005    CD_MOUNTED_OK=yes
1006    ExtractDataDisksAndLoadModules
1007    # Fake the conf file to force it to NFS mode, even if we made originally a CD (mandatory for mondorestore to work correctly)
1008    sed -i "s/backup-media-type.*/backup-media-type netfs/" /tmp/mondorestore.cfg
1009elif [ "`grep -i usb= /proc/cmdline`" ] || [ "`grep -i usb /tmp/mondorestore.cfg 2>/dev/null | grep media-type`" ]; then
1010    . /usr/sbin/start-usb
1011
1012    # Simulate a local CD
1013    echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
1014    CD_MOUNTED_OK=yes
1015    ExtractDataDisksAndLoadModules
1016else
1017    HandleCDROM
1018    ExtractDataDisksAndLoadModules
1019fi
1020res=$?
1021ConfigureLoggingDaemons
1022if [ -e "/tmp/USE-DEVFS" ] ; then
1023    umount -d /mnt/cdrom 2> /dev/null
1024    mv /dev/cdrom /cdrom.lnk 2> /dev/null
1025    CD_MOUNTED_OK=""
1026    RunDevfsd
1027fi
1028
1029if [ "`grep -i excludedevs /proc/cmdline`" ]; then
1030    export MINDI_EXCLUDE_DEVS="`cat /proc/cmdline | sed 's~.*excludedevs=\"\(.*\)\".*~\1~'` mondonone"
1031    for d in $MINDI_EXCLUDE_DEVS ; do
1032        echo "Mountlist exclusion == $d"
1033        if [ -e "/tmp/mountlist.txt" ]; then
1034            perl -i -ne 'print $_ unless m~$d~' /tmp/mountlist.txt
1035        fi
1036    done
1037else
1038    export MINDI_EXCLUDE_DEVS=" "
1039fi
1040
1041LoadKeymap
1042WelcomeMessage
1043RstHW
1044echo "Starting potential Raid/Multipath ..."
1045[ -e "/tmp/mountlist.txt" ] && cp -f /tmp/mountlist.txt /tmp/mountlist.original
1046PauseForRaids
1047StartRaids
1048StartMpath
1049StartDRBD
1050# avoids messagesa round leaked fd - Cf lvm man page
1051export LVM_SUPPRESS_FD_WARNINGS=1
1052StartLvms
1053CopyBootDevEntry
1054mkdir -p /tmp/tmpfs
1055sleep 2
1056#clear
1057if [ -e "/dev/md0" ] && [ ! -e "/dev/md/0" ] && [ -e "/tmp/mountlist.txt" ] && [ "`grep /dev/md/ /tmp/mountlist.txt`" != "" ] ; then
1058    LogIt "Creating /dev/md/* softlinks just in case." 1
1059    mkdir -p /dev/md
1060    cp -af /dev/md0 /dev/md/0 2> /dev/null
1061    cp -af /dev/md1 /dev/md/1 2> /dev/null
1062    cp -af /dev/md2 /dev/md/2 2> /dev/null
1063fi
1064
1065if ! [ "`grep -i "pxe" /proc/cmdline`" ] ; then
1066    res="`cat /mnt/cdrom/archives/THIS-CD-NUMBER 2> /dev/null`"
1067    [ "$res" != "1" ] && [ "$res" != "" ] && Die "This is CD #$res in the series. Please insert CD #1."
1068    [ -e "/cdrom.lnk" ] && mv -f /cdrom.lnk /dev/cdrom && mount /dev/cdrom /mnt/cdrom && CD_MOUNTED_OK=yes
1069    [ "$CD_MOUNTED_OK" != "yes" ] && TryAgainToFindCD
1070    #mount | grep cdrom > /dev/null 2> /dev/null || mount /dev/cdrom /mnt/cdrom >> $LOGFILE 2>> $LOGFILE
1071fi
1072hack-cfg-if-necessary || LogIt "Cannot find hack-cfg-if-necessary"
1073
1074# Log some useful info
1075LogIt "init (from mindi v$MINDI_VER-r${MINDI_REV})"
1076LogIt "/proc/cmdline is:"
1077LogIt "----------"
1078cat /proc/cmdline  >> $LOGFILE
1079LogIt "----------"
1080LogIt "df result:"
1081LogIt "----------"
1082df >> $LOGFILE
1083LogIt "-------------"
1084LogIt "mount result:"
1085LogIt "-------------"
1086mount >> $LOGFILE
1087LogIt "-------------"
1088LogIt "lsmod result:"
1089LogIt "-------------"
1090lsmod >> $LOGFILE
1091LogIt "-------------"
1092LogIt "dmesg result:"
1093LogIt "-------------"
1094dmesg >> $LOGFILE
1095LogIt "-------------"
1096LogIt "/proc/mdstat:" >> $LOGFILE
1097LogIt "-------------"
1098cat /proc/mdstat  >> $LOGFILE
1099LogIt "-------------"
1100LogIt "/proc/swaps:" >> $LOGFILE
1101LogIt "-------------"
1102cat /proc/swaps  >> $LOGFILE
1103LogIt "-------------"
1104LogIt "/proc/filesystems:" >> $LOGFILE
1105LogIt "-------------"
1106cat /proc/filesystems  >> $LOGFILE
1107LogIt "-------------"
1108LogIt "/proc/partitions:" >> $LOGFILE
1109LogIt "-------------"
1110cat /proc/partitions  >> $LOGFILE
1111LogIt "-------------"
1112LogIt "fdisk result:" >> $LOGFILE
1113LogIt "-------------"
1114fdisk -l  >> $LOGFILE
1115LogIt "-------------"
1116if [ -f /tmp/mondorestore.cfg ]; then
1117    LogIt "/tmp/mondorestore.cfg result:" >> $LOGFILE
1118    LogIt "-------------"
1119    cat /tmp/mondorestore.cfg  >> $LOGFILE
1120fi
1121
1122ide-opt
1123
1124#ctrlaltdel soft
1125for i in null stdout stdin stderr ; do
1126    cp -af /dev/$i /tmp
1127done
1128# Not sure it's still needed, but shouldn't hurt
1129if [ -e "/tmp/USE-UDEV" ] ; then
1130    ua=`UdevadmTrigger`
1131fi
1132if grep "backup-media-type" /tmp/mondorestore.cfg > /dev/null 2> /dev/null ; then
1133    LogIt "backup-media-type is specified in config file - great."
1134    LogIt "Calling post-init"
1135    # start-netfs moved it under /tmp as the NFS share is already unmounted
1136    if [ _"$pre" != _"" ] && [ "`echo $pre | grep -E '^/tmp/isodir'`" ]; then
1137        pre=`echo $pre | sed 's|^/tmp/isodir|/tmp|'`
1138    fi
1139    if [ _"$pre" != _"" ] && [ -x $pre ]; then
1140        echo "Executing preliminary script $pre"
1141        LogIt "Executing preliminary script $pre"
1142        $pre
1143    fi
1144    post-init
1145else
1146    LogIt "backup-media-type is not specified in config file."
1147    LogIt "I think this media has no archives on it."
1148fi
1149if [ "`grep -i "post=" /proc/cmdline`" ] ; then
1150    for i in `cat /proc/cmdline` ; do
1151        echo $i | grep -qi post= && post=`echo $i | cut -d= -f2`
1152    done
1153    # start-netfs moved it under /tmp as the NFS share is already unmounted
1154    if [ "`echo $post | grep -E '^/tmp/isodir'`" ]; then
1155        post=`echo $post | sed 's|^/tmp/isodir|/tmp|'`
1156    fi
1157    if [ -r $post ]; then
1158        echo "Executing final script $post"
1159        LogIt "Executing final script $post"
1160        if [ "`echo $post | grep -E '^/mnt/RESTORING'`" ]; then
1161            mr-mount-me
1162        fi
1163        chmod 755 $post
1164        $post
1165        if [ "`echo $post | grep -E '^/mnt/RESTORING'`" ]; then
1166            mr-unmount-me
1167        fi
1168    fi
1169fi
1170if grep "RESTORE" /proc/cmdline > /dev/null 2> /dev/null ; then
1171    echo "Rebooting in 10 seconds automatically as per reboot order"
1172    echo -en "Press ^C to interrupt if you have to ..."
1173    for i in 1 2 3 4 5 6 7 8 9 10 ; do
1174        sleep 1
1175        echo -en "."
1176    done
1177    echo "Boom."
1178    sleep 1
1179else
1180    GetShell
1181fi
1182CaughtSoftReset
Note: See TracBrowser for help on using the repository browser.