source: branches/stable/mindi/mindi @ 1099

Last change on this file since 1099 was 1099, checked in by andree, 12 years ago

Add capability to mindi to detect what type of filesystem the kernel
will be able to access in initrd image during restore via new function
GetInitrdFilesystemToUse?(). Currently detected are: ext2, cpio and
cramfs. Note that the latter is not currently implemented in terms of
creating the initrd image.

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