source: branches/2.2.4/mindi/mindi @ 1518

Last change on this file since 1518 was 1518, checked in by Bruno Cornec, 13 years ago

Add support for scsi_transport_spi to mindi

  • Property svn:keywords set to Rev Id
File size: 113.3 KB
Line 
1#!/bin/bash
2
3# $Id: mindi 1518 2007-06-26 11:56:06Z bruno $
4#
5#-----------------------------------------------------------------------------
6# mindi - mini-Linux distro based on the user's filesystem & distribution
7#
8# Mindi can create a multi-floppy boot/root kit. The first floppy is the boot
9# disk: it contains a kernel, a ramdisk etc. The second disk is data disk #1;
10# the third disk is data disk #2; and so it goes.
11#
12# See http://www.mondorescue.org for details.
13#-----------------------------------------------------------------------------
14
15### Which arch are we on (useful for ia64 port)
16ARCH=`/bin/arch`
17
18ADDITIONAL_BOOT_PARAMS="devfs=nomount noresume selinux=0 barrier=off"
19
20#RUN_AFTER_INITIAL_BOOT_PHASE="echo \"Who lives in a pineapple under the sea?\" > /tmp/spongebob.squarepants.txt"
21    # after booting from floppy/CD image but before
22    # accessing auxiliary data disks (or the rest of
23    # the CD), the initscript will run the above command.
24
25RUN_AFTER_BOOT_PHASE_COMPLETE="ide-opt"
26    # after the emergency boot process is complete but
27    # before the init script runs mondorestore, it will
28    # run this command.
29
30MINDI_REV=RRR
31MINDI_VER=VVV
32
33MINDI_VERSION=${MINDI_VER}-r$MINDI_REV
34MINDI_PREFIX=XXX
35MINDI_CONF=YYY
36MINDI_SBIN=${MINDI_PREFIX}/sbin
37MINDI_LIB=LLL
38
39# Temporary directory for mindi
40TMPDIR=/tmp
41
42EXTRA_SPACE=24576         ; # increase if you run out of ramdisk space
43BOOT_SIZE=8192       ;      # size of the boot disk
44WRITE_BOOT_FLOPPIES="yes" ; # do you want to be propted to write floppy images
45PROMPT_WRITE_BOOT_FLOPPIES="yes"
46    # do you want to be prompted to write
47    # floppy images out to floppy disks?
48    # if 'no', images will not be written to floppies
49
50PROMPT_MAKE_CD_IMAGE="yes"
51    # Ask if you want to make a CD Image to be written?
52    # if this is set to 'no', then the image will be created automatically
53
54USE_OWN_KERNEL="yes"
55    # If set to "no", you will be prompted for whether or not
56    # you want to use your own kernel, or the supplied default.
57    # If "yes" mindi will automatically use your own kernel.
58
59MY_FSTAB=/etc/fstab
60FLOPPY_MODS="ide-floppy floppy"
61TAPE_MODS="ht st osst ide-tape ide_tape"
62SCSI_MODS="3w-xxxx 3w_xxxx 3x_9xxx 3x-9xxx 53c7,8xx a100u2w a320raid aacraid adpahci advansys aha152x aha1542 aha1740 aic79xx aic79xx_mod aic7xxx aic7xxx_mod aic7xxx_old AM53C974 atp870u BusLogic cciss cpqfc dmx3191d dpt_i2o dtc eata eata_dma eata_pio fdomain gdth g_NCR5380 i2o_block i2o_core ide-scsi ieee1394 imm in2000 initio ips iscsi isp megaraid megaraid_mm megaraid_mbox mptbase mptscsih mptsas mptspi mptfc mptscsi mptctl NCR53c406a ncr53c8xx nsp32 pas16 pci2000 pci2220i pcmcia ppa psi240i qla1280 qla2200 qla2300 qlogicfas qlogicfc qlogicisp raw1394 scsi_debug scsi_mod scsi_transport_sas scsi_transport_spi sd_mod seagate sg sim710 sr_mod sym53c416 sym53c8xx sym53c8xx_2 t128 tmscsim u14-34f ultrastor wd7000 vmhgfs"
63
64# ide-probe-mod
65IDE_MODS="ide ide-generic ide-detect ide-mod ide-disk ide-cd ide_cd ide-cs ide-core ide_core edd paride ata_generic ata_piix libata via82cxxx generic nvidia ahci sata_nv cmd64x"
66PCMCIA_MODS="pcmcia_core ds yenta_socket"
67USB_MODS="usb-storage usb-ohci usb-uhci usbcore usb_storage input hid uhci_hcd ehci_hcd uhci-hcd ehci-hcd ohci-hcd ohci_hcd usbkbd usbhid keybdev mousedev"
68CDROM_MODS="$TAPE_MODS $FLOPPY_MODS $IDE_MODS af_packet cdrom isocd isofs inflate_fs nls_iso8859-1 nls_cp437 sg sr_mod zlib_inflate $USB_MODS $PCMCIA_MODS"
69NET_MODS="sunrpc nfs nfs_acl lockd fscache loop mii 3c59x e100 bcm5700 bnx2 e1000 eepro100 ne2k-pci tg3 pcnet32 8139cp 8139too 8390 forcedeth vmxnet vmnet"
70EXTRA_MODS="$CDROM_MODS vfat fat loop md-mod linear raid0 raid1 xor raid5 raid456 lvm-mod dm-mod dm-snapshot dm-zero dm-mirror jfs xfs xfs_support pagebuf reiserfs ext2 ext3 minix nfs nfs_acl nfsd lockd sunrpc jbd mbcache"
71# Replace with that line for HP OCMP e.g.
72#DENY_MODS="MPS_Driver_Mapper mps octtldrv tscttl streams"
73DENY_MODS=""
74# Force some modules to be included
75FORCE_MODS=""
76
77LOGFILE=/var/log/mindi.log
78FDDEVICE=/dev/fd0             ; # 1.44MB floppy #0
79CACHE_LOC=/var/cache/mindi
80FORCE_DUAL_FLOPPIES=no
81BOOT_MEDIA_MESSAGE="\
82To format and restore all files automatically, type 'nuke' <enter>.\n\
83To restore some/all files interactively, type 'interactive' <enter>.\n\
84To compare the archives with your filesystem, type 'compare' <enter>.\n\
85To boot to a command-line prompt (expert mode), type 'expert' <enter>.\n\
86You may add one or more of the following parameters as well:-\n\n\
87        donteject - mondorestore will not eject the CD; this is useful if, for\n\
88                    instance, your PC's case has a concealed CD-ROM drive\n\
89        noresize  - your mountlist will not be adjusted to use your unallocated\n\
90                    hard disk space\n\
91        textonly  - do not call any Newt library routines; this is unattractive\n\
92                    but valuable if you find your Newt library has bugs in it\n\n\
93e.g. Type 'nuke donteject textonly' if you have an unstable Newt library and \n\
94a PC whose CD-ROM drive tray would be damaged if it unexpectedly ejected.\n\
95"
96FDISK=$MINDI_SBIN/parted2fdisk
97
98# Using a config file allow to overwrite some values
99MINDI_CONFIG="$MINDI_CONF/mindi.conf"
100if [ -f $MINDI_CONFIG ]; then
101    . $MINDI_CONFIG
102fi
103
104# Now we can create what we need
105export MINDI_TMP=`mktemp -d $TMPDIR/mindi.XXXXXXXXXX`
106mkdir -p $MINDI_TMP
107
108# Purge from potential old run
109rm -rf $CACHE_LOC/* 2> /dev/null
110mkdir -p $CACHE_LOC
111# ----------------------------------------------------------------------------
112
113
114AbortHere() {
115    [ "$mountpoint" ] && umount $mountpoint 2>> $LOGFILE
116    Die "Program is terminating in response to signal received from OS/user"
117}
118
119
120HackSyslinuxFile() {
121    local incoming
122    incoming=`ReadLine`
123    while [ "$incoming" ] ; do
124        echo -en "$incoming" | sed s/24000/$1/
125        if [ "`echo "$incoming" | grep append`" ] ; then
126            echo -en " $ADDITIONAL_BOOT_PARAMS"
127        fi
128        echo -en "\n"
129        incoming=`ReadLine`
130    done
131    if [ -e "$MINDI_LIB/memtest.img" ] ; then
132        echo -en "label memtest\n  kernel memdisk\n  append initrd=memtest.img\n\n"
133    fi
134}
135
136
137Aborted() {
138    trap SIGHUP SIGTERM SIGTRAP SIGINT
139    [ "$CACHE_LOC" != "" ] && rm -f $CACHE_LOC/mindi*img $CACHE_LOC/*gz $CACHE_LOC/mindi.iso
140    [ "$minidir_root" != "" ] && rm -Rf $minidir_root/*
141    Die "User abort."
142}
143
144
145AddFileToCfgIfExists() {
146    [ -e "$1" ] && echo -en "$2 `cat $1`\n" >> $3
147}
148
149
150AddFileToDir() {
151    local filename minidir_root noof_disks diskno res filesize disksize would_occupy zipsize complevel cutoff compressed_fname siz
152    filename=$1
153    minidir_root=$2
154    noof_disks=$3
155
156    diskno=$noof_disks
157    mkdir -p $minidir_root/$diskno
158    [ "$LAST_COMPRESSED_SIZE" = "" ] && LAST_COMPRESSED_SIZE=0
159    if [ ! -e "$filename" ] ; then
160        if [ -h "$filename" ] ; then
161            cp --parents -pRdf $filename $minidir_root/$diskno 2>> $LOGFILE
162            return $noof_disks
163        else
164            Die "AddFileToDir asked me to add $filename, which does not exist. Oops! Did you run out of disk space or is your Linux distro severely broken?"
165        fi
166    fi
167
168    # move to the disk that has room on it (or end up using the last, if all full)
169    while [ "$diskno" -lt "40" ] ; do
170        mkdir -p $minidir_root/$diskno
171        filesize=`du -sk $filename | cut -f1`
172        cp --parents -Rdf $filename $minidir_root/$diskno 2>> $LOGFILE
173        if [ "$filesize" -le "4" ] ; then
174            siz=$filesize
175        elif [ ! -f "$filename" ] ; then
176            siz=0
177        else
178            siz=`grep -m 1 "$filename.gz$" $minidir_root/compressed/compressed.txt | cut -f1`
179            [ "$siz" = "" ] && Die "FIXME - can't find $filename's size."
180            siz=$(($siz-2));# to allow for sectors & the fact that they round up
181        fi
182        [ ! "$siz" ] && siz=4
183        [ "$siz" -lt "0" ] && siz=0
184        LAST_COMPRESSED_SIZE=$(($LAST_COMPRESSED_SIZE+$siz))
185        [ "$LAST_COMPRESSED_SIZE" -le "$MAX_COMPRESSED_SIZE" ] &&return $diskno
186        echo "disk=$diskno siz=$LAST_COMPRESSED_SIZE" >> $LOGFILE
187        LAST_COMPRESSED_SIZE=0
188        rm -f $minidir_root/$diskno/$filename
189        diskno=$(($diskno+1))
190    done
191    return 0 ; # failed
192}
193
194
195AddKeyboardMappingFile() {
196    local mappath r included_list included_item i res ii sss
197    mappath=$1
198    KBDEPTH=$(($KBDEPTH+1))
199    [ "$KBDEPTH" -gt "128" ] && Die "Edit $MINDI_SBIN/mindi and disable FindAndAddUserKeyboardMappingFile (line 2160, approx.)"
200    if [ -e "$bigdir/$mappath" ] ; then
201        echo "$mappath already added" >> $LOGFILE
202        return
203    elif [ -d "$bigdir/$mappath" ] ; then
204        echo "Cannot add $mappath: it's a directory. Sorry."
205        return
206    fi
207    echo "Added kbd map $mappath" >> $LOGFILE
208    if [ ! -e "$mappath" ] ; then
209        mappath=`find $KEYDIR/keymaps | grep "i[3-8]86" | grep "$locale[^r][^/]" | grep -vx " *#.*"`
210        if [ ! -e "$mappath" ] ; then
211            LogIt "Cannot add $mappath: kbd map file not found"
212            return
213        fi
214    else
215        echo -en "`basename $mappath | tr '.' '#' | sed s/#kmap#gz// | sed s/#inc#gz//` " | tr '#' '.'
216    fi
217
218    mkdir -p $bigdir/etc
219    cp --parents -pRdf $mappath $bigdir 2>> $LOGFILE || LogIt "AKMF -- Could not copy $mappath to $bigdir"
220    if [ "`echo $mappath | grep -F ".gz"`" ] ; then
221        included_list=`gzip -dc $mappath | grep -Fi include | sed s/'"'// | sed s/'"'// | cut -d' ' -f2`
222    else
223        included_list=`grep -Fi include $mappath | sed s/'"'// | sed s/'"'// | cut -d' ' -f2`
224    fi
225    for included_item in $included_list ; do
226        if [ ! -e "$included_item" ] ; then
227            sss=`find $KEYDIR/keymaps | grep -F "${included_item}.inc"`
228            [ "$sss" = "" ] && sss=`find $KEYDIR/keymaps | grep -F "$included_item"`
229            for ii in $sss ; do
230                [ -e "$ii" ] && AddKeyboardMappingFile $ii
231            done
232        else
233            AddKeyboardMappingFile $included_item
234        fi
235    done
236}
237
238
239ChopUpAndCopyFile() {
240    local filename slicesize outdir res biggienumber filesize sliceno noof_slices testfile scratchfile
241    filename=$1
242    outdir=$2
243    slicesize=$3
244    biggienumber=$4
245
246    [ -d "$filename" ] && Die "Cannot chop up $filename: it's a directory. Please amend deplist.txt accordingly."
247    mkdir -p $outdir
248
249    sliceno=0
250    scratchfile=$MINDI_TMP/blah.$$.dat
251    cp -f $filename $scratchfile 2>> $LOGFILE || Die "CUACF -- cannot copy $filename to $scratchfile - did you run out of disk space?"
252    [ "`head $scratchfile -n1 | grep -F "bin/sh"`" != "" ] && StripComments $scratchfile "-$filename-"
253    [ "`echo "$filename" | grep -F "etc/termcap"`" != "" ] && StripComments $scratchfile "-$filename-"
254    if [ "`echo "$filename" | grep -F "lib/modules/" | grep "\.*o\.gz"`" != "" ] ; then
255        mv $scratchfile $scratchfile.gz
256        gunzip -f $scratchfile || LogIt "Cannot gunzip $scratchfile.gz"
257        filename=`echo "$filename" | tr '.' '#' | sed s/#o#gz/#o/ | sed s/#ko#gz/#ko/ | tr '#' '.'`
258    fi
259    filesize=`du -sk $scratchfile | cut -f1`
260    noof_slices=$(($filesize/$slicesize))
261    echo "$filename" > $outdir/slice-$biggienumber.name
262    echo "$filesize" > $outdir/slice-$biggienumber.size
263    [ -x "$scratchfile" ] && StripExecutable $scratchfile "-$filename-"
264    while [ "$sliceno" -le "$noof_slices" ] ; do
265        dd if=$scratchfile skip=$(($sliceno*$slicesize)) of=$outdir/slice-$biggienumber.`printf "%03d" $sliceno` bs=1k count=$slicesize &> /dev/null
266        sliceno=$(($sliceno+1))
267    done
268    rm -f $scratchfile
269}
270
271
272CopyBootBFile() {
273    local copy_to copy_from possible_locations liloc
274    copy_to=$1
275    copy_from=/boot/boot.b
276    liloc=`which lilo.real 2>/dev/null`
277    [ $liloc ] || liloc=`which lilo 2>/dev/null`
278    if [ $liloc ]; then
279        if ! [ `strings $liloc | grep "boot\.b"` ]; then
280            LogIt "boot.b files built into lilo; I'll create a dummy."
281            > $copy_to
282            return 0
283        fi
284    fi
285    if [ ! -f "$copy_from" ] ; then
286        LogIt "OK, you don't have a /boot/boot.b file, which is odd because\n most _good_ Linux distributions come with one, even if it's only a softlink"
287        copy_from=`grep install= /etc/lilo.conf | grep "\.b" | cut -d'=' -f2`
288        if [ ! -f "$copy_from" ] ; then
289            LogIt "Nor can I find it from your /etc/lilo.conf file. This is very odd."
290            copy_from=`FindSensibleBootBFile`
291            LogIt "I'm going to use '$copy_from'"
292        fi
293    fi
294    cp -f $copy_from $copy_to 2>> $LOGFILE || LogIt "CBBF -- warning -- cannot find your boot.b file. That's it, I quit... (j/k)"
295}
296
297
298CopyDependenciesToDirectory() {
299    local outdir incoming fname filesize counter
300    outdir=$1
301    mkdir -p $outdir
302    incoming=`ReadLine`
303    counter=0
304    while [ "$incoming" != "" ] ; do
305        if [ -d "$incoming" ] ; then
306            find $incoming/* -maxdepth 0 2> /dev/null | CopyDependenciesToDirectory $outdir
307        elif [ -e "$incoming" ] ; then
308            filesize=`du -sk $incoming | cut -f1`
309            if [ "$filesize" -gt "$(($CHOPSIZE*2))" ] && [ ! -h "$incoming" ] ; then
310                ChopUpAndCopyFile $incoming $outdir $CHOPSIZE $BIGNO
311                BIGNO=$(($BIGNO+1))
312            else
313                cp --parents -Rdf $incoming $outdir 2> /dev/null || Die "Cannot copy $incoming to $outdir - did you run out of disk space?"
314                if [ "`echo "$incoming" | grep "lib/modules/.*\..*o\.gz"`" != "" ] ; then
315                    gunzip -f $outdir/$incoming || LogIt "Cannot gunzip $outdir/$incoming"
316                fi
317                [ -x "$outdir" ] && StripExecutable $outdir "-$filename-"
318            fi
319            counter=$(($counter+1))
320            if [ "$counter" -ge "5" ] ; then
321                counter=0
322                echo -en "."
323            fi
324        fi
325        incoming=`ReadLine`
326    done
327}
328
329
330CopyImageToDisk() {
331    local image dev procno res comment
332    image=$1
333    dev=$2
334    comment=$3
335    [ ! -f "$image" ] && [ ! -b "$image" ] && Die "Image $image does not exist. Did you run out of disk space?"
336    Prompt "About to write $comment. Please press ENTER."
337    echo -en "Formatting disk..."
338    if which fdformat > /dev/null ; then
339        fdformat -n $dev > /dev/null 2> /dev/null || Die "Cannot format $dev - is your Linux distro broken?"
340    elif which superformat > /dev/null ; then
341        superformat $dev > /dev/null 2> /dev/null || Die "Cannot format $dev - is your Linux distro broken?"
342    else
343        Die "Please install either fdformat or superformat."
344    fi
345    echo -en "\rWriting $comment"
346    if echo $image | grep "mindi-[r|b]oot\.1440" &> /dev/null ; then
347        cat $image > $dev &
348    else
349        dd if=$image of=$dev &> /dev/null &
350    fi
351    procno=$!
352    ps $procno > /dev/null 2> /dev/null
353    while [ "$?" -eq "0" ] ; do
354        sleep 3
355        echo -en "."
356        ps $procno > /dev/null 2> /dev/null
357    done
358    echo -e "$DONE"
359    LogIt "$comment has been written."
360}
361
362
363CountItemsIn() {
364    local r
365    r=0
366    for q in $1 ; do
367        r=$(($r+1))
368    done
369    echo $r
370}
371
372
373CreateDataDiskImagesFromTarballs() {
374    local tardir outdir diskno noof_disks kp
375    tardir=$1
376    outdir=$2
377    noof_disks=$3
378
379    mkdir -p $outdir
380    diskno=1
381    echo -en "Creating data disk "
382    while [ "$diskno" -le "$noof_disks" ] ; do
383        echo -en "#$diskno..."
384        cp -f $tardir/$diskno.tar.gz $outdir 2>> $LOGFILE || LogIt "[line 424] Cannot copy $tardir/$diskno.tar.gz to $outdir"
385        CreateOneDataDiskImage $tardir/$diskno.tar.gz $outdir/mindi-data-$diskno.img $diskno $noof_disks
386        diskno=$(($diskno+1))
387    done
388    mv -f $tardir/all.tar.gz $outdir
389    du -sk $outdir/*gz >> $LOGFILE
390    echo -e "$DONE"
391}
392
393
394
395CreateOneDataDiskImage() {
396    local tarball imagefile dev diskno noof_disks mountpoint
397    tarball=$1
398    imagefile=$2
399    diskno=$3
400    noof_disks=$4
401
402    mountpoint=$MINDI_TMP/mountpoint.$$
403    mkdir -p $mountpoint
404    dd if=/dev/zero of=$imagefile bs=1k count=1440 &> /dev/null || LogIt "Cannot dd (CODI)"
405    mke2fs -N 12 -F $imagefile >> $LOGFILE 2>> $LOGFILE
406    mount -t ext2 -o loop $imagefile $mountpoint || Die "Can't loopmount $imagefile to $mountpoint! The reason may be missing support for loopfs or ext2 (or both) in the running kernel."
407    mv $tarball $mountpoint/
408    if [ "$?" -ne "0" ] ; then
409        umount $mountpoint
410        Die "Tarball $tarball is too big for disk! (CODI)\nTell Dev Team to adjust MAX_COMPRESSED_SIZE"
411    fi
412    [ "$diskno" -eq "$noof_disks" ] && echo "This is the last disk ($diskno=$noof_disks)" >> $mountpoint/LAST-DISK
413    umount $mountpoint || LogIt "Cannot umount (CODI)"
414    rmdir $mountpoint || LogIt "Cannot rmdir (CODI)"
415}
416
417
418# Last function called before exiting
419# Parameter is exit code value
420MindiExit() {
421    local my_partitions
422
423    echo "Mindi $MINDI_VERSION is exiting" >> $LOGFILE
424    echo "End date : `date`" >> $LOGFILE
425    if [ _"$MONDO_SHARE" != _"" ] ; then
426        echo "------------- mindi logfile included -------------------------" >> /var/log/mondoarchive.log
427        cat $LOGFILE >> /var/log/mondoarchive.log
428        echo "--------------------------------------------------------------">> /var/log/mondoarchive.log
429    fi
430
431    sync
432    cd /
433
434    # Unmount whtat could remain mounted
435    my_partitions=`mount | grep -F $$ | cut -f1 -d' '`
436    [ "$my_partitions" != "" ] && umount $my_partitions
437    # Clean temporary files only when standalone mindi
438    if [ _"$MINDI_TMP" != _"$MONDO_TMP" ]; then
439        rm -Rf $MINDI_TMP
440    fi
441    exit $1
442}
443
444Die() {
445    local i
446    if [ "$1" = "" ] ; then
447        LogIt "FATAL ERROR"
448    else
449        LogIt "FATAL ERROR. $1"
450    fi
451
452    LogIt "Please e-mail a copy of $LOGFILE to the mailing list."
453    LogIt "See http://www.mondorescue.org for more information."
454    LogIt "WE CANNOT HELP unless you enclose that file.\n"
455    MindiExit -1
456}
457
458
459DropOptimizedLibraries() {
460    local outdir filelist list_of_optimized_libraries optimized_lib_name vanilla_lib_name reason msg resolved res
461    filelist=$1
462    outdir=$2
463
464    list_of_optimized_libraries=`grep "lib/i[5-7]86/" $filelist`
465    if [ "$list_of_optimized_libraries" = "" ] ; then
466        return 0
467    fi
468    echo -en "Dropping i686-optimized libraries if appropriate"
469    for optimized_lib_name in $list_of_optimized_libraries ; do
470        echo -en "."
471        reason=""
472        vanilla_lib_name=`echo "$optimized_lib_name" | sed -e 's/i[5-7]86//' -e 's/cmov//' -e 's/nosegneg//' | tr -s '/' '/'`
473        echo "$vanilla_lib_name" >> $filelist
474        resolved=$vanilla_lib_name
475        echo "Adding $resolved to filelist" >> $LOGFILE
476        resolved=`ReadAllLink $resolved`
477        echo "Adding $resolved to filelist" >> $LOGFILE
478        mkdir -p $outdir$optimized_lib_name > /dev/null 2> /dev/null
479        rmdir $outdir$optimized_lib_name > /dev/null 2> /dev/null
480        ln -sf $vanilla_lib_name $outdir$optimized_lib_name
481        echo "Excluding $optimized_lib_name" >> $LOGFILE
482        grep -Fvx "$optimized_lib_name" "$filelist" > $filelist.tmp
483        echo "Replacing it with $vanilla_lib_name" >> $LOGFILE
484        echo "$vanilla_lib_name" >> $filelist.tmp
485        mv -f $filelist.tmp $filelist
486    done
487    $AWK '{ print $1; }' $filelist | sort -u > $filelist.tmp
488    mv -f $filelist.tmp $filelist
489    echo -e "$DONE"
490}
491
492
493FindAndAddUserKeyboardMappingFile() {
494    local r res mapfile mappath included_item included_list keyfile mp locale
495    LogIt "Analyzing your keyboard's configuration."
496    KEYDIR=/lib/kbd
497    [ ! -e "$KEYDIR" ] && KEYDIR=/usr/share/kbd     # Slackware
498    [ ! -e "$KEYDIR" ] && KEYDIR=/usr/lib/kbd
499    [ ! -e "$KEYDIR" ] && KEYDIR=/usr/share
500    if [ ! -e "$KEYDIR" ] ; then
501        LogIt "Keyboard mapping directory not found. I shall use default map at boot-time."
502        return 0
503    fi
504    if [ -e "/etc/sysconfig/keyboard" ] ; then
505        echo "Red Hat-style config detected." >> $LOGFILE
506        keyfile=/etc/sysconfig/keyboard
507    elif [ -e "/etc/rc.d/rc.keymap" ] ; then
508        echo "Slackware-style config detected." >> $LOGFILE
509        keyfile=/etc/rc.d/rc.keymap
510    elif [ -e "/etc/rc.config" ] ; then
511        echo "Debian-style config detected." >> $LOGFILE
512        keyfile=/etc/rc.config
513    elif [ -e "/etc/console/boottime.kmap.gz" ] ; then
514        echo "Debian-style config detected." >> $LOGFILE
515        echo -en "Adding the following keyboard mapping tables: "
516        mkdir -p $bigdir/tmp
517        echo "/etc/console/boottime.kmap.gz" > $bigdir/tmp/KEYMAP-LIVES-HERE
518        KBDEPTH=0
519        mkdir -p $bigdir/etc/console
520        cp /etc/console/boottime.kmap.gz $bigdir/etc/console 2>> $LOGFILE
521        echo -e "$DONE"
522        return 0
523    elif [ -e "/etc/conf.d/keymaps" ] ; then
524        echo "Gentoo-style config detected." >> $LOGFILE
525        keyfile=/etc/conf.d/keymaps
526    else
527        echo -en "Searching for rc.config ..."
528        keyfile=`find /etc -name rc.config | head -n1`
529        if [ "$keyfile" = "" ] || [ ! -e "$keyfile" ] ; then
530            LogIt "Unknown config detected. Default keyboard map will be used."
531            return
532        else
533            echo "Found $keyfile" >> $LOGFILE
534        fi
535    fi
536    if [ ! -e "$KEYDIR/keymaps" ] ; then
537        LogIt "Keyboard mapping directory not found. Default keyboard map will be used."
538        return
539    fi
540    echo "keyfile=$keyfile" >> $LOGFILE
541    locale=`grep -F KEYTABLE "$keyfile" | tr -d '"' |cut -d'=' -f2`
542    [ ! "$locale" ] && locale=`grep '.map$' "$keyfile" | sed 's/^.* //'`        # Slackware
543    [ ! "$locale" ] && locale=`grep -E '^KEYMAP=' "$keyfile" | tr -d '"' |cut -d'=' -f2`        # Gentoo
544    echo "locale=$locale" >> $LOGFILE
545    mp=`find $KEYDIR/keymaps | grep "i[3-8]86" | grep -F "/${locale}." | grep -vx " *#.*"`
546    [ ! "$mp" ] && mp=`find $KEYDIR/keymaps | grep "i[3-8]86" | grep "$locale[^r][^/]" | grep -vx " *#.*"`
547    # If we have multiple keymaps then log it !!
548    echo "$mp" | grep -q " "
549    if [ $? -eq 0 ]; then
550        echo "WARNING: Multiple keymaps found: $mp" | tee -a $LOGFILE
551        echo "The following one will be used" >> $LOGFILE
552    fi
553    for i in $mp ; do
554        mappath=$i
555        [ -e "$i" ] && [ ! -d "$i" ] && break
556    done
557    if [ ! -e "$mappath" ] || [ -d "$mappath" ] ; then
558       mappath=$(locate */kbd/keymaps/*/$locale)
559    fi
560    echo "mappath = $mappath" >> $LOGFILE
561    if [ ! -e "$mappath" ] || [ -d "$mappath" ] ; then
562        LogIt "Keyboard mapping file not found. Default keyboard map will be used."
563        return
564    fi
565    echo -en "Adding the following keyboard mapping tables: "
566    mkdir -p $bigdir/tmp
567    echo "$mappath" > $bigdir/tmp/KEYMAP-LIVES-HERE
568    KBDEPTH=0
569    AddKeyboardMappingFile $mappath
570    echo -e "$DONE"
571    return 0
572}
573
574
575FindIsolinuxBinary() {
576    ISOLINUX=/usr/lib/isolinux.bin
577    [ ! -e "$ISOLINUX" ] && ISOLINUX=/usr/lib/syslinux/isolinux.bin
578    [ ! -e "$ISOLINUX" ] && ISOLINUX=/usr/lib64/syslinux/isolinux.bin
579    [ ! -e "$ISOLINUX" ] && ISOLINUX=/usr/share/syslinux/isolinux.bin
580    [ ! -e "$ISOLINUX" ] && ISOLINUX=/usr/share/lib/syslinux/isolinux.bin
581    [ ! -e "$ISOLINUX" ] && ISOLINUX=/usr/share/lib64/syslinux/isolinux.bin
582    [ ! -e "$ISOLINUX" ] && ISOLINUX=`locate isolinux.bin | grep -x "/.*/isolinux.bin"`
583    [ ! -e "$ISOLINUX" ] && Die "Please install isolinux first. If your syslinux RPM doesn't include isolinux, you may download an isolinux RPM from Mondo's website - go to http://www.mondorescue.com and click on 'Download'"
584    echo "Found isolinux.bin at $ISOLINUX" >> $LOGFILE
585}
586
587
588FindLiloBinary() {
589    if which lilo &> /dev/null ; then
590        if which lilo.real > /dev/null 2> /dev/null ; then
591            LILO_EXE=lilo.real
592            LogIt "lilo.real found; will be used instead of lilo (*grumble* *mutter*)"
593        else
594            LILO_EXE=lilo
595        fi
596        $LILO_EXE -V | grep -F "21.6" > /dev/null && Die "Please upgrade LILO. Your version has a serious bug. If you're not _using_ LILO, fine, uninstall it. :)"
597    else
598        LILO_EXE=`which false`
599    fi
600}
601
602
603FindSensibleBootBFile() {
604    local i out last
605    out=""
606    last=""
607    for i in `find /boot -type f | grep -v chain | grep -v os2 | sort -u` ; do
608    if [ "`strings $i 2> /dev/null | head -n1`" = "LILO" ] ; then
609        out="$out $i"
610        last="$i"
611    fi
612    done
613    echo "$last"
614}
615
616
617FindSpecificModuleInPath() {
618    local modpaths pwd line
619    pwd=`pwd`
620    if [ "$YOUR_KERNEL_SUCKS" ] ; then
621        cd $MINDI_TMP
622    else
623        cd /
624    fi
625    if [ ! -e "$1" ] ; then
626        LogIt "WARNING - cannot search specific path '$1'"
627        return 1
628    fi
629    modpaths=`find $1 -name $2.*o -type f`
630    [ "$?" -ne "0" ] && Die "find $1 -name $2.o -type f --- failed"
631    [ "$modpaths" = "" ] && modpaths=`find $1 -name $2.o.gz -type f`
632    [ "$modpaths" = "" ] && modpaths=`find $1 -name $2.ko.gz -type f`
633    [ "$modpaths" = "" ] && modpaths=`find $1 -name $2 -type f`
634    echo "$modpaths"
635    cd $pwd
636}
637
638
639GenerateGiantDependencyList() {
640    local incoming loc fname list_of_files i tempfile outfile progress filelist res r mapfile mappath included_list included_item old_pwd tempdepfile modres noof_lines lvmversion lvmresolved
641
642    echo -en "Analyzing dependency requirements"
643    outfile=$1
644    tempfile=$MINDI_TMP/$$.txt
645    incoming=`ReadLine`
646
647    > $tempfile
648    progress=0
649    res=0
650    noof_lines=$2
651    while [ "$incoming" != "" ] ; do
652        if echo "$incoming" | grep -x " *#.*" &> /dev/null ; then
653            incoming=`ReadLine`
654            continue
655        fi
656        if [ "$incoming" = "LVMFILES:" ] ; then
657            break
658        fi
659        filelist=`GenerateListForFile "$incoming"`
660        r=$?
661        [ "$r" -ne "0" ] && LogIt "$incoming not found"
662        res=$(($res+$r))
663#       echo "'$incoming' generates filelist '$filelist'" >> $LOGFILE
664        for fname in $filelist ; do
665            [ "$fname" != "" ] && echo "$fname" >> $tempfile
666        done
667        progress=$(($progress+1))
668        echo -en "\r\t\t\t\t\t\t\t\t"
669        i=$(($progress*100))
670        i=$(($i/$noof_lines))
671        echo -en "$i"
672        echo -en "%"
673        modres=$(($progress%4))
674        [ "$modres" -eq "0" ] && echo -en "\t/"
675        [ "$modres" -eq "1" ] && echo -en "\t-"
676        [ "$modres" -eq "2" ] && echo -en "\t\\"
677        [ "$modres" -eq "3" ] && echo -en "\t|"
678        incoming=`ReadLine`
679    done
680    if  [ "$incoming" = "LVMFILES:" ] ; then
681        incoming=`ReadLine`
682        lvmversion=""
683        while [ "$incoming" != "" ] ; do
684            if echo "$incoming" | grep -x " *#.*" &> /dev/null ; then
685                incoming=`ReadLine`
686                continue
687            fi
688            filelist=`GenerateListForFile "$incoming"`
689            for tool in $filelist ; do
690                lvmresolved=`readlink -f $tool`
691                if [ "$tool" = "$lvmresolved" ]; then
692                    echo "$tool" >> $tempfile
693                elif echo "$lvmresolved" | grep "lvmiopversion" &> /dev/null ; then
694                    if [ "$lvmversion" = "" ] ; then
695                        lvmversion=`$lvmresolved`
696                        echo "$lvmresolved" >> $tempfile
697                    fi
698                    toolstripped=`echo $tool | $AWK -F / '{print $NF;}'`
699                    if [ "$lvmversion" = "200" ]; then
700                        # pvdata and lvmcreate_initrd don't exist in LVM2
701                        case "$toolstripped" in
702                        "pvdata")
703                            continue
704                            ;;
705                        "lvmcreate_initrd")
706                            continue
707                            ;;
708                        esac
709                    fi
710                    toolpath="/sbin/lvm-"$lvmversion"/"$toolstripped
711                    if [ -e "$toolpath" ] ; then
712                        echo "$toolpath" >> $tempfile
713                        echo "$tool" >> $tempfile
714                    else
715                        toolpath="/lib/lvm-"$lvmversion"/"$toolstripped
716                    fi
717                    if [ -e "$toolpath" ] ; then
718                        echo "$toolpath" >> $tempfile
719                        echo "$tool" >> $tempfile
720                    else
721                        echo "Where are your LVM-Tools? Couldn't find $tool"
722                    fi
723                else
724                    echo "$tool" >> $tempfile
725                fi
726            done
727            progress=$(($progress+1))
728            echo -en "\r\t\t\t\t\t\t\t\t"
729            i=$(($progress*100))
730            i=$(($i/$noof_lines))
731            echo -en "$i"
732            echo -en "%"
733            modres=$(($progress%4))
734            [ "$modres" -eq "0" ] && echo -en "\t/"
735            [ "$modres" -eq "1" ] && echo -en "\t-"
736            [ "$modres" -eq "2" ] && echo -en "\t\\"
737            [ "$modres" -eq "3" ] && echo -en "\t|"
738            incoming=`ReadLine`
739        done
740    fi
741    echo -en "$DONE\nMaking complete dependency list"
742
743    tr -s '/' '/' < $tempfile | sort -u > $tempfile.new
744    mv -f $tempfile.new $tempfile
745    > $outfile.pre
746    progress=0
747    noof_lines=`cat $tempfile | wc -l`
748    for fname in `cat $tempfile` ; do
749        echo "$fname" >> $outfile.pre
750        LocateDeps $fname >> $outfile.pre
751        progress=$(($progress+1))
752        echo -en "\r\t\t\t\t\t\t\t\t"
753        i=$(($progress*100))
754        i=$(($i/$noof_lines))
755        echo -en "$i"
756        echo -en "%"
757        modres=$(($progress%4))
758        [ "$modres" -eq "0" ] && echo -en "\t/"
759        [ "$modres" -eq "1" ] && echo -en "\t-"
760        [ "$modres" -eq "2" ] && echo -en "\t\\"
761        [ "$modres" -eq "3" ] && echo -en "\t|"
762    done
763    if [ _"$MONDO_SHARE" != _"" ] ; then
764        mkdir -p $bigdir/tmp
765        mkdir -p $bigdir/sbin
766        mkdir -p $bigdir/bin
767        if [ -e "$MINDI_TMP/post-nuke.tgz" ] ; then
768            LogIt "\nIncorporating post-nuke tarball"
769            old_pwd=`pwd`
770            cd $bigdir
771            tar -zxf $MINDI_TMP/post-nuke.tgz || LogIt "Error occurred when untarring post-nuke tarball"
772            cd $old_pwd
773        fi
774        if cp -f $MINDI_TMP/mondo*restore $bigdir/usr/bin 2>> $LOGFILE ; then
775            LocateDeps $bigdir/usr/bin/mondo*restore >> $outfile.pre
776        else
777            LogIt "Cannot find mondo*restore in mondo's tempdir, $MINDI_TMP"
778            LogIt "I bet you've got a spare copy of Mondo or Mindi floating around on your system."
779            LogIt "If Mindi was called by Mondo then send me a bug report.\n It not, type 'ps ax' to see which Mondo-related process is still running;\n then kill it. :-)\n Finally, run Mindi again."
780            Die "Odd."
781        fi
782        cp -f $MINDI_TMP/BOOTLOADER.* $bigdir 2> /dev/null || LogIt "\nMondo v1.2x defaults to LILO as the bootloader, BTW."
783        if [ -e "$MINDI_TMP/NFS-DEV" ] ; then
784            LogIt "Incorporating NFS-related settings"
785            for r in NFS-* ISO-PREFIX ; do
786                cp -f $MINDI_TMP/$r $bigdir/tmp 2>> $LOGFILE || Die "Cannot copy $r - did you run out of disk space?"
787                echo "Copying $r to ramdisk" >> $LOGFILE
788            done
789        fi
790    fi
791    tr ' ' '\n' < $outfile.pre | tr -s '/' '/' | grep -Fvx "" | sort -u | grep -Ev "/libX11|/libXext|/libXi|/libgtk|/libgdk" > $outfile
792    rm -f $tempfile $outfile.pre
793    [ "$res" -eq "0" ] && echo -e "\r\t\t\t\t\t\t\t\t$DONE" || echo "\nFailed."
794    return $res
795}
796
797
798GenerateListForFile() {
799    local files_found loc fname incoming i res
800    incoming="$1"
801    files_found=""
802    res=0
803
804    for fname in $incoming ; do
805        files_found="$files_found `LocateFile $fname`"
806    done
807
808    echo "$files_found" | tr ' ' '\n' | sort -u | tr '\n' ' '
809}
810
811
812# Returns all disk devices which are part of a raid array
813GetAllRaidMembers() {
814    $AWK "/^[[:space:]]*#/ {next} /^[[:space:]]*device/ if(\$2) {print \$2}" < /etc/raidtab
815}
816
817
818GetFileSizeList() {
819    local i
820    for i in `find $1 -type d -o -print` ; do
821        du -sk $i
822    done
823}
824
825
826GetHomeDir() {
827    local res loc
828    loc=`which $1 2>/dev/null`
829    res=`file $loc | $AWK '{print $NF;}'`
830    dirname $res
831}
832
833
834# Check kernel filesystem capabilites for accessing initrd image
835#
836# Interface definition:
837# param #1: absolute path to kernel image
838GetInitrdFilesystemToUse() {
839
840    # interface test: make sure we have one parameter
841    if [ $# -ne 1 ]; then
842        Die "GetInitrdFilesystemToUse(): Expected 1 parameter, got $#."
843    fi
844
845    # interface parameters
846    local lvKernelImage=$1
847
848    # local constants (filesystem magic strings)
849    local lcMagicCramfs="<3>cramfs: wrong magic"
850    local lcMagicExt2fs="<3>EXT2-fs: blocksize too small for device."
851    local lcMagicInitfs="<6>checking if image is initramfs..."
852
853    # local variables
854    local lvOffset
855    local lvScanRes
856    local lvUseFilesystem
857
858    # say where we are.
859    echo "  GetInitrdFilesystemToUse(): called with parameter: $lvKernelImage.\n" >> $LOGFILE
860
861    # verify that file exists
862    [ ! -f $lvKernelImage ] && Die "File $lvKernelImage not found. Terminating."
863
864    # get offet of gzip magic "1f8b0800" in file
865    lvOffset=`od -vA n -t x1 $lvKernelImage | tr -d '[:space:]' | awk '{ print match($0, "1f8b0800")}'`
866    [ $lvOffset -eq 0 ] && Die "gzip magic not found in file $lvKernelImage. Terminating."
867    lvOffset=`expr $lvOffset / 2`
868    echo "  GetInitrdFilesystemToUse(): gzip magic found at lvOffset $lvOffset.\n" >> $LOGFILE
869
870    # scan kernel image for initrd filessystem support
871    lvScanRes=`dd ibs=1 skip=$lvOffset if=$lvKernelImage obs=1M 2>/dev/null | gunzip -c 2> /dev/null | strings | grep -e "$lcMagicCramfs" -e "$lcMagicExt2fs" -e "$lcMagicInitfs"`
872
873    # determine which filesystem to use for initrd image: ext2fs, gzip'ed cpio (initramfs ) or cramfs
874    if [ `echo $lvScanRes | grep -c "$lcMagicExt2fs"` -eq 1 ]; then
875        lvUseFilesystem="ext2fs"
876    elif [ `echo $lvScanRes | grep -c "$lcMagicInitfs"` -eq 1 ]; then
877        lvUseFilesystem="initramfs"
878    elif [ `echo $lvScanRes | grep -c "$lcMagicCramfs"` -eq 1 ]; then
879        lvUseFilesystem="cramfs"
880    else
881        lvUseFilesystem="UNSUPPORTED"
882    fi
883
884    # say what we are using
885    echo "  GetInitrdFilesystemToUse(): Filesytem to use for initrd is $lvUseFilesystem.\n" >> $LOGFILE
886
887    # return file system to use
888    echo "$lvUseFilesystem"
889
890}
891
892# Searches parent raid device of given disk device
893# $1: disk device (i.e. /dev/hda1)
894GetParentRaidDev() {
895    $AWK "/^[[:space:]]*#/ {next} /^[[:space:]]*raiddev/ {dev=\$2} /^[[:space:]]*device/ {if(\$2==\"$1\") {print dev; exit}}" < /etc/raidtab
896}
897
898
899# Searches members of raid device
900# $1: raid device (/dev/md...)
901GetRaidDevMembers() {
902    $AWK "/^[[:space:]]*#/ {next} /^[[:space:]]*raiddev/ {if(dev) exit; if(\$2 == \"$1\") dev=\$2} /^[[:space:]]*device/ {if(dev) {print \$2}}" < /etc/raidtab
903}
904
905
906HackPathsToFailsafe() {
907    local incoming newpath kver stub i pwd
908    kver=`uname -r`
909    incoming=`ReadLine`
910    pwd=`pwd`
911    cd $MINDI_TMP
912    while [ "$incoming" != "" ] ; do
913        stub=`basename $incoming`
914        newpath=`FindSpecificModuleInPath lib/modules/$FAILSAFE_KVER $stub`
915        for i in $newpath ; do
916            echo "$i"
917        done
918        read incoming
919    done
920    cd $pwd
921}
922
923
924ListAllPartitions() {
925    local res currline partition all_partitions ap_orig remaining i j
926
927    grep -vx " *#.*" $MY_FSTAB | grep -vx " *none.*" | $AWK '/^\/dev\/[imhs]d||^LABEL\=\/|^UUID=/ && !/fdd|cdr|zip|floppy/ {print $1}'
928    [ -e "/etc/raidtab" ] && $AWK '/^ *device/ {print $2}' /etc/raidtab
929    return
930}
931
932
933ListImagesForUser() {
934    local path fname
935    path=$1
936    echo -en "In the directory '$path' you will find the images:-\n"
937    for fname in `ls $path | grep -F mindi-` ; do
938        printf "%19s " $fname
939    done
940    echo " "
941}
942
943
944ListKernelModulePaths() {
945    local module_list module fname oss r kern
946    oss="/root/oss/modules"
947    module_list="$MODULES"
948    # Remove unwanted modules from list
949    for i in $DENY_MODS; do
950        module_list=`echo ${module_list} | tr ' ' '\n' | grep -Ev "^${i}$" | tr '\n' ' '`
951    done
952###
953### Sq-Modification ... Use kernelname for module search path if specified
954###
955    # kern="`uname -r`"
956    if [ "${kernelname}" != "" -a "${kernelname}" != "FAILSAFE" ]
957    then
958        kern=${kernelname}
959    else
960        kern="`uname -r`"
961    fi
962###
963### Sq-Mod End
964###
965    for module in $module_list $EXTRA_MODS ; do
966        r=`find /lib/modules/$kern -type f | grep "/${module}\..*o" | tail -n1`
967        echo "module $module --> $r" >> $LOGFILE
968        [ "$r" ] && echo "$r"
969        [ -f "$oss" ] && find $oss | grep -F $module
970    done
971    find /lib/modules/$kern/modules.* -type f 2> /dev/null
972    [ -f "$oss" ] && find $oss.* 2> /dev/null
973}
974
975
976LocateDeps() {
977    local incoming fname deps
978    incoming="$1"
979    for fname in $incoming ; do
980        if [ ! -e "$fname" ] ; then
981            echo "WARNING - $fname does not exist; cannot be LDD'd." >> $LOGFILE
982            if echo $fname | grep lvm &> /dev/null ; then
983                echo "This warning only affects you if you are using LVM." >> $LOGFILE
984                if echo "$MODULES" | grep lvm &> /dev/null ; then
985                    echo "I think you are, so please take heed!" >> $LOGFILE
986#                   LogIt "Where is liblvm? You need it."
987                else
988                    echo "I don't think you are, so don't worry about it." >> $LOGFILE
989                fi
990            fi
991        elif [ -h "$fname" ] && [ -x "$fname" ] ; then
992            echo "$fname is softlink" >> $LOGFILE
993        else
994            ldd $fname 2> /dev/null | ProcessLDD $fname
995        fi
996    done
997}
998
999
1000# Give all symlinks recursively of a full path name
1001ReadAllLink() {
1002    file="$1"
1003
1004    if [ ! -h $file ]; then
1005        echo "$file"
1006        return 0
1007    fi
1008
1009    link=`readlink -f $file`
1010    d=`dirname $file`
1011    if [ ! -e "$link" -a ! -e "$d/$link" ]; then
1012        echo "Problem with dead link on $file -> $link" >> $LOGFILE
1013    fi
1014    if [ -h "$d" ]; then
1015        echo "$link $d"
1016    else
1017        echo "$link"
1018    fi
1019}
1020
1021
1022LocateFile() {
1023    local i path fname_to_find location output resolved tmp stub cache_id loclist
1024    fname_to_find="$1"
1025    if echo "$fname_to_find" | grep -x "/.*" ; then
1026        output="$fname_to_find"
1027        if [ -h "$output" ] ; then
1028            output="`ReadAllLink $output` $output"
1029        fi
1030        echo "$output"
1031        return 0
1032    fi
1033    output=""
1034    #for path in /etc /usr /usr/bin /usr/sbin /bin /usr/X11R6/bin /sbin /usr/local/bin /usr/local/sbin /usr/lib /usr/lib64 /lib /lib64 /usr/local/lib /usr/local/lib64 /usr/X11R6/lib /usr/X11R6/lib64 `find /usr/lib64 /lib64 /usr/local/lib64 /usr/X11R6/lib64 -type d` ; do
1035    for path in /etc /usr /usr/bin /usr/sbin /bin /usr/X11R6/bin /sbin /usr/local/bin /usr/local/sbin /usr/lib /usr/lib64 /usr/lib64/* /lib /lib64 /lib64/* /usr/local/lib /usr/local/lib64 /usr/local/lib64/* /usr/X11R6/lib /usr/X11R6/lib64 /usr/X11R6/lib64/* ; do
1036        [ ! -d "$path" ] && continue
1037        for location in "$path/$fname_to_find" ; do
1038            [ ! -e "$location" ] && continue
1039            output="$location $output"
1040            if [ -h "$location" ] ; then
1041                output="`ReadAllLink $location` $output"
1042            fi
1043        done
1044    done
1045    if [ "$output" = "" ] ; then
1046        return 1
1047    fi
1048    echo "$output"
1049    return 0
1050}
1051
1052
1053LogIt() {
1054    if [ -e /dev/stderr ] ; then
1055        echo -e "$1" >> /dev/stderr
1056    elif [ -e /usr/bin/logger ] ; then
1057        /usr/bin/logger -s $1
1058    fi
1059    echo -e "$1" >> $LOGFILE
1060}
1061
1062
1063# Called by TurnTgzIntoRdz, to make /tmp/mondo-restore.cfg
1064MakeMondoConfigFile() {
1065    local outfile use_lzo use_comp use_star
1066    outfile=$1
1067    > $outfile
1068    [ "$TAPESIZE" ]     && echo "media-size $TAPESIZE" >> $outfile
1069    [ "$TAPEDEV" ]          && echo "media-dev $TAPEDEV" >> $outfile
1070    [ "$FILES_IN_FILELIST" ]    && echo "files-in-filelist $FILES_IN_FILELIST" >> $outfile
1071    [ "$LAST_FILELIST_NUMBER" ] && echo "last-filelist-number $LAST_FILELIST_NUMBER" >> $outfile
1072    [ "$INTERNAL_TAPE_BLOCK_SIZE" ] && echo "internal-tape-block-size $INTERNAL_TAPE_BLOCK_SIZE" >> $outfile
1073    use_lzo=$USE_LZO; [ "$use_lzo" = "" ] && use_lzo="no"
1074    use_gzip=$USE_GZIP; [ "$use_gzip" = "" ] && use_gzip="no"
1075    use_comp=$USE_COMP; [ "$use_comp" = "" ] && use_comp="yes"
1076    use_star=$USE_STAR; [ "$use_star" = "" ] && use_star="no"
1077    echo "use-lzo $use_lzo" >> $outfile
1078    echo "use-gzip $use_gzip" >> $outfile
1079    echo "use-star $use_star" >> $outfile
1080    echo "use-comp $use_comp" >> $outfile
1081    echo "datestamp `date`" >> $outfile
1082    [ "$ESTIMATED_TOTAL_NOOF_SLICES" ] && echo "total-slices $ESTIMATED_TOTAL_NOOF_SLICES" >> $outfile
1083    AddFileToCfgIfExists $MINDI_TMP/NFS-CLIENT-IPADDR nfs-client-ipaddr $outfile
1084    AddFileToCfgIfExists $MINDI_TMP/NFS-CLIENT-NETMASK nfs-client-netmask $outfile
1085    AddFileToCfgIfExists $MINDI_TMP/NFS-CLIENT-BROADCAST nfs-client-broadcast $outfile
1086    AddFileToCfgIfExists $MINDI_TMP/NFS-CLIENT-DEFGW  nfs-client-defgw  $outfile
1087    AddFileToCfgIfExists $MINDI_TMP/NFS-SERVER-MOUNT  nfs-server-mount  $outfile
1088    AddFileToCfgIfExists $MINDI_TMP/NFS-SERVER-PATH   nfs-server-path   $outfile
1089    AddFileToCfgIfExists $MINDI_TMP/NFS-DEV           nfs-dev           $outfile
1090    AddFileToCfgIfExists $MINDI_TMP/NFS-SERVER-IPADDR nfs-server-ipaddr $outfile
1091    AddFileToCfgIfExists $MINDI_TMP/ISO-DEV           iso-dev           $outfile
1092    AddFileToCfgIfExists $MINDI_TMP/ISO-MNT           iso-mnt           $outfile
1093    AddFileToCfgIfExists $MINDI_TMP/ISO-PREFIX        iso-prefix        $outfile
1094    AddFileToCfgIfExists $MINDI_TMP/ISODIR            isodir            $outfile
1095    AddFileToCfgIfExists $MINDI_TMP/BOOTLOADER.DEVICE bootloader.device $outfile
1096    AddFileToCfgIfExists $MINDI_TMP/BOOTLOADER.NAME   bootloader.name   $outfile
1097    AddFileToCfgIfExists $MINDI_TMP/KEYMAP-LIVES-HERE keymap-lives-here $outfile
1098    AddFileToCfgIfExists $MINDI_TMP/TAPEDEV-HAS-DATA-DISKS tapedev-has-data-disks $outfile
1099    AddFileToCfgIfExists $MINDI_TMP/BACKUP-MEDIA-TYPE backup-media-type $outfile
1100    AddFileToCfgIfExists $MINDI_TMP/DIFFERENTIAL      differential      $outfile
1101    AddFileToCfgIfExists $MINDI_TMP/ACL      acl      $outfile
1102    AddFileToCfgIfExists $MINDI_TMP/XATTR      xattr      $outfile
1103}
1104
1105
1106MakeModuleLoadingScript() {
1107    local module fname params modpath kver outerloop i modpaths kver searchpath list_to_echo j
1108    tmpmodprobe_flag=$1
1109    outfile=$2
1110    > $outfile || Die "Cannot create empty $outfile"
1111    echo -en "#\041/bin/sh\n\n" >> $outfile
1112    echo "echo -en \"Loading your modules...\"" >> $outfile
1113    if [ "$YOUR_KERNEL_SUCKS" ] ; then
1114        kver=$FAILSAFE_KVER
1115        cd $MINDI_TMP
1116        searchpath=lib/modules/$kver
1117    else
1118###
1119### Sq-Modification ... Use kernelname for module search path if specified
1120###
1121        #kver=`uname -r`
1122        if [ "${kernelname}" != "" ]
1123        then
1124            kver=${kernelname}
1125        else
1126            kver=`uname -r`
1127        fi
1128###
1129### Sq-Modification end
1130###
1131        searchpath=/lib/modules/$kver
1132    fi
1133   
1134    echo -en "for outerloop in 1 2 3 4 5 ; do\necho -en \".\"\n" >> $outfile
1135    # BERLIOS: That code is duplicated - Should be done once only
1136    list_to_echo="$MODULES"
1137    # Remove unwanted modules from list
1138    for i in $DENY_MODS; do
1139        list_to_echo=`echo ${list_to_echo} | tr ' ' '\n' | grep -Ev "^${i}$" | tr '\n' ' '`
1140    done
1141
1142    # Make temporary modprobe.conf file if we are told so
1143    if [ "$tmpmodprobe_flag" = "Y" ] ; then
1144        infile="$MINDI_TMP/modprobe.conf.mindi"
1145        find /etc/modprobe.d -maxdepth 1 -name "*" -xtype f -print0 | xargs -0 cat > $infile
1146    else
1147        infile="/etc/modules.conf"
1148    fi
1149    for module in $list_to_echo $EXTRA_MODS ; do
1150        params=`sed -n "s/^options \\+$module \\+//p" $infile`
1151        modpaths=`FindSpecificModuleInPath $searchpath $module`
1152        for i in $modpaths ; do
1153            echo "MyInsmod $i $params > /dev/null 2> /dev/null" \
1154                | tr '.' '#' \
1155                | sed s/#o#gz/#o/ \
1156                | sed s/#o#gz/#o/ \
1157                | sed s/#ko#gz/#ko/ \
1158                | sed s/#ko#gz/#ko/ \
1159                | tr '#' '.' >> $outfile
1160            echo -en "$i added to module list.\n" >> $LOGFILE
1161        done
1162    done
1163    echo -en "done\n" >> $outfile
1164    echo "echo \"Done.\"" >> $outfile
1165    chmod +x $outfile
1166    cd /
1167    # Remove temporary modprobe.conf file if we have created one
1168    if [ "$tmpmodprobe_flag" = "Y" ] ; then
1169        rm -f $infile
1170    fi
1171}
1172
1173
1174MakeMountlist() {
1175    local scratchdir mountlist all_partitions current_partition \
1176partition_size partition_format outstring partition_number \
1177partition_mountpt c_p lwm_info psz lvm_dev unofficial_outstring \
1178absolute_partition old_partition_fmt current_lvolume
1179
1180    echo "Your raw fstab file looks like this:" >> $LOGFILE
1181    echo "------------------------------------" >> $LOGFILE
1182    cat $MY_FSTAB >> $LOGFILE
1183    echo "Your mountlist will look like this:" | tee -a $LOGFILE
1184    echo "-----------------------------------" >> $LOGFILE
1185
1186# scratchdir, mountlist(OUT)
1187    scratchdir=$MINDI_TMP
1188    mountlist=$1
1189
1190# NB: partition = device
1191# NB: mountpt = where the device is mounted
1192
1193    [ -e "$MY_FSTAB" ] || Die "Cannot find your fstab file ($MY_FSTAB)"
1194
1195    rm -f $mountlist
1196    mkdir -p $mountlist
1197    rm -Rf $mountlist
1198    > $mountlist
1199    echo -en "\rHang on...\r"
1200    all_partitions=""
1201   
1202    if [ $LVM != "false" ]; then
1203        echo -en "\rAnalyzing LVM...\r"
1204        $MINDI_LIB/analyze-my-lvm > $MINDI_TMP/lvm.res
1205        if [ $? -ne 0 ]; then
1206            LVM="false"
1207        fi
1208        all_partitions=`cat $MINDI_TMP/lvm.res | grep -F ">>>" | cut -d' ' -f2-32`
1209    fi
1210    all_partitions="$all_partitions `ListAllPartitions 2> /dev/null`"
1211#    echo "all partitions = $all_partitions" > /dev/stderr
1212    for i in $IMAGE_DEVS ; do
1213        mount | grep -F "$i " > /dev/null 2> /dev/null && Die "Sorry, $i is already mounted! CANNOT DO IMAGEDEV on it if it's mounted."
1214    done
1215    [ "$IMAGE_DEVS" != "" ] && all_partitions="`echo "$all_partitions $IMAGE_DEVS" | tr ' ' '\n' | sort -u | tr '\n ' ' '`"
1216    printf "        %-15s %-15s %-15s %-15s %-15s %-15s\n" DEVICE MOUNTPOINT FORMAT "SIZE (MB)" LABEL UUID | tee -a $LOGFILE
1217    useless_dev="/dev/floppy /dev/fd0h1440 /dev/fd0H1440 /dev/cdrom /dev/cdrom/cdrom /dev/cdrom/cdrom1 /dev/cdrom/cdrom2 /dev/cdrom0 /dev/cdrom1 /dev/cdrom2 /dev/cdrom3 /dev/cdrw /dev/scd /dev/ram :/ /dev/sr0 /dev/sr1 /dev/cdrom1"
1218    for c_p in $all_partitions ; do
1219        [ "`echo "$useless_dev" | grep -F "$c_p"`" != "" ] || [ "`echo "$c_p" | grep ":"`" != "" ] && continue
1220        [ "`echo "$c_p" | grep -x "/dev/cdroms.*"`" ] && continue
1221        if [ -h "$c_p" ] && [ "`echo "$c_p" | grep -F "/dev/hd"`" = "" ] && [ "`echo "$c_p" | grep -F "/dev/sd"`" = "" ] && [ "`echo "$c_p" | grep -F "/dev/md"`" = "" ] ; then
1222            current_partition=`readlink -f $c_p`
1223            [ "`echo "$current_partition" | grep -F "/dev/mapper"`" != "" ] && current_partition="$c_p"
1224            [ "`echo "$useless_dev" | grep -F "$current_partition"`" ] && continue
1225        else
1226            current_partition="$c_p"
1227        fi
1228        [ "$c_p" = "none" ] && continue
1229        redhat_label=""
1230        uuid=""
1231        absolute_partition=`readlink -f $c_p`
1232        partition_mountpt=`tr -s '\t' ' ' < $MY_FSTAB | /bin/grep -w "$current_partition" | /bin/grep -vx " *#.*" | $AWK '{print $2}' | head -n1`
1233
1234        # Detects noauto partitions not mounted and exclude them
1235        partition_option=`tr -s '\t' ' ' < $MY_FSTAB | /bin/grep -w "$current_partition" | /bin/grep -vx " *#.*" | $AWK '{print $4}' | head -n1`
1236        if [ "`echo "$partition_option" | grep -i noauto`" != "" ] && [ "`mount | grep -w "$partition_mountpt"`" =  "" ] ; then
1237            continue
1238        fi
1239
1240        # This part tries to retrieve the correct device from a LABEL line in /etc/fstab
1241        # current_partition contains only first column of /etc/fstab
1242        if [ "`echo "$current_partition" | /bin/grep -i "LABEL="`" != "" ]; then
1243            str_to_find_fmt_with=$current_partition
1244            redhat_label=`echo "$current_partition" | cut -d'=' -f2`
1245            actual_dev=""
1246
1247            # 1st try, findfs - the RHEL way of finding labels and their partitions
1248            if [ -x "/sbin/findfs" ]; then
1249                actual_dev=`/sbin/findfs LABEL=${redhat_label} 2> /dev/null`
1250            fi
1251   
1252            # 2nd try : blkid, the good way for all LABEL except swap
1253            if [ "x$actual_dev" = "x" -a -x "/sbin/blkid" ]; then
1254                actual_dev=`/sbin/blkid | /bin/grep "$redhat_label" | grep LABEL= | cut -d':' -f1`
1255                # For LVM FS it will give a /dev/dm-# which should then be converted
1256                if [ $LVM = "v2" ] && [ "`echo $actual_dev | grep '/dev/dm'`" ]; then
1257                    major=`/bin/ls -l $actual_dev | $AWK '{print $5}'`
1258                    minor=`/bin/ls -l $actual_dev | $AWK '{print $6}'`
1259                    for dev in `ls /dev/mapper/*`; do
1260                        major1=`/bin/ls -l $dev | $AWK '{print $5}'`
1261                        minor1=`/bin/ls -l $dev | $AWK '{print $6}'`
1262                        if [ $major1 = $major ] && [ $minor1 = $minor ]; then
1263                            actual_dev=`/bin/ls -l $dev | $AWK '{print $10}'`
1264                            break
1265                        fi
1266                    done
1267                fi
1268            fi
1269   
1270            # 3rd try, which works on a standard partition (ext2/3), but not on swap
1271            # For LVM gives a /dev/mapper entry
1272            if [ "x$actual_dev" = "x" ]; then
1273                actual_dev=`/bin/mount -l | /bin/grep "\[$redhat_label\]" | cut -d' ' -f1`
1274            fi
1275   
1276            # 4th try, with vol_id
1277            # SWAP only
1278            if [ "x$actual_dev" = "x" -a -x "/sbin/vol_id" ]; then
1279                list_swaps=`cat /proc/swaps | /bin/grep "/dev/" | $AWK '{ print $1 }' `
1280                for dev_swap in $list_swaps ; do
1281                    dev_exists=`/sbin/vol_id $dev_swap | /bin/grep "$redhat_label"`
1282                    if [ "x$dev_exists" != "x" ]; then
1283                        actual_dev=$dev_swap
1284                        break;
1285                    fi
1286                done
1287            fi
1288
1289            # 5th try : pre-formated LABEL. Format is : LABEL=SWAP-mydevice or SW-mydevice. e.g. : LABEL=SWAP-hda5
1290            # LABEL=SW-cciss/c0d0p3 (RDP)
1291            # or could be a string that isn't a complete device name (eg. LABEL =SWAP-cciss/c0d0p)
1292            # SWAP only
1293            if [ "x$actual_dev" = "x" -a  _"`echo $current_partition | /bin/grep -iE 'LABEL=SWAP|LABEL=SW-'`" != _"" ]; then
1294                    for try_dev in `tail +2 /proc/swaps | cut -d' ' -f1`
1295                    do
1296                        # Location of the swap label for kernel 2.6
1297                        try_dev_label=`dd bs=1 count=16 skip=1052 if=$try_dev 2> /dev/null`
1298                        if [ "x$try_dev_label" = "x$redhat_label" ]; then
1299                            actual_dev=$try_dev
1300                        fi
1301                    done
1302            fi
1303
1304            # Check if one of all those tries has known success
1305            if [ "x$actual_dev" != "x" ]; then
1306                current_partition=$actual_dev
1307            else
1308                Die "Your system uses a LABEL partition ($current_partition), but you lack the tool to support it.\nPlease replace labels with their correct devices in /etc/fstab or install findfs|blkid|vol_id"
1309            fi
1310        # This part tries to retrieve the correct device from a UUID line in /etc/fstab
1311        # current_partition contains only first column of /etc/fstab
1312        elif [ "`echo "$current_partition" | /bin/grep -i "UUID="`" != "" ]; then
1313            str_to_find_fmt_with=$current_partition
1314            uuid=`echo "$current_partition" | cut -d'=' -f2`
1315            actual_dev=""
1316
1317            # 1st try, findfs - the RHEL way of finding labels and their partitions
1318            if [ -x "/sbin/findfs" ]; then
1319                actual_dev=`/sbin/findfs UUID=${uuid} 2> /dev/null`
1320            fi
1321   
1322            # 2nd try : blkid, the good way for all LABEL except swap
1323            if [ "x$actual_dev" = "x" -a -x "/sbin/blkid" ]; then
1324                actual_dev=`/sbin/blkid | /bin/grep "$uuid" | grep UUID= | cut -d':' -f1`
1325                # For LVM FS it will give a /dev/dm-# which should then be converted
1326                if [ $LVM = "v2" ] && [ "`echo $actual_dev | grep '/dev/dm'`" ]; then
1327                    major=`/bin/ls -l $actual_dev | $AWK '{print $5}'`
1328                    minor=`/bin/ls -l $actual_dev | $AWK '{print $6}'`
1329                    for dev in `ls /dev/mapper/*`; do
1330                        major1=`/bin/ls -l $dev | $AWK '{print $5}'`
1331                        minor1=`/bin/ls -l $dev | $AWK '{print $6}'`
1332                        if [ $major1 = $major ] && [ $minor1 = $minor ]; then
1333                            actual_dev=`/bin/ls -l $dev | $AWK '{print $10}'`
1334                            break
1335                        fi
1336                    done
1337                fi
1338            fi
1339   
1340            # 3th try, with vol_id
1341            if [ "x$actual_dev" = "x" -a -x "/sbin/vol_id" ]; then
1342                list_dev=`mount | /bin/grep -E '^/' | $AWK '{ print $1 }' `
1343                for dev in $list_dev ; do
1344                    dev_exists=`/sbin/vol_id $dev | /bin/grep "$uuid"`
1345                    if [ "x$dev_exists" != "x" ]; then
1346                        actual_dev=$dev
1347                        break;
1348                    fi
1349                done
1350            fi
1351
1352            # Check if one of all those tries has known success
1353            if [ "x$actual_dev" != "x" ]; then
1354                current_partition=$actual_dev
1355            else
1356                Die "Your system uses a UUID partition ($current_partition), but you lack the tool to support it.\nPlease replace labels with their correct devices in $MY_FSTAB or install findfs|blkid|vol_id"
1357            fi
1358        else
1359            str_to_find_fmt_with=$current_partition
1360        fi
1361
1362        partition_format=`$AWK '$1 == "'"$str_to_find_fmt_with"'" {print $3}' $MY_FSTAB`
1363        # Some distributions such as Debian do not put /dev/<VG>/<LV> in fstab
1364        # for LVM partitions but use /dev/mapper/<VG>-<LV> instead. Fortunately,
1365        # the former is then a link to the latter, so we test whether
1366        # $current_partition is actually such a link or not and set
1367        # $current_lvolume accordingly. On Debian you may find more than one answer
1368        # so we remove the one corresponding to /dev/.static
1369        # On RedHat even if the device name is different (/dev/mapper/<VG><LV>), the
1370        # principle is the same and we need to find the link to it as well.
1371        # Note that $current_lvolume may well be an
1372        # ordinary device. It is just to make sure that we feed the right value
1373        # into any of the LVM tools if possible.
1374
1375        current_lvolume="$current_partition"
1376        if [ $LVM = "v2" ] && [ "`echo $current_partition | grep -E '^/dev/mapper/'`" ]; then
1377            # .static dir are a Debian specificity
1378            current_lvolume="`find /dev -lname "$current_partition" | grep -Ev '^/dev/\.static/'`"
1379            echo $current_lvolume | grep -q ' '
1380            if [ $? -eq 0 ]; then
1381                echo "WARNING: Multiple Logical Volumes found. Report to dev team" >> $LOGFILE 
1382            fi
1383        fi
1384        #
1385        # End of LVM device style variation code (other than $current_lvolume).
1386
1387        if [ $LVM != "false" ] && [ "`$LVMCMD lvdisplay $current_lvolume 2> /dev/null`" ]; then
1388            # Size computed via LVM not directly
1389            partition_size="lvm"
1390        else
1391            partition_size=`SizeOfPartition $current_partition`
1392            [ "`echo "$current_partition" | grep "[0-9]"`" = "" ] && continue
1393            [ "`echo "$current_partition" | grep -c "^/"`" -ne "1" ] && continue
1394            if [ "$partition_format" = "swap" ] || [ "$partition_mountpt" = "swap" ] ; then
1395                partition_size=`grep -Fv "Priority" /proc/swaps | tr -s '\t' ' ' | grep -F "$current_partition" | $AWK '{print $3}'`
1396                [ "$partition_mountpt" != "swap" ] && partition_mountpt="swap"
1397                [ "$partition_format" != "swap" ] && partition_format="swap"
1398                if [ "$partition_size" = "" ] ; then
1399                    totalsize=0
1400                    items=0
1401                    for i in `tr -s ' ' '\t' < /proc/swaps | grep -Fv "Filename" | cut -f3` ; do
1402                        totalsize=$(($totalsize+$i))
1403                        items=$(($items+1))
1404                    done
1405                    [ "$items" -gt "0" ] && partition_size=$(($totalsize/$items)) || partition_size=0
1406                    [ "$partition_size" -lt "125000" ] && partition_size=125000
1407                    echo "I'm guessing $c_p is $(($partition_size/1024))MB" >> $LOGFILE
1408                fi
1409            fi
1410        fi
1411        [ "$partition_mountpt" = "swap" ] && partition_format="swap"
1412        [ "$partition_format" = "swap" ] && partition_mountpt="swap"
1413        if [ "$partition_mountpt" = "" ] ; then
1414            if [ "`$LVMCMD pvdisplay $current_lvolume 2> /dev/null`" != "" ] ; then
1415                if  [ "`grep -F device /etc/raidtab 2> /dev/null | grep -w $current_partition`" ] ; then
1416                    partition_mountpt="raid"
1417                    partition_format="raid"
1418                else
1419                    partition_mountpt="lvm"
1420                    partition_format="lvm"
1421                fi
1422            fi
1423        fi
1424        psz=$partition_size
1425        echo "Examining $current_partition (mount=$partition_mountpt fmt=$partition_format psz=$psz)" >> $LOGFILE
1426        [ "$psz" != "lvm" ] && psz=$(($psz/1024))
1427        if [ "`echo " $IMAGE_DEVS " | grep -F " $current_partition "`" != "" ] ; then
1428            partition_mountpt="image"
1429            old_partition_fmt=$partition_format
1430            partition_format="`$FDISK -l 2>> $LOGFILE | tr '*' ' ' | tr '+' ' ' | tr -s ' ' '\t' | grep -w "$absolute_partition" | cut -f5`"
1431            partition_size=$(($partition_size+1)); # just in case
1432            if [ "$partition_format" = "Linux" ] ; then
1433                echo "Are you imaging a mounted swap partition? Silly..." >> $LOGFILE
1434                echo "Reverting format from $old_partition_fmt to $partition_format" >> $LOGFILE
1435                partition_format=$old_partition_fmt
1436            fi
1437        fi
1438        if [ "$EXCLUDE_DEVS" ] && [ "`echo " $EXCLUDE_DEVS " | grep -F " $current_partition "`" ] || [ "`echo " $EXCLUDE_DEVS " | grep " $current_partition "`" ] ; then
1439            echo "Excluding $current_partition from mountlist" >> $LOGFILE
1440            continue
1441        fi
1442        if [ ! "$partition_mountpt" ] ; then
1443            echo "------- $FDISK -l $qq log ------------" >> $LOGFILE
1444            for qq in "" `find /dev/ida/c*d* ! -name '*p*'` ; do
1445                partition_format=`$FDISK -l $qq 2>> $LOGFILE | grep -w "$c_p" | sed 's/12/|/' | tr -s '\t' ' ' | cut -d'|' -f2 | cut -d' ' -f2-9`
1446                [ "$partition_format" ] && break
1447            done
1448            echo "------- $FDISK log end ------------" >> $LOGFILE
1449            if [ "$partition_format" = "Compaq diagnostics" ] ; then
1450                partition_format="compaq"
1451            elif [ ! "`grep -F device /etc/raidtab 2> /dev/null | grep -w $current_partition`" ] ; then
1452                LogIt "Unable to find mountpoint of $current_partition - ignoring"
1453                continue
1454            fi
1455        fi
1456        partition_format="`echo "$partition_format" | cut -d',' -f1`"; # in case user has ext3,ext2 or something dumb like that
1457        [ "$partition_format" = "auto" ] && partition_format="`mount | grep -w $current_partition | $AWK '{print$5;}'`"; # in case user uses 'auto' (dumb!)
1458        unofficial_outstring=`printf "\t%-15s %-15s %-15s %7s %-15s %-15s\n" $current_partition $partition_mountpt $partition_format $psz "$redhat_label" $uuid`
1459        if [ "$current_partition" = "" ] ; then
1460            echo "Unknown partition (outstring = $unofficial_outstring)" >> $LOGFILE
1461        elif [ "$partition_mountpt" = "" ] && [ -f "/etc/raidtab" ] ; then
1462            if [ "`grep -F device /etc/raidtab 2>/dev/null | grep -F $current_partition`" ] ; then
1463                partition_mountpt=raid
1464                partition_format=raid
1465                printf "\t%-15s %-15s %-15s %7s %-15s %-15s\n" $current_partition $partition_mountpt $partition_format $psz "$redhat_label" $uuid | tee -a $LOGFILE
1466                printf "%s %s %s %s %s %s\n" $current_partition $partition_mountpt $partition_format $partition_size "$redhat_label" $uuid >> $mountlist
1467            else
1468                echo "Unknown mountpoint (outstring = $unofficial_outstring)" >> $LOGFILE
1469            fi
1470        elif [ "$partition_format" = "" ] ; then
1471            echo "Unknown format (outstring = $unofficial_outstring)" >> $LOGFILE
1472        elif [ "$partition_size" = "" ] ; then
1473            echo "Unknown partition size (outstring = $unofficial_outstring)" >> $LOGFILE
1474        elif [ "$partition_mountpt" = "/proc" ] || [ "$partition_mountpt" = "/dev/pts" ] ; then
1475            continue
1476        else
1477            if [ "$partition_format" = "dos" ] || [ "$partition_format" = "msdos" ] ; then
1478                echo "Stupid bastard..." >> $LOGFILE
1479                partition_format="vfat"
1480            fi
1481            printf "\t%-15s %-15s %-15s %7s %-15s %-15s\n" $current_partition $partition_mountpt $partition_format $psz "$redhat_label" $uuid | tee -a $LOGFILE
1482            printf "%s %s %s %s %s %s\n" $current_partition $partition_mountpt $partition_format $partition_size "$redhat_label" $uuid >> $mountlist
1483        fi
1484    done
1485}
1486
1487
1488MakeSureNumberIsInteger() {
1489    res=`echo "$1" | tr -s '\-[0-9]' ' '`
1490    if [ "$res" != " " ] && [ "$res" != "" ] ; then
1491    echo "result = '$res'"
1492        Die "$1 should be an integer"
1493    fi
1494}
1495
1496
1497MakeSyslinuxMessageFile() {
1498    mkdir -p $1
1499    rmdir $1
1500    echo -en "  " > $1
1501    if [ "`grep -Fi "debian" /etc/issue.net 2> /dev/null`" ] ; then
1502        sed s/ZZZZZ/$MINDI_VERSION/ $MINDI_LIB/msg-txt | sed s/YYYYY/"Mondo Rescue"/ | sed s/XXXXX/"a cousin of"/ | sed s%DDDDD%"Debian GNU\/`uname -s` `cut -d ' ' -f 3 /etc/issue.net` `hostname`"% | sed s/KKKKK/"Kernel `uname  -r` on a `uname -m`"/ | sed s/TTTTT/"`LC_TIME=C date`"/ >> $1.tmp
1503    else
1504        sed s/ZZZZZ/$MINDI_VERSION/ $MINDI_LIB/msg-txt | sed s/YYYYY/"Mondo Rescue"/ | sed s/XXXXX/"a cousin of"/ | sed s/DDDDD/"`grep -i "linux" /etc/issue.net | head -n1 | tr -s ' ' ' '`"/ | sed s/KKKKK/"`grep -i "kernel" /etc/issue.net | head -n1 | tr -s ' ' ' '`"/ | sed s/TTTTT/"`LC_TIME=C date`"/ | sed s/' '\\r' 'on' 'an' '\/' '`uname -r`' 'on' 'an' '`uname -m`/ >> $1.tmp
1505    fi
1506    sed s/%r/"`uname -r`"/ $1.tmp | sed s/%t/"`hostname`"/ > $1
1507    rm -f $1.tmp
1508    if [ "$CDRECOVERY" != "yes" ] ; then
1509        if [ -e "$MINDI_TMP/NFS-DEV" ] ; then
1510            echo -en "Press <enter> to continue.\n" >> $1
1511        elif [ ! "$MINDI_TMP" ] ; then
1512            echo -en "FYI, this is _not_ a Mondo Rescue CD.\n" >> $1
1513            if [ -e "$MINDI_LIB/memtest.img" ] ; then
1514                echo -en "Type 'memtest' <Enter> to test your PC's memory intensively.\nJust press <Enter> to go to the main test menu.\n" >> $1
1515            fi
1516        else
1517            echo -en "$BOOT_MEDIA_MESSAGE" >> $1
1518        fi
1519    else
1520        echo -en " \
1521To restore your disk to factory defaults, type 'RESTORE' <enter>.\n\
1522CAUTION: THIS WILL ERASE YOUR WHOLE DISK !!!\n" >> $1
1523    fi
1524}
1525
1526
1527MoveHyperlinkSensibly() {
1528    local filename minidir_root resides_on_diskno noof_disks old_diskno d old_pwd
1529    filename=$1
1530    minidir_root=$2
1531    resides_on_diskno=$3
1532    noof_disks=$4
1533
1534    [ -h "$minidir_root/$resides_on_diskno/$filename" ] || Die "$filename isn't a softlink (or doesn't exist): how can I move it sensibly?"
1535
1536    old_diskno=$resides_on_diskno
1537    d=1
1538    while [ "$d" -le "$noof_disks" ] ; do
1539        if [ "$d" -ne "$old_diskno" ] ; then
1540            old_pwd=`pwd`
1541            cd $minidir_root/$old_diskno
1542            cp --parents -Rdf $filename $minidir_root/$d/ 2>> $LOGFILE || Die "Can't move $filename (sensibly) from $old_diskno to $d"
1543            rm -f $filename
1544            cd $old_pwd
1545        fi
1546# when the softlink is resolvable, our work here is done
1547        [ -e "$minidir_root/$d/$filename" ] && return 0
1548        old_diskno=$d
1549        d=$(($d+1))
1550    done
1551    return 1
1552}
1553
1554
1555
1556OfferToCopyImagesToDisks() {
1557    local imagesdir i imagename dev count boot_dev data_dev
1558    imagesdir=$1
1559    boot_dev=$2
1560    data_dev=$3
1561
1562    echo -en "Would you like to create boot+data floppy disks now (y/[n]) ?"
1563    read i
1564    [ "$i" != "y" ] && [ "$i" != "Y" ] && return
1565    mount | grep -F /dev/fd > /dev/null && Die "Please unmount your floppies first."
1566    echo "WARNING! THIS WILL ERASE YOUR FLOPPY DISKS."
1567    [ ! -e "$boot_dev" ] && Die "Cannot find $boot_dev - is your Linux distro broken?"
1568    [ ! -e "$data_dev" ] && Die "Cannot find $data_dev - is your Linux distro broken?"
1569    i=`find $imagesdir -type f | grep -F "/mindi-root.1" 2> /dev/null`
1570    j=`find $imagesdir -type f | grep -F "/mindi-boot" | grep -Ev "2880|$BOOT_SIZE"`
1571    if [ "$i" ] ; then
1572        CopyImageToDisk $j $data_dev "boot disk"
1573        CopyImageToDisk $i $data_dev "root disk"
1574    else
1575        CopyImageToDisk $j $boot_dev "boot/root disk"
1576    fi
1577    count=1
1578    for i in `find $imagesdir | grep -F mindi-data` ; do
1579        CopyImageToDisk $i $data_dev "data disk #$count"
1580        count=$(($count+1))
1581    done
1582}
1583
1584
1585OfferToMakeBootableISO() {
1586    local i old_pwd
1587    if [ "$PROMPT_MAKE_CD_IMAGE" = "yes" ] && [ _"$MONDO_SHARE" = _"" ] ; then
1588        echo -en "Shall I make a bootable CD image? (y/[n]) "
1589        read i
1590        [ "$i" != "y" ] && [ "$i" != "Y" ] && return 0
1591    fi
1592    if [ ! "$MINDI_TMP" ] ; then
1593        LogIt "NB: Mindi's bootable CD always uses isolinux."
1594        LogIt "For a bootable CD w/LILO, please use Mondo."
1595    fi
1596    rm -Rf $MINDI_TMP/iso
1597    mkdir -p $MINDI_TMP/iso/{images,archives,isolinux}
1598    cp -f $1/*.img $1/*.gz $MINDI_TMP/iso/images 2>> $LOGFILE || LogIt "OfferToMakeBootableISO: Cannot copy $i to $MINDI_TMP/iso/images"
1599    old_pwd=`pwd`
1600    cd $MINDI_TMP/iso
1601    echo "mindi_lib = $MINDI_LIB" >> $LOGFILE
1602    for i in memdisk memtest.bin memtest.img ; do
1603        j=$MINDI_LIB/$i
1604        k=$MINDI_TMP/iso/isolinux
1605        if [ -e "$j" ] ; then
1606            LogIt "Copying $j to $k"
1607            cp -f $j $k 2> /dev/null || Die "Failed to copy $j to $k"
1608            cp -f $j $MINDI_TMP 2> /dev/null || Die "Failed to copy $j to $MINDI_TMP"
1609            cp -f $j $MONDO_ROOT 2>> $LOGFILE || Die "Failed to copy $j to $MONDO_ROOT"
1610        fi
1611    done
1612    MakeSyslinuxMessageFile $MINDI_TMP/iso/isolinux/message.txt
1613    cp $kernelpath $MINDI_TMP/iso/isolinux/vmlinuz 2> /dev/null || Die "Cannot copy vmlinuz ($kernelpath) to mondo root ($MINDI_TMP/iso/isolinux/vmlinuz). Did you run out of disk space?"
1614    cp $kernelpath $MONDO_ROOT/vmlinuz 2> /dev/null || Die "Cannot copy vmlinuz ($kernelpath) to mondo root ($MONDO_ROOT/vmlinuz). Did you run out of disk space?"
1615    cp $MINDI_TMP/mindi.rdz $MINDI_TMP/iso/isolinux/initrd.img 2>> $LOGFILE
1616    cp $MINDI_TMP/mindi.rdz $MONDO_ROOT/initrd.img 2>> $LOGFILE
1617    [ -e "$iso_cfg_file" ] || Die "FIXME - unable to find $iso_cfg_file - this should never occur"
1618    cd $MINDI_TMP/iso/isolinux
1619    cat $iso_cfg_file | HackSyslinuxFile $ramdisk_size $MINDI_TMP/iso > isolinux.cfg || Die "Cannot copy isolinux.cfg from mindi_home to tmp_root - did you run out of disk space?"
1620    if [ -e "$MINDI_TMP/NFS-DEV" ] ; then
1621        mv isolinux.cfg isolinux.cfg.old
1622        sed s/interactive/iso/ isolinux.cfg.old > isolinux.cfg
1623    fi
1624    if [ "$ARCH" != "ia64" ] ; then
1625        cp $ISOLINUX isolinux.bin 2> /dev/null || Die "Cannot copy isolinux.bin ($ISOLINUX) to tmp_root - did you run out of disk space?"
1626        cp $ISOLINUX ../ 2>> $LOGFILE
1627    fi
1628    cd $MINDI_TMP/iso
1629    if [ "$ARCH" != "ia64" ] ; then
1630        cp -f $MINDI_TMP/iso/isolinux/{isolinux.cfg,initrd.img,vmlinuz,isolinux.bin,message.txt} $MONDO_ROOT 2> /dev/null || Die "Cannot copy core files to ramdisk for boot disk. Did you run out of disk space?"
1631    fi
1632    [ "$MONDO_SHARE" ] && cp -f $MONDO_SHARE/autorun .    2>> $LOGFILE
1633    if [ "$ARCH" != "ia64" ] ; then
1634        mkisofs -U -J -r -o $CACHE_LOC/mindi.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table . > /dev/null 2> $MINDI_TMP/mkisofs.log
1635    else
1636        mkisofs -J -r -o $CACHE_LOC/mindi.iso -b images/mindi-bootroot.$BOOT_SIZE.img -c isolinux/boot.cat -no-emul-boot . > /dev/null 2> $MINDI_TMP/mkisofs.log
1637    fi
1638    if [ "$?" -ne "0" ] ; then
1639        echo "----------- mkisofs's errors --------------" >> $LOGFILE
1640        cat $MINDI_TMP/mkisofs.log >> $LOGFILE
1641        echo "mkisofs returned the following errors:-"
1642        cat $MINDI_TMP/mkisofs.log
1643        LogIt "Failed to create ISO image."
1644    else
1645        echo "Created bootable ISO image at $CACHE_LOC/mindi.iso" >> $LOGFILE
1646    fi
1647    rm -f $MINDI_TMP/mkisofs.log
1648    cd $old_pwd
1649}
1650
1651
1652PluralOrNot() {
1653    [ "$1" -gt "1" ] && echo -en "s"
1654}
1655
1656
1657MakeMessageFile() {
1658    local disksize
1659    disksize=$1
1660    if [ "`grep -Fi "debian" /etc/issue.net 2> /dev/null`" ] ; then
1661        sed s/ZZZZZ/$MINDI_VERSION/ $MINDI_LIB/msg-txt | sed s/YYYYY/"Mondo Rescue"/ | sed s/XXXXX/"a cousin of"/ | sed s%DDDDD%"Debian GNU\/`uname -s` `cut -d ' ' -f 3 /etc/issue.net` `hostname`"% | sed s/KKKKK/"Kernel `uname  -r` on a `uname -m`"/ | sed s/TTTTT/"`LC_TIME=C date`"/
1662    else
1663        sed s/ZZZZZ/$MINDI_VERSION/ $MINDI_LIB/msg-txt | sed s/YYYYY/"Mondo Rescue"/ | sed s/XXXXX/"a cousin of"/ | sed s/DDDDD/"`grep -i "linux" /etc/issue.net | head -n1 | tr -s ' ' ' '`"/ | sed s/KKKKK/"`grep -i "kernel" /etc/issue.net | head -n1 | tr -s ' ' ' '`"/ | sed s/TTTTT/"`LC_TIME=C date`"/ | sed s/' 'r' 'on' 'an' 'm/' '`uname -r`' 'on' 'an' '`uname -m`/
1664    fi
1665    if [ "$disksize" -gt "2880" ] ; then
1666        if [ _"$MONDO_SHARE" != _"" ] ; then
1667            if [ "$CDRECOVERY" != "yes" ] ; then
1668                if [ -e "$MINDI_TMP/NFS-DEV" ] ; then
1669                    echo -en "Press <enter> to continue.\n"
1670                elif [ ! "$MINDI_TMP" ] ; then
1671                    echo -en "FYI, this is _not_ a Mondo Rescue CD.\n"
1672                else
1673                    echo -en "$BOOT_MEDIA_MESSAGE"
1674                fi
1675            fi
1676        fi
1677    fi
1678    if [ "$CDRECOVERY" = "yes" ] ; then
1679        echo -en "\
1680To restore your disk to factory defaults, type 'RESTORE' <enter>.\n\
1681CAUTION: THIS WILL ERASE YOUR WHOLE DISK !!!\n"
1682    fi
1683    echo -en "\n\n\n"
1684}
1685
1686
1687write_full_floppy_of_kernel() {
1688    local mtpt image old_pwd res disksize
1689
1690    res=0
1691    old_pwd=`pwd`
1692    KERN_DISK_MADE=1
1693    disksize=$3
1694    rand1=$RANDOM
1695    rand2=$RANDOM
1696    image=$MINDI_TMP/$rand1.$rand2.img
1697    mtpt=$MINDI_TMP/$rand1.$rand2.mtpt
1698    dd if=/dev/zero of=$image bs=1k count=$disksize &> /dev/null
1699    mke2fs -N 26 -F $image &> /dev/null
1700    mkdir -p $mtpt
1701    mount -o loop $image $mtpt
1702    cd $mtpt
1703    mkdir -p {dev,tmp,boot}
1704    cp -f $1 vmlinuz 2>> $LOGFILE
1705    if [ "$?" -ne "0" ] ; then
1706        LogIt "Failed to copy $1 to ramdisk"
1707        cd $old_pwd
1708        umount $mtpt
1709        rmdir $mtpt
1710        rm $image
1711        return 1
1712    fi
1713
1714    rdev vmlinuz 2,0
1715    rdev -R vmlinuz 0
1716    rdev -r vmlinuz 49152
1717
1718    tar -zxf $MINDI_LIB/dev.tgz || LogIt "Cannot untar dev.tgz"
1719    # BERLIOS : Useless and generating errors on CentOS ? (losetup miss a param)
1720    #losetup /dev/loop0 > /dev/null 2> /dev/null
1721    #[ "$?" -eq "0" ] || losetup /dev/loop0 -d || Die "Please free up /dev/loop0 by typing 'losetup /dev/loop0 -d'.\nReboot if necessary."
1722    CopyBootBFile $mtpt/boot.b
1723
1724    MakeLiloConfFile $disksize >> bdlilo.conf
1725
1726    chmod 644 bdlilo.conf
1727    MakeMessageFile $disksize > message
1728    lilo -v -C bdlilo.conf -r $mtpt
1729    res=$?
1730
1731    cd $old_pwd
1732    umount $mtpt
1733    mv -f $image $2
1734    rmdir $mtpt
1735
1736    return $res
1737}
1738
1739
1740MakeLiloConfFile() {
1741    local disksize options i ooo
1742    disksize=$1
1743    options=""
1744
1745    if [ "$ARCH" != "ia64" ] ; then
1746        echo -en "boot=/dev/loop0\ndisk=/dev/loop0\n"
1747    fi
1748    if [ "$disksize" -eq "2880" ] ; then
1749        echo -en "bios=0x00\nsectors=36\nheads=2\ncylinders=80\n"
1750    elif [ "$disksize" -eq "1722" ] ; then
1751        echo -en "bios=0x00\nsectors=21\nheads=2\ncylinders=82\n"
1752    elif [ "$disksize" -gt "2880" ] ; then
1753        /bin/true
1754    else
1755        echo -en "bios=0x00\nsectors=18\nheads=2\ncylinders=80\n"
1756    fi
1757    if [ "$ARCH" != "ia64" ] ; then
1758        echo -en "install=/boot.b\nmap=/boot.map\n"
1759    fi
1760    if [ "$CDRECOVERY" = "yes" ] ; then
1761        echo -en "default=RESTORE\n"
1762    elif [ "$disksize" -gt "2880" ] && [ _"$MONDO_SHARE" != _"" ] ; then
1763        if [ -e "$MINDI_TMP/NFS-DEV" ] ; then
1764            echo -en "default=iso\n"
1765        else
1766            echo -en "default=interactive\n"
1767        fi
1768    else
1769        echo -en "default=expert\n"
1770    fi
1771
1772    echo -en "prompt\n"
1773    if [ "$ARCH" != "ia64" ] ; then
1774        echo -en "vga=normal\nbackup=/dev/null\nmessage=/message\n"
1775    fi
1776    if [ "$CDRECOVERY" != "yes" ] ; then
1777        echo -en "timeout=300\n"
1778    fi
1779    echo -en "\n"
1780    if [ "$CDRECOVERY" = "yes" ] ; then
1781        options="RESTORE expert"
1782    elif [ "$disksize" -gt "2880" ] ; then
1783        if [ _"$MONDO_SHARE" != _"" ] ; then
1784            if [ -e "$MINDI_TMP/NFS-DEV" ] ; then
1785                options="iso"
1786            else
1787                options="interactive expert compare iso nuke isonuke"
1788# hda hdb hdc hdd"
1789            fi
1790        else
1791            options="expert"
1792        fi
1793    else
1794        options="expert"
1795    fi
1796    for i in $options ; do
1797        ooo=$i
1798        [ "$ooo" = "RESTORE" ] && ooo="nuke"
1799        if [ "$ARCH" = "ia64" ] ; then
1800            rootpart="root=/dev/ram0\n\t"
1801        else
1802            rootpart=""
1803        fi
1804        outstr="image=/vmlinuz\n\tlabel=$i\n\tinitrd=/mindi.rdz\n\t${rootpart}append=\" rw ramdisk=$ramdisksize ramdisk_size=$ramdisksize maxcpus=1 $ooo_mode $ADDITIONAL_BOOT_PARAMS"
1805   
1806        outstr=$outstr" $ooo_mode"
1807        outstr=$outstr"\"\n"
1808        if [ "$disksize" = "1440" ] ; then
1809            echo -en "$outstr" | sed s/initrd=.*// | grep -v root=
1810        else
1811            echo -en "$outstr"
1812        fi
1813    done
1814}
1815
1816
1817PrepareBootDiskImage_LILO() {
1818    local disksize imagesdir dev imagefile mountpoint fname i kernelpath ramdisksize cfg_file testpath options retval outstr old_pwd ooo max_kernel_size liloconf
1819    imagesdir=$1
1820    disksize=$2
1821    kernelpath=$3
1822    ramdisksize=$4
1823
1824    retval=0
1825    [ ! -e "$kernelpath" ] && Die "PBDI - cannot find $kernelpath kernel"
1826    echo -en "Making "$disksize"KB boot disk..."
1827    TurnTgzIntoRdz $MINDI_LIB/rootfs $MINDI_TMP/mindi.rdz $ramdisksize $disksize `du -sk $kernelpath | cut -f1` || Die "Could not turn rootfs into mindi.rdz; are you SURE your kernel supports loopfs?"
1828    if [ "$ARCH" != "ia64" ] ; then
1829        [ "$disksize" != "1722" ] && [ "$disksize" != "2880" ] && [ "$disksize" != "$BOOT_SIZE" ] && Die "PDBI - disksize is $disksize - bad size"
1830    fi
1831    echo -en "..."
1832    imagefile=$imagesdir/mindi-bootroot.$disksize.img
1833    mountpoint=$MINDI_TMP/mountpoint.$$
1834    mkdir -p $mountpoint
1835    dd if=/dev/zero of=$imagefile bs=1k count=$disksize &> /dev/null || Die "Cannot dd blank file"
1836    if [ "$ARCH" = "ia64" ] ; then
1837        mkdosfs $imagefile >> $LOGFILE 2>> $LOGFILE
1838        t=vfat
1839    else
1840        mke2fs -N 26 -m 0 -F $imagefile >> $LOGFILE 2>> $LOGFILE
1841        t=ext2
1842    fi
1843    mount -t $t -o loop $imagefile $mountpoint || LogIt "Cannot mount (PBDI)"
1844    # copy Mindi's skeleton fs & lilo/syslinux/whatever stuff into it
1845    mkdir -p $mountpoint/etc
1846    if [ "$ARCH" != "ia64" ] ; then
1847        liloconf=$mountpoint/etc/lilo.conf
1848    else
1849        liloconf=$mountpoint/elilo.conf
1850    fi
1851    old_pwd=`pwd`
1852    cd $mountpoint
1853    if [ "$ARCH" != "ia64" ] ; then
1854        tar -zxf $MINDI_LIB/dev.tgz || LogIt "Cannot untar dev.tgz"
1855    fi
1856    cd $old_pwd
1857    # BERLIOS : Useless and generating errors on CentOS ? (losetup miss a param)
1858    #losetup /dev/loop0 > /dev/null 2> /dev/null
1859    #[ "$?" -eq "0" ] || losetup /dev/loop0 -d || Die "Please free up /dev/loop0 by typing   'losetup /dev/loop0 -d'.\nReboot if necessary."
1860    CopyBootBFile $mountpoint/boot.b
1861
1862    MakeLiloConfFile $disksize > $liloconf
1863
1864    # Copy it so that CD-ROM menu entry is satisfied
1865    if [ "$ARCH" = "ia64" ] ; then
1866        mountefi=0
1867        df -T | grep /boot/efi | grep -q vfat
1868        if [ $? -ne 0 ]; then
1869            mount /boot/efi
1870            if [ $? -ne 0 ]; then
1871                echo "You have to mount your EFI partition when using mindi"
1872                MindiExit -1
1873            fi
1874            mountefi=1
1875        fi
1876        cp /boot/efi/elilo.efi $mountpoint
1877        cp $liloconf $mountpoint/elilo.efi $mountpoint/efi/boot
1878        if [ $mountefi -eq 1 ]; then
1879            umount /boot/efi 2>&1 > /dev/null
1880        fi
1881    fi
1882
1883    echo "Copying $MINDI_TMP/mindi.rdz to $mountpoint..." >> $LOGFILE
1884    cp -f $MINDI_TMP/mindi.rdz $mountpoint 2>> $LOGFILE
1885    if [ "$?" -ne "0" ] ; then
1886        LogIt "Failed to copy $MINDI_TMP/mindi.rdz to $mountpoint"
1887        cat $MINDI_TMP/mtpt.$$ >> $LOGFILE
1888        LogIt "Please unload some of your modules and try again."
1889        rm -f $MINDI_TMP/mtpt.$$
1890        LogIt "Cannot incorporate mindi.rdz in bootdisk (kernel / modules too big?)"
1891        retval=$(($retval+1))
1892    fi
1893    MakeMessageFile $disksize > $mountpoint/message
1894
1895    mkdir -p $mountpoint/tmp
1896    cp -f $MINDI_TMP/mondo-restore.cfg $mountpoint/tmp &> /dev/null
1897    if [ -e "$MINDI_LIB/memtest.img" ] ; then
1898        echo -en "image=/memtest.bin\nlabel=memtest\nn" >> $liloconf
1899        echo -en "image=/memdisk\nlabel=memtest\nappend=\"initrd=memtest.img\"\n" >> $liloconf
1900#        echo "Yep, this is a multi-function CD" > $mountpoint/MULTIFUNC
1901    fi
1902
1903    # copy the kernel across
1904    [ "$mountpoint" != "" ] && rm -Rf $mountpoint/lost+found
1905    dd if=/dev/zero of=$mountpoint/zero bs=1k count=16 &> /dev/null
1906    free_space=`df -k -P $mountpoint | tail -n1 | tr -s ' ' '\t' | cut -f4`
1907    cp -f $kernelpath $mountpoint/vmlinuz > /dev/null 2> /dev/null
1908    if [ "$?" -ne "0" ] || [ "$FORCE_DUAL_FLOPPIES" = "yes" ] ; then
1909        echo "Files at mountpoint ($mountpoint) :-" >> $LOGFILE
1910        du -sk $mountpoint/* >> $LOGFILE
1911        echo "--- end of list of files ---" >> $LOGFILE
1912        echo -en "Kernel size = `du -sk $kernelpath | cut -f1` K\nRamdisk free = $free_space K\n\
1913Sorry, your kernel is too big for a boot/root floppy.\nI'll try the new boot/root two-disk thingy.\n" >> $LOGFILE
1914        rm -f $mountpoint/vmlinuz
1915        cd $old_pwd
1916        umount $mountpoint || Die "Cannot unmount mountpoint ($mountpoint)"
1917        rmdir $mountpoint || LogIt "Cannot rmdir (PBDI)"
1918    #   losetup /dev/loop0 -d
1919        res=0
1920        write_full_floppy_of_kernel $kernelpath $imagesdir/mindi-boot.1440.img 1440
1921        res=$(($res+$?))
1922        cp -f $MINDI_TMP/mindi.rdz $imagesdir/mindi-root.1440.img 2>> $LOGFILE
1923        res=$(($res+$?))
1924        rm -f $imagefile
1925        if [ "$res" -ne "0" ]; then
1926            LogIt "WARNING - failed to create 1.44MB boot/root floppies"
1927            rm -f $imagesdir/mindi-*.1440.img
1928        fi
1929        return $res
1930    fi
1931    free_space=`df -k -P $mountpoint | tail -n1 | tr -s ' ' '\t' | cut -f4`
1932    max_kernel_size=$(($free_space+`du -sk $kernelpath | cut -f1`))
1933    echo "Free space left on floppy = $free_space KB" >> $LOGFILE
1934    echo "Max kernel size on $disksize KB floppy (est'd) = $max_kernel_size K" >> $LOGFILE
1935# make it bootable
1936    rm -f $mountpoint/zero
1937    [ -e "$MINDI_LIB/memdisk" ] && cp -f $MINDI_LIB/memdisk $mountpoint 2>> $LOGFILE
1938    if [ "$disksize" -gt "2880" ] && [ ! "$KERN_DISK_MADE" ] ; then
1939        if [ "$ARCH" != "ia64" ] ; then
1940        $LILO_EXE $LILO_OPTIONS -r $mountpoint >> $LOGFILE 2>> $LOGFILE
1941        else
1942            /bin/true
1943        fi
1944    elif [ ! "$KERN_DISK_MADE" ] ; then
1945# 12/28/2001 - if 1.72MB floppy then don't use LILO's optimizations at all
1946        $LILO_EXE -r $mountpoint >> $LOGFILE 2>> $LOGFILE
1947    else
1948        echo "Not running LILO. It's not that kind of disk." >> $LOGFILE
1949    fi
1950
1951    # BERLIOS  does not test necessarily what it expects
1952    if [ $? -ne "0" ] ; then
1953        if [ "`grep -F "/tmp/dev.0" $LOGFILE`" ] ; then
1954            LogIt "The '/tmp/dev.0' error is NOT Mindi's fault. It is LILO's."
1955            LogIt "Please reboot your PC as a workaround."
1956            Die "LILO sneezed and Mindi caught a cold. Please read the README / FAQ."
1957        fi
1958        echo "$LILO_EXE -r $mountpoint ...failed."
1959        echo -en "Press ENTER to continue."; read line
1960        LogIt "Cannot run lilo on $mountpoint\nPlease upgrade/downgrade your version of LILO. It has a bug."
1961        retval=$(($retval+1))
1962    fi
1963    cp -f $liloconf $MINDI_TMP/lilo.conf 2>> $LOGFILE
1964    if [ "$ARCH" = "ia64" ] ; then
1965        cp `dirname $kernelpath`/*.efi $mountpoint 2>> $LOGFILE
1966    fi
1967    umount $mountpoint || Die "Cannot unmount mountpoint ($mountpoint)"
1968    echo -en "..."
1969    rmdir $mountpoint || LogIt "Cannot rmdir (PBDI)"
1970    if [ "$retval" -eq "0" ] ; then
1971        echo -en "...$DONE\r"
1972        if [ "$KERN_DISK_MADE" ] ; then
1973            LogIt "... $disksize KB boot disks were created OK\r"
1974        fi
1975    else
1976        echo -en "...failed\r"
1977        LogIt $disksize"KB boot disk was NOT created\r"
1978        rm -f $imagefile
1979    fi
1980    [ "$retval" -ne "0" ] && LogIt "PrepareBootDiskImage() is returning nonzero"
1981    return $retval
1982}
1983
1984
1985PrepareBootDiskImage_SYSLINUX() {
1986    local disksize imagesdir dev imagefile mountpoint fname i kernelpath ramdisksize cfg_file testpath options retval outstr old_pwd ooo max_kernel_size bootimage
1987    imagesdir=$1
1988    disksize=$2
1989    kernelpath=$3
1990    ramdisksize=$4
1991    do_boot_root_thingy=""
1992    local retval old_pwd
1993    retval=0
1994
1995    [ ! -e "$kernelpath" ] && Die "PBDI - cannot find $kernelpath kernel"
1996    echo -en "Making "$disksize"KB boot disk..."
1997    TurnTgzIntoRdz $MINDI_LIB/rootfs $MINDI_TMP/mindi.rdz $ramdisksize $disksize `du -sk $kernelpath | cut -f1` || Die "Could not turn rootfs into mindi.rdz; are you SURE your kernel supports loopfs?"
1998    [ "$disksize" != "1722" ] && [ "$disksize" != "2880" ] && [ "$disksize" != "$BOOT_SIZE" ] && Die "PDBI - disksize is $disksize - bad size"
1999    echo -en "..."
2000    imagefile=$imagesdir/mindi-bootroot.$disksize.img
2001    mountpoint=$MINDI_TMP/mountpoint.$$
2002    mkdir -p $mountpoint
2003# If I format a 1722KB data file & run syslinux on it, the resultant image
2004# won't boot. So, I have formatted a floppy, called syslinux on/to it, and
2005# used 'dd' to copy it to sys-disk.raw (zipped to sys-disk.raw.gz).
2006# If I extract it, mount it, copy my files to it, etc. then the resultant
2007# image _is_ bootable. I don't know why syslinux and/or mkfs.vfat won't
2008# play nicely and I don't care. :) I have worked around the problem. -Hugo, 06/27/2002
2009    if [ "$disksize" = "1722" ] ; then
2010        gzip -dc $MINDI_LIB/sys-disk.raw.gz > $imagefile || Die "Cannot dd blank file"
2011    else
2012        dd if=/dev/zero of=$imagefile bs=1k count=$disksize &> /dev/null || Die "Cannot dd blank file"
2013        mkfs.vfat $imagefile >> $LOGFILE 2>> $LOGFILE
2014        syslinux $imagefile >> $LOGFILE 2>> $LOGFILE
2015    fi
2016    mount -t vfat -o loop $imagefile $mountpoint || LogIt "Cannot mount (PBDI)"
2017
2018    # copy Mindi's skeleton fs & lilo/syslinux/whatever stuff into it
2019    old_pwd=`pwd`
2020    MakeSyslinuxMessageFile $mountpoint/message.txt
2021    cd $mountpoint
2022    [ -e "$sys_cfg_file" ] || Die "Obi Wan, word up?"
2023    cat $sys_cfg_file | HackSyslinuxFile $ramdisk_size $mountpoint > syslinux.cfg || Die "Cannot copy syslinux.cfg from mindi_home to tmp_root"
2024    if [ -e "$MINDI_TMP/NFS-DEV" ] ; then
2025        mv syslinux.cfg syslinux.cfg.orig
2026        sed s/interactive/iso/ syslinux.cfg.orig > syslinux.cfg
2027    fi
2028    cd $old_pwd
2029    echo "Copying $MINDI_TMP/mindi.rdz to $mountpoint/initrd.img..." >> $LOGFILE
2030    cp -f $MINDI_TMP/mindi.rdz $mountpoint/initrd.img 2>> $LOGFILE
2031    if [ "$?" -ne "0" ] ; then
2032        LogIt "Failed to copy $MINDI_TMP/mindi.rdz to $mountpoint"
2033        cat $MINDI_TMP/mtpt.$$ >> $LOGFILE
2034        LogIt "Please unload some of your modules and try again."
2035        rm -f $MINDI_TMP/mtpt.$$
2036        LogIt "Cannot incorporate mindi.rdz in bootdisk (kernel / modules too big?)"
2037        retval=$(($retval+1))
2038    fi
2039
2040    mkdir -p $mountpoint/tmp
2041    cp -f $MINDI_TMP/mondo-restore.cfg $mountpoint/tmp &> /dev/null
2042
2043    # copy the kernel across
2044    [ "$mountpoint" != "" ] && rm -Rf $mountpoint/lost+found
2045    dd if=/dev/zero of=$mountpoint/zero bs=1k count=16 &> /dev/null
2046    free_space=`df -k -P $mountpoint | tail -n1 | tr -s ' ' '\t' | cut -f4`
2047    cp -f $kernelpath $mountpoint/vmlinuz &> /dev/null
2048    if [ "$?" -ne "0" ] || [ "$FORCE_DUAL_FLOPPIES" = "yes" ] ; then
2049        echo "Files at mountpoint ($mountpoint) :-" >> $LOGFILE
2050        du -sk $mountpoint/* >> $LOGFILE
2051        echo "--- end of list of files ---" >> $LOGFILE
2052        echo -en "Kernel size = `du -sk $kernelpath | cut -f1` K\nRamdisk free = $free_space K\n\
2053Sorry, your kernel is too big for a boot/root floppy.\nI'll try the new boot/root two-disk thingy.\n" >> $LOGFILE
2054        rm -f $mountpoint/vmlinuz
2055        cd $old_pwd
2056        umount $mountpoint || Die "Cannot unmount mountpoint ($mountpoint)"
2057        rmdir $mountpoint || LogIt "Cannot rmdir (PBDI)"
2058
2059        res=0
2060        write_full_floppy_of_kernel $kernelpath $imagesdir/mindi-boot.1440.img 1440
2061        res=$(($res+$?))
2062        cp -f $MINDI_TMP/mindi.rdz $imagesdir/mindi-root.1440.img 2>> $LOGFILE
2063        res=$(($res+$?))
2064        rm -f $imagefile
2065        if [ "$res" -ne "0" ]; then
2066            LogIt "WARNING - failed to create 1.44MB boot/root floppies"
2067            rm -f $imagesdir/mindi-*.1440.img
2068        fi
2069        return $res
2070    fi
2071    free_space=`df -k -P $mountpoint | tail -n1 | tr -s ' ' '\t' | cut -f4`
2072    max_kernel_size=$(($free_space+`du -sk $kernelpath | cut -f1`))
2073    echo "Free space left on floppy = $free_space KB" >> $LOGFILE
2074    echo "Max kernel size on $disksize KB floppy (est'd) = $max_kernel_size K" >> $LOGFILE
2075
2076    # make it bootable
2077    rm -f $mountpoint/zero
2078    mkdir -p $mountpoint/etc
2079    [ -e "$MINDI_LIB/memdisk" ] && cp -f $MINDI_LIB/memdisk $mountpoint 2>> $LOGFILE
2080    umount $mountpoint || Die "Cannot unmount mountpoint ($mountpoint)"
2081    echo -en "..."
2082    rmdir $mountpoint || LogIt "Cannot rmdir (PBDI)"
2083
2084    if [ "$retval" -eq "0" ] ; then
2085        echo -en "...$DONE\r"
2086        if [ "$KERN_DISK_MADE" ] ; then
2087            rm -f $imagefile
2088            LogIt "... $disksize KB boot disks were created OK\r"
2089        fi
2090    else
2091        echo -en "...failed\r"
2092        LogIt $disksize"KB boot disk was NOT created\r"
2093        rm -f $imagefile
2094    fi
2095    [ "$retval" -ne "0" ] && LogIt "PrepareBootDiskImage() is returning nonzero"
2096    return $retval
2097}
2098
2099
2100PrepareDataDiskImages() {
2101    local needlist bigdir minidir_root tardir diskdir imagesdir res i j k old_pwd lines
2102
2103    imagesdir=$1
2104    rm -f $imagesdir/mindi-*.img $imagesdir/[0-9]*.tar.gz $imagesdir/mindi.iso
2105    needlist=$MINDI_TMP/what-we-need.txt
2106    bigdir=$MINDI_TMP/bigdir
2107    minidir_root=$MINDI_TMP/minidir
2108    mkdir -p $minidir_root
2109    mkdir -p $bigdir/usr/bin
2110    tardir=$MINDI_TMP/tardir
2111
2112    lines=`grep -vx " *#.*" $MINDI_CONF/deplist.txt | grep -vx "" | wc -l`
2113    cat $MINDI_CONF/deplist.txt | GenerateGiantDependencyList $needlist $lines
2114    res=$?
2115    if [ "$YOUR_KERNEL_SUCKS" ]; then
2116        pwd=`pwd`
2117        cd $MINDI_TMP
2118        for i in `ListKernelModulePaths | HackPathsToFailsafe` ; do
2119            cp --parents -pRdf ./$i $bigdir 2>> $LOGFILE || Die "PDDI can't cp $i->$bigdir"
2120            if [ "`du -sk $i | cut -f1`" -lt "$(($CHOPSIZE*2))" ] ; then
2121                cp --parents -pRdf $i $bigdir 2>> $LOGFILE
2122            else
2123                ChopUpAndCopyFile $i $bigdir $CHOPSIZE $BIGNO
2124                BIGNO=$(($BIGNO+1))
2125            fi
2126        done
2127        for i in $EXTRA_MODS ; do
2128            j=`find lib/modules/$FAILSAFE_KVER -name $i.*o 2> /dev/null`
2129            [ ! "$j" ] && echo "WARNING - cannot find failsafe module $i.o" >> $LOGFILE
2130            for k in $j ; do
2131                if [ "`du -sk $k | cut -f1`" -lt "$CHOPSIZE" ] ; then
2132                    cp --parents -pRdf $k $bigdir 2>> $LOGFILE
2133                else
2134                    ChopUpAndCopyFile $k $bigdir $CHOPSIZE $BIGNO
2135                    BIGNO=$(($BIGNO+1))
2136                fi
2137                echo "Added failsafe module $k to ramdisk" >> $LOGFILE
2138            done
2139        done
2140        cd $pwd
2141    else
2142        ListKernelModulePaths >> $needlist
2143    fi
2144    if [ "$res" -ne "0" ] ; then
2145        Die "You have $res file`PluralOrNot $res` present in dependency list\nbut absent from filesystem."
2146    fi
2147    FindAndAddUserKeyboardMappingFile
2148    mkdir -p $bigdir/tmp
2149    if [ _"$MONDO_SHARE" != _"" ] ; then
2150        MakeMondoConfigFile $MINDI_TMP/mondo-restore.cfg
2151        cp -f $MINDI_TMP/mondo-restore.cfg $bigdir/tmp &> /dev/null
2152    fi
2153    [ -d "/mnt/.boot.d" ] && echo "Oh Jebus" > $bigdir/tmp/DUMBASS-GENTOO
2154    DropOptimizedLibraries $needlist $bigdir
2155    echo -en "Assembling dependency files"
2156    CopyDependenciesToDirectory < $needlist $bigdir
2157
2158    # also copy io.sys and msdos.sys, if we can find them
2159    for i in `mount | cut -d' ' -f3` ; do
2160        for j in io.sys msdos.sys ; do
2161            [ -e "$i/$j" ] && cp -f $i/$j $bigdir 2>> $LOGFILE
2162        done
2163    done
2164
2165    # master boot record, too
2166    i=`cat $MINDI_TMP/BOOTLOADER.DEVICE 2> /dev/null`
2167    if [ "$i" ] ; then
2168        LogIt "Backing up $i's MBR"
2169        dd if=$i of=$bigdir/BOOTLOADER.MBR bs=446 count=1 >> $LOGFILE 2>> $LOGFILE
2170        sleep 1
2171        sync
2172        j=$i
2173        [ -h "$j" ] && j=`readlink -f $j`
2174        LogIt "Creating /dev/boot_device ($j)"
2175        mkdir -p $bigdir/dev
2176        cp -pRdf $j $bigdir/dev/boot_device 2> /dev/null || Die "Unable to create /dev/boot_device on ramdisk"
2177    fi
2178
2179    old_pwd=`pwd`
2180    cd $bigdir
2181
2182    [ -e "$MINDI_LIB/aux-tools" ] || Die "aux-tools not found in Mindi's home dir. Do you have multiple copies of Mindi lying around? Please delete them. No, don't e-mail me and ask how. ;) Use 'rm'."
2183    cp -Rdf $MINDI_LIB/aux-tools/* . 2>> $LOGFILE || LogIt "WARNING - error occurred while unzipping aux-tools"
2184    if [ -e "$MINDI_LIB/x11-tools.tgz" ] ; then
2185        tar -zxf $MINDI_LIB/x11-tools.tgz 2>> $LOGFILE || LogIt "WARNING - error occurred while unzipping x11-tools.tgz"
2186    fi
2187    if [ -e "$MONDO_SHARE/restore-scripts" ] ; then
2188        cp -Rdf $MONDO_SHARE/restore-scripts/* . 2>> $LOGFILE
2189        [ "$?" -ne "0" ] && [ _"$MONDO_SHARE" != _"" ] && Die "Cannot find/install $MONDO_SHARE/restore-scripts"
2190    fi
2191    [ -d "/lib/dev-state" ] && cp --parents -pRdf /lib/dev-state . 2>> $LOGFILE
2192    cd $old_pwd
2193    echo -e "$DONE"
2194    TOTAL_BIGDIR_SIZE=`du -sk $bigdir | cut -f1`
2195    SplitDirectoryIntoMinidirs $bigdir $minidir_root
2196    noof_disks=$?
2197    [ "$noof_disks" -eq "0" ] && Die "Too much stuff!"
2198#    if [ "$noof_disks" -ge "8" ] ; then
2199#        LogIt "You are putting a ludicrously large amount of data on these disks."
2200#        LogIt "I shall therefore double EXTRA_SPACE, just in case your ramdisk is too small."
2201#        EXTRA_SPACE=$(($EXTRA_SPACE*2))
2202#    fi
2203    MakeMountlist $MINDI_TMP/mountlist.txt
2204    mkdir -p $minidir_root/$noof_disks/tmp
2205    cp -f $MINDI_TMP/mountlist.txt $minidir_root/$noof_disks/tmp/mountlist.txt 2> /dev/null || Die "Cannot copy mountlist.txt from $MINDI_TMP to data disk"
2206    [ _"$MONDO_SHARE" != _"" ] && cp -f $minidir_root/$noof_disks/tmp/mountlist.txt $MINDI_TMP/. 2>> $LOGFILE
2207    [ $LVM != "false" ] && $MINDI_LIB/analyze-my-lvm > $minidir_root/$noof_disks/tmp/i-want-my-lvm || LVM="false"
2208    cat $minidir_root/$noof_disks/tmp/mountlist.txt >> $LOGFILE
2209    ZipMinidirsIntoTarballs $minidir_root $tardir $noof_disks
2210    CreateDataDiskImagesFromTarballs $tardir $imagesdir $noof_disks
2211    FRIENDLY_OUTSTRING="Boot and data disk images were created."
2212    # One 1.72MB boot disk, one 2.88MB boot disk and $noof_disks data disk images
2213    rmdir $tardir $bigdir
2214    rm -f $needlist
2215    return $noof_disks
2216}
2217
2218
2219ProcessLDD() {
2220    local main_fname incoming j i fname f newf
2221    main_fname=$1
2222    read incoming
2223    while [ "$incoming" != "" ] ; do
2224        # We take the full path name of the dyn. lib. we want
2225        incoming=`echo "$incoming" | sed '/[[:blank:]]*.*[[:blank:]]*=>[[:blank:]]*(.*/d ; s/[[:blank:]]*.*[[:blank:]]*=>[[:blank:]]*\(\/.*\)/\1/ ; s/[[:blank:]]*\(\/.*\)[[:blank:]]*(.*/\1/'`
2226        for f in `echo "$incoming"` ; do
2227            echo "$f `ReadAllLink $f`"
2228        done
2229        read incoming
2230    done
2231}
2232
2233
2234Prompt() {
2235    echo -en "$1"
2236    read line
2237}
2238
2239
2240ReadLine() {
2241    local i incoming
2242    read incoming
2243    i=0
2244    while [ "$i" -le "32" ] && [ "$incoming" = "" ] ; do
2245        i=$(($i+1))
2246        read incoming
2247    done
2248    echo "$incoming"
2249}
2250
2251
2252RejigHyperlinks() {
2253    local minidir_root noof_disks fname path diskno old_pwd awk_loc gawk_loc dir i
2254    minidir_root=$1
2255    noof_disks=$2
2256
2257    old_pwd=`pwd`
2258    diskno=1
2259    while [ "$diskno" -le "$noof_disks" ] ; do
2260        mkdir -p $minidir_root/$diskno
2261        cd $minidir_root/$diskno
2262        for fname in `find -type d -o -print` ; do
2263            [ -h "$minidir_root/$diskno/$fname" ] && MoveHyperlinkSensibly $fname $minidir_root $diskno $noof_disks
2264        done
2265        diskno=$(($diskno+1))
2266    done
2267
2268    cd $old_pwd
2269    return
2270}
2271
2272
2273ReplaceIndividualLine() {
2274    local orig_file new_file lino newstring lines_total lines_remaining
2275
2276    orig_file=$1.orig
2277    mv -f $1 $orig_file || Die "Cannot move $1 to $orig_file"
2278    new_file=$1
2279    lino=$2
2280    newstring="$3"
2281    if [ "$lino" = "" ] || [ "$lino" -lt "1" ] ; then
2282        echo "Can't find string" >> $LOGFILE
2283        return 1
2284    fi
2285    lines_total=`wc -l $orig_file | $AWK '{print $1;}'`
2286    lines_remaining=$(($lines_total-$lino))
2287    head -n$(($lino-1)) $orig_file > $new_file
2288    echo "$newstring" >> $new_file
2289    echo "# The above line was added by Mindi, at the user's instruction" >> $new_file
2290    tail -n$lines_remaining $orig_file >> $new_file
2291    echo "Replace line $lino of $new_file with user-specified line, '$newstring'" >> $LOGFILE
2292    [ -x "$orig_file" ] && chmod +x $new_file
2293    rm -f $orig_file
2294    return 0
2295}
2296
2297
2298ScanCDandTape() {
2299    local i
2300
2301    for i in /dev/st0 /dev/ht0 /dev/cdrom /dev/cdrom0 /dev/cdrom 1 ; do
2302        dd if=$i of=/dev/null bs=64k count=1 &> /dev/null
2303    done
2304}
2305
2306
2307SizeOfPartition() {
2308    local devpath drive res stub
2309    device=$1
2310    if [ "`echo "$device" | grep -F "/dev/md"`" != "" ] ; then
2311        res=`SizeOfRaidPartition $device`
2312        [ "$res" = "" ] && Die "Cannot find $device's size - is your /etc/raidtab sane?"
2313        echo "$res"
2314        return 0
2315    fi
2316    # patch from Bill <bill@iwizard.biz> - 2003/08/25
2317    res=`$FDISK -s $device 2>> $LOGFILE`
2318    # end patch
2319    [ "$res" = "" ] && res=`df -k -P -x supermount | tr -s '\t' ' ' | grep -F "$device " | cut -d' ' -f2`
2320    [ "$res" = "" ] && res="-1"
2321    echo $res
2322    return 0
2323}
2324
2325
2326SizeOfRaidPartition() {
2327    local real_dev smallest_size silly tmp
2328
2329    silly=999999999
2330    smallest_size=$silly
2331
2332    for real_dev in `GetRaidDevMembers $1` ; do
2333        tmp=`SizeOfPartition $real_dev`
2334        [ "$tmp" -lt "$smallest_size" ] && smallest_size=$tmp
2335    done
2336
2337    if [ "$smallest_size" = "$silly" ] ; then
2338        echo "-1"
2339        return 1
2340    else
2341        echo "$smallest_size"
2342        return 0
2343    fi
2344}
2345
2346
2347StripComments()
2348{
2349    local tempfile
2350
2351    tempfile=$MINDI_TMP/$$.strip.txt
2352    cp -f $1 $tempfile 2>> $LOGFILE
2353    $AWK '{if (substr($0,0,1)!="#" || substr($0,0,3)=="#!/") {print $0;};}' $tempfile > $1
2354    rm -f $tempfile
2355    echo "Stripped comments from $2" >> $LOGFILE
2356}
2357
2358
2359SplitDirectoryIntoMinidirs() {
2360    local bigdir minidir_root i noof_disks old_pwd res
2361
2362    bigdir=$1
2363    minidir_root=$2
2364    [ "$minidir_root" != "" ] && rm -Rf $minidir_root/*
2365
2366    TryToFitDataIntoSeveralDirs $bigdir $minidir_root
2367    noof_disks=$?
2368    if [ "$noof_disks" -eq "0" ] ; then
2369        echo "Failed to fit data into several dirs."
2370        return 0
2371    fi
2372    RejigHyperlinks $minidir_root $noof_disks
2373    [ "$bigdir" != "" ] && rm -Rf $bigdir/*
2374    return $noof_disks
2375}
2376
2377
2378StripExecutable()
2379{
2380    local tmpfile
2381
2382    tmpfile=$MINDI_TMP/stripped.$$.dat
2383    [ -d "$1" ] || [ -h "$1" ] && return
2384    cp -f $1 $tmpfile 2>> $LOGFILE
2385    strip $tmpfile 2> /dev/null
2386    if [ "$?" -eq "0" ] ; then
2387        cp -f $tmpfile $1 2>> $LOGFILE
2388        echo "Stripped binary $2" >> $LOGFILE
2389    fi
2390    rm -f $tmpfile
2391}
2392
2393
2394TemporarilyCompressAllFiles() {
2395    local i orig_fname out_fname out_list
2396
2397    i=0
2398    out_list=$2/compressed/compressed.txt
2399    mkdir -p $2/compressed
2400    > $out_list
2401    for orig_fname in $1 ; do
2402        out_fname=$2/compressed/$orig_fname.gz
2403        mkdir -p $out_fname 2> /dev/null
2404        rmdir $out_fname 2> /dev/null
2405        gzip -c6 $orig_fname > $out_fname 2> /dev/null
2406        i=$(((($i+1))%15))
2407        [ "$i" -eq "0" ] && echo -en "."
2408        du -sk $out_fname >> $out_list
2409    done
2410}
2411
2412
2413TryToFindKernelPath() {
2414    local fname incoming res fkern_ver we_want_version possible_kernels noof_kernels kernelpath kdate duff_kernels
2415
2416    we_want_version=`uname -r`
2417    possible_kernels=""
2418    duff_kernels=""
2419   
2420    if [ "$ARCH" = "ia64" ] ; then
2421       root="/boot/efi/efi"
2422    else
2423       root="/"
2424    fi
2425    for fname in `find $root -maxdepth 2 -type f | grep -E 'lin|kern' | grep -Ev '^/proc/|^/net/'` ; do
2426        [ ! -e "$fname" ] && continue
2427        [ "$fname" = "/boot/vmlinuz.shipped" ] && [ -f "/boot/vmlinuz" ] && continue; # ignore SuSE's extra kernel
2428        file $fname | grep -q gzip
2429        if [ "$?" -eq "0" ] ; then
2430            # Used by ia64
2431            fkern_ver=`gzip -cd $fname | strings 2> /dev/null | grep "[2-9]+*[.][0-9]+*[.][0-9]+*[^\@]*@"`
2432        else
2433            fkern_ver=`strings $fname 2> /dev/null | grep "[2-9]+*[.][0-9]+*[.][0-9]+*[^\@]*@"`
2434        fi
2435        [ "$fkern_ver" = "" ] && continue
2436        [ "`echo "$fkern_ver" |grep -F "$we_want_version "`" = "" ] && continue
2437        [ -f "$fname" ] || continue
2438        [ -h "$fname" ] && continue
2439        kdate=`uname -v | $AWK '{for(i=1;i<NF;i++){if(index($i,":")){print $i;};};}' | $AWK '{print $NF;}'`
2440        file $fname | grep -q gzip
2441        if [ "$?" -eq "0" ] ; then
2442            # Used by ia64
2443            if [ "`gzip -cd $fname | strings 2> /dev/null | grep -F "$kdate"`" = "" ] ; then
2444                LogIt "Have you recompiled your kernel \"$fname\" w/o rebooting? Naughty but I'll allow it..."
2445                duff_kernels="$fname $duff_kernels"
2446            else
2447                [ "`echo "$fname" | grep -F "vmlinux"`" ] && continue
2448                possible_kernels="$fname $possible_kernels"
2449            fi
2450        else
2451            if [ "`strings $fname 2> /dev/null | grep -F "$kdate"`" = "" ] ; then
2452                LogIt "Have you recompiled your kernel \"$fname\" w/o rebooting?\n Naughty but I'll allow it..."
2453                duff_kernels="$fname $duff_kernels"
2454            else
2455                [ "`echo "$fname" | grep -F "vmlinux"`" ] && continue
2456                possible_kernels="$fname $possible_kernels"
2457            fi
2458        fi
2459    done
2460    if [ ! "$possible_kernels" ] && uname -a | grep Knoppix > /dev/null ; then
2461        possible_kernels=`find /boot/vmlinuz-2.* | tail -n1`
2462    fi
2463    if [ ! "$possible_kernels" ] ; then
2464        LogIt "No kernel matches exactly. Are there any duff kernels?"
2465        possible_kernels="$duff_kernels"
2466        if [ ! "$possible_kernels" ] ; then
2467            LogIt "Sorry, no duff kernels either"
2468        else
2469            LogIt "I bet you're running Debian or Gentoo, aren't you?"
2470            LogIt "Your kernel doesn't have a sane builddate. Oh well..."
2471        fi
2472    fi
2473    possible_kernels=`echo "$possible_kernels" | tr -s ' ' '\n' | sort -u | tr '\n' ' '`
2474    noof_kernels=`CountItemsIn "$possible_kernels"`
2475    if [ "$noof_kernels" -eq "0" ] ; then
2476        LogIt "Could not find your kernel."
2477        if [ -e "/boot/vmlinuz" ] ; then
2478            LogIt "Using /boot/vmlinuz as a last resort."
2479            output=/boot/vmlinuz
2480        else
2481            output=""
2482        fi
2483    elif [ "$noof_kernels" -eq "1" ] ; then
2484        kernelpath=`echo "$possible_kernels" | sed s/' '//`
2485        echo "Your kernel is $kernelpath (v`uname -r`)" >> $LOGFILE
2486        output="$kernelpath"
2487    else
2488        for i in $possible_kernels ; do
2489            if echo $i | grep "`uname -r`" ; then
2490                LogIt "OK, I used my initiative and found that "
2491                LogIt "$i is probably your kernel. "
2492                output="$i"
2493                return
2494            fi
2495        done
2496        if echo " $possible_kernels " | grep -F "/boot/vmlinuz " &> /dev/null ; then
2497            output=/boot/vmlinuz
2498            echo "Schlomo, this one's for you." >> $LOGFILE
2499        else
2500            LogIt "Two or more possible kernels found. You may specify any one of them and the "
2501            LogIt "boot disks will still work, probably. If one does not work, try another."
2502            LogIt "$possible_kernels"
2503            echo ""
2504        fi
2505    fi
2506    echo "$output" | tr -s ' ' '\n' | sort -u | tr '\n' ' '
2507}
2508
2509
2510TryToFitDataIntoSeveralDirs() {
2511    local bigdir minidir_root noof_disks diskno list_of_files filename old_pwd progress
2512    local i retval noof_disks total_files list_of_devs
2513
2514    bigdir=$1
2515    minidir_root=$2
2516    BIG_CLUNKY_SIZE_COUNTER=0
2517    retval=0
2518    noof_disks=1
2519
2520    echo -en "\r                                                                            \rDividing data into several groups..."
2521    old_pwd=`pwd`
2522    cd $bigdir
2523    list_of_files=`GetFileSizeList . | sort -nr | cut -f2 | grep -Fv "/dev/"`
2524    progress=0
2525    total_files=`CountItemsIn "$list_of_files"`
2526    if [ "`echo "$filename" | grep -x "/dev/.*"`" ] ; then
2527        filesize=1
2528    fi
2529    mkdir -p $minidir_root/$noof_disks
2530    if [ -e "dev" ] ; then
2531        echo "Copying dev/* to $minidir_root/$noof_disks" >> $LOGFILE
2532        cp --parents -pRdf dev $minidir_root/$noof_disks 2>> $LOGFILE
2533    fi
2534    TemporarilyCompressAllFiles "$list_of_files" $minidir_root
2535    for filename in $list_of_files ; do
2536        AddFileToDir $filename $minidir_root $noof_disks
2537        i=$?
2538        if [ "$i" -gt "$noof_disks" ] ; then
2539            noof_disks=$i
2540            echo -en "\r\t\t\t\t\t\t($noof_disks disks)"
2541        fi
2542        if [ "$i" -eq "0" ] ; then
2543            LogIt "Cannot add file $filename to minidir $minidir_root"
2544            retval=$(($retval+1))
2545        fi
2546        progress=$(($progress+1))
2547        echo -en "\r\t\t\t\t\t\t\t\t$(($progress*100/$total_files))% complete\r"
2548    done
2549    cd $old_pwd
2550    echo -en "\rThe files have been subdivided into $noof_disks directories.                                                            \r"
2551    [ "$minidir_root" != "" ] && rm -Rf $minidir_root/compressed
2552    if [ "$retval" -gt "0" ] ; then
2553        return 0
2554    else
2555        return $noof_disks
2556    fi
2557}
2558
2559
2560TurnTgzIntoRdz() {
2561    local tgz_dir_fname rdz_fname ramdisksize tempfile mountpoint old_pwd nodes disksize kernelsize maxsize res currsize not_copied j k floppy_modules s w
2562
2563    tgz_dir_fname=$1
2564    rdz_fname=$2
2565    ramdisksize=$3
2566    disksize=$4
2567    kernelsize=$5
2568    maxsize=$(($disksize-$kernelsize))
2569    maxsize=$(($maxsize*2)); # to allow for compression of 50%
2570    tempfile=$MINDI_TMP/temp.rd
2571    mountpoint=$MINDI_TMP/mnt1
2572    res=0
2573    echo -en "..."
2574    dd if=/dev/zero of=$tempfile bs=1k count=$ramdisk_size &> /dev/null || Die "Not enough room for temporary ramdisk (TurnTgzIntoRdz)"
2575    echo -en "..."
2576    mke2fs -b 1024 -m 1 -i 2048 -F $tempfile >> $LOGFILE 2>> $LOGFILE
2577    echo -en "..."
2578    mkdir -p $mountpoint
2579    mount -t ext2 -o loop $tempfile $mountpoint || Die "Cannot loopmount $tempfile to $mountpoint! The reason may be missing support for loopfs or ext2 (or both) in the running kernel."
2580    echo -en "..."
2581    old_pwd=`pwd`
2582    cd $mountpoint
2583    cp -Rdf $tgz_dir_fname/* . 2>&1 >> $LOGFILE
2584    tar -zxf symlinks.tgz || Die "Cannot untar symlinks.tgz"
2585    cd dev || Die "Can't cd to dev"
2586    tar -zxf dev-entries.tgz || Die "Cannot untar dev-entries.tgz"
2587    rm -f dev-entries.tgz
2588    cd ..
2589
2590    for w in insmod.static insmod.static.old ; do
2591        s=`which $w 2> /dev/null`
2592        if [ -e "$s" ] ; then
2593            cp --parents -af $s . 2>> $LOGFILE
2594        fi
2595    done
2596
2597    mkdir -p tmp
2598    [ -e "/dev/.devfsd" ] && echo "/dev/.devfsd found" > tmp/USE-DEVFS
2599    for w in cdrom floppy groovy-stuff ; do
2600        mkdir -p mnt/$w
2601    done
2602    #if [ "$RUN_AFTER_INITIAL_BOOT_PHASE" ] ; then
2603        #ReplaceIndividualLine sbin/init `grep -n "#WHOLIVESINAPINEAPPLEUNDERTHESEA#" sbin/init | cut -d':' -f1` "$RUN_AFTER_INITIAL_BOOT_PHASE"
2604    #fi
2605    if [ "$RUN_AFTER_BOOT_PHASE_COMPLETE" ] ; then
2606        ReplaceIndividualLine sbin/init `grep -n "#ABSORBENTANDYELLOWANDPOROUSISHE#" sbin/init | cut -d':' -f1` "$RUN_AFTER_BOOT_PHASE_COMPLETE"
2607    fi
2608
2609    cp --parents -Rdf /dev/fd0*[1,2][4,7,8]* . 2> /dev/null
2610    cd $old_pwd
2611    echo -en "..."
2612    MakeModuleLoadingScript $TMPMODPROBE_FLAG $mountpoint/sbin/insert-all-my-modules
2613    echo -en "..."
2614    old_pwd=`pwd`
2615    if [ "$YOUR_KERNEL_SUCKS" ] ; then
2616        cd $MINDI_TMP
2617        floppy_modules_path=lib/modules/$FAILSAFE_KVER
2618    else
2619        cd /
2620###
2621### Sq-Modification... Use kernel name in module path if specified.
2622###
2623        #floppy_modules_path=lib/modules/`uname -r`
2624        if [ "${kernelname}" != "" ]
2625        then
2626            floppy_modules_path=lib/modules/${kernelname}
2627        else
2628            floppy_modules_path=lib/modules/`uname -r`
2629        fi
2630###
2631### Sq-Modification end
2632###
2633    fi
2634    floppy_modules=""
2635    if [ "$disksize" -lt "2880" ] ; then
2636        list_of_groovy_mods="$FLOPPY_MODS $FORCE_MODS $IDE_MODS ide-scsi sr_mod cdrom isocd isofs `WhichOfTheseModulesAreLoaded "$SCSI_MODS"`"
2637    else
2638        list_of_groovy_mods="$CDROM_MODS $FORCE_MODS `WhichOfTheseModulesAreLoaded "$SCSI_MODS"`"
2639    fi
2640    if [ -e "$MINDI_TMP/NFS-DEV" ] ; then
2641        # For PXE boot
2642        list_of_groovy_mods="$list_of_groovy_mods $NET_MODS"
2643    fi
2644    [ -e "$floppy_modules_path" ] || LogIt "path $floppy_modules_path does not exist.\n If you're not using a modular kernel then you're NUTS."
2645    for i in $list_of_groovy_mods ; do
2646        floppy_modules="$floppy_modules `FindSpecificModuleInPath $floppy_modules_path $i`"
2647    done
2648    for i in $floppy_modules ; do
2649        [ -e "$i" ] && s=`du -sk $i | cut -f1` || s=""
2650        [ "$YOUR_KERNEL_SUCKS" ] && i=$MINDI_TMP/$i
2651        echo "Adding $i ($s KB) to the rootfs" >> $LOGFILE
2652        cp -df $i $mountpoint/ 2>/dev/null || LogIt "Unable to copy $i to $mountpoint"
2653        [ "`echo "$i" | grep -F ".gz"`" ] && gunzip -f $mountpoint/`basename $i`
2654    done
2655    if [ ! -e "/sbin/devfsd" ] || [ "$disksize" -lt "2880" ] || [ "$kernelpath" = "$MINDI_LIB/vmlinuz" ] ; then
2656        echo "Deleting devfsd daemon from ramdisk" >> $LOGFILE
2657        [ ! -e "/sbin/devfsd" ] && echo "...because /sbin/devfsd not found" >> $LOGFILE
2658        [ "$disksize" -lt "2880" ] && echo "...because disksize = $disksize" >> $LOGFILE
2659        [ "$kernelpath" = "$MINDI_LIB/vmlinuz" ] && echo "...because kernel is failsafe" >> $LOGFILE
2660        rm -f $mountpoint/sbin/devfsd
2661    fi
2662    cd $old_pwd
2663    [ "$TAPEDEV" ] && echo -en "$TAPEDEV" > $mountpoint/tmp/TAPEDEV-LIVES-HERE
2664    dd if=/dev/zero of=$mountpoint/zero &> /dev/null
2665    rm -f $mountpoint/zero
2666    if [ _"$MONDO_SHARE" != _"" ] ; then
2667        MakeMondoConfigFile $mountpoint/tmp/mondo-restore.cfg
2668        cp -f $mountpoint/tmp/mondo-restore.cfg $MINDI_TMP &> /dev/null
2669        cp -f $MINDI_TMP/mountlist.txt $mountpoint/tmp/ 2>/dev/null || Die "Cannot copy mountlist to ramdisk"
2670        echo -en "$FILES_IN_FILELIST" > $mountpoint/tmp/FILES-IN-FILELIST
2671        echo -en "$LAST_FILELIST_NUMBER" > $mountpoint/tmp/LAST-FILELIST-NUMBER
2672        [ "$USE_LZO" = "yes" ] && echo -en "Pras 4 Pres 2004" >> $mountpoint/tmp/USING-LZO
2673        [ "$USE_GZIP" = "yes" ] && echo -en "YES" >> $mountpoint/tmp/USING-GZIP
2674        [ "$USE_COMP" = "yes" ] && echo -en "Compression, yep" >> $mountpoint/tmp/USING-COMP
2675        [ "$USE_STAR" = "yes" ] && echo -en "Using star. Hooray." >> $mountpoint/tmp/USING-STAR
2676    fi
2677    mkdir -p $mountpoint/tmp
2678    mkdir -p $mountpoint/proc
2679    echo "$disksize" > $mountpoint/tmp/$disksize.siz
2680    find $mountpoint -name CVS -exec rm -rf '{}' \;
2681    # Determine what filesystem to use for initrd image
2682    echo "Call GetInitrdFilesystemToUse() with parameter ${kernelpath} to get filesystem to use for initrd." >> $LOGFILE
2683    gvFileSystem=`GetInitrdFilesystemToUse ${kernelpath}`
2684    [ -z  gvFileSystem ] && Die "GetFilesystemToUse() failed. Terminating."
2685    case "$gvFileSystem" in
2686    "ext2fs")
2687        # say what will be used
2688        echo "Creating an ext2 initrd image..." >> $LOGFILE
2689        # kernel expects linuxrc in ext2 filesystem
2690        ( cd $mountpoint && ln -sf sbin/init linuxrc )
2691        # unmount loop filesystem and create image file using the standard approach
2692        umount $mountpoint || Die "Cannot unmount $tempfile"
2693        dd if=$tempfile bs=1k 2> /dev/null | gzip -v9 > $rdz_fname 2> /dev/null
2694        # log that we are done
2695        echo "...done." >> $LOGFILE
2696    ;;
2697    "initramfs")
2698        # say what will be used
2699        echo "Creating a gzip'ed cpio (AKA initramfs) initrd image..." >> $LOGFILE
2700        # make sure that cpio is there
2701        which cpio &> /dev/null; [ $? -eq 0 ] || Die "cpio not found. Please install package cpio and try again."
2702        # go into filesystem
2703        cd $mountpoint
2704        # kernel expects init in cpio filesystem
2705        ln -sf sbin/init init
2706        # create cpio image file and unmount loop filesystem
2707        find . -print | cpio -o -H newc | gzip -9 > $old_pwd/$rdz_fname 2> /dev/null
2708        cd $old_pwd
2709        umount $mountpoint || Die "Cannot unmount $tempfile"
2710        # log that we are done
2711        echo "...done." >> $LOGFILE
2712    ;;
2713    *)
2714        Die "Filesystem $gvFileSystem not supported for initrd image. Terminating."
2715    ;;
2716    esac
2717    if [ "$res" -eq "0" ] ; then
2718        echo -en "..."
2719    else
2720        echo -en "\rMade an rdz WITH ERRORS.           \n"
2721    fi
2722    return 0
2723}
2724
2725
2726WhichOfTheseModulesAreLoaded() {
2727    local modname loaded_modules
2728    loaded_modules="$MODULES"
2729    for modname in $1 ; do
2730        [ "`echo "$loaded_modules" | grep -w "$modname"`" ] && echo "$modname"
2731    done
2732}
2733
2734
2735ZipMinidirsIntoTarballs() {
2736    local minidir_root tardir noof_disks diskno old_pwd i
2737    minidir_root=$1
2738    tardir=$2
2739    noof_disks=$3
2740
2741    echo -en "Tarring and zipping the group`PluralOrNot $noof_disks`..."
2742    mkdir -p $tardir
2743    mkdir -p $minidir_root/all
2744    old_pwd=`pwd`
2745    diskno=1
2746    while [ "$diskno" -le "$noof_disks" ] ; do
2747        cd $minidir_root/$diskno || LogIt "WARNING - cannot cd to $minidir_root/$diskno"
2748        tar -cf - . 2>> $LOGFILE | gzip -9 > $tardir/$diskno.tar.gz || Die "Can't tar/gzip disk#$diskno; please tell Dev Team -exactly- what the errors where."
2749        diskno=$(($diskno+1))
2750        echo -n "..."
2751        cp -pRdf * $minidir_root/all 2>> $LOGFILE
2752    done
2753    mkdir -p $minidir_root/all/tmp
2754    cd $minidir_root/all
2755    size_of_all_tools=`du -sk . | cut -f1`
2756    if [ _"$MONDO_SHARE" != _"" ] ; then
2757        for q in filelist.full.gz biggielist.txt ; do
2758            [ ! -e "$MINDI_TMP/$q" ] && Die "Cannot find $MINDI_TMP/$q"
2759            cp -pRdf $MINDI_TMP/$q tmp/ 2>> $LOGFILE
2760        done
2761        mkdir -p $minidir_root/all/tmp
2762        echo -en "$FILES_IN_FILELIST" > $minidir_root/all/tmp/FILES-IN-FILELIST 2> /dev/null
2763        echo -en "$LAST_FILELIST_NUMBER" > $minidir_root/all/tmp/LAST-FILELIST-NUMBER 2> /dev/null
2764    fi
2765    tar -b 4096 -cf - * 2> /dev/null | gzip -9 > $tardir/all.tar.gz
2766    dd if=/dev/zero bs=1k count=64 >> $imagesdir/all.tar.gz 2> /dev/null
2767    [ "`du -sm $imagesdir/all.tar.gz | cut -f1`" -ge "30" ] && Die "You have too many tools in your shed"
2768    cd $old_pwd
2769    [ "$minidir_root" != "" ] && rm -Rf $minidir_root
2770    echo -e "$DONE"
2771}
2772
2773
2774##############################################################################
2775#----------------------------------- Main -----------------------------------#
2776##############################################################################
2777
2778
2779> $LOGFILE
2780echo "mindi v$MINDI_VERSION" >> $LOGFILE
2781echo "$ARCH architecture detected" >> $LOGFILE
2782echo "mindi called with the following arguments:" >> $LOGFILE
2783echo "$@" >> $LOGFILE
2784echo "Start date : `date`" >> $LOGFILE
2785
2786if [ -e "/etc/conf.modules" ] && [ ! -e "/etc/modules.conf" ] ; then
2787    LogIt "WARNING - Ancient distro detected." 1
2788    ln -sf /etc/conf.modules /etc/modules.conf
2789fi
2790[ -e "/sbin/mkdosfs" ] && [ ! -e "/sbin/mkfs.vfat" ] && ln -sf /sbin/mkdosfs /sbin/mkfs.vfat
2791
2792# Log some capital variables
2793[ "$MINDI_PREFIX" = "XXX" ] && Die "Mindi has not been installed correctly."
2794echo "MINDI_LIB = $MINDI_LIB" >> $LOGFILE
2795echo "MINDI_SBIN = $MINDI_SBIN" >> $LOGFILE
2796[ "$MINDI_CONF" = "YYY" ] && Die "Mindi has not been installed correctly."
2797echo "MINDI_CONF = $MINDI_CONF" >> $LOGFILE
2798echo "MONDO_SHARE = $MONDO_SHARE" >> $LOGFILE
2799
2800trap AbortHere SIGTERM SIGHUP SIGQUIT SIGKILL SIGABRT SIGINT
2801
2802# Sanity checks
2803which which > /dev/null 2> /dev/null || Die "Please install 'which'."
2804which strings > /dev/null 2> /dev/null || Die "Please install binutils and libbinutils; you have no 'strings' executable."
2805which gawk > /dev/null 2> /dev/null || Die "Gawk is missing from your computer. Please install gawk. You may find the package on Debian's website. How did I know you're running Debian? Because only Debian would be stupid enough not to include gawk in your distribution."
2806which gawk > /dev/null 2> /dev/null && AWK=`which gawk 2>/dev/null` || AWK="`which awk 2>/dev/null`"
2807if which awk &> /dev/null ; then
2808    if ! which gawk &> /dev/null ; then
2809        LogIt "You have awk but not gawk.\nPlease note that mindi works fine with a _sane_ awk binary.\nIf your awk binary misbehaves then please contact your vendor\nor distribution's mailing list for technical support.\n"
2810    fi
2811fi
2812which mke2fs > /dev/null 2> /dev/null || Die "Please put mke2fs in system path"
2813[ ! -e "$FDISK" ] && Die "Cannot find (s)fdisk"
2814
2815[ "`uname -r | grep "2.4.[0-6]" | grep -v "2.4.[0-9][0-9]"`" != "" ] &&  echo "WARNING! Your kernel may have buggy loopfs code. Consider upgrading to 2.4.7"
2816# If we have a 2.6 kernel, the system uses module-init-tools which means that we
2817# may have the modprobe configuration spread out across multiple files in
2818# directory /etc/modprobe.d. If this is the case we concatenate these files into
2819# a temporary file for further processing. Otherwise we continue in the standard
2820# way. Note further that in case /etc/modprobe.d exists, we use it and ignore
2821# /etc/modprobe.conf which is exactly what module-init-tools does. The temporary
2822# modprobe.conf file is created in MakeModuleLoadingScript. AL041128.
2823if [ -d "/etc/modprobe.d" ] && [ `uname -r | cut -c1-3` = "2.6" ] ; then
2824    TMPMODPROBE_FLAG="Y"
2825else
2826    TMPMODPROBE_FLAG="N"
2827    [ -e "/etc/modprobe.conf" ] && [ ! -e "/etc/modules.conf" ] && ln -sf /etc/modprobe.conf /etc/modules.conf
2828    [ ! -e "/etc/modules.conf" ] && Die "/etc/modules.conf not found; you may have to create a softlink from /etc/conf.modules to /etc/modules.conf; of course, all good distros use modules.conf anyway..."
2829fi
2830
2831# Update the PATH variable if incomplete
2832if [ -e "/sbin/mkfs" ] && ! which mkfs &> /dev/null ; then
2833    PATH=$PATH:/sbin:/usr/sbin
2834    export PATH
2835    echo "Your PATH did not include /sbin or /usr/sbin. I have fixed that, temporarily." >> $LOGFILE
2836    echo "However, you may wish to ask your vendor to provide a permanent fix..." >> $LOGFILE
2837    echo " Or you might like to call 'su -' instead of 'su', for example." >> $LOGFILE
2838fi
2839
2840[ -f "$MINDI_LIB/vmlinuz" ] && FAILSAFE_KVER=`strings $MINDI_LIB/vmlinuz 2> /dev/null | grep -E "2\.[46]" | cut -d' ' -f1`
2841
2842if ! which mkfs.vfat &> /dev/null ; then
2843    Die "mkfs.vfat missing from your filesystem - please install your dosfstools RPM or DEB package. Perhaps your PATH environmental variable is broken, too?"
2844fi
2845
2846### BERLIOS
2847### Fix as it's not mandatory on ia64
2848if [ "$ARCH" = "ia64" ] ; then
2849    if which elilo &> /dev/null ; then
2850        LILO_EXE=elilo
2851    else
2852        LILO_EXE=`which false`
2853    fi
2854else
2855    FindIsolinuxBinary
2856    FindLiloBinary
2857fi
2858# BERLIOS: Remove as too dangerous and now useless
2859#grep -F " $MINDI_TMP " /proc/mounts | grep -F tmpfs > /dev/null 2> /dev/null && MINDI_TMP=/home/tmpmondo && mkdir -p $MINDI_TMP && LogIt "Changing MINDI_TMP to $MINDI_TMP because you're using tmpfs for /tmp\n" ; # tmpfs doesn't like Mindi and /tmp, for some reason
2860trap "Aborted" SIGTERM
2861DONE="\r\t\t\t\t\t\t\t\tDone.         "
2862CHOPSIZE=240
2863BIGNO=0
2864MAX_COMPRESSED_SIZE=1300
2865kernelpath=""
2866MONDO_ROOT=/var/cache/mondo
2867mkdir -p $MONDO_ROOT
2868
2869if [ -d "/proc/lvm" ]; then
2870    # LVM v1
2871    LVMCMD=""
2872    LVM="v1"
2873elif [ -d "/dev/mapper" ]; then
2874    # LVM v2
2875    LVMCMD="lvm"
2876    LVM="v2"
2877else
2878    LVM="false"
2879fi
2880echo "LVM set to $LVM" >> $LOGFILE
2881echo "----------" >> $LOGFILE
2882echo "df result:" >> $LOGFILE
2883echo "----------" >> $LOGFILE
2884df -T >> $LOGFILE
2885echo "-------------" >> $LOGFILE
2886echo "mount result:" >> $LOGFILE
2887echo "-------------" >> $LOGFILE
2888mount >> $LOGFILE
2889echo "-------------" >> $LOGFILE
2890if [ -e /etc/raidtab ]; then
2891    echo "-------------" >> $LOGFILE
2892    echo "/etc/raidtab content:" >> $LOGFILE
2893    echo "-------------" >> $LOGFILE
2894    cat /etc/raidtab >> $LOGFILE
2895fi
2896echo "-------------" >> $LOGFILE
2897echo "cat /proc/cmdline:" >> $LOGFILE
2898echo "-------------" >> $LOGFILE
2899cat /proc/cmdline >> $LOGFILE
2900echo "-------------" >> $LOGFILE
2901echo "lsmod result:" >> $LOGFILE
2902echo "-------------" >> $LOGFILE
2903lsmod >> $LOGFILE
2904MODULES="`cat /proc/modules | awk '{print $1}'`"
2905if [ -e /usr/sbin/esxcfg-module ]; then
2906    echo "VMWare ESX server detected - Enabling dedicated support" >> $LOGFILE
2907    echo "-------------" >> $LOGFILE
2908    echo "VMWare modules" >> $LOGFILE
2909    echo "-------------" >> $LOGFILE
2910    esxcfg-module -l >> $LOGFILE
2911    MODULES="$MODULES `esxcfg-module -l | awk '{print $1}'`"
2912fi
2913echo "-------------" >> $LOGFILE
2914echo "Liste of extra modules is:" >> $LOGFILE
2915echo "$EXTRA_MODS" >> $LOGFILE
2916echo "-------------" >> $LOGFILE
2917
2918if [ "$#" -ge "2" ] ; then
2919    if [ "$1" = "--max-compressed-size" ] ; then
2920        MAX_COMPRESSED_SIZE=$2
2921        shift; shift
2922    fi
2923fi
2924
2925FLOPPY_WAS_MOUNTED=""
2926for mtpt in /media/floppy /mnt/floppy /floppy ; do
2927    if mount | grep -w $mtpt &> /dev/null ; then
2928        FLOPPY_WAS_MOUNTED="$FLOPPY_WAS_MOUNTED $mtpt"
2929        umount $mtpt
2930    fi
2931done
2932
2933if [ "$#" -ne "0" ] ; then
2934    if [ "$1" = "--findkernel" ] ; then
2935        res=`TryToFindKernelPath`
2936        # Avoids logfile content for mondo
2937        export MONDO_SHARE=""
2938        if [ "$res" = "" ] ; then
2939            MindiExit -1
2940        else
2941            echo "$res"
2942            MindiExit 0
2943        fi
2944    elif [ "$1" = "--makemountlist" ] ; then
2945        [ ! "$2" ] && Die "Please specify the output file"
2946        MakeMountlist $2
2947        # Avoids logfile content for mondo
2948        export MONDO_SHARE=""
2949        MindiExit $?
2950    elif [ "$1" = "-V" ] || [ "$1" = "-v" ] || [ "$1" = "--version" ] || [ "$1" = "-version" ] ; then
2951        echo "Mindi v$MINDI_VERSION"
2952        # Avoids logfile content for mondo
2953        export MONDO_SHARE=""
2954        MindiExit 0
2955    elif [ "$#" -ge "9" ] && [ "$1" = "--custom" ] ; then
2956        MONDO_TMP=$2
2957        # Change MINDI_TMP for the one provided by mondo
2958        # So that it can get back the built files
2959        mv $MINDI_TMP/* $MINDI_TMP/.??* $MONDO_TMP 2> /dev/null
2960        rmdir $MINDI_TMP
2961        export MINDI_TMP=$MONDO_TMP
2962        mkdir -p $MINDI_TMP
2963        # This is the scratch dir in mondo
2964        CACHE_LOC=$3
2965        if [ _"$CACHE_LOC" != _"" ]; then
2966            mkdir -p $CACHE_LOC
2967        fi
2968        kernelpath=$4; [ "$kernelpath" = "(null)" ] && kernelpath=""
2969###
2970### Sq-Modification...
2971### Attempt to locate kernel specific module path
2972### if module path is found then use it other wise use uname -r to set it...
2973###
2974        kernelname=`echo $kernelpath | cut -d'-' -f2-`
2975        LogIt "kernelname = $kernelname"
2976        LogIt "kernelpath = $kernelpath"
2977        if [ ! -d "/lib/modules/$kernelname" ] && [ "$kernelpath" != "FAILSAFE" ]
2978        then
2979           LogIt "Module path for ${kernelpath} not found..."
2980           LogIt "using running kernel\'s modules."
2981           kernelname=`uname -r`
2982        else
2983           LogIt "Using modules for kernel: ${kernelname}"
2984        fi
2985###
2986### end of Sq-Modification
2987###
2988        TAPEDEV=$5
2989        TAPESIZE=$6
2990        FILES_IN_FILELIST=$7
2991        USE_LZO=$8
2992        CDRECOVERY=$9
2993        if [ "${10}" = "(null)" ] || [ "${10}" = "" ] ; then
2994            IMAGE_DEVS=""
2995        else
2996            IMAGE_DEVS="`echo "${10}" | tr '|' ' '`"
2997        fi
2998        if [ "${11}" ] ; then
2999            LILO_OPTIONS=""
3000    #       LogIt "LILO will use conservative settings, to be compatible with older BIOSes."
3001        fi
3002        LAST_FILELIST_NUMBER=${12}
3003        ESTIMATED_TOTAL_NOOF_SLICES=${13}
3004        EXCLUDE_DEVS="${14}"
3005        USE_COMP="${15}"
3006        USE_LILO="${16}"
3007        USE_STAR="${17}"
3008        INTERNAL_TAPE_BLOCK_SIZE="${18}"
3009        DIFFERENTIAL="${19}"
3010        USE_GZIP="${20}"
3011        NOT_BOOT="${21}"
3012        [ "$USE_COMP" = "" ] && USE_COMP=yes
3013        [ "$USE_GZIP" = "" ] && USE_GZIP=no
3014        [ "$NOT_BOOT" = "" ] && NOT_BOOT=no
3015        [ "$TAPEDEV" ] && LogIt "This is a tape-based backup. Fine."
3016        [ "$kernelpath" = "" ] && kernelpath=`TryToFindKernelPath`
3017        kernelname=`echo $kernelpath | cut -d'-' -f2-`
3018        if [ ! -d "/lib/modules/$kernelname" ] && [ "$kernelpath" != "FAILSAFE" ]
3019        then
3020           LogIt "Module path for ${kernelpath} not found..."
3021           LogIt "using running kernel\'s modules."
3022           kernelname=`uname -r`
3023        else
3024           LogIt "Using modules for kernel: ${kernelname}"
3025        fi
3026        [ "$CDRECOVERY" = "yes" ] && [ "$TAPEDEV" != "" ] && Die "Sorry, you can't use --cd-recovery and --write-tapes at the same time"
3027        MONDO_ROOT=`echo $CACHE_LOC | sed 's/\(.*\)\/.*/\1/'`
3028        if [ _"$MONDO_ROOT" != _"" ]; then
3029            mkdir -p $MONDO_ROOT
3030        else
3031            Die "MONDO_ROOT is undefined"
3032        fi
3033    else
3034        echo "Syntax: mindi (--custom ....)" >> /dev/stderr
3035        MindiExit -1
3036    fi
3037fi
3038#ScanCDandTape
3039[ "$CDRECOVERY" = "yes" ] || CDRECOVERY=no
3040if [ "$CDRECOVERY" = "yes" ] ; then
3041    iso_cfg_file=$MINDI_LIB/isolinux-H.cfg
3042    sys_cfg_file=$MINDI_LIB/syslinux-H.cfg
3043else
3044    iso_cfg_file=$MINDI_LIB/isolinux.cfg
3045    sys_cfg_file=$MINDI_LIB/syslinux.cfg
3046fi
3047
3048[ -e "$iso_cfg_file" ] || Die "Cannot find $iso_cfg_file"
3049if [ _"$MONDO_SHARE" = _"" ] ; then
3050    LogIt "Mindi Linux mini-distro generator v$MINDI_VERSION"
3051    LogIt "Latest Mindi is available from http://www.mondorescue.org"
3052    LogIt "BusyBox sources are available from http://www.busybox.net"
3053    LogIt "------------------------------------------------------------------------------"
3054else
3055    echo "You are using Mindi-Linux v$MINDI_VERSION to make boot+data disks" >> $LOGFILE
3056fi
3057if [ -f $MINDI_LIB/rootfs/bin/busybox ]; then
3058    LogIt "Mindi-`$MINDI_LIB/rootfs/bin/busybox 2>&1 | head -1`"
3059else
3060    LogIt "Unable to find mindi-busybox, please install it"
3061    MindiExit -1
3062fi
3063
3064# for Mandrake 9.2, which comes with two aes.o.gz modules :-/
3065insmod /lib/modules/`uname -r`/*/*/misc/aes.*o.gz >> $LOGFILE 2>> $LOGFILE
3066for i in loop cdrom ide-cd isofs linear raid0 raid1 raid5 ; do
3067    insmod $i >> $LOGFILE 2>> $LOGFILE
3068done
3069
3070KERN_DISK_MADE=""
3071
3072echo "DIFFERENTIAL = $DIFFERENTIAL" >> $LOGFILE
3073echo "INTERNAL TAPE BLOCK SIZE = $INTERNAL_TAPE_BLOCK_SIZE" >> $LOGFILE
3074echo "NOT_BOOT = '$NOT_BOOT'" >> $LOGFILE
3075if [ "$NOT_BOOT" != "" ] && [ "$NOT_BOOT" != "0" ] && [ "$NOT_BOOT" != "no" ] ; then
3076    LogIt "Just creating mondo-restore.cfg and a small all.tar.gz for Mondo. Nothing else."
3077    MakeMondoConfigFile $MINDI_TMP/mondo-restore.cfg
3078    MakeMountlist $MINDI_TMP/mountlist.txt
3079    mkdir -p $MINDI_TMP/small-all/tmp
3080    cd $MINDI_TMP/small-all
3081    cp -f $MINDI_TMP/{mountlist.txt,mondo-restore.cfg,filelist.full.gz,biggielist.txt} tmp 2>/dev/null || Die "Cannot copy small all.tar.gz"
3082    tar -cv tmp | gzip -9 > $MINDI_TMP/all.tar.gz || Die "Cannot make small all.tar.gz"
3083    sleep 2
3084    LogIt "Done. Exiting."
3085    MindiExit 0
3086fi
3087
3088if [ "$kernelpath" = "" ] ; then
3089    [ _"$MONDO_SHARE" != _"" ] && Die "Please use -k <path> to specify kernel."
3090    echo -en "Do you want to use your own kernel to build the boot disk ([y]/n) ?"
3091    read ch
3092    if [ "$ch" != "n" ] && [ "$ch" != "N" ] ; then
3093        USE_OWN_KERNEL="yes"
3094    fi
3095    if [ "$USE_OWN_KERNEL" = "yes" ]; then
3096        YOUR_KERNEL_SUCKS=""
3097        kernelpath=`TryToFindKernelPath`
3098        if [ "$kernelpath" = "" ] ; then
3099            echo -n "Please enter kernel path : "
3100            read kernelpath
3101        fi
3102    else
3103        YOUR_KERNEL_SUCKS="That's why you're using mine, dude. :-)"
3104    fi
3105fi
3106if [ _"$MONDO_SHARE" = _"" ] ; then
3107    echo -en "Would you like to use LILO (instead of syslinux)\nfor your boot CD/floppies (y/[n]) ?"
3108    read ch
3109    if [ "$ch" != "y" ] && [ "$ch" != "Y" ] ; then
3110        USE_LILO=no
3111    else
3112        USE_LILO=yes
3113    fi
3114fi
3115if [ "$YOUR_KERNEL_SUCKS" != "" ] || [ "$kernelpath" = "" ] || [ "$kernelpath" = "SUCKS" ] || [ "$kernelpath" = "FAILSAFE" ] ; then
3116    kernelpath=$MINDI_LIB/vmlinuz
3117    LogIt "I shall include Mindi's failsafe kernel, not your kernel, in the boot disks."
3118    LogIt "However, you are still running your kernel. If Mindi fails to create your"
3119    LogIt "disks then it may still be a result of a problem with your kernel."
3120    pwd=`pwd`
3121    cd $MINDI_TMP
3122    bzip2 -dc $MINDI_LIB/lib.tar.bz2 | tar -x || Die "Cannot unzip lib.tar.bz2"
3123    cd $pwd
3124    YOUR_KERNEL_SUCKS="Your kernel sucks"
3125fi
3126echo -e "Mindi's temp dir = $MINDI_TMP \nMindi's output dir=$CACHE_LOC" >> $LOGFILE
3127[ "$(($RANDOM%64))" -eq "0" ] && LogIt "Dude, I've looked inside your computer and it's really dusty..."
3128
3129[ "$YOUR_KERNEL_SUCKS" ] && [ ! "$FAILSAFE_KVER" ] && Die "Please install mindi-kernel package. You need it.\nGo to http://www.mondorescue.org and download it, then install it."
3130
3131PrepareDataDiskImages $CACHE_LOC
3132noof_disks=$?
3133ramdisk_size=$(($size_of_all_tools+$EXTRA_SPACE))
3134rds=$(($ramdisk_size-$((ramdisk_size%4096))))
3135ramdisk_size=$rds
3136
3137echo "Ramdisk will be $ramdisk_size KB" >> $LOGFILE
3138if [ "$USE_LILO" = "yes" ] ; then
3139    if [ "$ARCH" = "ia64" ] ; then
3140        PrepareBootDiskImage_LILO $CACHE_LOC $BOOT_SIZE $kernelpath $ramdisk_size || Die "Failed to create ia64 floppy disk image."
3141    else
3142        if ! PrepareBootDiskImage_LILO $CACHE_LOC 1722 $kernelpath $ramdisk_size ; then
3143            LogIt "WARNING - failed to create 1.72MB boot image."
3144            LogIt "Please reduce your kernel's size if you want to make a 1.72MB floppy disk."
3145        fi
3146        if ! PrepareBootDiskImage_LILO $CACHE_LOC 2880 $kernelpath $ramdisk_size ;  then
3147            LogIt "WARNING - failed to create 2.88MB floppy disk image."
3148            LogIt "Please reduce your kernel's size if you want to make a 2.88MB floppy disk."
3149            PrepareBootDiskImage_LILO $CACHE_LOC $BOOT_SIZE $kernelpath $ramdisk_size || Die "Failed to create 2.88MB floppy disk image."
3150        fi
3151    fi
3152else
3153    if ! PrepareBootDiskImage_SYSLINUX $CACHE_LOC 1722 $kernelpath $ramdisk_size ; then
3154        LogIt "WARNING - failed to create 1.72MB boot image."
3155        LogIt "Please reduce your kernel's size if you want to make a 1.72MB floppy disk."
3156        if ! PrepareBootDiskImage_SYSLINUX $CACHE_LOC 2880 $kernelpath $ramdisk_size ; then
3157            LogIt "WARNING - failed to create 2.88MB floppy disk image."
3158            LogIt "Please reduce your kernel's size if you want to make a 2.88MB floppy disk."
3159            PrepareBootDiskImage_SYSLINUX $CACHE_LOC $BOOT_SIZE $kernelpath $ramdisk_size || Die "Failed to create 5.76MB floppy disk image."
3160        fi
3161    fi
3162fi
3163
3164[ -e "$MINDI_LIB/memtest.img" ] && BOOT_MEDIA_MESSAGE="$BOOT_MEDIA_MESSAGE\n\
3165...Or type 'memtest' to test your PC's RAM thoroughly.\n"
3166
3167if [ _"$MONDO_SHARE" = _"" ] ; then
3168    ListImagesForUser $CACHE_LOC
3169    boot_dev=/dev/fd0u1722
3170    [ ! -e "$boot_dev" ] && mknod $boot_dev b 2 60
3171    [ ! -e "$boot_dev" ] && boot_dev=/dev/fd0H1722
3172    [ ! -e "$boot_dev" ] && Die "Oh Lord, will you PLEASE tell the vendor to create the 1.72MB devices in /dev?"
3173    if [ "$PROMPT_WRITE_BOOT_FLOPPIES" = "yes" ]; then
3174        OfferToCopyImagesToDisks $CACHE_LOC $boot_dev $FDDEVICE
3175    fi
3176    OfferToMakeBootableISO $CACHE_LOC
3177    LogIt "Finished."
3178elif [ "$TAPEDEV" ] ; then
3179    OfferToMakeBootableISO $CACHE_LOC
3180    if [ -e "$CACHE_LOC/all.tar.gz" ] ; then
3181        cp -f $CACHE_LOC/all.tar.gz $MINDI_TMP/ 2>> $LOGFILE
3182    else
3183        Die "Cannot find all.tar.gz, to be written to tape"
3184    fi
3185else
3186    OfferToMakeBootableISO $CACHE_LOC
3187fi
3188# cleanup
3189LogIt "$FRIENDLY_OUTSTRING"
3190for mtpt in $FLOPPY_WAS_MOUNTED ; do
3191    mount $mtpt
3192done
3193MindiExit 0
Note: See TracBrowser for help on using the repository browser.