source: branches/2.2.9/mindi/mindi @ 2857

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