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

Last change on this file since 3653 was 3653, checked in by Bruno Cornec, 4 years ago

Stop using the word PC and use machines instead

  • Property svn:keywords set to Id
File size: 32.5 KB
Line 
1#!/bin/bash
2#
3# $Id: rcS 3653 2017-02-23 01:05:46Z 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 $GROOVY
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 $GROOVY
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
650StartRaids() {
651    local raid_devices i
652
653    if [ "`grep -i noraid /proc/cmdline`" ]; then
654        return;
655    fi
656
657    if [ -f "/tmp/mountlist.txt" ]; then
658        raid_devices=`grep /dev/md /tmp/mountlist.txt | cut -d' ' -f1`
659    else
660        raid_devices=""
661    fi
662
663    if which raidstart > /dev/null 2> /dev/null ; then
664        for i in $raid_devices ; do
665            if grep `basename $i` /proc/mdstat > /dev/null 2> /dev/null ; then
666                LogIt "$i is started already; no need to run 'raidstart $i'" 1
667            else
668                LogIt "Running 'raidstart $i'" 1
669                raidstart $i
670            fi
671        done
672    elif which mdrun > /dev/null 2> /dev/null ; then
673        if [ "`grep -i nomd /proc/cmdline`" ]; then
674            return;
675        fi
676        LogIt "Running 'mdrun'" 1
677        mdrun
678    elif which mdadm > /dev/null 2> /dev/null ; then
679        if [ "`grep -i nomd /proc/cmdline`" ]; then
680            return;
681        fi
682        LogIt "Running 'mdadm'" 1
683        for i in $raid_devices ; do
684            if grep `basename $i` /proc/mdstat > /dev/null 2> /dev/null ; then
685                LogIt "$i is started already; no need to run 'mdadm $i'" 1
686            else
687                if [ -f /etc/mdadm.conf ] ; then
688                    LogIt "Running 'mdadm $i' with user supplied /etc/mdadm.conf" 1
689                    mdadm -A $i -c /etc/mdadm.conf
690                elif [ -f /etc/mdadm/mdadm.conf ] ; then
691                    LogIt "Running 'mdadm $i' with user supplied /etc/mdadm/mdadm.conf" 1
692                    mdadm -A $i -c /etc/mdadm/mdadm.conf
693                else
694                    LogIt "Running 'mdadm $i'" 1
695                    mdadm -Ac partitions -m dev $i
696                fi
697            fi
698        done
699    elif which dmraid > /dev/null 2> /dev/null ; then
700        if [ "`grep -i nodmraid /proc/cmdline`" ]; then
701            return;
702        fi
703        for x in $(/sbin/dmraid -ay -i -p -t 2>/dev/null | grep -E  -iv "^no " |awk -F ':' '{ print $1 }') ; do
704            echo "Please send the result of /sbin/dmraid -ay -i -p -t to the devteam !!!"
705            return
706            #dmname=$(resolve_dm_name $x)
707            #[ -z "$dmname" ] && continue
708            #/sbin/dmraid  -ay -i -p "$dmname" >/dev/null 2>&1
709            #/sbin/kpartx -a -p p "/dev/mapper/$dmname"
710        done
711    else
712        LogIt "Warning: Neither 'raidstart' nor 'mdrun''found. RAID devices may not have started." 1
713    fi
714
715}
716
717
718TryAgainToFindCD() {
719    local res
720    mount | grep /mnt/cdrom && return 0
721    [ "`grep "backup_media_type" /tmp/mondorestore.cfg 2> /dev/null | grep "cdstream"`" ] && return
722    LogIt "Trying to mount CD-ROM a 2nd time..."
723    find-and-mount-cdrom --second-try
724    res=$?
725    if [ "$res" -eq "0" ] ; then
726        CD_MOUNTED_OK=yes
727        LogIt "CD-ROM drive mounted successfully." 1
728    else
729        LogIt "I still cannot find or mount the CD-ROM drive, by the way."
730    fi
731}
732
733
734
735UseTmpfs()
736{
737    local mount_cmd
738    echo -en "Mounting /tmp/tmpfs..."
739    mkdir -p /tmp/tmpfs
740# For technical reasons, some sets are as large as 16MB.
741# I am allowing 32MB because selective restore occupies a lot of space.
742    for size in 256m 128m 64m 48m 40m 32m ; do
743        mount_cmd="mount /dev/shm -t tmpfs -o size=$size" ; # was 34m until 04/2003
744        LogIt "Trying '$mount_cmd'"
745        $mount_cmd /tmp/tmpfs 2>> $LOGFILE
746        res=$?
747        [ "$res" -eq "0" ] && break
748    done
749    if [ "$res" -ne "0" ] ; then
750        LogIt "Failed. I could not run '$mount_cmd /tmp/tmpfs'. Your kernel is BROKEN or you do not have enough RAM." 1
751        umount /tmp/tmpfs > /dev/null 2> /dev/null
752        rmdir /tmp/tmpfs
753        ln -sf /mnt/RESTORING/tmp /tmp/tmpfs; # used by mondorestore
754        LogIt "Failed to mount /tmp/tmpfs; using ugly softlink instead"
755    else
756        LogIt "Great. Pivot succeeded w/ size=$size" 1
757        echo -en "Pivoting /tmp..."
758        umount /tmp/tmpfs
759        mkdir -p /tmp.old
760        mv /tmp/* /tmp.old/
761        # Try to Deal with a busybox bug on inexistant links
762        cp /tmp/* /tmp.old/ 2> /dev/null
763        rm -f /tmp/*
764        $mount_cmd /tmp
765        mv /tmp.old/* /tmp/ 2> /dev/null
766        # Try to Deal with a busybox bug on inexistant links
767        cp /tmp.old/* /tmp/ 2> /dev/null
768        rm -rf /tmp.old
769        mkdir -p /tmp/tmpfs
770        mkdir -p $GROOVY
771    echo "Done."
772    LogIt "Successfully mounted dynamic /tmp ramdisk"
773#   mkdir -p /tmp/tmpfs/var
774#        mv -f /var/log /tmp/tmpfs/var/log
775#        ln -sf /tmp/tmpfs/var/log /var/log
776    fi
777}
778
779
780WelcomeMessage()
781{
782    echo "********************************************************************"
783    echo "MINDI-LINUX by Mondo Dev Team - web site: http://www.mondorescue.org"
784    echo "Boot disk based on AlfaLinux & Trinux. BusyBox by Erik Andersen."
785which petris > /dev/null 2> /dev/null && echo "Petris was written by Peter Seidler <p.seidler@mail1.stofanet.dk>."
786    echo "Executables and source code are covered by the GNU GPL. No warranty."
787    echo "running on $ARCH"
788    echo "********************************************************************"
789}
790
791EnableCcissIfAppropriate() {
792    local f
793
794    # In some cases, the OBDR tape remains in CD-ROM mode. This branch solves that issue (#498)
795    if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2>/dev/null`" ] && [ -x /usr/bin/hpsa_obdr_mode ]; then
796        for lun in `/usr/bin/hpsa_obdr_mode -m query /dev/cciss/* 2>&1 | grep "is an OBDR device" | cut -d' ' -f4`; do
797            /usr/bin/hpsa_obdr_mode -m query -l $lun /dev/cciss/* 2>&1 | grep -q "is in CD-ROM mode"
798            if [ $? -eq 0 ]; then
799                # This tape/lun should be brought back in Sequential mode
800                LogIt "Putting back tape $lun in Sequential mode..."
801                /usr/bin/hpsa_obdr_mode -m tape -l $lun /dev/cciss/* 2>&1 > /dev/null
802            fi
803        done
804    fi
805
806    # Now engage all Smart Array controllers to detect attached devices
807    if [ -d  "/proc/driver/cciss" ]; then
808        for f in /proc/driver/cciss/cciss* ; do
809            LogIt "Engaging $f"
810            echo "engage scsi" > $f
811            LogIt "...result=$?"
812        done
813    fi
814}
815
816ModprobeAllModules() {
817
818    echo "Searching for modules to install..."
819    if [ ! -e /tmp/modules ]; then
820        LogIt "Unable to find the modules list. Computing one. This may take some time..."
821        lismod=`find /lib/modules -name '*\.ko*' -o -name '*\.o*'`
822        for m in $lismod; do
823            k=`basename $m | sed 's/\.ko.*$//'`
824            j=`basename $k | sed 's/\.o.*$//'`
825            echo "$j" >> /tmp/modules
826        done
827    fi
828    # loading forced modules first
829    for j in `cat /tmp/modules`; do
830        echo "$FORCE_MODS" | grep -q "$j "
831        if [ $? -eq 0 ]; then
832            echo "Forcing first $j..."
833            modprobe -q $j 2> /dev/null
834        fi
835    done
836    for j in `cat /tmp/modules`; do
837        echo "$DENY_MODS" | grep -q "$j "
838        if [ $? -eq 0 ]; then
839            echo "Denying $j..."
840            continue
841        fi
842        echo "$FORCE_MODS" | grep -q "$j "
843        if [ $? -eq 0 ]; then
844            continue
845        fi
846        echo "Probing $j..."
847        modprobe -q $j 2> /dev/null
848    done
849}
850
851UdevadmTrigger() {
852
853# This function should just echo the return value $ua
854
855if [ -x /sbin/udevadm ]; then
856    ua="/sbin/udevadm"
857elif [ -x /usr/bin/udevadm ]; then
858    ua="/usr/bin/udevadm"
859else
860    LogIt "WARNING: Unable to find udevadm. Not triggering udev"
861    ua="/bin/true"
862fi
863
864v="`$ua --version`"
865
866if [ -x "$v" ]; then
867    LogIt "Triggering udev again..."
868    if [ "$v" -ge "146" -a "$v" -lt "174" ]; then
869        $ua trigger --type=failed
870    elif [ "$v" -lt "146" ]; then
871        $ua trigger --retry-failed
872    else
873        LogIt "... not needed"
874    fi
875fi
876echo $ua
877}
878
879ExtractDataDisksAndLoadModules() {
880    LogIt "Installing additional tools ..." 1
881    # TODO: test return value
882    install-additional-tools
883
884    # Retry failed udev events now that local filesystems are mounted read-write
885    # (useful for rules creating network ifcfg files)
886    if [ -e "/tmp/USE-UDEV" ] ; then
887        ua=`UdevadmTrigger`
888    fi
889}
890
891# ------------------------ main -----------------------
892
893MINDI_VER=PBVER
894MINDI_REV=PBREV
895trap CaughtSoftReset SIGTERM
896trap GetShell SIGINT
897LOGFILE=/var/log/mondorestore.log
898PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/mondo:/usr/games
899GROOVY=/tmp/groovy-stuff
900USER=root
901ARCH=`uname -m`
902
903export PATH GROOVY USER LOGFILE ARCH
904
905mount -o remount,rw /
906rm -f $LOGFILE
907
908mkdir /proc 2> /dev/null
909mount /proc /proc -t proc
910
911# Creates log file
912LogIt ""
913# Backup fd
914exec 4>&2 5>&1
915# Redirect every message to the LOGFILE
916exec > >(tee -a ${LOGFILE})
917exec 2> >(tee -a ${LOGFILE} >&2)
918
919echo "Welcome to init (from mindi ${MINDI_VER}-r${MINDI_REV})"
920
921mkdir /sys 2> /dev/null
922mount /sys /sys -t sysfs
923
924[ ! "$GROOVY" ] && Die "I'm not groovy!"
925for i in $GROOVY /mnt/cdrom /mnt/isodir /var/local/petris /tmp/isodir; do
926    mkdir -p $i
927done
928# For ESX 3
929[ -d /proc/vmware ] && ! grep -q /vmfs /proc/mounts && mount -t vmfs /vmfs /vmfs
930
931if [ "`grep -i denymods /proc/cmdline`" ]; then
932    export DENY_MODS="`cat /proc/cmdline | sed 's~.*denymods=\"\(.*\)\".*~\1~'` mondonone"
933else
934    export DENY_MODS=" "
935fi
936if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2> /dev/null`" ]; then
937    # Do not try to load usb storage when dealing with OBDR it makes the modprobe hang :-(
938    export DENY_MODS="usb-storage $DENY_MODS"
939fi
940if [ "`grep -i forcemods /proc/cmdline`" ]; then
941    export FORCE_MODS="`cat /proc/cmdline | sed 's~.*forcemods=\"\(.*\)\".*~\1~'` mondonone"
942else
943    export FORCE_MODS=" "
944fi
945echo "Activating a potential USB keyboard/mouse"
946StartUSBKbd
947
948if [ -f /proc/sys/kernel/exec-shield ]; then
949    echo 0 > /proc/sys/kernel/exec-shield 2>> /dev/null
950fi
951
952if [ -f /proc/modules ]; then
953    echo "/sbin/modprobe" > /proc/sys/kernel/modprobe
954fi
955
956if [ -e "/tmp/USE-UDEV" ] ; then
957    RunUdevd
958fi
959ExtractDevTarballs
960LaunchTerminals
961
962# Keeping kernel silent for module insertion
963PKLVL=`cut -f1 /proc/sys/kernel/printk`
964echo 0 > /proc/sys/kernel/printk
965ModprobeAllModules
966echo $PKLVL > /proc/sys/kernel/printk
967
968EnableCcissIfAppropriate
969#-------------------------------
970#WHOLIVESINAPINEAPPLEUNDERTHESEA#;# --- don't touch this :)
971#-------------------------------
972UseTmpfs
973if [ ! -e "/tmp/mondorestore.cfg" ] ; then
974    LogIt "WARNING: /tmp/mondorestore.cfg not found - forging an empty one"
975    LogIt "         Hopefully this is a Mindi media"
976    touch /tmp/mondorestore.cfg
977fi
978if [ "`grep -i pxe /proc/cmdline`" ] || [ "`grep -i net /proc/cmdline`" ] || [ "`grep 'netfs-proto ' /tmp/mondorestore.cfg`" ]; then
979    # We need to get here exported variables from start-netfs
980    . /usr/sbin/start-netfs
981fi
982
983if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2>/dev/null`" ] || [ "`grep -i ' udev' /tmp/mondorestore.cfg 2>/dev/null`" ]; then
984    HandleTape
985    ExtractDataDisksAndLoadModules
986elif [ "`grep -i pxe /proc/cmdline`" ]; then
987    # Simulate a local CD
988    echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
989    CD_MOUNTED_OK=yes
990    ExtractDataDisksAndLoadModules
991    # Fake the conf file to force it to NFS mode, even if we made originally a CD (mandatory for mondorestore to work correctly)
992    sed -i "s/backup-media-type.*/backup-media-type netfs/" /tmp/mondorestore.cfg
993elif [ "`grep -i usb= /proc/cmdline`" ] || [ "`grep -i usb /tmp/mondorestore.cfg 2>/dev/null | grep media-type`" ]; then
994    . /usr/sbin/start-usb
995
996    # Simulate a local CD
997    echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
998    CD_MOUNTED_OK=yes
999    ExtractDataDisksAndLoadModules
1000else
1001    HandleCDROM
1002    ExtractDataDisksAndLoadModules
1003fi
1004res=$?
1005ConfigureLoggingDaemons
1006if [ -e "/tmp/USE-DEVFS" ] ; then
1007    umount -d /mnt/cdrom 2> /dev/null
1008    mv /dev/cdrom /cdrom.lnk 2> /dev/null
1009    CD_MOUNTED_OK=""
1010    RunDevfsd
1011fi
1012
1013if [ "`grep -i excludedevs /proc/cmdline`" ]; then
1014    export MINDI_EXCLUDE_DEVS="`cat /proc/cmdline | sed 's~.*excludedevs=\"\(.*\)\".*~\1~'` mondonone"
1015    for d in $MINDI_EXCLUDE_DEVS ; do
1016        echo "Mountlist exclusion == $d"
1017        if [ -e "/tmp/mountlist.txt" ]; then
1018            perl -i -ne 'print $_ unless m~$d~' /tmp/mountlist.txt
1019        fi
1020    done
1021else
1022    export MINDI_EXCLUDE_DEVS=" "
1023fi
1024
1025LoadKeymap
1026WelcomeMessage
1027RstHW
1028echo "Starting potential Raid/Multipath ..."
1029[ -e "/tmp/mountlist.txt" ] && cp -f /tmp/mountlist.txt /tmp/mountlist.original
1030PauseForRaids
1031StartRaids
1032StartMpath
1033# avoids messagesa round leaked fd - Cf lvm man page
1034export LVM_SUPPRESS_FD_WARNINGS=1
1035StartLvms
1036CopyBootDevEntry
1037mkdir -p /tmp/tmpfs
1038sleep 2
1039#clear
1040if [ -e "/dev/md0" ] && [ ! -e "/dev/md/0" ] && [ -e "/tmp/mountlist.txt" ] && [ "`grep /dev/md/ /tmp/mountlist.txt`" != "" ] ; then
1041    LogIt "Creating /dev/md/* softlinks just in case." 1
1042    mkdir -p /dev/md
1043    cp -af /dev/md0 /dev/md/0 2> /dev/null
1044    cp -af /dev/md1 /dev/md/1 2> /dev/null
1045    cp -af /dev/md2 /dev/md/2 2> /dev/null
1046fi
1047
1048if ! [ "`grep -i "pxe" /proc/cmdline`" ] ; then
1049    res="`cat /mnt/cdrom/archives/THIS-CD-NUMBER 2> /dev/null`"
1050    [ "$res" != "1" ] && [ "$res" != "" ] && Die "This is CD #$res in the series. Please insert CD #1."
1051    [ -e "/cdrom.lnk" ] && mv -f /cdrom.lnk /dev/cdrom && mount /dev/cdrom /mnt/cdrom && CD_MOUNTED_OK=yes
1052    [ "$CD_MOUNTED_OK" != "yes" ] && TryAgainToFindCD
1053    #mount | grep cdrom > /dev/null 2> /dev/null || mount /dev/cdrom /mnt/cdrom >> $LOGFILE 2>> $LOGFILE
1054fi
1055hack-cfg-if-necessary || LogIt "Cannot find hack-cfg-if-necessary"
1056
1057# Log some useful info
1058LogIt "init (from mindi v$MINDI_VER-r${MINDI_REV})"
1059LogIt "/proc/cmdline is:"
1060LogIt "----------"
1061cat /proc/cmdline  >> $LOGFILE
1062LogIt "----------"
1063LogIt "df result:"
1064LogIt "----------"
1065df >> $LOGFILE
1066LogIt "-------------"
1067LogIt "mount result:"
1068LogIt "-------------"
1069mount >> $LOGFILE
1070LogIt "-------------"
1071LogIt "lsmod result:"
1072LogIt "-------------"
1073lsmod >> $LOGFILE
1074LogIt "-------------"
1075LogIt "dmesg result:"
1076LogIt "-------------"
1077dmesg >> $LOGFILE
1078LogIt "-------------"
1079LogIt "/proc/mdstat:" >> $LOGFILE
1080LogIt "-------------"
1081cat /proc/mdstat  >> $LOGFILE
1082LogIt "-------------"
1083LogIt "/proc/swaps:" >> $LOGFILE
1084LogIt "-------------"
1085cat /proc/swaps  >> $LOGFILE
1086LogIt "-------------"
1087LogIt "/proc/filesystems:" >> $LOGFILE
1088LogIt "-------------"
1089cat /proc/filesystems  >> $LOGFILE
1090LogIt "-------------"
1091LogIt "/proc/partitions:" >> $LOGFILE
1092LogIt "-------------"
1093cat /proc/partitions  >> $LOGFILE
1094LogIt "-------------"
1095LogIt "fdisk result:" >> $LOGFILE
1096LogIt "-------------"
1097fdisk -l  >> $LOGFILE
1098LogIt "-------------"
1099if [ -f /tmp/mondorestore.cfg ]; then
1100    LogIt "/tmp/mondorestore.cfg result:" >> $LOGFILE
1101    LogIt "-------------"
1102    cat /tmp/mondorestore.cfg  >> $LOGFILE
1103fi
1104
1105ide-opt
1106
1107#ctrlaltdel soft
1108for i in null stdout stdin stderr ; do
1109    cp -af /dev/$i /tmp
1110done
1111# Not sure it's still needed, but shouldn't hurt
1112if [ -e "/tmp/USE-UDEV" ] ; then
1113    ua=`UdevadmTrigger`
1114fi
1115if grep "backup-media-type" /tmp/mondorestore.cfg > /dev/null 2> /dev/null ; then
1116    LogIt "backup-media-type is specified in config file - great."
1117    LogIt "Calling post-init"
1118    # start-netfs moved it under /tmp as the NFS share is already unmounted
1119    if [ _"$pre" != _"" ] && [ "`echo $pre | grep -E '^/tmp/isodir'`" ]; then
1120        pre=`echo $pre | sed 's|^/tmp/isodir|/tmp|'`
1121    fi
1122    if [ _"$pre" != _"" ] && [ -x $pre ]; then
1123        echo "Executing preliminary script $pre"
1124        LogIt "Executing preliminary script $pre"
1125        $pre
1126    fi
1127    post-init
1128else
1129    LogIt "backup-media-type is not specified in config file."
1130    LogIt "I think this media has no archives on it."
1131fi
1132if [ "`grep -i "post=" /proc/cmdline`" ] ; then
1133    for i in `cat /proc/cmdline` ; do
1134        echo $i | grep -qi post= && post=`echo $i | cut -d= -f2`
1135    done
1136    # start-netfs moved it under /tmp as the NFS share is already unmounted
1137    if [ "`echo $post | grep -E '^/tmp/isodir'`" ]; then
1138        post=`echo $post | sed 's|^/tmp/isodir|/tmp|'`
1139    fi
1140    if [ -r $post ]; then
1141        echo "Executing final script $post"
1142        LogIt "Executing final script $post"
1143        if [ "`echo $post | grep -E '^/mnt/RESTORING'`" ]; then
1144            mr-mount-me
1145        fi
1146        chmod 755 $post
1147        $post
1148        if [ "`echo $post | grep -E '^/mnt/RESTORING'`" ]; then
1149            mr-unmount-me
1150        fi
1151    fi
1152fi
1153if grep "RESTORE" /proc/cmdline > /dev/null 2> /dev/null ; then
1154    echo "Rebooting in 10 seconds automatically as per reboot order"
1155    echo -en "Press ^C to interrupt if you have to ..."
1156    for i in 1 2 3 4 5 6 7 8 9 10 ; do
1157        sleep 1
1158        echo -en "."
1159    done
1160    echo "Boom."
1161    sleep 1
1162else
1163    GetShell
1164fi
1165CaughtSoftReset
Note: See TracBrowser for help on using the repository browser.