Ticket #414: mindi.modified.040710

File mindi.modified.040710, 108.2 KB (added by Michael Shapiro, 11 years ago)

mindi modified with fixes

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