source: branches/3.2/mindi/rootfs/etc/init.d/rcS @ 3526

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

Forging a /tmp/mondorestore.cfg file at restore time if using mindi standalone in order to avoid error messages from grep.

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