Ticket #648: mindi.648

File mindi.648, 123.3 KB (added by msross, 7 years ago)

mindi with additional boot configuration options and default configuration generation

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