source: branches/stable/mindi/rootfs/sbin/init @ 870

Last change on this file since 870 was 870, checked in by bruno, 13 years ago
  • Checks variables berfore getting them from env during restore
  • source start-nfs to get env. Var. correctly set for mondorestore
  • Property svn:keywords set to Id
File size: 15.1 KB
Line 
1#!/bin/sh
2#
3# $Id: init 870 2006-10-03 14:12:38Z bruno $
4#
5# init script launched during the restore process
6#------------------------------------------------------------
7
8
9CaughtSoftReset() {
10    trap SIGTERM
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 "OK."
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
49
50Die() {
51    LogIt "Fatal error! $1" 1
52    exit 1
53}
54
55
56
57ExtractDevTarballs() {
58    cd /
59    for fname in ataraid.tgz ida.tgz rd.tgz raw.tgz cciss.tgz nst.tgz dm.tgz vc.tgz ; do
60        if [ ! -e "/$fname" ] ; then
61            LogIt "/$fname not found; cannot extract to /." 1
62        else
63            echo -en "\rExtracting /$fname...     "
64            tar -zxf /$fname || LogIt "Error occurred while extracting /$fname"
65        fi
66    done
67    echo -en "\r"
68    LogIt "Extracted additional /dev entries OK.     " 1
69}
70
71
72
73LaunchTerminals() {
74    openvt 2 /bin/sh
75    openvt 3 /bin/sh
76    openvt 4 /bin/sh
77    openvt 5 /bin/sh
78    openvt 6 /bin/sh
79    openvt 7 /bin/sh /sbin/wait-for-petris
80}
81
82
83LoadKeymap() {
84    local fname
85    fname=`cat /tmp/KEYMAP-LIVES-HERE 2> /dev/null`
86    [ "$fname" = "" ] && return
87    if which loadkeys > /dev/null 2> /dev/null ; then
88        loadkeys $fname
89# loadkmap != loadkeys
90#    elif which loadkmap > /dev/null 2> /dev/null ; then
91#        loadkmap < $fname
92    else
93        LogIt "Using default keyboard map." 1
94    fi
95}
96
97
98UntarTapeStuff() {
99    local old_pwd res
100    old_pwd=`pwd`
101    cd $GROOVY
102    [ "$1" != "" ] && tapedev=$1
103    [ ! "$tapedev" ] && tapedev=`grep media-dev /tmp/mondo-restore.cfg | tr -s ' ' ' ' | cut -d' ' -f2`
104#    tar -zxf $tapedev
105    dd if=$tapedev bs=32k count=1024 | tar -zx
106    res=$?
107    if [ "$res" -eq "0" ] ; then
108    grep -v media-dev /tmp/mondo-restore.cfg > /tmp/mr.cfg
109    echo "media-dev $tapedev" >> /tmp/mr.cfg
110        cp -f /tmp/mr.cfg /tmp/mondo-restore.cfg
111    fi
112    cd $old_pwd
113    return $res
114}
115
116HandleTape() {
117    local res tapedev
118    tapedev="" ; # will be set by UntarTapeStuff()
119    cd $GROOVY
120    UntarTapeStuff $tapedev || UntarTapeStuff /dev/st0 || UntarTapeStuff /dev/osst0 || UntarTapeStuff /dev/ht0
121    res=$?
122    while [ "$res" -ne "0" ] ; do
123        LogIt "$tapedev failed to act as extended data disk for booting." 1
124        LogIt "Please specify an alternate tape device," 1
125        LogIt "or hit <Enter> to boot from CD/floppies." 1
126        echo -en "---> "
127        read tapedev
128        if [ "$tapedev" ] ; then
129            LogIt "User specified $tapedev instead"
130            UntarTapeStuff $tapedev
131            res=$?
132        else
133            LogIt "User opted not to specify an alternate tapedev"
134            res=1
135        break
136        fi
137    done
138
139    if [ "$res" -ne "0" ] ; then
140    cd /
141    LogIt "Failed to use tape as extended datadisk. Reverting to floppies." 1
142    HandleCDROMorFloppies
143    res=$?
144    else
145#   clear
146    LogIt "Using tape as extended datadisk. Good." 3
147    echo "Using tape as extd ddisk." > /tmp/TAPEDEV-HAS-DATA-DISKS
148    res=0
149        CD_MOUNTED_OK=yes
150    fi
151    return $res
152}
153
154
155
156HandleCDROMorFloppies() {
157    find-and-mount-cdrom
158    res=$?
159    if [ "$res" -ne "0" ] ; then
160    LogIt "First call to find-and-mount-cdrom failed." 1
161    LogIt "Sleeping for 3 seconds and trying again." 1
162    sleep 3
163    find-and-mount-cdrom
164    res=$?
165    fi
166    if [ "$res" -eq "0" ] ; then
167#        clear
168    LogIt "OK, I am running on a CD-ROM. Good." 3
169        CD_MOUNTED_OK=yes
170    elif [ "$res" -eq "1" ] ; then
171#        clear
172    LogIt "OK, I am running on floppies. Good." 3
173    CD_MOUNTED_OK=""
174    else
175    LogIt "OK, I am falling back to floppy mode." 3
176        LogIt "That means you'll have to copy the data disk images from" 3
177        LogIt "the CD/hard disk/whatever to physical 1.44MB disks and" 3
178        LogIt "insert them, one after the other. Please see the manual." 3
179        LogIt "The images are in /images on the CD, or /root/images/mindi" 3
180        LogIt "on your hard disk. Your archives are probably fine but" 3
181        LogIt "your tape streamer and/or CD-ROM drive are eccentric. :-)" 3
182    CD_MOUNTED_OK=""
183    fi
184    return 0
185}
186
187
188HowMuchFreeSpaceOnRamdisk() {
189   df -m -P | grep /dev/ram | head -n1 | tr -s '\t' ' ' | cut -d' ' -f4
190}
191
192
193
194InsertEssentialModules() {
195#   Load the VIA IDE module first thing if it exists (requires ide-core).
196#   This is to ensure that DMA is working for VIA chipsets with 2.6 kernels.
197    for module in /ide-core.ko /via82cxxx.ko; do
198        [ -f "$module" ] && MyInsmod $module > /dev/null 2> /dev/null
199    done
200#   Then load the remaining modules in normal (i.e. arbitrary) order.
201    for j in 1 2 3 4 5 ; do
202        for i in `ls /*.*o* 2> /dev/null` ; do
203            [ -f "$i" ] && MyInsmod $i > /dev/null 2> /dev/null
204        done
205    done
206}
207
208
209
210PauseForRaids() {
211    if [ "`dmesg | grep -i "RAID Controller"`" != "" ] || [ "`dmesg | grep -i "Vendor: 3ware"`" != "" ] ; then
212        LogIt "RAID controller(s) detected. Pausing 10 seconds to let them warm up." 1
213        echo -en "Pausing..."
214        for i in 1 2 3 4 5 6 7 8 9 10 ; do
215            sleep 1
216            echo -en "$(($i*10))%..."
217        done
218        echo "Done."
219    fi
220}
221
222
223# 06/13/2002 --- DuckX's & Hugo's new routine
224# 10/11/2003 --- added a simple devfsd.conf file
225# 01/24/2005 --- cater for arbitrary locations of devfsd.conf by using the
226#                config file path compiled into devfsd
227RunDevfsd() {
228    loc=`which devfsd 2> /dev/null`
229    if [ "$loc" != "" ] ; then
230    LogIt "Devfs found. Testing kernel support..."
231    if [ ! -e "/dev/.devfsd" ] ; then
232        mount -t devfs devfs /dev 2>> $LOGFILE
233            if [ "$?" -ne "0" ] ; then
234        LogIt "Error while trying to mount devfs"
235        else
236        LogIt "Devfs mounted OK"
237        fi
238    fi
239    #check if the kernel supports devfs
240    if [ -e "/dev/.devfsd" ] ; then
241        [ -d "/proc/1" ] || mount -n /proc
242        LogIt "Kernel support found. Creating config file and starting devfsd"
243        conffile=`strings $loc | grep "devfsd.conf$"`
244        [ "$conffile" ] || conffile="/etc/devfsd.conf"
245        confpath=`echo $conffile | sed "s/\/devfsd\.conf$//"`
246        [ -d "$confpath" ] || mkdir -p $confpath
247        echo -en "REGISTER .* MKOLDCOMPAT\nUNREGISTER .* RMOLDCOMPAT\n" > $conffile
248        devfsd /dev &
249        sleep 5
250    else
251        LogIt "No devfs kernel support."
252    fi
253    fi
254}
255
256
257
258
259
260
261#RunDevfsd() {
262#    loc=`which devfsd 2> /dev/null`
263#    if [ "$loc" != "" ] ; then
264#        LogIt "Starting devfsd"
265#        devfsd /dev &
266#        sleep 5
267#    fi
268#}
269
270
271
272SpaceTests() {
273    [ -e "/tmp/filelist.full" ] && cp /tmp/filelist.full /tmp/FLF
274    if [ "`HowMuchFreeSpaceOnRamdisk`" -le "3" ] ; then
275        LogIt "Ramdisk is a bit smaller than I would like." 1
276        LogIt "Please re-run Mondo/Mindi but edit /usr/local/mindi first" 1
277        LogIt "and set EXTRA_SPACE to something high, e.g. 8000" 1
278        Die "Aborting. Please reboot."
279    fi
280    rm -f /tmp/FLF
281}
282
283
284
285StartLvms() {
286    if [ -e "/tmp/i-want-my-lvm" ] ; then
287        LogIt "Scanning LVM's..." 1
288        if which lvm ; then
289            MyInsmod dm-mod
290            MyInsmod dm_mod
291            lvm vgscan --mknodes
292        else
293            vgscan
294        fi
295        grep -E "^#.*vgchange" /tmp/i-want-my-lvm | sed "s/^#[ ]*//" > /tmp/start-lvm
296        chmod +x /tmp/start-lvm
297        echo -en "Starting LVM's..."
298        /tmp/start-lvm &
299        for i in 1 2 3 4 5 ; do
300        echo -en "."
301        sleep 1
302        done
303        echo "Done."
304    fi
305# If necessary, cannibalize 'analyze-my-lvm'; copy some of its code here,
306# pipe vgscan's output, strip it, run 'vgchange' on its output, etc.etc.
307    LogIt "LVM's have been started."
308}
309
310
311StartRaids() {
312    local raid_devices i
313    if which raidstart > /dev/null 2> /dev/null ; then
314        raid_devices=`grep /dev/md /tmp/mountlist.txt | cut -d' ' -f1`
315        for i in $raid_devices ; do
316            if grep `basename $i` /proc/mdstat > /dev/null 2> /dev/null ; then
317                LogIt "$i is started already; no need to run 'raidstart $i'" 1
318            else
319                LogIt "Running 'raidstart $i'" 1
320                raidstart $i
321            fi
322        done
323    elif which mdrun > /dev/null 2> /dev/null ; then
324        LogIt "Running 'mdrun'" 1
325        mdrun
326    else
327        LogIt "Warning: Neither 'raidstart' nor 'mdrun''found. RAID devices may not have started." 1
328    fi
329
330}
331
332
333TryAgainToFindCD() {
334    local res
335    mount | grep /mnt/cdrom && return 0
336    [ "`grep "backup_media_type" /tmp/mondo-restore.cfg | grep "cdstream"`" ] && return
337    LogIt "Trying to mount CD-ROM a 2nd time..."
338    find-and-mount-cdrom --second-try
339    res=$?
340    if [ "$res" -eq "0" ] ; then
341        CD_MOUNTED_OK=yes
342        LogIt "CD-ROM drive mounted successfully." 1
343    else
344        LogIt "I still cannot find or mount the CD-ROM drive, by the way."
345    fi
346}
347
348
349
350UseTmpfs()
351{
352    local mount_cmd
353    echo -en "Mounting /tmp/tmpfs..."
354    mkdir -p /tmp/tmpfs
355# For technical reasons, some sets are as large as 16MB.
356# I am allowing 32MB because selective restore occupies a lot of space.
357    for size in 64m 48m 40m 32m ; do
358        mount_cmd="mount /dev/shm -t tmpfs -o size=$size" ; # was 34m until 04/2003
359        LogIt "Trying '$mount_cmd'"
360    $mount_cmd /tmp/tmpfs 2>> /$LOGFILE
361    res=$?
362    [ "$res" -eq "0" ] && break
363    done
364    if [ "$res" -ne "0" ] ; then
365        LogIt "Failed. I could not run '$mount_cmd /tmp/tmpfs'. Your kernel is BROKEN or you do not have enough RAM." 1
366        umount /tmp/tmpfs > /dev/null 2> /dev/null
367    rmdir /tmp/tmpfs
368        ln -sf /mnt/RESTORING/tmp /tmp/tmpfs; # used by mondo-restore
369    LogIt "Failed to mount /tmp/tmpfs; using ugly softlink instead"
370    else
371        LogIt "Great. Pivot succeeded w/ size=$size" 1
372        echo -en "Pivoting /tmp..."
373        umount /tmp/tmpfs
374        mkdir -p /tmp.old
375        mv -f /tmp/* /tmp.old/
376        $mount_cmd /tmp
377        mv /tmp.old/* /tmp/
378        rmdir /tmp.old
379        mkdir -p /tmp/tmpfs
380        mkdir -p $GROOVY
381    echo "Done."
382    LogIt "Successfully mounted dynamic /tmp ramdisk"
383#   mkdir -p /tmp/tmpfs/var
384#        mv -f /var/log /tmp/tmpfs/var/log
385#        ln -sf /tmp/tmpfs/var/log /var/log
386    fi
387}
388
389
390WelcomeMessage()
391{
392    echo "********************************************************************"
393    echo "MINDI-LINUX by Mondo Dev Team - web site: http://www.mondorescue.org"
394    echo "Boot disk based on AlfaLinux & Trinux. BusyBox by Erik Andersen."
395which petris > /dev/null 2> /dev/null && echo "Petris was written by Peter Seidler <p.seidler@mail1.stofanet.dk>."
396    echo "Executables and source code are covered by the GNU GPL. No warranty."
397    echo "running on $ARCH"
398    echo "********************************************************************"
399}
400
401
402EnableCcissIfAppropriate() {
403    local i fname
404    for i in 0 1 2 3 ; do
405    fname="/proc/driver/cciss/cciss$i"
406    if [ -e "$fname" ] ; then
407        LogIt "Engaging $fname"
408        echo "engage scsi" > $fname
409        LogIt "...result=$?"
410    fi
411    done
412}
413
414
415ExtractDataDisksAndLoadModules() {
416    echo "Installing additional tools ..."
417    install-additional-tools
418    echo "Inserting modules ..."
419    insert-all-my-modules > $LOGFILE 2> $LOGFILE
420}
421
422# ------------------------ main -----------------------
423
424trap CaughtSoftReset SIGTERM
425LOGFILE=/tmp/mondo-restore.log
426PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/mondo:/usr/local/mondo:/usr/local/mindi:.
427GROOVY=/tmp/groovy-stuff
428USER=root
429export PATH GROOVY USER
430
431mount -o remount rw /
432[ ! "$GROOVY" ] && Die "I'm not groovy!"
433for i in $GROOVY /mnt/floppy /mnt/cdrom /mnt/isodir /var/local/petris /tmp/isodir; do
434    mkdir -p $i
435done
436#/bin/update
437mount /proc/ /proc -v -t proc
438mkdir /sys 2> /dev/null
439mount /sys/ /sys -v -t sysfs 2>> $LOGFILE
440rm -f /foozero
441
442echo 0 > /proc/sys/kernel/exec-shield 2>> /dev/null
443
444ExtractDevTarballs
445LaunchTerminals
446InsertEssentialModules
447EnableCcissIfAppropriate
448#-------------------------------
449#WHOLIVESINAPINEAPPLEUNDERTHESEA#;# --- don't touch this :)
450#-------------------------------
451UseTmpfs
452if [ ! -e "/tmp/mondo-restore.cfg" ] ; then
453    LogIt "Warning - /tmp/mondo-restore.cfg not found"
454fi
455
456if [ "`grep -i 'tape ' /tmp/mondo-restore.cfg`" ] || [ "`grep -i udev /tmp/mondo-restore.cfg`" ] ; then
457    HandleTape
458    ExtractDataDisksAndLoadModules
459elif [ "`grep -i pxe /proc/cmdline`" ]; then
460    # We need to get here exported variables from start-nfs
461    . /sbin/start-nfs
462
463    # Simulate a local CD
464    echo "/mnt/cdrom" > /tmp/CDROM-LIVES-HERE
465    CD_MOUNTED_OK=yes
466    ExtractDataDisksAndLoadModules
467else
468    HandleCDROMorFloppies
469    ExtractDataDisksAndLoadModules
470    # We need to get here exported variables from start-nfs
471    . /sbin/start-nfs
472fi
473res=$?
474ConfigureLoggingDaemons
475if [ -e "/tmp/USE-DEVFS" ] ; then
476    umount /mnt/cdrom 2> /dev/null
477    mv /dev/cdrom /cdrom.lnk 2> /dev/null
478    CD_MOUNTED_OK=""
479    RunDevfsd
480fi
481echo "Starting potential Raid/LVMs ..."
482PauseForRaids
483StartRaids
484StartLvms
485CopyBootDevEntry
486LoadKeymap
487mkdir -p /tmp/tmpfs
488sleep 2
489#clear
490if [ -e "/dev/md0" ] && [ ! -e "/dev/md/0" ] && [ "`grep /dev/md/ /tmp/mountlist.txt`" != "" ] ; then
491    LogIt "Creating /dev/md/* softlinks just in case." 1
492    mkdir -p /dev/md
493    cp -af /dev/md0 /dev/md/0 2> /dev/null
494    cp -af /dev/md1 /dev/md/1 2> /dev/null
495    cp -af /dev/md2 /dev/md/2 2> /dev/null
496fi
497export ARCH=`uname -m`
498WelcomeMessage
499# SpaceTests; # Mandrake Linux 8.1 doesn't like this
500[ -e "/tmp/mountlist.txt" ] && cp -f /tmp/mountlist.txt /tmp/mountlist.original
501
502if ! [ "`grep -i "pxe" /proc/cmdline`" ] ; then
503    res="`cat /mnt/cdrom/archives/THIS-CD-NUMBER 2> /dev/null`"
504    [ "$res" != "1" ] && [ "$res" != "" ] && Die "This is CD #$res in the series. Please insert CD #1."
505    [ -e "/cdrom.lnk" ] && mv -f /cdrom.lnk /dev/cdrom && mount /dev/cdrom /mnt/cdrom && CD_MOUNTED_OK=yes
506    [ "$CD_MOUNTED_OK" != "yes" ] && TryAgainToFindCD
507    #mount | grep cdrom > /dev/null 2> /dev/null || mount /dev/cdrom /mnt/cdrom >> $LOGFILE 2>> $LOGFILE
508fi
509hack-cfg-if-necessary || LogIt "Cannot find hack-cfg-if-necessary"
510openvt 8 /usr/bin/tail -f $LOGFILE
511
512#-------------------------------
513#ABSORBENTANDYELLOWANDPOROUSISHE#;# --- don't touch this either :)
514#-------------------------------
515
516lsmod > /tmp/ramdisk-lsmod.txt 2> /dev/null
517cut -d' ' -f1 /tmp/original-lsmod.txt /tmp/original-lsmod.txt /tmp/ramdisk-lsmod.txt | sort | uniq -d > /tmp/missing-modules.txt
518echo "Warning - these modules did not make it onto the ramdisk" >> $LOGFILE
519cat /tmp/missing-modules.txt >> $LOGFILE
520
521#ctrlaltdel soft
522for path in /usr.bin /usr/bin ; do
523    fname=$path/nano
524    [ -e "$fname" ] && ln -sf $fname /usr/bin/pico
525done
526res=`which nano 2> /dev/null`
527[ "$res" ] && ln -sf /usr/bin/
528for i in null stdout stdin stderr ; do
529    cp -af /dev/$i /tmp
530done
531if grep "backup-media-type" /tmp/mondo-restore.cfg > /dev/null 2> /dev/null ; then
532    LogIt "backup-media-type is specified in config file - great."
533    LogIt "Calling post-init $mountlist"
534    post-init
535else
536    LogIt "backup-media-type is not specified in config file."
537    LogIt "I think this CD/floppy has no archives on it."
538fi
539echo -en "Type 'exit' to reboot the PC\r"
540umount /mnt/cdrom 2> /dev/null
541mount / -o rw,remount > /dev/null 2> /dev/null
542sh
543CaughtSoftReset
544# reboot
Note: See TracBrowser for help on using the repository browser.