source: branches/2.2.5/mindi/mindi @ 1876

Last change on this file since 1876 was 1876, checked in by bruno, 12 years ago

More logs on dependencies computation to help debug

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