source: trunk/mindi/mindi @ 1079

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

merge -r1045:1078 £SVN_M/branches/stable

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