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

Last change on this file since 3286 was 3286, checked in by Bruno Cornec, 7 years ago
  • Improve mondo-prep using mr_asprintf more (was behind other C modules). To be contiued
  • Property svn:keywords set to Id
File size: 31.5 KB
Line 
1#!/bin/bash
2#
3# $Id: rcS 3286 2014-05-03 08:13:12Z 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 ]; 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    mount -n -o size=5M,mode=0755 -t tmpfs none /dev
386    MakeExtraNodes
387    mount -n -t devpts -o mode=620 none /dev/pts
388    mount -n -t tmpfs  none /dev/shm
389    if [ -e /proc/sys/kernel/hotplug ]; then
390        echo > /proc/sys/kernel/hotplug
391    fi
392    PKLVL=`cut -f1 /proc/sys/kernel/printk`
393    echo 0 > /proc/sys/kernel/printk
394    # Many possibilities depending on udev versions
395    if [ -x /sbin/startudev ]; then
396        /sbin/startudev
397    fi
398    if [ -x /sbin/start_udev ]; then
399        /sbin/start_udev
400    fi
401    # Depending on udevd version it gives back the hand or not :-(
402    ps | grep -v grep |grep udevd 2> /dev/null 1> /dev/null
403    if [ $? -ne 0 ]; then
404        echo "Waiting for udev to start..."
405        if [ -x /sbin/udevd ]; then
406            /sbin/udevd --daemon &
407        elif [ -x /usr/bin/udevd ]; then
408            /usr/bin/udevd --daemon &
409        elif [ -x /usr/lib/systemd/systemd-udevd ]; then
410            # from https://github.com/hut/minirc/blob/master/rc
411            echo "INFO: This is the systemd version of udev"
412            mkdir -p /run/systemd/journal
413            mount -n -t tmpfs tmpfs /sys/fs/cgroup
414            #mount -n -t cgroup cgroup /sys/fs/cgroup/systemd
415            /usr/lib/systemd/systemd-udevd --daemon
416            udevadm trigger --action=add --type=subsystems
417            udevadm trigger --action=add --type=devices
418        fi
419        sleep 5
420        LogIt "udev started manually" 1
421    fi
422    mkdir -p /dev/.udev/queue/
423    if [ -x /sbin/udevtrigger ]; then
424        /sbin/udevtrigger
425    fi
426    echo "Waiting for udev to discover..."
427    CreateDevMakedev
428    # Newer version use udevadm for that
429    if [ -x /sbin/udevsettle ]; then
430        /sbin/udevsettle --timeout=10
431        LogIt "Discovering with udevsettle"
432    elif [ -x /sbin/udevadm ] || [ -x /usr/bin/udevadm ]; then
433        ua=`UdevadmTrigger`
434        $ua settle --timeout=10
435        LogIt "Discovering with udevadm"
436    fi
437    # It seems we need to have more static devs on some distro were
438    # udev as some other requirements to be covered later on.
439    # So in the mean time:
440    for d in `ls /dev.static`; do
441        if [ ! -e /dev/$d ]; then
442            mv /dev.static/$d /dev
443        fi
444    done
445    echo $PKLVL > /proc/sys/kernel/printk
446}
447
448RstHW() {
449
450    # Restore the HW configuration if available (NOT by default)
451    answer="NO"
452
453    grep -q nohw /proc/cmdline
454    if [ "$?" -eq 0 ]; then
455        return
456    fi
457    if [ -x ./mindi-rsthw ]; then
458        grep -q RESTORE /proc/cmdline
459        if [ "$?" -ne 0 ]; then
460            #clear
461            echo "*********************************************************************"
462            echo "Do you want to restore the HW configuration of the original machine ?"
463            echo "(This may dammage your hardware so be sure to check twice before saying yes)"
464            echo "*********************************************************************"
465            echo "Please confirm by typing YES exactly as written here (NO by default)"
466            echo -n "--> "
467            read answer
468        else
469            answer="YES"
470        fi
471        if [ "$answer" = "YES" ] ; then
472            ./mindi-rsthw
473        fi
474    fi
475}
476
477
478StartUSBKbd() {
479# Prepare minimal USB env in case we have USB kbd such as with iLO
480[ -d /proc/bus/usb ] && ! grep -q /proc/bus/usb /proc/mounts && mount -t usbfs none /proc/bus/usb
481echo "$DENY_MODS" | grep -Eq 'uhcd_hci |usbhid |usbcore '
482if [ $? -eq 0 ]; then
483    return
484fi
485modprobe -q uhcd_hci 2> /dev/null
486modprobe -q usbhid 2> /dev/null
487}
488
489
490
491StartLvms() {
492    if [ "`grep -i nolvm /proc/cmdline`" ]; then
493        return;
494    fi
495    if [ -e "/tmp/i-want-my-lvm" ] ; then
496        # Recent LVM need this caching daemon to work
497        if [ -x /usr/sbin/lvmetad ]; then
498            mkdir -p /run/lvm
499            /usr/sbin/lvmetad
500        fi
501        LogIt "Scanning LVM's..." 1
502        if which lvm ; then
503            modprobe -q dm-mod 2> /dev/null
504            modprobe -q dm_mod 2> /dev/null
505            lvm vgscan --mknodes
506        else
507            vgscan
508        fi
509
510        # Exclude devices we may not want
511        rm -f /tmp/restorevgs
512        for d in $MINDI_EXCLUDE_DEVS ; do
513            echo "LVM exclusion == $d"
514            # LVM: $4 to be checked if changed
515            EXCLUDE_VGS=`grep " $d" /tmp/i-want-my-lvm | grep vgcreate | awk '{print $4}'`
516            vg=`echo $EXCLUDE_VGS | sed "s/ /|/g"`
517            if [ "$vg" != "" ]; then
518                re=" $d|$vg"
519            else 
520                re=" $d"
521            fi
522            # Remove VGs from i-want-my-lvm
523            grep -Ev "$re" /tmp/i-want-my-lvm > /tmp/i-want-my-lvm.new
524            mv /tmp/i-want-my-lvm.new /tmp/i-want-my-lvm
525            # Prepare  script to restore the VG exluded here if needed
526            for v in $EXCLUDE_VGS; do
527                echo "vgcfgrestore $v" >> /tmp/restorevgs
528                # Remove LVs from mountlist
529                # LVM: sed to be checked if changed
530                EXCLUDE_LVS=`grep " $v" /tmp/i-want-my-lvm | grep lvcreate | sed "s/^.*-n \([^ ][^ ]*\) .*$/$1/"`
531                for l in $EXCLUDE_LVS; do
532                    # FIXME: Should search for all possible device names here
533                    if [ -f "/tmp/mountlist.txt" ]; then
534                        grep -Ev "/dev/$v/$l" /tmp/mountlist.txt > /tmp/mountlist.txt.new
535                        grep -Ev "/dev/mapper/${v}-$l" /tmp/mountlist.txt.new > /tmp/mountlist.txt
536                    fi
537                done
538            done
539        done
540
541        # LVM: remove #
542        grep -E "^#.*vgchange" /tmp/i-want-my-lvm | sed "s/^#[ ]*//" > /tmp/start-lvm
543        chmod +x /tmp/start-lvm
544        echo -en "Starting LVM's..."
545        /tmp/start-lvm &
546        for i in 1 2 3 4 5 ; do
547            echo -en "."
548            sleep 1
549        done
550        echo "Done."
551    fi
552# If necessary, cannibalize 'analyze-my-lvm'; copy some of its code here,
553# pipe vgscan's output, strip it, run 'vgchange' on its output, etc.etc.
554    LogIt "LVM's have been started."
555}
556
557StartPowerPath() {
558
559    # Taken from the init script of EMC PowerPath on RHEL
560    if [ -f /etc/emcp_devicesDB.dat ]; then 
561        if [ -f /etc/emcp_devicesDB.idx ]; then
562            /etc/opt/emcpower/emcpmgr map -p > /dev/null 2>&1
563        fi
564    fi
565
566    /sbin/powermt config > /dev/null 2>&1
567    #   Wait for udev to finish creating emcpower devices
568    #
569    pdfound=1
570    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
571        pdfound=1
572        for pd in `/bin/ls -d /sys/block/emcpower* 2> /dev/null`; do
573            bpd=`basename $pd`
574            if [ ! -e /dev/$bpd ]; then
575                pdfound=0
576                sleep 2
577                break;
578            fi
579        done
580        if [ "$pdfound" -eq 1 ]; then
581            break
582        fi
583    done
584    if [ "$pdfound" -eq 0 ]; then
585        echo "Unable to start PowerPath"
586    fi
587
588    /sbin/powermt load      > /dev/null 2>&1
589    /etc/opt/emcpower/emcpmgr map   > /dev/null 2>&1
590    /etc/opt/emcpower/powercf -C    > /dev/null 2>&1
591    if [ ! -e /etc/powermt.custom ]; then 
592        /sbin/powermt save      > /dev/null 2>&1
593    fi 
594    /sbin/powermt register  > /dev/null 2>&1
595    /sbin/powermig transition -startup -noprompt    > /dev/null 2>&1
596}
597
598StartMpath() {
599    if [ "`grep -i nompath /proc/cmdline`" ]; then
600        return;
601    fi
602    if [ -f "/tmp/mountlist.txt" ] && [ "`grep mpath /tmp/mountlist.txt`" ]; then
603        if [ -x /sbin/multipath ]; then
604            echo "Starting Mpath..."
605            mkdir -p /var/lib/multipath
606            cat > /etc/multipath.conf << EOF
607defaults {
608        user_friendly_names yes
609}
610EOF
611            /sbin/multipath -v 0
612            if [ -x /sbin/kpartx ]; then
613                /sbin/dmsetup ls --target multipath --exec "/sbin/kpartx -a -p p"
614            fi
615            LogIt "Multipath started"
616        fi
617    fi
618}
619
620StartRaids() {
621    local raid_devices i
622
623    if [ "`grep -i noraid /proc/cmdline`" ]; then
624        return;
625    fi
626
627    if [ -f "/tmp/mountlist.txt" ]; then
628        raid_devices=`grep /dev/md /tmp/mountlist.txt | cut -d' ' -f1`
629    else
630        raid_devices=""
631    fi
632
633    if which raidstart > /dev/null 2> /dev/null ; then
634        for i in $raid_devices ; do
635            if grep `basename $i` /proc/mdstat > /dev/null 2> /dev/null ; then
636                LogIt "$i is started already; no need to run 'raidstart $i'" 1
637            else
638                LogIt "Running 'raidstart $i'" 1
639                raidstart $i
640            fi
641        done
642    elif which mdrun > /dev/null 2> /dev/null ; then
643        if [ "`grep -i nomd /proc/cmdline`" ]; then
644            return;
645        fi
646        LogIt "Running 'mdrun'" 1
647        mdrun
648    elif which mdadm > /dev/null 2> /dev/null ; then
649        if [ "`grep -i nomd /proc/cmdline`" ]; then
650            return;
651        fi
652        LogIt "Running 'mdadm'" 1
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 'mdadm $i'" 1
656            else
657                if [ -f /etc/mdadm.conf ] ; then
658                    LogIt "Running 'mdadm $i' with user supplied /etc/mdadm.conf" 1
659                    mdadm -A $i -c /etc/mdadm.conf
660                elif [ -f /etc/mdadm/mdadm.conf ] ; then
661                    LogIt "Running 'mdadm $i' with user supplied /etc/mdadm/mdadm.conf" 1
662                    mdadm -A $i -c /etc/mdadm/mdadm.conf
663                else
664                    LogIt "Running 'mdadm $i'" 1
665                    mdadm -Ac partitions -m dev $i
666                fi
667            fi
668        done
669    elif which dmraid > /dev/null 2> /dev/null ; then
670        if [ "`grep -i nodmraid /proc/cmdline`" ]; then
671            return;
672        fi
673        for x in $(/sbin/dmraid -ay -i -p -t 2>/dev/null | grep -E  -iv "^no " |awk -F ':' '{ print $1 }') ; do
674            echo "Please send the result of /sbin/dmraid -ay -i -p -t to the devteam !!!"
675            return
676            #dmname=$(resolve_dm_name $x)
677            #[ -z "$dmname" ] && continue
678            #/sbin/dmraid  -ay -i -p "$dmname" >/dev/null 2>&1
679            #/sbin/kpartx -a -p p "/dev/mapper/$dmname"
680        done
681    else
682        LogIt "Warning: Neither 'raidstart' nor 'mdrun''found. RAID devices may not have started." 1
683    fi
684
685}
686
687
688TryAgainToFindCD() {
689    local res
690    mount | grep /mnt/cdrom && return 0
691    [ "`grep "backup_media_type" /tmp/mondorestore.cfg 2> /dev/null | grep "cdstream"`" ] && return
692    LogIt "Trying to mount CD-ROM a 2nd time..."
693    find-and-mount-cdrom --second-try
694    res=$?
695    if [ "$res" -eq "0" ] ; then
696        CD_MOUNTED_OK=yes
697        LogIt "CD-ROM drive mounted successfully." 1
698    else
699        LogIt "I still cannot find or mount the CD-ROM drive, by the way."
700    fi
701}
702
703
704
705UseTmpfs()
706{
707    local mount_cmd
708    echo -en "Mounting /tmp/tmpfs..."
709    mkdir -p /tmp/tmpfs
710# For technical reasons, some sets are as large as 16MB.
711# I am allowing 32MB because selective restore occupies a lot of space.
712    for size in 256m 128m 64m 48m 40m 32m ; do
713        mount_cmd="mount /dev/shm -t tmpfs -o size=$size" ; # was 34m until 04/2003
714        LogIt "Trying '$mount_cmd'"
715        $mount_cmd /tmp/tmpfs 2>> $LOGFILE
716        res=$?
717        [ "$res" -eq "0" ] && break
718    done
719    if [ "$res" -ne "0" ] ; then
720        LogIt "Failed. I could not run '$mount_cmd /tmp/tmpfs'. Your kernel is BROKEN or you do not have enough RAM." 1
721        umount /tmp/tmpfs > /dev/null 2> /dev/null
722        rmdir /tmp/tmpfs
723        ln -sf /mnt/RESTORING/tmp /tmp/tmpfs; # used by mondorestore
724        LogIt "Failed to mount /tmp/tmpfs; using ugly softlink instead"
725    else
726        LogIt "Great. Pivot succeeded w/ size=$size" 1
727        echo -en "Pivoting /tmp..."
728        umount /tmp/tmpfs
729        mkdir -p /tmp.old
730        mv /tmp/* /tmp.old/
731        # Try to Deal with a busybox bug on inexistant links
732        cp /tmp/* /tmp.old/
733        rm -f /tmp/*
734        $mount_cmd /tmp
735        mv /tmp.old/* /tmp/
736        # Try to Deal with a busybox bug on inexistant links
737        cp /tmp.old/* /tmp/
738        rm -rf /tmp.old
739        mkdir -p /tmp/tmpfs
740        mkdir -p $GROOVY
741    echo "Done."
742    LogIt "Successfully mounted dynamic /tmp ramdisk"
743#   mkdir -p /tmp/tmpfs/var
744#        mv -f /var/log /tmp/tmpfs/var/log
745#        ln -sf /tmp/tmpfs/var/log /var/log
746    fi
747}
748
749
750WelcomeMessage()
751{
752    echo "********************************************************************"
753    echo "MINDI-LINUX by Mondo Dev Team - web site: http://www.mondorescue.org"
754    echo "Boot disk based on AlfaLinux & Trinux. BusyBox by Erik Andersen."
755which petris > /dev/null 2> /dev/null && echo "Petris was written by Peter Seidler <p.seidler@mail1.stofanet.dk>."
756    echo "Executables and source code are covered by the GNU GPL. No warranty."
757    echo "running on $ARCH"
758    echo "********************************************************************"
759}
760
761EnableCcissIfAppropriate() {
762    local f
763
764    # In some cases, the OBDR tape remains in CD-ROM mode. This branch solves that issue (#498)
765    if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2>/dev/null`" ] && [ -x /usr/bin/hpsa_obdr_mode ]; then
766        for lun in `/usr/bin/hpsa_obdr_mode -m query /dev/cciss/* 2>&1 | grep "is an OBDR device" | cut -d' ' -f4`; do
767            /usr/bin/hpsa_obdr_mode -m query -l $lun /dev/cciss/* 2>&1 | grep -q "is in CD-ROM mode"
768            if [ $? -eq 0 ]; then
769                # This tape/lun should be brought back in Sequential mode
770                LogIt "Putting back tape $lun in Sequential mode..."
771                /usr/bin/hpsa_obdr_mode -m tape -l $lun /dev/cciss/* 2>&1 > /dev/null
772            fi
773        done
774    fi
775
776    # Now engage all Smart Array controllers to detect attached devices
777    if [ -d  "/proc/driver/cciss" ]; then
778        for f in /proc/driver/cciss/cciss* ; do
779            LogIt "Engaging $f"
780            echo "engage scsi" > $f
781            LogIt "...result=$?"
782        done
783    fi
784}
785
786ModprobeAllModules() {
787
788    echo "Searching for modules to install..."
789    lismod=`find /lib/modules -name '*\.ko*' -o -name '*\.o*'`
790    # loading forced modules first
791    for m in $lismod; do
792        k=`basename $m | sed 's/\.ko.*$//'`
793        j=`basename $k | sed 's/\.o.*$//'`
794        echo "$FORCE_MODS" | grep -q "$j "
795        if [ $? -eq 0 ]; then
796            echo "Forcing first $j..."
797            modprobe -q $j 2> /dev/null
798        fi
799    done
800    for m in $lismod; do
801        k=`basename $m | sed 's/\.ko.*$//'`
802        j=`basename $k | sed 's/\.o.*$//'`
803        echo "$DENY_MODS" | grep -q "$j "
804        if [ $? -eq 0 ]; then
805            echo "Denying $j..."
806            continue
807        fi
808        echo "$FORCE_MODS" | grep -q "$j "
809        if [ $? -eq 0 ]; then
810            continue
811        fi
812        echo "Probing $j..."
813        modprobe -q $j 2> /dev/null
814    done
815}
816
817UdevadmTrigger() {
818
819# This function should just echo the return value $ua
820
821if [ -x /sbin/udevadm ]; then
822    ua="/sbin/udevadm"
823elif [ -x /usr/bin/udevadm ]; then
824    ua="/usr/bin/udevadm"
825else
826    LogIt "WARNING: Unable to find udevadm. Not triggering udev"
827    ua="/bin/true"
828fi
829
830v="`$ua --version`"
831
832if [ -x "$v" ]; then
833    LogIt "Triggering udev again..."
834    if [ "$v" -ge "146" -a "$v" -lt "174" ]; then
835        $ua trigger --type=failed
836    elif [ "$v" -lt "146" ]; then
837        $ua trigger --retry-failed
838    else
839        LogIt "... not needed"
840    fi
841fi
842echo $ua
843}
844
845ExtractDataDisksAndLoadModules() {
846    LogIt "Installing additional tools ..." 1
847    # BCO: test return value
848    install-additional-tools
849    # Keep the kernel silent again
850    PKLVL=`cut -f1 /proc/sys/kernel/printk`
851    echo 0 > /proc/sys/kernel/printk
852    ModprobeAllModules
853    echo $PKLVL > /proc/sys/kernel/printk
854
855    # Retry failed udev events now that local filesystems are mounted read-write
856    # (useful for rules creating network ifcfg files)
857    if [ -e "/tmp/USE-UDEV" ] ; then
858        ua=`UdevadmTrigger`
859    fi
860}
861
862# ------------------------ main -----------------------
863
864MINDI_VER=PBVER
865MINDI_REV=PBREV
866trap CaughtSoftReset SIGTERM
867trap GetShell SIGINT
868LOGFILE=/var/log/mondorestore.log
869PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/mondo:/usr/games
870GROOVY=/tmp/groovy-stuff
871USER=root
872ARCH=`uname -m`
873
874export PATH GROOVY USER LOGFILE ARCH
875
876mount -o remount rw /
877rm -f $LOGFILE
878
879mkdir /proc 2> /dev/null
880mount /proc /proc -t proc
881
882# Creates log file
883LogIt ""
884# Backup fd
885exec 4>&2 5>&1
886# Redirect every message to the LOGFILE
887exec > >(tee -a ${LOGFILE})
888exec 2> >(tee -a ${LOGFILE} >&2)
889
890echo "Welcome to init (from mindi ${MINDI_VER}-r${MINDI_REV})"
891
892mkdir /sys 2> /dev/null
893mount /sys /sys -t sysfs
894
895[ ! "$GROOVY" ] && Die "I'm not groovy!"
896for i in $GROOVY /mnt/cdrom /mnt/isodir /var/local/petris /tmp/isodir; do
897    mkdir -p $i
898done
899# For ESX 3
900[ -d /proc/vmware ] && ! grep -q /vmfs /proc/mounts && mount -t vmfs /vmfs /vmfs
901
902if [ "`grep -i denymods /proc/cmdline`" ]; then
903    export DENY_MODS="`cat /proc/cmdline | sed 's~.*denymods=\"\(.*\)\".*~\1~'` mondonone"
904else
905    export DENY_MODS=" "
906fi
907if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2> /dev/null`" ]; then
908    # Do not try to load usb storage when dealing with OBDR it makes the modprobe hang :-(
909    export DENY_MODS="usb-storage $DENY_MODS"
910fi
911if [ "`grep -i forcemods /proc/cmdline`" ]; then
912    export FORCE_MODS="`cat /proc/cmdline | sed 's~.*forcemods=\"\(.*\)\".*~\1~'` mondonone"
913else
914    export FORCE_MODS=" "
915fi
916if [ "`grep -i excludedevs /proc/cmdline`" ]; then
917    export MINDI_EXCLUDE_DEVS="`cat /proc/cmdline | sed 's~.*excludedevs=\"\(.*\)\".*~\1~'` mondonone"
918    for d in $MINDI_EXCLUDE_DEVS ; do
919        echo "Mountlist exclusion == $d"
920        if [ -e "/tmp/mountlist.txt" ]; then
921            perl -i -ne 'print $_ unless m~$d~' /tmp/mountlist.txt
922        fi
923    done
924else
925    export MINDI_EXCLUDE_DEVS=" "
926fi
927
928echo "Activating a potential USB keyboard/mouse"
929StartUSBKbd
930
931if [ -f /proc/sys/kernel/exec-shield ]; then
932    echo 0 > /proc/sys/kernel/exec-shield 2>> /dev/null
933fi
934
935if [ -f /proc/modules ]; then
936    echo "/sbin/modprobe" > /proc/sys/kernel/modprobe
937fi
938
939if [ -e "/tmp/USE-UDEV" ] ; then
940    RunUdevd
941fi
942ExtractDevTarballs
943LaunchTerminals
944
945# Keeping kernel silent for module insertion
946PKLVL=`cut -f1 /proc/sys/kernel/printk`
947echo 0 > /proc/sys/kernel/printk
948ModprobeAllModules
949echo $PKLVL > /proc/sys/kernel/printk
950
951EnableCcissIfAppropriate
952#-------------------------------
953#WHOLIVESINAPINEAPPLEUNDERTHESEA#;# --- don't touch this :)
954#-------------------------------
955UseTmpfs
956if [ ! -e "/tmp/mondorestore.cfg" ] ; then
957    LogIt "Warning - /tmp/mondorestore.cfg not found"
958fi
959if [ "`grep -i pxe /proc/cmdline`" ] || [ "`grep -i net /proc/cmdline`" ]; then
960    # We need to get here exported variables from start-netfs
961    . /sbin/start-netfs
962fi
963
964if [ "`grep -i 'obdr ' /tmp/mondorestore.cfg 2>/dev/null`" ] || [ "`grep -i ' udev' /tmp/mondorestore.cfg 2>/dev/null`" ]; then
965    HandleTape
966    ExtractDataDisksAndLoadModules
967elif [ "`grep -i pxe /proc/cmdline`" ]; then
968    # Simulate a local CD
969    echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
970    CD_MOUNTED_OK=yes
971    ExtractDataDisksAndLoadModules
972    # Fake the conf file to force it to NFS mode, even if we made originally a CD (mandatory for mondorestore to work correctly)
973    sed -i "s/backup-media-type.*/backup-media-type netfs/" /tmp/mondorestore.cfg
974elif [ "`grep -i usb= /proc/cmdline`" ] || [ "`grep -i usb /tmp/mondorestore.cfg 2>/dev/null | grep media-type`" ]; then
975    . /sbin/start-usb
976
977    # Simulate a local CD
978    echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
979    CD_MOUNTED_OK=yes
980    ExtractDataDisksAndLoadModules
981else
982    HandleCDROM
983    ExtractDataDisksAndLoadModules
984    # We need to get here exported variables from start-netfs
985    . /sbin/start-netfs
986fi
987res=$?
988ConfigureLoggingDaemons
989if [ -e "/tmp/USE-DEVFS" ] ; then
990    umount -d /mnt/cdrom 2> /dev/null
991    mv /dev/cdrom /cdrom.lnk 2> /dev/null
992    CD_MOUNTED_OK=""
993    RunDevfsd
994fi
995LoadKeymap
996WelcomeMessage
997RstHW
998echo "Starting potential Raid/Multipath ..."
999[ -e "/tmp/mountlist.txt" ] && cp -f /tmp/mountlist.txt /tmp/mountlist.original
1000PauseForRaids
1001StartRaids
1002StartMpath
1003StartLvms
1004CopyBootDevEntry
1005mkdir -p /tmp/tmpfs
1006sleep 2
1007#clear
1008if [ -e "/dev/md0" ] && [ ! -e "/dev/md/0" ] && [ -e "/tmp/mountlist.txt" ] && [ "`grep /dev/md/ /tmp/mountlist.txt`" != "" ] ; then
1009    LogIt "Creating /dev/md/* softlinks just in case." 1
1010    mkdir -p /dev/md
1011    cp -af /dev/md0 /dev/md/0 2> /dev/null
1012    cp -af /dev/md1 /dev/md/1 2> /dev/null
1013    cp -af /dev/md2 /dev/md/2 2> /dev/null
1014fi
1015
1016if ! [ "`grep -i "pxe" /proc/cmdline`" ] ; then
1017    res="`cat /mnt/cdrom/archives/THIS-CD-NUMBER 2> /dev/null`"
1018    [ "$res" != "1" ] && [ "$res" != "" ] && Die "This is CD #$res in the series. Please insert CD #1."
1019    [ -e "/cdrom.lnk" ] && mv -f /cdrom.lnk /dev/cdrom && mount /dev/cdrom /mnt/cdrom && CD_MOUNTED_OK=yes
1020    [ "$CD_MOUNTED_OK" != "yes" ] && TryAgainToFindCD
1021    #mount | grep cdrom > /dev/null 2> /dev/null || mount /dev/cdrom /mnt/cdrom >> $LOGFILE 2>> $LOGFILE
1022fi
1023hack-cfg-if-necessary || LogIt "Cannot find hack-cfg-if-necessary"
1024
1025# Log some useful info
1026LogIt "init (from mindi v$MINDI_VER-r${MINDI_REV})"
1027LogIt "/proc/cmdline is:"
1028LogIt "----------"
1029cat /proc/cmdline  >> $LOGFILE
1030LogIt "----------"
1031LogIt "df result:"
1032LogIt "----------"
1033df >> $LOGFILE
1034LogIt "-------------"
1035LogIt "mount result:"
1036LogIt "-------------"
1037mount >> $LOGFILE
1038LogIt "-------------"
1039LogIt "lsmod result:"
1040LogIt "-------------"
1041lsmod >> $LOGFILE
1042LogIt "-------------"
1043LogIt "dmesg result:"
1044LogIt "-------------"
1045dmesg >> $LOGFILE
1046LogIt "-------------"
1047LogIt "/proc/mdstat:" >> $LOGFILE
1048LogIt "-------------"
1049cat /proc/mdstat  >> $LOGFILE
1050LogIt "-------------"
1051LogIt "/proc/swaps:" >> $LOGFILE
1052LogIt "-------------"
1053cat /proc/swaps  >> $LOGFILE
1054LogIt "-------------"
1055LogIt "/proc/filesystems:" >> $LOGFILE
1056LogIt "-------------"
1057cat /proc/filesystems  >> $LOGFILE
1058LogIt "-------------"
1059LogIt "/proc/partitions:" >> $LOGFILE
1060LogIt "-------------"
1061cat /proc/partitions  >> $LOGFILE
1062LogIt "-------------"
1063LogIt "fdisk result:" >> $LOGFILE
1064LogIt "-------------"
1065fdisk -l  >> $LOGFILE
1066LogIt "-------------"
1067if [ -f /tmp/mondorestore.cfg ]; then
1068    LogIt "/tmp/mondorestore.cfg result:" >> $LOGFILE
1069    LogIt "-------------"
1070    cat /tmp/mondorestore.cfg  >> $LOGFILE
1071fi
1072
1073ide-opt
1074
1075#ctrlaltdel soft
1076for i in null stdout stdin stderr ; do
1077    cp -af /dev/$i /tmp
1078done
1079# Not sure it's still needed, but shouldn't hurt
1080if [ -e "/tmp/USE-UDEV" ] ; then
1081    ua=`UdevadmTrigger`
1082fi
1083if grep "backup-media-type" /tmp/mondorestore.cfg > /dev/null 2> /dev/null ; then
1084    LogIt "backup-media-type is specified in config file - great."
1085    LogIt "Calling post-init"
1086    # start-netfs moved it under /tmp as the NFS share is already unmounted
1087    if [ _"$pre" != _"" ] && [ "`echo $pre | grep -E '^/tmp/isodir'`" ]; then
1088        pre=`echo $pre | sed 's|^/tmp/isodir|/tmp|'`
1089    fi
1090    if [ _"$pre" != _"" ] && [ -x $pre ]; then
1091        echo "Executing preliminary script $pre"
1092        LogIt "Executing preliminary script $pre"
1093        $pre
1094    fi
1095    post-init
1096else
1097    LogIt "backup-media-type is not specified in config file."
1098    LogIt "I think this media has no archives on it."
1099fi
1100if [ "`grep -i "post=" /proc/cmdline`" ] ; then
1101    for i in `cat /proc/cmdline` ; do
1102        echo $i | grep -qi post= && post=`echo $i | cut -d= -f2`
1103    done
1104    # start-netfs moved it under /tmp as the NFS share is already unmounted
1105    if [ "`echo $post | grep -E '^/tmp/isodir'`" ]; then
1106        post=`echo $post | sed 's|^/tmp/isodir|/tmp|'`
1107    fi
1108    if [ -r $post ]; then
1109        echo "Executing final script $post"
1110        LogIt "Executing final script $post"
1111        if [ "`echo $post | grep -E '^/mnt/RESTORING'`" ]; then
1112            mount-me
1113        fi
1114        chmod 755 $post
1115        $post
1116        if [ "`echo $post | grep -E '^/mnt/RESTORING'`" ]; then
1117            unmount-me
1118        fi
1119    fi
1120fi
1121if grep "RESTORE" /proc/cmdline > /dev/null 2> /dev/null ; then
1122    echo "Rebooting in 10 seconds automatically as per reboot order"
1123    echo -en "Press ^C to interrupt if you have to ..."
1124    for i in 1 2 3 4 5 6 7 8 9 10 ; do
1125        sleep 1
1126        echo -en "."
1127    done
1128    echo "Boom."
1129    sleep 1
1130else
1131    GetShell
1132fi
1133CaughtSoftReset
Note: See TracBrowser for help on using the repository browser.