source: MondoRescue/branches/stable/mindi/mindi @ 1600

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

Add a MAX_DISKS variable and increase to 99 (idea from Tilman Schmidt <tilman_at_imap.cc>)

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