source: MondoRescue/branches/2.2.10/mindi/mindi @ 2651

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

svn merge -r 2634:2650 svn+ssh://bruno@svn.mondorescue.org/mondo/svn/mondorescue/branches/2.2.9

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