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

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