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

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

r3867@localhost: bruno | 2010-06-10 01:35:22 +0200

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