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

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