source: MondoRescue/branches/stable/mondo/src/common/libmondo-tools.c @ 1639

Last change on this file since 1639 was 1639, checked in by Bruno Cornec, 12 years ago

More conf file items handled

  • Property svn:keywords set to Id
File size: 34.9 KB
Line 
1/* $Id: libmondo-tools.c 1639 2007-09-20 20:20:45Z bruno $
2misc tools
3*/
4
5/**
6 * @file
7 * Miscellaneous tools that didn't really fit anywhere else.
8 */
9
10#include "my-stuff.h"
11#include "mr_mem.h"
12#include "mr_msg.h"
13#include "mr_file.h"
14#include "mr_gettext.h"
15#include "mr_conf.h"
16
17#include "mondostructures.h"
18#include "libmondo-tools.h"
19#include "newt-specific-EXT.h"
20#include "libmondo-files-EXT.h"
21#include "libmondo-fork-EXT.h"
22#include "libmondo-raid-EXT.h"
23#include <sys/socket.h>
24#include <netdb.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <sys/utsname.h>
28
29/*@unused@*/
30//static char cvsid[] = "$Id: libmondo-tools.c 1639 2007-09-20 20:20:45Z bruno $";
31
32extern int g_tape_buffer_size_MB;
33extern char *g_erase_tmpdir_and_scratchdir;
34extern char *g_serial_string;
35extern bool g_text_mode;
36extern int g_currentY;
37extern int g_current_media_number;
38extern char *MONDO_LOGFILE;
39
40extern struct mr_ar_conf *mr_conf;
41
42/**
43 * @addtogroup globalGroup
44 * @{
45 */
46bool g_remount_cdrom_at_end,    ///< TRUE if we unmounted the CD-ROM and should remount it when done with the backup.
47 g_remount_floppy_at_end;       ///< TRUE if we unmounted the floppy and should remount it when done with the backup.
48bool g_cd_recovery;             ///< TRUE if we're making an "autonuke" backup.
49double g_kernel_version;
50
51/**
52 * The place where /boot is mounted. - Used locally only
53 */
54static char *g_boot_mountpt = NULL;
55
56/**
57 * The serial string (used to differentiate between backups) of the current backup.
58 */
59char *g_serial_string = NULL;
60
61/**
62 * The location where tmpfs is mounted, or "" if it's not mounted.
63 */
64char *g_tmpfs_mountpt = NULL;
65char *g_magicdev_command = NULL;
66
67/* @} - end of globalGroup */
68
69
70extern pid_t g_buffer_pid;
71extern pid_t g_main_pid;
72
73extern t_bkptype g_backup_media_type;
74extern char *g_backup_media_string;
75
76extern bool am_I_in_disaster_recovery_mode(void);
77
78
79/**
80 * @addtogroup utilityGroup
81 * @{
82 */
83/**
84 * Assertion handler. Prints a friendly message to the user,
85 * offering to ignore all, dump core, break to debugger,
86 * exit, or ignore. Intended to be used with an assert() macro.
87 *
88 * @param file The file in which the assertion triggered.
89 * @param function The function (@c __FUNCTION__) in which the assertion triggered.
90 * @param line The line number of the assert() statement.
91 * @param exp The expression that failed (as a string).
92 */
93void _mondo_assert_fail(const char *file,
94                        const char *function, int line, const char *exp)
95{
96    static int ignoring_assertions = 0;
97    bool is_valid = TRUE;
98
99    log_it("ASSERTION FAILED: `%s' at %s:%d in %s", exp, file, line,
100           function);
101    if (ignoring_assertions) {
102        log_it("Well, the user doesn't care...");
103        return;
104    }
105#ifndef _XWIN
106    if (!g_text_mode)
107        newtSuspend();
108#endif
109    printf(_("ASSERTION FAILED: `%s'\n"), exp);
110    printf(_("\tat %s:%d in %s\n\n"), file, line, function);
111    printf(_("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? "));
112    do {
113        is_valid = TRUE;
114        switch (toupper(getchar())) {
115        case 'A':               // ignore (A)ll
116            ignoring_assertions = 1;
117            break;
118        case 'B':               // a(B)ort
119            signal(SIGABRT, SIG_DFL);   /* prevent SIGABRT handler from running */
120            raise(SIGABRT);
121            break;              /* "can't get here" */
122        case 'D':               // (D)ebug, aka asm("int 3")
123#ifdef __IA32__
124            __asm__ __volatile__("int $3"); // break to debugger
125#endif
126            break;
127        case 'E':               // (E)xit
128            fatal_error("Failed assertion -- see above for details");
129            break;              /* "can't get here" */
130        case 'I':               // (I)gnore
131            break;
132            /* These next two work as follows:
133               the `default' catches the user's invalid choice and says so;
134               the '\n' catches the newline on the end and prints the prompt again.
135             */
136        case '\n':
137            printf
138                (_("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? "));
139            break;
140        default:
141            is_valid = FALSE;
142            printf(_("Invalid choice.\n"));
143            break;
144        }
145    } while (!is_valid);
146
147    if (ignoring_assertions) {
148        log_it("Ignoring ALL assertions from now on.");
149    } else {
150        log_it("Ignoring assertion: %s", exp);
151    }
152
153    getchar();                  // skip \n
154
155#ifndef _XWIN
156    if (!g_text_mode)
157        newtResume();
158#endif
159}
160
161/**
162 * Clean's up users' KDE desktops.
163 * @bug Details about this function are unknown.
164 */
165void clean_up_KDE_desktop_if_necessary(void)
166{
167    char *tmp;
168
169    mr_asprintf(&tmp,
170             "for i in `find /root /home -type d -name Desktop -maxdepth 2`; do \
171file=$i/.directory; if [ -f \"$file\" ] ; then mv -f $file $file.old ; \
172awk '{if (index($0, \"rootimagesmindi\")) { while (length($0)>2) { getline;} ; } \
173else { print $0;};}' $file.old  > $file ; fi ; done");
174    run_program_and_log_output(tmp, 5);
175    mr_free(tmp);
176}
177
178
179/**
180 * Locate mondoarchive's home directory. Searches in /usr/share/mondo,
181 * /usr/local/share/mondo, /opt, or if all else fails, search /usr.
182 *
183 * @param home_sz String to store the home directory ("" if it could not be found).
184 * @return 0 for success, nonzero for failure.
185 */
186int find_and_store_mondoarchives_home(char *home_sz)
187{
188    assert(home_sz != NULL);
189    strcpy(home_sz, MONDO_SHARE);
190    return (0);
191}
192
193
194char *get_architecture(void) {
195#ifdef __IA32__
196#   ifdef __X86_64__
197        return ("x86_64");
198#   else
199        return ("i386");
200#   endif
201#endif
202#ifdef __X86_64__
203    return ("x86_64");
204#endif
205#ifdef __IA64__
206    return ("ia64");
207#endif
208    return ("unknown");
209}
210
211
212char *get_uname_m(void) {
213
214    struct utsname utsn;
215    char *tmp = NULL;
216
217    uname(&utsn);
218    mr_asprintf(&tmp, utsn.machine);
219    return (tmp);
220}
221
222
223
224/* BERLIOS: Use uname instead */
225double get_kernel_version(void)
226{
227    char *p, tmp[200];
228    double d;
229#ifdef __FreeBSD__
230    // JOSH - FIXME :)
231    d = 5.2;                    // :-)
232#else
233    strcpy(tmp, call_program_and_get_last_line_of_output("uname -r"));
234    p = strchr(tmp, '.');
235    if (p) {
236        p = strchr(++p, '.');
237        if (p) {
238            while (*p) {
239                *p = *(p + 1);
240                p++;
241            }
242        }
243    }
244//  mr_msg(1, "tmp = '%s'", tmp);
245    d = atof(tmp);
246#endif
247    mr_msg(1, "g_kernel_version = %f", d);
248    return (d);
249}
250
251
252/**
253 * Get the current time.
254 * @return number of seconds since the epoch.
255 */
256long get_time(void)
257{
258    return (long) time((void *) 0);
259}
260
261
262/**
263 * Initialize a RAID volume structure, setting fields to zero. The
264 * actual hard drive is unaffected.
265 *
266 * @param raidrec The RAID volume structure to initialize.
267 * @note This function is system dependent.
268 */
269#ifdef __FreeBSD__
270void initialize_raidrec(struct vinum_volume *raidrec)
271{
272    int i, j;
273    raidrec->volname[0] = '\0';
274    raidrec->plexes = 0;
275    for (i = 0; i < 9; ++i) {
276        raidrec->plex[i].raidlevel = -1;
277        raidrec->plex[i].stripesize = 0;
278        raidrec->plex[i].subdisks = 0;
279        for (j = 0; j < 9; ++j) {
280            strcpy(raidrec->plex[i].sd[j].which_device, "");
281        }
282    }
283}
284#else
285void initialize_raidrec(struct raid_device_record *raidrec)
286{
287    assert(raidrec != NULL);
288    raidrec->raid_device[0] = '\0';
289    raidrec->raid_level = -9;
290    raidrec->persistent_superblock = 1;
291    raidrec->chunk_size = 64;
292    raidrec->parity = -1;
293    raidrec->data_disks.entries = 0;
294    raidrec->spare_disks.entries = 0;
295    raidrec->parity_disks.entries = 0;
296    raidrec->failed_disks.entries = 0;
297    raidrec->additional_vars.entries = 0;
298}
299#endif
300
301
302/**
303 * Insert modules that Mondo requires.
304 * Currently inserts @c msdos, @c vfat, and @c loop for Linux;
305 * @c msdosfs and @c ext2fs for FreeBSD.
306 */
307void insmod_crucial_modules(void)
308{
309#ifdef __FreeBSD__
310    system("kldstat | grep msdosfs || kldload msdosfs 2> /dev/null");
311    system("kldstat | grep ext2fs  || kldload ext2fs 2> /dev/null");
312#else
313    system("modprobe -a msdos vfat loop &> /dev/null");
314#endif
315}
316
317
318/**
319 * Finish configuring the backup information structure. Call this function
320 * to set the parameters that depend on those that can be given on the command
321 * line.
322 *
323 * @param bkpinfo The backup information structure. Fields modified/used:
324 * - Used: @c bkpinfo->backup_data
325 * - Used: @c bkpinfo->backup_media_type
326 * - Used: @c bkpinfo->writer_speed
327 * - Used: @c bkpinfo->compression_level
328 * - Used: @c bkpinfo->include_paths
329 * - Used: @c bkpinfo->prefix
330 * - Used: @c bkpinfo->isodir
331 * - Used: @c bkpinfo->manual_tray
332 * - Used: @c bkpinfo->make_cd_use_lilo
333 * - Used: @c bkpinfo->media_device
334 * - Used: @c bkpinfo->nfs_mount
335 * - Used: @c bkpinfo->nonbootable_backup
336 * - Used: @c bkpinfo->scratchdir
337 * - Used: @c bkpinfo->tmpdir
338 * - Used: @c bkpinfo->use_lzo
339 * - Modified: @c bkpinfo->call_before_iso
340 * - Modified: @c bkpinfo->call_make_iso
341 * - Modified: @c bkpinfo->optimal_set_size
342 * - Modified: @c bkpinfo->zip_exe
343 * - Modified: @c bkpinfo->zip_suffix
344 *
345 * @return number of errors, or 0 for success.
346 * @note Also creates directories that are specified in the @c bkpinfo structure but
347 * do not exist.
348 */
349int post_param_configuration(struct s_bkpinfo *bkpinfo)
350{
351    char *extra_cdrom_params = NULL;
352    char *mondo_mkisofs_sz = NULL;
353    char *command = NULL;
354    char *hostname = NULL;
355    char *ip_address = NULL;
356    int retval = 0;
357    long avm = 0;
358    char *colon = NULL;
359    char *tmp = NULL;
360    char *call_before_iso_user = NULL;
361    int rdsiz_MB;
362    char *iso_dev = NULL;
363    char *iso_mnt = NULL;
364    char *iso_tmp = NULL;
365    char *iso_path = NULL;
366    FILE *fd1 = NULL;
367
368    assert(bkpinfo != NULL);
369    bkpinfo->optimal_set_size =
370        (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 16 : 16) *
371        1024;
372
373    mr_msg(1, "Foo");
374    if (bkpinfo->backup_media_type == tape) {
375        mr_msg(1, "Bar");
376        mr_asprintf(&tmp, "mt -f %s status", bkpinfo->media_device);
377        mr_msg(1, "tmp = '%s'", tmp);
378        if (run_program_and_log_output(tmp, 3)) {
379            fatal_error
380                ("Unable to open tape device. If you haven't specified it with -d, do so. If you already have, check your parameter. I think it's wrong.");
381        }
382        mr_free(tmp);
383    }
384    make_hole_for_dir(bkpinfo->scratchdir);
385    make_hole_for_dir(bkpinfo->tmpdir);
386    if (bkpinfo->backup_media_type == iso)
387        make_hole_for_dir(bkpinfo->isodir);
388
389    run_program_and_log_output("uname -a", 5);
390    run_program_and_log_output("cat /etc/*-release", 5);
391    sprintf(g_tmpfs_mountpt, "%s/tmpfs", bkpinfo->tmpdir);
392    mr_asprintf(&command, "mkdir -p %s", g_tmpfs_mountpt);
393    paranoid_system(command);
394    mr_free(command);
395    rdsiz_MB = PPCFG_RAMDISK_SIZE + g_tape_buffer_size_MB;
396#ifdef __FreeBSD__
397    mr_asprintf(&tmp,
398           call_program_and_get_last_line_of_output
399           ("vmstat | tail -1 | tr -s ' ' | cut -d' ' -f6"));
400    avm += atol(tmp);
401    mr_free(tmp);
402    mr_asprintf(&tmp,
403           call_program_and_get_last_line_of_output
404           ("swapinfo | grep -v Device | tr -s ' ' | cut -d' ' -f4 | tr '\n' '+' | sed 's/+$//' | bc"));
405    avm += atol(tmp);
406    mr_free(tmp);
407    mr_asprintf(&command, "mdmfs -s %d%c md9 %s", rdsiz_MB, 'm',
408             g_tmpfs_mountpt);
409#else
410    mr_asprintf(&tmp,
411           call_program_and_get_last_line_of_output
412           ("free | grep ':' | tr -s ' ' '\t' | cut -f2 | head -n1"));
413    avm += atol(tmp);
414    mr_free(tmp);
415    mr_asprintf(&command, "mount /dev/shm -t tmpfs %s -o size=%d%c",
416            g_tmpfs_mountpt, rdsiz_MB, 'm');
417    run_program_and_log_output("cat /proc/cpuinfo", 5);
418    /* BERLIOS: rpm is not necessarily there ! */
419    run_program_and_log_output
420        ("rpm -q newt newt-devel slang slang-devel ncurses ncurses-devel gcc",
421         5);
422#endif
423    if (avm / 1024 > rdsiz_MB * 3) {
424        if (run_program_and_log_output(command, 5)) {
425            g_tmpfs_mountpt[0] = '\0';
426            log_it("Failed to mount tmpfs");
427        } else {
428            log_it("Tmpfs mounted OK - %d MB", rdsiz_MB);
429        }
430    } else {
431        g_tmpfs_mountpt[0] = '\0';
432        log_it("It doesn't seem you have enough swap to use tmpfs. Fine.");
433    }
434    mr_free(command);
435
436    if (bkpinfo->use_lzo) {
437        strcpy(bkpinfo->zip_exe, "lzop");
438        strcpy(bkpinfo->zip_suffix, "lzo");
439    } else if (bkpinfo->use_gzip) {
440        strcpy(bkpinfo->zip_exe, "gzip");
441        strcpy(bkpinfo->zip_suffix, "gz");
442    } else if (bkpinfo->compression_level != 0) {
443        strcpy(bkpinfo->zip_exe, "bzip2");
444        strcpy(bkpinfo->zip_suffix, "bz2");
445    } else {
446        bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
447    }
448
449// CD-R or CD-RW or DVD
450    if (bkpinfo->backup_media_type == cdrw
451        || bkpinfo->backup_media_type == dvd
452        || bkpinfo->backup_media_type == iso
453        || bkpinfo->backup_media_type == nfs
454        || bkpinfo->backup_media_type == cdr) {
455        if (!bkpinfo->manual_tray) {
456            mr_strcat(extra_cdrom_params, "-waiti ");
457        }
458        if (bkpinfo->backup_media_type == cdrw) {
459            mr_strcat(extra_cdrom_params, "blank=fast ");
460        }
461        if (bkpinfo->nonbootable_backup) {
462            mr_asprintf(&mondo_mkisofs_sz, "%s -no-boot", mr_conf->iso_creation_opt);
463        } else if
464#ifdef __FreeBSD__
465            (TRUE)
466#else
467            (bkpinfo->make_cd_use_lilo)
468#endif
469#ifdef __IA64__
470    {
471        mr_asprintf(&mondo_mkisofs_sz, "%s -b images/mindi-boot.2880.img -c boot.cat", mr_conf->iso_creation_opt);
472    }
473#else
474    {
475        mr_asprintf(&mondo_mkisofs_sz, "%s -b isolinux.bin -c boot.cat", mr_conf->iso_creation_opt);
476    }
477#endif
478        else {
479            mr_asprintf(&mondo_mkisofs_sz, "%s -b isolinux.bin -c boot.cat", mr_conf->iso_creation_opt);
480        }
481        if (bkpinfo->manual_tray) {
482            if (strstr(mr_conf->iso_burning_cmd,"growisofs") != NULL) {
483                fatal_error("Unable to use manual CD tray with growisofs");
484            }
485            if (bkpinfo->call_before_iso[0] == '\0') {
486                sprintf(bkpinfo->call_before_iso,
487                        "%s %s -o %s/temporary.iso . 2>> %s",
488                        mr_conf->iso_creation_cmd,mondo_mkisofs_sz, bkpinfo->tmpdir, MONDO_LOGFILE);
489            } else {
490                mr_asprintf(&call_before_iso_user, bkpinfo->call_before_iso);
491                sprintf (bkpinfo->call_before_iso,
492                                    "(%s %s -o %s/temporary.iso . 2>> %s ; %s )",
493                        mr_conf->iso_creation_cmd,mondo_mkisofs_sz, bkpinfo->tmpdir, MONDO_LOGFILE, call_before_iso_user);
494                mr_free(call_before_iso_user);
495            }
496            log_it("bkpinfo->call_before_iso = %s", bkpinfo->call_before_iso);
497            sprintf(bkpinfo->call_make_iso,
498                    "%s %s %s dev=%s speed=%d %s/temporary.iso",
499                    mr_conf->iso_burning_cmd,
500                    extra_cdrom_params, 
501                    mr_conf->iso_burning_opt, 
502                    bkpinfo->iso_burning_dev,
503                    bkpinfo->writer_speed, 
504                    bkpinfo->tmpdir);
505        } else {
506            if (bkpinfo->backup_media_type == iso) {
507                sprintf(bkpinfo->call_make_iso,
508                    "%s %s . 2>> %s",
509                        mr_conf->iso_creation_cmd,
510                        mondo_mkisofs_sz, 
511                        MONDO_LOGFILE);
512            } else {
513                if (strstr(mr_conf->iso_burning_cmd,"growisofs") != NULL) {
514                    if (getenv ("SUDO_COMMAND")) {
515                        mr_asprintf(&command, "strings %s | grep -c SUDO_COMMAND", mr_conf->iso_burning_cmd);
516                        if (!strcmp(call_program_and_get_last_line_of_output(command), "1")) {
517                            fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
518                        }
519                        mr_free(command);
520                    }
521                    sprintf(bkpinfo->call_make_iso,
522                        "%s %s %s %s -Z %s -speed=%d . 2>> %s",
523                        mr_conf->iso_burning_cmd,
524                        mondo_mkisofs_sz, 
525                        extra_cdrom_params, 
526                        mr_conf->iso_burning_opt,
527                        bkpinfo->iso_burning_dev,
528                        bkpinfo->writer_speed,
529                        MONDO_LOGFILE);
530                } else {
531                    sprintf(bkpinfo->call_make_iso,
532                        "%s %s . 2>> %s | %s %s %s dev=%s speed=%d -",
533                        mr_conf->iso_creation_cmd,
534                        mondo_mkisofs_sz, 
535                        MONDO_LOGFILE,
536                        mr_conf->iso_burning_cmd,
537                        extra_cdrom_params, 
538                        mr_conf->iso_burning_opt,
539                        bkpinfo->iso_burning_dev,
540                        bkpinfo->writer_speed);
541                }
542            mr_free(mondo_mkisofs_sz);
543            mr_free(extra_cdrom_params);
544            }
545        }
546    }                           // end of CD code
547
548    if (bkpinfo->backup_media_type == iso) {
549
550/* Patch by Conor Daly <conor.daly@met.ie>
551 * 23-june-2004
552 * Break up isodir into iso_mnt and iso_path
553 * These will be used along with iso-dev at restore time
554 * to locate the ISOs where ever they're mounted
555 */
556
557        log_it("isodir = %s", bkpinfo->isodir);
558        mr_asprintf(&command, "df -P %s | tail -n1 | cut -d' ' -f1",
559                 bkpinfo->isodir);
560        log_it("command = %s", command);
561        mr_asprintf(&iso_dev, call_program_and_get_last_line_of_output(command));
562        mr_free(command);
563        log_it("res of it = %s", iso_dev);
564
565        fd1 = mr_fopen(MONDORESTORECFG, "a");
566        mr_fprintf(fd1, "iso-dev=%s\n", iso_dev);
567
568        mr_asprintf(&command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3",
569                 iso_dev);
570        mr_free(iso_dev);
571
572        log_it("command = %s", command);
573        mr_asprintf(&iso_mnt,call_program_and_get_last_line_of_output(command));
574        mr_free(command);
575
576        log_it("res of it = %s", iso_mnt);
577        mr_fprintf(fd1, "iso-mnt=%s\n", iso_mnt);
578        log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
579
580        mr_asprintf(&iso_tmp, "%s", bkpinfo->isodir);
581        if (strlen(iso_tmp) >= strlen(iso_mnt)) {
582            mr_asprintf(&iso_path, "%s", iso_tmp + strlen(iso_mnt));
583        } else {
584            mr_asprintf(&iso_path, "");
585        }
586        mr_free(iso_tmp);
587        mr_free(iso_mnt);
588
589        log_it("isodir: %s", iso_path);
590        mr_fprintf(fd1, "isodir=%s\n", iso_path);
591        mr_free(iso_path);
592
593        log_it("iso-prefix: %s",  bkpinfo->prefix);
594        mr_fprintf(fd1, "iso-prefix=%s\n", bkpinfo->prefix);
595
596        mr_fclose(fd1);
597    }                           // end of iso code
598
599    if (bkpinfo->backup_media_type == nfs) {
600        mr_asprintf(&hostname, bkpinfo->nfs_mount);
601        colon = strchr(hostname, ':');
602        if (!colon) {
603            log_it("nfs mount doesn't have a colon in it");
604            retval++;
605        } else {
606            struct hostent *hent;
607
608            *colon = '\0';
609            hent = gethostbyname(hostname);
610            if (!hent) {
611                log_it("Can't resolve NFS mount (%s): %s", hostname,
612                       hstrerror(h_errno));
613                retval++;
614            } else {
615                mr_asprintf(&ip_address, "%s%s", inet_ntoa((struct in_addr)
616                                                *((struct in_addr *) hent->h_addr)),
617                                                strchr(bkpinfo->nfs_mount, ':'));
618                strcpy(bkpinfo->nfs_mount, ip_address);
619                mr_free(ip_address);
620            }
621        }
622        store_nfs_config(bkpinfo);
623        mr_free(hostname);
624    }
625
626    log_it("Finished processing incoming params");
627    if (retval) {
628        fprintf(stderr, "Type 'man mondoarchive' for help.\n");
629    }
630
631    if (strlen(bkpinfo->tmpdir) < 2 || strlen(bkpinfo->scratchdir) < 2) {
632        log_it("tmpdir or scratchdir are blank/missing");
633        retval++;
634    }
635    if (bkpinfo->include_paths[0] == '\0') {
636        //      fatal_error ("Why no backup path?");
637        strcpy(bkpinfo->include_paths, "/");
638    }
639    chmod(bkpinfo->scratchdir, 0700);
640    chmod(bkpinfo->tmpdir, 0700);
641    g_backup_media_type = bkpinfo->backup_media_type;
642    g_backup_media_string = bkpinfo->backup_media_string;
643    return (retval);
644}
645
646
647/**
648 * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
649 * Seeds the random-number generator, loads important modules, checks the sanity
650 * of the user's Linux distribution, and deletes logfile.
651 * @param bkpinfo The backup information structure. Will be initialized.
652 * @return number of errors (0 for success)
653 */
654int pre_param_configuration(struct s_bkpinfo *bkpinfo)
655{
656    int res = 0;
657
658    make_hole_for_dir(MNT_CDROM);
659    assert(bkpinfo != NULL);
660    srandom((unsigned long) (time(NULL)));
661    insmod_crucial_modules();
662    reset_bkpinfo(bkpinfo);     // also sets defaults ('/'=backup path, 3=compression level)
663    if (bkpinfo->disaster_recovery) {
664        if (!does_nonMS_partition_exist()) {
665            fatal_error
666                ("I am in disaster recovery mode\nPlease don't run mondoarchive.");
667        }
668    }
669
670    run_program_and_log_output("rm -Rf /tmp/changed.files*", FALSE);
671    res += some_basic_system_sanity_checks();
672    if (res) {
673        log_it("Your distribution did not pass Mondo's sanity test.");
674    }
675    g_current_media_number = 1;
676    bkpinfo->postnuke_tarball[0] = bkpinfo->nfs_mount[0] = '\0';
677    return (res);
678}
679
680
681
682
683/**
684 * Reset all fields of the backup information structure to a sensible default.
685 * @param bkpinfo The @c bkpinfo to reset.
686 */
687void reset_bkpinfo(struct s_bkpinfo *bkpinfo)
688{
689    char *tmp = NULL;
690
691    mr_msg(1, "Hi");
692
693    assert(bkpinfo != NULL);
694    /* BERLIOS : Useless ?? */
695    memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
696
697    bkpinfo->manual_tray = mr_conf->manual_tray;
698    bkpinfo->internal_tape_block_size = mr_conf->internal_tape_blocksize;
699    mr_asprintf(&tmp,mr_conf->media_device);
700    bkpinfo->media_device = tmp;
701    mr_asprintf(&tmp,mr_conf->iso_burning_dev);
702    bkpinfo->iso_burning_dev = tmp;
703
704    bkpinfo->media_size = mr_conf->iso_burning_speed;
705    bkpinfo->boot_loader = '\0';
706    bkpinfo->boot_device[0] = '\0';
707    bkpinfo->zip_exe[0] = '\0';
708    bkpinfo->zip_suffix[0] = '\0';
709    bkpinfo->restore_path[0] = '\0';
710    bkpinfo->use_lzo = FALSE;
711    bkpinfo->use_gzip = FALSE;
712    bkpinfo->do_not_compress_these[0] = '\0';
713    bkpinfo->verify_data = FALSE;
714    bkpinfo->backup_data = FALSE;
715    bkpinfo->restore_data = FALSE;
716    bkpinfo->disaster_recovery =
717        (am_I_in_disaster_recovery_mode()? TRUE : FALSE);
718    if (bkpinfo->disaster_recovery) {
719        strcpy(bkpinfo->isodir, "/");
720    } else {
721        strcpy(bkpinfo->isodir, "/var/cache/mondo/iso");
722    }
723    mr_asprintf(&tmp,mr_conf->prefix);
724    bkpinfo->prefix = tmp;
725
726    bkpinfo->scratchdir[0] = '\0';
727    bkpinfo->make_filelist = TRUE;  // unless -J supplied to mondoarchive
728    sprintf(bkpinfo->tmpdir, "/tmp/tmpfs/mondo.tmp.%d", (int) (random() % 32768));  // for mondorestore
729    bkpinfo->optimal_set_size = 0;
730    bkpinfo->backup_media_type = none;
731    bkpinfo->backup_media_string[0] = '\0';
732    strcpy(bkpinfo->include_paths, "/");
733    bkpinfo->exclude_paths[0] = '\0';
734    bkpinfo->call_before_iso[0] = '\0';
735    bkpinfo->call_make_iso[0] = '\0';
736    bkpinfo->call_after_iso[0] = '\0';
737    bkpinfo->image_devs[0] = '\0';
738    bkpinfo->postnuke_tarball[0] = '\0';
739    bkpinfo->kernel_path[0] = '\0';
740    bkpinfo->nfs_mount[0] = '\0';
741    bkpinfo->nfs_remote_dir[0] = '\0';
742    bkpinfo->wipe_media_first = FALSE;
743    bkpinfo->differential = FALSE;
744    bkpinfo->writer_speed = mr_conf->iso_burning_speed;
745    bkpinfo->compression_level = 3;
746}
747
748
749/**
750 * Get the remaining free space (in MB) on @p partition.
751 * @param partition The partition to check free space on (either a device or a mountpoint).
752 * @return The free space on @p partition, in MB.
753 */
754long free_space_on_given_partition(char *partition)
755{
756    char *command = NULL;
757    char *out_sz = NULL;
758    long res = 0L;
759
760    assert_string_is_neither_NULL_nor_zerolength(partition);
761
762    mr_asprintf(&command, "df -m -P %s &> /dev/null", partition);
763    if (system(command)) {
764        return (-1);
765    }                           // partition does not exist
766    mr_free(command);
767
768    mr_asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
769            partition);
770    mr_asprintf(&out_sz, call_program_and_get_last_line_of_output(command));
771    mr_free(command);
772
773    if (strlen(out_sz) == 0) {
774        return (-1);
775    }                           // error within df, probably
776    res = atol(out_sz);
777    mr_free(out_sz);
778    return (res);
779}
780
781
782/**
783 * Check the user's system for sanity. Checks performed:
784 * - make sure user has enough RAM (32mb required, 64mb recommended)
785 * - make sure user has enough free space in @c /
786 * - check kernel for ramdisk support
787 * - make sure afio, cdrecord, bzip2, awk, md5sum, strings, mindi, and buffer exist
788 * - make sure CD-ROM is unmounted
789 * - make sure /etc/modules.conf exists
790 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
791 *
792 * @return number of problems with the user's setup (0 for success)
793 */
794int some_basic_system_sanity_checks()
795{
796
797    /*@ buffers ************ */
798    char *tmp = NULL;
799
800    /*@ int's *************** */
801    int retval = 0;
802    long Lres;
803
804
805    mvaddstr_and_log_it(g_currentY, 0,
806                        "Checking sanity of your Linux distribution");
807#ifndef __FreeBSD__
808    if (system("which mkfs.vfat &> /dev/null")
809        && !system("which mkfs.msdos &> /dev/null")) {
810        log_it
811            ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
812        run_program_and_log_output
813            ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
814    }
815    mr_asprintf(&tmp,
816           call_program_and_get_last_line_of_output
817           ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
818    if (atol(tmp) < 35000) {
819        retval++;
820        log_to_screen(_("You must have at least 32MB of RAM to use Mondo."));
821    }
822    if (atol(tmp) < 66000) {
823        log_to_screen
824            (_("WARNING! You have very little RAM. Please upgrade to 64MB or more."));
825    }
826    mr_free(tmp);
827#endif
828
829    Lres = free_space_on_given_partition(MONDO_CACHE);
830    log_it("Free space on given partition = %ld MB", Lres);
831
832    if (Lres < 50) {
833        fatal_error("Your "MONDO_CACHE" partition has <50MB free. Please adjust your partition table to something saner or make "MONDO_CACHE" a symbolic link"); 
834    }
835
836    if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
837        retval++;
838        log_to_screen
839            ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
840        fatal_error
841            ("Please use \"su -\", not \"su\" to become root. OK? ...and please don't e-mail the mailing list or me about this. Just read the message. :)");
842    }
843#ifndef __FreeBSD__
844    if (run_program_and_log_output
845        ("grep ramdisk /proc/devices", FALSE)) {
846        if (!ask_me_yes_or_no
847            (_("Your kernel has no ramdisk support. That's mind-numbingly stupid but I'll allow it if you're planning to use a failsafe kernel. Are you?")))
848        {
849            //          retval++;
850            log_to_screen
851                (_("It looks as if your kernel lacks ramdisk and initrd support."));
852            log_to_screen
853                (_("I'll allow you to proceed but FYI, if I'm right, your kernel is broken."));
854        }
855    }
856#endif
857    retval += whine_if_not_found(mr_conf->iso_creation_cmd);
858    retval += whine_if_not_found(mr_conf->iso_burning_cmd);
859    retval += whine_if_not_found(MKE2FS_OR_NEWFS);
860    retval += whine_if_not_found("bzip2");
861    retval += whine_if_not_found("gzip");
862    retval += whine_if_not_found("awk");
863    retval += whine_if_not_found("md5sum");
864    retval += whine_if_not_found("strings");
865    retval += whine_if_not_found("mindi");
866    retval += whine_if_not_found("buffer");
867
868    // abort if Windows partition but no ms-sys and parted
869    if (!run_program_and_log_output
870        ("mount | grep -w vfat | grep -vE \"/dev/fd|nexdisk\"", 0)
871        ||
872        !run_program_and_log_output
873        ("mount | grep -w dos | grep -vE \"/dev/fd|nexdisk\"", 0)) {
874        log_to_screen(_("I think you have a Windows 9x partition."));
875        retval += whine_if_not_found("parted");
876#ifndef __IA64__
877        /* IA64 always has one vfat partition for EFI even without Windows */
878        // retval +=
879        if (!find_home_of_exe("ms-sys")) {
880            log_to_screen("Please install ms-sys just in case.");
881        }
882#endif
883    }
884
885    if (!find_home_of_exe("cmp")) {
886        if (!find_home_of_exe("true")) {
887            whine_if_not_found("cmp");
888        } else {
889            log_to_screen
890                ("Your system lacks the 'cmp' binary. I'll create a dummy cmp for you.");
891            if (run_program_and_log_output
892                ("cp -f `which true` /usr/bin/cmp", 0)) {
893                fatal_error("Failed to create dummy 'cmp' file.");
894            }
895        }
896    }
897    run_program_and_log_output
898        ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
899    mr_asprintf(&tmp,
900           call_program_and_get_last_line_of_output
901           ("mount | grep -E \"cdr(om|w)\""));
902    if (strcmp("", tmp)) {
903        if (strstr(tmp, "autofs")) {
904            log_to_screen
905                (_("Your CD-ROM is mounted via autofs. I therefore cannot tell"));
906            log_to_screen
907                (_("if a CD actually is inserted. If a CD is inserted, please"));
908            log_to_screen(_("eject it. Thank you."));
909            log_it
910                ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
911        } else
912            if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
913            retval++;
914            fatal_error
915                ("Your CD-ROM drive is mounted. Please unmount it.");
916        }
917    }
918    mr_free(tmp);
919#ifndef __FreeBSD__
920    if (!does_file_exist("/etc/modules.conf")) {
921        if (does_file_exist("/etc/conf.modules")) {
922            log_it("Linking /etc/modules.conf to /etc/conf.modules");
923            run_program_and_log_output
924                ("ln -sf /etc/conf.modules /etc/modules.conf", 5);
925        } else if (does_file_exist("/etc/modprobe.d")) {
926            log_it
927                ("Directory /etc/modprobe.d found. mindi will use its contents.");
928        } else if (does_file_exist("/etc/modprobe.conf")) {
929            log_it("Linking /etc/modules.conf to /etc/modprobe.conf");
930            run_program_and_log_output
931                ("ln -sf /etc/modprobe.conf /etc/modules.conf", 5);
932        } else {
933            retval++;
934            log_to_screen
935                (_("Please find out what happened to /etc/modules.conf"));
936        }
937    }
938#endif
939
940    run_program_and_log_output("cat /etc/fstab", 5);
941#ifdef __FreeBSD__
942    run_program_and_log_output("vinum printconfig", 5);
943#else
944    run_program_and_log_output("cat /etc/raidtab", 5);
945#endif
946
947    if (run_program_and_log_output("mindi -V", 1)) {
948        log_to_screen(_("Could not ascertain mindi's version number."));
949        log_to_screen
950            (_("You have not installed Mondo and/or Mindi properly."));
951        log_to_screen(_("Please uninstall and reinstall them both."));
952        fatal_error("Please reinstall Mondo and Mindi.");
953    }
954    if (run_program_and_log_output
955        ("mindi --makemountlist /tmp/mountlist.txt.test", 5)) {
956        log_to_screen
957            (_("Mindi --makemountlist /tmp/mountlist.txt.test failed for some reason."));
958        log_to_screen
959            (_("Please run that command by hand and examine /var/log/mindi.log"));
960        log_to_screen
961            (_("for more information. Perhaps your /etc/fstab file is insane."));
962        log_to_screen
963            (_("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see."));
964        retval++;
965    }
966
967    if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
968        && !does_file_exist("/etc/raidtab")) {
969        log_to_screen
970            (_("You have RAID partitions but no /etc/raidtab - creating one from "MDSTAT_FILE));
971        create_raidtab_from_mdstat("/etc/raidtab");
972    }
973
974    if (retval) {
975        mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
976    } else {
977        mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
978    }
979    return (retval);
980}
981
982/**
983 * Retrieve the line containing @p label from the config file.
984 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
985 * @param label What to read from the file.
986 * @param value Where to put it.
987 * @return 0 for success, 1 for failure.
988 */
989int read_cfg_var(char *config_file, char *label, char *value)
990{
991    /*@ buffer ****************************************************** */
992    char *command = NULL;
993    char *tmp = NULL;
994
995    /*@ end vars *************************************************** */
996
997    assert_string_is_neither_NULL_nor_zerolength(config_file);
998    assert_string_is_neither_NULL_nor_zerolength(label);
999
1000    if (!does_file_exist(config_file)) {
1001        mr_asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
1002                 config_file);
1003        log_to_screen(tmp);
1004        mr_free(tmp);
1005        value[0] = '\0';
1006        return (1);
1007    /* } BERLIOS: Useless:
1008         else if (strstr(value, "/dev/") && strstr(value, "t0")
1009               && !strcmp(label, "media-dev")) {
1010        mr_msg(2, "FYI, I shan't read new value for %s - already got %s",
1011                label, value);
1012        return (0);
1013    */ } else {
1014        mr_asprintf(&command, "grep '%s .*' %s| cut -d'=' -f2,3,4,5",
1015                label, config_file);
1016        strcpy(value, call_program_and_get_last_line_of_output(command));
1017        mr_free(command);
1018        if (strlen(value) == 0) {
1019            return (1);
1020        } else {
1021            return (0);
1022        }
1023    }
1024}
1025
1026
1027/**
1028 * Remount @c supermount if it was unmounted earlier.
1029 */
1030void remount_supermounts_if_necessary()
1031{
1032    if (g_remount_cdrom_at_end) {
1033        run_program_and_log_output("mount " MNT_CDROM, FALSE);
1034    }
1035}
1036
1037/**
1038 * Unmount @c supermount if it's mounted.
1039 */
1040void unmount_supermounts_if_necessary()
1041{
1042    if (run_program_and_log_output
1043        ("mount | grep cdrom | grep super", FALSE) == 0) {
1044        g_remount_cdrom_at_end = TRUE;
1045        run_program_and_log_output("umount " MNT_CDROM, FALSE);
1046    }
1047}
1048
1049/**
1050 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1051 */
1052void mount_boot_if_necessary()
1053{
1054    char *tmp = NULL;
1055    char *tmp1 = NULL;
1056    char *command = NULL;
1057
1058    mr_msg(1, "Started sub");
1059    mr_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
1060    g_boot_mountpt[0] = '\0';
1061    mr_msg(4, "Done. Great. Setting command to something");
1062    mr_asprintf(&command,
1063             "grep -v ':' /etc/fstab | grep -vE '^#.*$' | grep -E \"[   ]/boot[     ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
1064    mr_msg(4, "Cool. Command = '%s'", command);
1065    mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
1066    mr_free(command);
1067
1068    mr_msg(4, "tmp = '%s'", tmp);
1069    if (tmp[0]) {
1070        log_it("/boot is at %s according to /etc/fstab", tmp);
1071        if ((strstr(tmp, "LABEL=") || strstr(tmp,"UUID="))) {
1072            if (!run_program_and_log_output("mount /boot", 5)) {
1073                strcpy(g_boot_mountpt, "/boot");
1074                mr_msg(1, "Mounted /boot");
1075            } else {
1076                log_it("...ignored cos it's a label or uuid :-)");
1077            }
1078        } else {
1079            mr_asprintf(&command, "mount | grep -E '^%s'", tmp);
1080            mr_msg(3, "command = %s", command);
1081            if (run_program_and_log_output(command, 5)) {
1082                strcpy(g_boot_mountpt, tmp);
1083                mr_asprintf(&tmp1,
1084                        "%s (your /boot partition) is not mounted. I'll mount it before backing up",
1085                        g_boot_mountpt);
1086                log_it(tmp1);
1087                mr_free(tmp1);
1088
1089                mr_asprintf(&tmp1, "mount %s", g_boot_mountpt);
1090                if (run_program_and_log_output(tmp1, 5)) {
1091                    g_boot_mountpt[0] = '\0';
1092                    mr_msg(1, "Plan B");
1093                    if (!run_program_and_log_output("mount /boot", 5)) {
1094                        strcpy(g_boot_mountpt, "/boot");
1095                        mr_msg(1, "Plan B worked");
1096                    } else {
1097                        mr_msg(1,
1098                                "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1099                    }
1100                }
1101                mr_free(tmp1);
1102            }
1103            mr_free(command);
1104        }
1105    }
1106    mr_free(tmp);
1107    mr_msg(1, "Ended sub");
1108}
1109
1110
1111/**
1112 * If we mounted /boot earlier, unmount it.
1113 */
1114void unmount_boot_if_necessary()
1115{
1116    char *tmp;
1117
1118    mr_msg(3, "starting");
1119    if (g_boot_mountpt[0]) {
1120        mr_asprintf(&tmp, "umount %s", g_boot_mountpt);
1121        if (run_program_and_log_output(tmp, 5)) {
1122            log_it("WARNING - unable to unmount /boot");
1123        }
1124        mr_free(tmp);
1125    }
1126    mr_msg(3, "leaving");
1127}
1128
1129
1130/**
1131 * Write a line to a configuration file. Writes a line of the form,
1132 * @c label @c value.
1133 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1134 * @param label What to call this bit of data you're writing.
1135 * @param value The bit of data you're writing.
1136 * @return 0 for success, 1 for failure.
1137 */
1138int write_cfg_var(char *config_file, char *label, char *value)
1139{
1140    /*@ buffers ***************************************************** */
1141    char *command = NULL;
1142    char *tempfile = NULL;
1143    char *tmp = NULL;
1144
1145
1146    /*@ end vars *************************************************** */
1147    assert_string_is_neither_NULL_nor_zerolength(config_file);
1148    assert_string_is_neither_NULL_nor_zerolength(label);
1149    assert(value != NULL);
1150
1151    if (!does_file_exist(config_file)) {
1152        mr_asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
1153                 config_file);
1154        log_to_screen(tmp);
1155        mr_free(tmp);
1156        return (1);
1157    }
1158    mr_asprintf(&tempfile,
1159           call_program_and_get_last_line_of_output
1160           ("mktemp -q /tmp/mojo-jojo.blah.XXXXXX"));
1161    if (does_file_exist(config_file)) {
1162        mr_asprintf(&command, "grep -vE '^%s .*$' %s > %s",
1163                label, config_file, tempfile);
1164        paranoid_system(command);
1165        mr_free(command);
1166    }
1167    mr_asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
1168    paranoid_system(command);
1169    mr_free(command);
1170
1171    mr_asprintf(&command, "mv -f %s %s", tempfile, config_file);
1172    paranoid_system(command);
1173    mr_free(command);
1174    unlink(tempfile);
1175    mr_free(tempfile);
1176    return (0);
1177}
1178
1179
1180/**
1181 * Allocate or free important globals, depending on @p mal.
1182 * @param mal If TRUE, malloc; if FALSE, free.
1183 */
1184void do_libmondo_global_strings_thing(int mal)
1185{
1186    if (mal) {
1187        iamhere("Malloc'ing globals");
1188        malloc_string(g_boot_mountpt);
1189        malloc_string(g_tmpfs_mountpt);
1190        malloc_string(g_erase_tmpdir_and_scratchdir);
1191        malloc_string(g_serial_string);
1192        malloc_string(g_magicdev_command);
1193    } else {
1194        iamhere("Freeing globals");
1195        mr_free(g_boot_mountpt);
1196        mr_free(g_tmpfs_mountpt);
1197        mr_free(g_erase_tmpdir_and_scratchdir);
1198        mr_free(g_serial_string);
1199        mr_free(g_magicdev_command);
1200    }
1201}
1202
1203/**
1204 * Allocate important globals.
1205 * @see do_libmondo_global_strings_thing
1206 */
1207void malloc_libmondo_global_strings(void)
1208{
1209    do_libmondo_global_strings_thing(1);
1210}
1211
1212/**
1213 * Free important globals.
1214 * @see do_libmondo_global_strings_thing
1215 */
1216void free_libmondo_global_strings(void)
1217{
1218    do_libmondo_global_strings_thing(0);
1219}
1220
1221
1222
1223/**
1224 * Stop @c magicdev if it's running.
1225 * The command used to start it is saved in @p g_magicdev_command.
1226 */
1227void stop_magicdev_if_necessary()
1228{
1229    strcpy(g_magicdev_command,
1230           call_program_and_get_last_line_of_output
1231           ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
1232    if (g_magicdev_command[0]) {
1233        mr_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1234        paranoid_system("killall magicdev");
1235    }
1236}
1237
1238
1239/**
1240 * Restart magicdev if it was stopped.
1241 */
1242void restart_magicdev_if_necessary()
1243{
1244    char *tmp = NULL;
1245
1246    if (g_magicdev_command && g_magicdev_command[0]) {
1247        mr_asprintf(&tmp, "%s &", g_magicdev_command);
1248        paranoid_system(tmp);
1249        mr_free(tmp);
1250    }
1251}
1252
1253/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.