source: branches/stable/mindi/mindi @ 1573

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

Remove floppy support for mindi

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