source: branches/3.2/mindi/mindi @ 3517

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