source: branches/2.2.9/mindi/mindi @ 2629

Last change on this file since 2629 was 2629, checked in by Bruno Cornec, 10 years ago

Integrate Michael Shapiro's patch on Xen Kernel support for RHEL 5 (only atm) and fixes #418 (Michael Shapiro)

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