source: trunk/mindi/rootfs/sbin/init @ 1106

Last change on this file since 1106 was 1106, checked in by bruno, 13 years ago

merge -r1082:1105 $SVN_M/branches/stable

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