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

Last change on this file since 1625 was 1625, checked in by Bruno Cornec, 13 years ago

remove g_loglevel and replaced with mr_conf->log_level

  • Property svn:keywords set to Id
File size: 35.3 KB
Line 
1/* $Id: libmondo-tools.c 1625 2007-09-08 23:00:49Z 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 1625 2007-09-08 23:00:49Z 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 = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
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    strcpy(bkpinfo->prefix, STD_PREFIX);
724
725    bkpinfo->scratchdir[0] = '\0';
726    bkpinfo->make_filelist = TRUE;  // unless -J supplied to mondoarchive
727    sprintf(bkpinfo->tmpdir, "/tmp/tmpfs/mondo.tmp.%d", (int) (random() % 32768));  // for mondorestore
728    bkpinfo->optimal_set_size = 0;
729    bkpinfo->backup_media_type = none;
730    bkpinfo->backup_media_string[0] = '\0';
731    strcpy(bkpinfo->include_paths, "/");
732    bkpinfo->exclude_paths[0] = '\0';
733    bkpinfo->call_before_iso[0] = '\0';
734    bkpinfo->call_make_iso[0] = '\0';
735    bkpinfo->call_after_iso[0] = '\0';
736    bkpinfo->image_devs[0] = '\0';
737    bkpinfo->postnuke_tarball[0] = '\0';
738    bkpinfo->kernel_path[0] = '\0';
739    bkpinfo->nfs_mount[0] = '\0';
740    bkpinfo->nfs_remote_dir[0] = '\0';
741    bkpinfo->wipe_media_first = FALSE;
742    bkpinfo->differential = FALSE;
743    bkpinfo->writer_speed = mr_conf->iso_burning_speed;
744// patch by Herman Kuster 
745    bkpinfo->differential = 0;
746// patch end
747    bkpinfo->compression_level = 3;
748}
749
750
751/**
752 * Get the remaining free space (in MB) on @p partition.
753 * @param partition The partition to check free space on (either a device or a mountpoint).
754 * @return The free space on @p partition, in MB.
755 */
756long free_space_on_given_partition(char *partition)
757{
758    char *command = NULL;
759    char *out_sz = NULL;
760    long res = 0L;
761
762    assert_string_is_neither_NULL_nor_zerolength(partition);
763
764    mr_asprintf(&command, "df -m -P %s &> /dev/null", partition);
765    if (system(command)) {
766        return (-1);
767    }                           // partition does not exist
768    mr_free(command);
769
770    mr_asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
771            partition);
772    mr_asprintf(&out_sz, call_program_and_get_last_line_of_output(command));
773    mr_free(command);
774
775    if (strlen(out_sz) == 0) {
776        return (-1);
777    }                           // error within df, probably
778    res = atol(out_sz);
779    mr_free(out_sz);
780    return (res);
781}
782
783
784/**
785 * Check the user's system for sanity. Checks performed:
786 * - make sure user has enough RAM (32mb required, 64mb recommended)
787 * - make sure user has enough free space in @c /
788 * - check kernel for ramdisk support
789 * - make sure afio, cdrecord, bzip2, awk, md5sum, strings, mindi, and buffer exist
790 * - make sure CD-ROM is unmounted
791 * - make sure /etc/modules.conf exists
792 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
793 *
794 * @return number of problems with the user's setup (0 for success)
795 */
796int some_basic_system_sanity_checks()
797{
798
799    /*@ buffers ************ */
800    char *tmp = NULL;
801
802    /*@ int's *************** */
803    int retval = 0;
804    long Lres;
805
806
807    mvaddstr_and_log_it(g_currentY, 0,
808                        "Checking sanity of your Linux distribution");
809#ifndef __FreeBSD__
810    if (system("which mkfs.vfat &> /dev/null")
811        && !system("which mkfs.msdos &> /dev/null")) {
812        log_it
813            ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
814        run_program_and_log_output
815            ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
816    }
817    mr_asprintf(&tmp,
818           call_program_and_get_last_line_of_output
819           ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
820    if (atol(tmp) < 35000) {
821        retval++;
822        log_to_screen(_("You must have at least 32MB of RAM to use Mondo."));
823    }
824    if (atol(tmp) < 66000) {
825        log_to_screen
826            (_("WARNING! You have very little RAM. Please upgrade to 64MB or more."));
827    }
828    mr_free(tmp);
829#endif
830
831    Lres = free_space_on_given_partition(MONDO_CACHE);
832    log_it("Free space on given partition = %ld MB", Lres);
833
834    if (Lres < 50) {
835        fatal_error("Your "MONDO_CACHE" partition has <50MB free. Please adjust your partition table to something saner or make "MONDO_CACHE" a symbolic link"); 
836    }
837
838    if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
839        retval++;
840        log_to_screen
841            ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
842        fatal_error
843            ("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. :)");
844    }
845#ifndef __FreeBSD__
846    if (run_program_and_log_output
847        ("grep ramdisk /proc/devices", FALSE)) {
848        if (!ask_me_yes_or_no
849            (_("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?")))
850        {
851            //          retval++;
852            log_to_screen
853                (_("It looks as if your kernel lacks ramdisk and initrd support."));
854            log_to_screen
855                (_("I'll allow you to proceed but FYI, if I'm right, your kernel is broken."));
856        }
857    }
858#endif
859    retval += whine_if_not_found(mr_conf->iso_creation_cmd);
860    retval += whine_if_not_found(mr_conf->iso_burning_cmd);
861    retval += whine_if_not_found(MKE2FS_OR_NEWFS);
862    retval += whine_if_not_found("bzip2");
863    retval += whine_if_not_found("gzip");
864    retval += whine_if_not_found("awk");
865    retval += whine_if_not_found("md5sum");
866    retval += whine_if_not_found("strings");
867    retval += whine_if_not_found("mindi");
868    retval += whine_if_not_found("buffer");
869
870    // abort if Windows partition but no ms-sys and parted
871    if (!run_program_and_log_output
872        ("mount | grep -w vfat | grep -vE \"/dev/fd|nexdisk\"", 0)
873        ||
874        !run_program_and_log_output
875        ("mount | grep -w dos | grep -vE \"/dev/fd|nexdisk\"", 0)) {
876        log_to_screen(_("I think you have a Windows 9x partition."));
877        retval += whine_if_not_found("parted");
878#ifndef __IA64__
879        /* IA64 always has one vfat partition for EFI even without Windows */
880        // retval +=
881        if (!find_home_of_exe("ms-sys")) {
882            log_to_screen("Please install ms-sys just in case.");
883        }
884#endif
885    }
886
887    if (!find_home_of_exe("cmp")) {
888        if (!find_home_of_exe("true")) {
889            whine_if_not_found("cmp");
890        } else {
891            log_to_screen
892                ("Your system lacks the 'cmp' binary. I'll create a dummy cmp for you.");
893            if (run_program_and_log_output
894                ("cp -f `which true` /usr/bin/cmp", 0)) {
895                fatal_error("Failed to create dummy 'cmp' file.");
896            }
897        }
898    }
899    run_program_and_log_output
900        ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
901    mr_asprintf(&tmp,
902           call_program_and_get_last_line_of_output
903           ("mount | grep -E \"cdr(om|w)\""));
904    if (strcmp("", tmp)) {
905        if (strstr(tmp, "autofs")) {
906            log_to_screen
907                (_("Your CD-ROM is mounted via autofs. I therefore cannot tell"));
908            log_to_screen
909                (_("if a CD actually is inserted. If a CD is inserted, please"));
910            log_to_screen(_("eject it. Thank you."));
911            log_it
912                ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
913        } else
914            if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
915            retval++;
916            fatal_error
917                ("Your CD-ROM drive is mounted. Please unmount it.");
918        }
919    }
920    mr_free(tmp);
921#ifndef __FreeBSD__
922    if (!does_file_exist("/etc/modules.conf")) {
923        if (does_file_exist("/etc/conf.modules")) {
924            log_it("Linking /etc/modules.conf to /etc/conf.modules");
925            run_program_and_log_output
926                ("ln -sf /etc/conf.modules /etc/modules.conf", 5);
927        } else if (does_file_exist("/etc/modprobe.d")) {
928            log_it
929                ("Directory /etc/modprobe.d found. mindi will use its contents.");
930        } else if (does_file_exist("/etc/modprobe.conf")) {
931            log_it("Linking /etc/modules.conf to /etc/modprobe.conf");
932            run_program_and_log_output
933                ("ln -sf /etc/modprobe.conf /etc/modules.conf", 5);
934        } else {
935            retval++;
936            log_to_screen
937                (_("Please find out what happened to /etc/modules.conf"));
938        }
939    }
940#endif
941
942    run_program_and_log_output("cat /etc/fstab", 5);
943#ifdef __FreeBSD__
944    run_program_and_log_output("vinum printconfig", 5);
945#else
946    run_program_and_log_output("cat /etc/raidtab", 5);
947#endif
948
949    if (run_program_and_log_output("mindi -V", 1)) {
950        log_to_screen(_("Could not ascertain mindi's version number."));
951        log_to_screen
952            (_("You have not installed Mondo and/or Mindi properly."));
953        log_to_screen(_("Please uninstall and reinstall them both."));
954        fatal_error("Please reinstall Mondo and Mindi.");
955    }
956    if (run_program_and_log_output
957        ("mindi --makemountlist /tmp/mountlist.txt.test", 5)) {
958        log_to_screen
959            (_("Mindi --makemountlist /tmp/mountlist.txt.test failed for some reason."));
960        log_to_screen
961            (_("Please run that command by hand and examine /var/log/mindi.log"));
962        log_to_screen
963            (_("for more information. Perhaps your /etc/fstab file is insane."));
964        log_to_screen
965            (_("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see."));
966        retval++;
967    }
968
969    if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
970        && !does_file_exist("/etc/raidtab")) {
971        log_to_screen
972            (_("You have RAID partitions but no /etc/raidtab - creating one from "MDSTAT_FILE));
973        create_raidtab_from_mdstat("/etc/raidtab");
974    }
975
976    if (retval) {
977        mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
978    } else {
979        mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
980    }
981    return (retval);
982}
983
984/**
985 * Retrieve the line containing @p label from the config file.
986 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
987 * @param label What to read from the file.
988 * @param value Where to put it.
989 * @return 0 for success, 1 for failure.
990 */
991int read_cfg_var(char *config_file, char *label, char *value)
992{
993    /*@ buffer ****************************************************** */
994    char *command = NULL;
995    char *tmp = NULL;
996
997    /*@ end vars *************************************************** */
998
999    assert_string_is_neither_NULL_nor_zerolength(config_file);
1000    assert_string_is_neither_NULL_nor_zerolength(label);
1001
1002    if (!does_file_exist(config_file)) {
1003        mr_asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
1004                 config_file);
1005        log_to_screen(tmp);
1006        mr_free(tmp);
1007        value[0] = '\0';
1008        return (1);
1009    /* } BERLIOS: Useless:
1010         else if (strstr(value, "/dev/") && strstr(value, "t0")
1011               && !strcmp(label, "media-dev")) {
1012        mr_msg(2, "FYI, I shan't read new value for %s - already got %s",
1013                label, value);
1014        return (0);
1015    */ } else {
1016        mr_asprintf(&command, "grep '%s .*' %s| cut -d'=' -f2,3,4,5",
1017                label, config_file);
1018        strcpy(value, call_program_and_get_last_line_of_output(command));
1019        mr_free(command);
1020        if (strlen(value) == 0) {
1021            return (1);
1022        } else {
1023            return (0);
1024        }
1025    }
1026}
1027
1028
1029/**
1030 * Remount @c supermount if it was unmounted earlier.
1031 */
1032void remount_supermounts_if_necessary()
1033{
1034    if (g_remount_cdrom_at_end) {
1035        run_program_and_log_output("mount " MNT_CDROM, FALSE);
1036    }
1037    if (g_remount_floppy_at_end) {
1038        run_program_and_log_output("mount " MNT_FLOPPY, FALSE);
1039    }
1040}
1041
1042/**
1043 * Unmount @c supermount if it's mounted.
1044 */
1045void unmount_supermounts_if_necessary()
1046{
1047    if (run_program_and_log_output
1048        ("mount | grep cdrom | grep super", FALSE) == 0) {
1049        g_remount_cdrom_at_end = TRUE;
1050        run_program_and_log_output("umount " MNT_CDROM, FALSE);
1051    }
1052    if (run_program_and_log_output
1053        ("mount | grep floppy | grep super", FALSE) == 0) {
1054        g_remount_floppy_at_end = TRUE;
1055        run_program_and_log_output("umount " MNT_FLOPPY, FALSE);
1056    }
1057}
1058
1059/**
1060 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1061 */
1062void mount_boot_if_necessary()
1063{
1064    char *tmp = NULL;
1065    char *tmp1 = NULL;
1066    char *command = NULL;
1067
1068    mr_msg(1, "Started sub");
1069    mr_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
1070    g_boot_mountpt[0] = '\0';
1071    mr_msg(4, "Done. Great. Setting command to something");
1072    mr_asprintf(&command,
1073             "grep -v ':' /etc/fstab | grep -vE '^#.*$' | grep -E \"[   ]/boot[     ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
1074    mr_msg(4, "Cool. Command = '%s'", command);
1075    mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
1076    mr_free(command);
1077
1078    mr_msg(4, "tmp = '%s'", tmp);
1079    if (tmp[0]) {
1080        log_it("/boot is at %s according to /etc/fstab", tmp);
1081        if ((strstr(tmp, "LABEL=") || strstr(tmp,"UUID="))) {
1082            if (!run_program_and_log_output("mount /boot", 5)) {
1083                strcpy(g_boot_mountpt, "/boot");
1084                mr_msg(1, "Mounted /boot");
1085            } else {
1086                log_it("...ignored cos it's a label or uuid :-)");
1087            }
1088        } else {
1089            mr_asprintf(&command, "mount | grep -E '^%s'", tmp);
1090            mr_msg(3, "command = %s", command);
1091            if (run_program_and_log_output(command, 5)) {
1092                strcpy(g_boot_mountpt, tmp);
1093                mr_asprintf(&tmp1,
1094                        "%s (your /boot partition) is not mounted. I'll mount it before backing up",
1095                        g_boot_mountpt);
1096                log_it(tmp1);
1097                mr_free(tmp1);
1098
1099                mr_asprintf(&tmp1, "mount %s", g_boot_mountpt);
1100                if (run_program_and_log_output(tmp1, 5)) {
1101                    g_boot_mountpt[0] = '\0';
1102                    mr_msg(1, "Plan B");
1103                    if (!run_program_and_log_output("mount /boot", 5)) {
1104                        strcpy(g_boot_mountpt, "/boot");
1105                        mr_msg(1, "Plan B worked");
1106                    } else {
1107                        mr_msg(1,
1108                                "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1109                    }
1110                }
1111                mr_free(tmp1);
1112            }
1113            mr_free(command);
1114        }
1115    }
1116    mr_free(tmp);
1117    mr_msg(1, "Ended sub");
1118}
1119
1120
1121/**
1122 * If we mounted /boot earlier, unmount it.
1123 */
1124void unmount_boot_if_necessary()
1125{
1126    char *tmp;
1127
1128    mr_msg(3, "starting");
1129    if (g_boot_mountpt[0]) {
1130        mr_asprintf(&tmp, "umount %s", g_boot_mountpt);
1131        if (run_program_and_log_output(tmp, 5)) {
1132            log_it("WARNING - unable to unmount /boot");
1133        }
1134        mr_free(tmp);
1135    }
1136    mr_msg(3, "leaving");
1137}
1138
1139
1140/**
1141 * Write a line to a configuration file. Writes a line of the form,
1142 * @c label @c value.
1143 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1144 * @param label What to call this bit of data you're writing.
1145 * @param value The bit of data you're writing.
1146 * @return 0 for success, 1 for failure.
1147 */
1148int write_cfg_var(char *config_file, char *label, char *value)
1149{
1150    /*@ buffers ***************************************************** */
1151    char *command = NULL;
1152    char *tempfile = NULL;
1153    char *tmp = NULL;
1154
1155
1156    /*@ end vars *************************************************** */
1157    assert_string_is_neither_NULL_nor_zerolength(config_file);
1158    assert_string_is_neither_NULL_nor_zerolength(label);
1159    assert(value != NULL);
1160
1161    if (!does_file_exist(config_file)) {
1162        mr_asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
1163                 config_file);
1164        log_to_screen(tmp);
1165        mr_free(tmp);
1166        return (1);
1167    }
1168    mr_asprintf(&tempfile,
1169           call_program_and_get_last_line_of_output
1170           ("mktemp -q /tmp/mojo-jojo.blah.XXXXXX"));
1171    if (does_file_exist(config_file)) {
1172        mr_asprintf(&command, "grep -vE '^%s .*$' %s > %s",
1173                label, config_file, tempfile);
1174        paranoid_system(command);
1175        mr_free(command);
1176    }
1177    mr_asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
1178    paranoid_system(command);
1179    mr_free(command);
1180
1181    mr_asprintf(&command, "mv -f %s %s", tempfile, config_file);
1182    paranoid_system(command);
1183    mr_free(command);
1184    unlink(tempfile);
1185    mr_free(tempfile);
1186    return (0);
1187}
1188
1189
1190/**
1191 * Allocate or free important globals, depending on @p mal.
1192 * @param mal If TRUE, malloc; if FALSE, free.
1193 */
1194void do_libmondo_global_strings_thing(int mal)
1195{
1196    if (mal) {
1197        iamhere("Malloc'ing globals");
1198        malloc_string(g_boot_mountpt);
1199        malloc_string(g_tmpfs_mountpt);
1200        malloc_string(g_erase_tmpdir_and_scratchdir);
1201        malloc_string(g_serial_string);
1202        malloc_string(g_magicdev_command);
1203    } else {
1204        iamhere("Freeing globals");
1205        mr_free(g_boot_mountpt);
1206        mr_free(g_tmpfs_mountpt);
1207        mr_free(g_erase_tmpdir_and_scratchdir);
1208        mr_free(g_serial_string);
1209        mr_free(g_magicdev_command);
1210    }
1211}
1212
1213/**
1214 * Allocate important globals.
1215 * @see do_libmondo_global_strings_thing
1216 */
1217void malloc_libmondo_global_strings(void)
1218{
1219    do_libmondo_global_strings_thing(1);
1220}
1221
1222/**
1223 * Free important globals.
1224 * @see do_libmondo_global_strings_thing
1225 */
1226void free_libmondo_global_strings(void)
1227{
1228    do_libmondo_global_strings_thing(0);
1229}
1230
1231
1232
1233/**
1234 * Stop @c magicdev if it's running.
1235 * The command used to start it is saved in @p g_magicdev_command.
1236 */
1237void stop_magicdev_if_necessary()
1238{
1239    strcpy(g_magicdev_command,
1240           call_program_and_get_last_line_of_output
1241           ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
1242    if (g_magicdev_command[0]) {
1243        mr_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1244        paranoid_system("killall magicdev");
1245    }
1246}
1247
1248
1249/**
1250 * Restart magicdev if it was stopped.
1251 */
1252void restart_magicdev_if_necessary()
1253{
1254    char *tmp = NULL;
1255
1256    if (g_magicdev_command && g_magicdev_command[0]) {
1257        mr_asprintf(&tmp, "%s &", g_magicdev_command);
1258        paranoid_system(tmp);
1259        mr_free(tmp);
1260    }
1261}
1262
1263/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.