source: branches/stable/mindi/mindi @ 1924

Last change on this file since 1924 was 1924, checked in by bruno, 11 years ago

svn merge -r 1902:1923 $SVN_M/branches/2.2.6

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