source: branches/stable/mindi/mindi @ 1693

Last change on this file since 1693 was 1693, checked in by Bruno Cornec, 12 years ago
  • Remove useless copy from mindi to mondo at end of USB handling
  • Fix PB macro vs tools/*2build issue
  • make_usb_fs change of interface (doesn't need a parameter)
  • Fix USB support in mondo to avoid multiple copies of files
  • Use first partiion in mondo for USB device
  • Fixes for USB CLI for both mondo/mindi
  • Try to add USB support for mondoarchive with new functions
  • du => deb for similarity with other distro type under pbconf
  • migrate gento build files under pb
  • remove now obsolete rpm spec file and gentoo build files from distributions
  • Remove DOCDIR usage in mindi + various build fixes

(merge -r1680:1692 $SVN_M/branches/2.2.5)

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