source: branches/2.2.10/mindi/mindi @ 2605

Last change on this file since 2605 was 2605, checked in by bruno, 9 years ago

r3761@localhost: bruno | 2010-03-18 01:37:30 +0100

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