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

Last change on this file since 538 was 538, checked in by bcornec, 14 years ago

Begining of modifications => DOS NOT WORK FOR NOW.
This is done to be merged in trunk. stable will later point back to current 2.0.8 content

Use of mindi conf file in mindi + /var/cache/mindi as a default place
Use of mondo conf file in mondo + separation of messages.
Ultimate goal is to have a cleaner design of files vs functions.

  • Property svn:keywords set to Id
File size: 41.5 KB
Line 
1/* libmondo-tools.c                                  misc tools
2   $Id: libmondo-tools.c 538 2006-05-13 16:39:55Z bcornec $
3.
4
5
606/28
7- abort if RAID partitions found but /etc/raidtab not found
8
906/25
10- don't insist on ms-sys; recommend it
11- don't fatal_error if can't open log file
12
1306/14
14- call mindi -V before calling mindi --makemountlist
15
1604/09
17- lots of malloc'd char*'s instead of char[]'s in post_param_config'n
18
1904/02
20- better sanity-checking, re: /etc/modules.conf
21
2202/15
23- abort if Windows dir but no ms-sys or parted
24
2502/08
26- missing ramdisk/initrd support is nonfatal now (but stupid)
27
2801/16
29- added g_kernel_version and get_kernel_version()
30
3101/08/2004
32- fixed /boot mounter thingy
33
3411/14/2003
35- if <50MB free in /root, softlink /root/images/mindi to
36  /home/root/images/mindi as a workaround
37
3810/23
39- streaming backups now generate 4MB sets; CDs, 8MB sets
40
4110/21
42- better at finding your /boot in order to mount it, if nec.
43- link /etc/modules.conf to /etc/conf.modules if former
44  is missing but latter is not
45- changed "/mnt/cdrom" to MNT_CDROM
46- changed "/mnt/floppy" to MNT_FLOPPY
47
4810/13
49- added stop_magicdev_if_necessary()
50- added restart_magicdev_if_necessary()
51
5210/02
53- don't eject if bkpinfo->please_dont_eject is TRUE
54
5509/28
56- fixed segfault in mount_..._necessary
57
5809/26
59- ask for cmp (or create workaround if possible)
60
6109/25
62- add DVD support
63
6409/24
65- don't overwrite existing media-dev value in bkpinfo IF it's
66  a tape device AND it's already populated by /dev/{something}
67  ...This helps w/ weird tape drives
68
6909/23
70- malloc/free global strings in new subroutines - malloc_libmondo_global_strings()
71  and free_libmondo_global_strings() - which are in libmondo-tools.c
72
7309/18
74- added stop_autofs_if_necessary() and restart_autofs_if_necessary()
75
7609/13
77- added log_debug_msg()
78- turned log_it() into a macro
79
8009/10
81- reduced PPCFG_RAMDISK to 150m (was 200m)
82- max set size is now 8MB (was 6)
83- added 'buffer' to requisite tools
84
8508/29
86- typical set size is now 5MB
87   
8807/22
89- increased PPCFG* to 200m
90
9105/07
92- changed grep -m1 to grep | head -n1 for Debian users
93- pre_param_...() also mkdir -p /mnt/cdrom, just in case
94
9505/05
96- cleaner logging of call to dd if=<tape> of=/dev/null
97- added Joshua Oreman's FreeBSD patches
98
9905/04
100- added Herman Kuster's multi-level bkp patch
101
10205/02
103- log_it() --- "Message repeated N times" (if it was)
104
10504/24
106- added lots of assert()'s and log_OS_error()'s
107- reset_bkpinfop() --- reset all media_size[] array
108- increased tmpfs ramdisk to size=100m
109
11004/22
111- post_param_configuration() --- store iso-dev and isodir
112
11304/05
114- re-enable tmpfs ramdisk/mountpt _but_ make sure size=80m
115- in post_param_configuration(), use cdrecord in preference
116  to dvdrecord now
117
11803/27
119- disabled tmpfs ramdisk/mountpt
120
12101/02/2003
122- bkpinfo->makefilelist = TRUE
123
12412/10/2002
125- added g_loglevel
126- fixed minor bug in clean_up_KDE_desktop_if_necessary()
127- log versions of newt, slang, ncurses
128- isodir defaults to / if disaster recovery
129
13011/28
131- unmount all CD devices at start
132- added clean_up_KDE_desktop_if_necessary()
133
13411/20
135- don't use lilo.conf.anaconda; abort instead
136- mount/unmount /boot partition for Gentoo 1.2 users
137
13811/13
139- call mindi --makemountlist as part of testing sanity of distro
140
14111/08
142- added a couple of chmod 700's (one for scratchdir, one for tmpdir)
143  to post_param_configuration()
144
14510/01 - 10/31
146- added checking of / and /root's free space to some_basic_sanity_checks()
147- moved some subroutines here from common/libmondo-archive
148
14909/01 - 09/30
150- don't write log_it()'s string to stdout, even if text mode
151- tell user the kernel is bad if it has no ramdisk support but allow
152  it if they want to use the failsafe kernel for booting
153- run_program_and_log_output() now takes boolean operator to specify
154  whether it will log its activities in the event of _success_
155- if mkfs.vfat not found but mkfs.msdos found then create a softlink
156  from the former to the latter
157
15808/01 - 08/31
159- if /etc/lilo.conf not found not /etc/lilo.conf.anaconda found
160  then create a softlink from the former to the latter, to work
161  around RH7.3's b0rken LILO support
162- handle unknown media size
163
16407/27
165- created
166*/
167
168
169/**
170 * @file
171 * Miscellaneous tools that didn't really fit anywhere else.
172 */
173
174#include "my-stuff.h"
175#include "mondostructures.h"
176#include "libmondo-tools.h"
177#include "newt-specific-EXT.h"
178#include "libmondo-files-EXT.h"
179#include "libmondo-fork-EXT.h"
180#include "libmondo-raid-EXT.h"
181#include <sys/socket.h>
182#include <netdb.h>
183#include <netinet/in.h>
184#include <arpa/inet.h>
185
186/*@unused@*/
187//static char cvsid[] = "$Id: libmondo-tools.c 538 2006-05-13 16:39:55Z bcornec $";
188
189extern int g_tape_buffer_size_MB;
190extern char *g_erase_tmpdir_and_scratchdir;
191extern char *g_serial_string;
192extern bool g_text_mode;
193extern int g_currentY;
194extern int g_current_media_number;
195
196/**
197 * @addtogroup globalGroup
198 * @{
199 */
200bool g_remount_cdrom_at_end,    ///< TRUE if we unmounted the CD-ROM and should remount it when done with the backup.
201 g_remount_floppy_at_end;       ///< TRUE if we unmounted the floppy and should remount it when done with the backup.
202bool g_cd_recovery;             ///< TRUE if we're making an "autonuke" backup.
203double g_kernel_version;
204
205/**
206 * The place where /boot is mounted.
207 */
208char *g_boot_mountpt = NULL;
209
210/**
211 * The location of Mondo's home directory.
212 */
213char *g_mondo_home = NULL;
214
215/**
216 * The serial string (used to differentiate between backups) of the current backup.
217 */
218char *g_serial_string = NULL;
219
220/**
221 * The location where tmpfs is mounted, or "" if it's not mounted.
222 */
223char *g_tmpfs_mountpt = NULL;
224char *g_magicdev_command = NULL;
225
226/**
227 * The default maximum level to log messages at or below.
228 */
229int g_loglevel = DEFAULT_DEBUG_LEVEL;
230
231/* @} - end of globalGroup */
232
233
234extern pid_t g_buffer_pid;
235extern pid_t g_main_pid;
236
237extern t_bkptype g_backup_media_type;
238
239extern bool am_I_in_disaster_recovery_mode(void);
240
241
242/**
243 * @addtogroup utilityGroup
244 * @{
245 */
246/**
247 * Assertion handler. Prints a friendly message to the user,
248 * offering to ignore all, dump core, break to debugger,
249 * exit, or ignore. Intended to be used with an assert() macro.
250 *
251 * @param file The file in which the assertion triggered.
252 * @param function The function (@c __FUNCTION__) in which the assertion triggered.
253 * @param line The line number of the assert() statement.
254 * @param exp The expression that failed (as a string).
255 */
256void _mondo_assert_fail(const char *file,
257                        const char *function, int line, const char *exp)
258{
259    static int ignoring_assertions = 0;
260    bool is_valid = TRUE;
261
262    log_it("ASSERTION FAILED: `%s' at %s:%d in %s", exp, file, line,
263           function);
264    if (ignoring_assertions) {
265        log_it("Well, the user doesn't care...");
266        return;
267    }
268#ifndef _XWIN
269    if (!g_text_mode)
270        newtSuspend();
271#endif
272    printf(_("ASSERTION FAILED: `%s'\n"), exp);
273    printf(_("\tat %s:%d in %s\n\n"), file, line, function);
274    printf(_("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? "));
275    do {
276        is_valid = TRUE;
277        switch (toupper(getchar())) {
278        case 'A':               // ignore (A)ll
279            ignoring_assertions = 1;
280            break;
281        case 'B':               // a(B)ort
282            signal(SIGABRT, SIG_DFL);   /* prevent SIGABRT handler from running */
283            raise(SIGABRT);
284            break;              /* "can't get here" */
285        case 'D':               // (D)ebug, aka asm("int 3")
286#ifdef __IA32__
287            __asm__ __volatile__("int $3"); // break to debugger
288#endif
289            break;
290        case 'E':               // (E)xit
291            fatal_error("Failed assertion -- see above for details");
292            break;              /* "can't get here" */
293        case 'I':               // (I)gnore
294            break;
295            /* These next two work as follows:
296               the `default' catches the user's invalid choice and says so;
297               the '\n' catches the newline on the end and prints the prompt again.
298             */
299        case '\n':
300            printf
301                (_("(I)gnore, ignore (A)ll, (D)ebug, a(B)ort, or (E)xit? "));
302            break;
303        default:
304            is_valid = FALSE;
305            printf(_("Invalid choice.\n"));
306            break;
307        }
308    } while (!is_valid);
309
310    if (ignoring_assertions) {
311        log_it("Ignoring ALL assertions from now on.");
312    } else {
313        log_it("Ignoring assertion: %s", exp);
314    }
315
316    getchar();                  // skip \n
317
318#ifndef _XWIN
319    if (!g_text_mode)
320        newtResume();
321#endif
322}
323
324/**
325 * Clean's up users' KDE desktops.
326 * @bug Details about this function are unknown.
327 */
328void clean_up_KDE_desktop_if_necessary(void)
329{
330    char *tmp;
331
332    malloc_string(tmp);
333    strcpy(tmp,
334           "for i in `find /root /home -type d -name Desktop -maxdepth 2`; do \
335file=$i/.directory; if [ -f \"$file\" ] ; then mv -f $file $file.old ; \
336awk '{if (index($0, \"rootimagesmindi\")) { while (length($0)>2) { getline;} ; } \
337else { print $0;};}' $file.old  > $file ; fi ; done");
338    run_program_and_log_output(tmp, 5);
339    paranoid_free(tmp);
340}
341
342
343/**
344 * Locate mondoarchive's home directory. Searches in /usr/local/mondo, /usr/share/mondo,
345 * /usr/local/share/mondo, /opt, or if all else fails, search /usr.
346 *
347 * @param home_sz String to store the home directory ("" if it could not be found).
348 * @return 0 for success, nonzero for failure.
349 */
350int find_and_store_mondoarchives_home(char *home_sz)
351{
352    assert(home_sz != NULL);
353    strcpy(home_sz, MONDO_SHARE);
354    return (0);
355}
356
357
358char *get_architecture()
359{
360#ifdef __IA32__
361    return ("i386");
362#endif
363#ifdef __IA64__
364    return ("ia64");
365#endif
366    return ("unknown");
367}
368
369
370
371double get_kernel_version()
372{
373    char *p, tmp[200];
374    double d;
375#ifdef __FreeBSD__
376    // JOSH - FIXME :)
377    d = 5.2;                    // :-)
378#else
379    strcpy(tmp, call_program_and_get_last_line_of_output("uname -r"));
380    p = strchr(tmp, '.');
381    if (p) {
382        p = strchr(++p, '.');
383        if (p) {
384            while (*p) {
385                *p = *(p + 1);
386                p++;
387            }
388        }
389    }
390//  log_msg(1, "tmp = '%s'", tmp);
391    d = atof(tmp);
392#endif
393    log_msg(1, "g_kernel_version = %f", d);
394    return (d);
395}
396
397
398
399
400
401/**
402 * Get the current time.
403 * @return number of seconds since the epoch.
404 */
405long get_time()
406{
407    return (long) time((void *) 0);
408}
409
410
411
412
413
414
415
416/**
417 * Initialize a RAID volume structure, setting fields to zero. The
418 * actual hard drive is unaffected.
419 *
420 * @param raidrec The RAID volume structure to initialize.
421 * @note This function is system dependent.
422 */
423#ifdef __FreeBSD__
424void initialize_raidrec(struct vinum_volume *raidrec)
425{
426    int i, j;
427    raidrec->volname[0] = '\0';
428    raidrec->plexes = 0;
429    for (i = 0; i < 9; ++i) {
430        raidrec->plex[i].raidlevel = -1;
431        raidrec->plex[i].stripesize = 0;
432        raidrec->plex[i].subdisks = 0;
433        for (j = 0; j < 9; ++j) {
434            strcpy(raidrec->plex[i].sd[j].which_device, "");
435        }
436    }
437}
438#else
439void initialize_raidrec(struct raid_device_record *raidrec)
440{
441    assert(raidrec != NULL);
442    raidrec->raid_device[0] = '\0';
443    raidrec->raid_level = 0;
444    raidrec->chunk_size = 4;
445    raidrec->persistent_superblock = 1;
446    raidrec->data_disks.entries = 0;
447    raidrec->spare_disks.entries = 0;
448    raidrec->parity_disks.entries = 0;
449    raidrec->failed_disks.entries = 0;
450    raidrec->additional_vars.entries = 0;
451}
452#endif
453
454
455
456
457/**
458 * Insert modules that Mondo requires.
459 * Currently inserts @c dos, @c fat, @c vfat, and @c osst for Linux;
460 * @c msdosfs and @c ext2fs for FreeBSD.
461 */
462void insmod_crucial_modules(void)
463{
464#ifdef __FreeBSD__
465    system("kldstat | grep msdosfs || kldload msdosfs 2> /dev/null");
466    system("kldstat | grep ext2fs  || kldload ext2fs 2> /dev/null");
467#else
468    system("modprobe dos &> /dev/null");
469    system("modprobe fat &> /dev/null");
470    system("modprobe vfat &> /dev/null");
471    //  system("modprobe osst &> /dev/null");
472#endif
473}
474
475
476/**
477 * Finish configuring the backup information structure. Call this function
478 * to set the parameters that depend on those that can be given on the command
479 * line.
480 *
481 * @param bkpinfo The backup information structure. Fields modified/used:
482 * - Used: @c bkpinfo->backup_data
483 * - Used: @c bkpinfo->backup_media_type
484 * - Used: @c bkpinfo->cdrw_speed
485 * - Used: @c bkpinfo->compression_level
486 * - Used: @c bkpinfo->include_paths
487 * - Used: @c bkpinfo->prefix
488 * - Used: @c bkpinfo->isodir
489 * - Used: @c bkpinfo->manual_cd_tray
490 * - Used: @c bkpinfo->make_cd_use_lilo
491 * - Used: @c bkpinfo->media_device
492 * - Used: @c bkpinfo->nfs_mount
493 * - Used: @c bkpinfo->nonbootable_backup
494 * - Used: @c bkpinfo->scratchdir
495 * - Used: @c bkpinfo->tmpdir
496 * - Used: @c bkpinfo->use_lzo
497 * - Modified: @c bkpinfo->call_before_iso
498 * - Modified: @c bkpinfo->call_make_iso
499 * - Modified: @c bkpinfo->optimal_set_size
500 * - Modified: @c bkpinfo->zip_exe
501 * - Modified: @c bkpinfo->zip_suffix
502 *
503 * @return number of errors, or 0 for success.
504 * @note Also creates directories that are specified in the @c bkpinfo structure but
505 * do not exist.
506 */
507int post_param_configuration(struct s_bkpinfo *bkpinfo)
508{
509    char *extra_cdrom_params;
510    char *mondo_mkisofs_sz;
511    char *command;
512    char *mtpt;
513    char *hostname, *ip_address;
514    int retval = 0;
515    long avm = 0;
516    char *colon;
517    char *cdr_exe;
518    char *tmp;
519    char call_before_iso_user[MAX_STR_LEN] = "\0";
520    int rdsiz_MB;
521    char *iso_dev;
522    char *iso_mnt;
523    char *iso_tmp;
524    char *iso_path;
525
526    assert(bkpinfo != NULL);
527    malloc_string(extra_cdrom_params);
528    malloc_string(mondo_mkisofs_sz);
529    malloc_string(command);
530    malloc_string(mtpt);
531    malloc_string(hostname);
532    malloc_string(ip_address);
533    malloc_string(cdr_exe);
534    malloc_string(tmp);
535    malloc_string(iso_dev);
536    malloc_string(iso_mnt);
537    malloc_string(iso_tmp);
538    malloc_string(iso_path);
539    bkpinfo->optimal_set_size =
540        (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 4 : 8) *
541        1024;
542
543    log_msg(1, "Foo");
544    if (bkpinfo->backup_media_type == tape) {
545        log_msg(1, "Bar");
546        sprintf(tmp, "mt -f %s status", bkpinfo->media_device);
547        log_msg(1, "tmp = '%s'", tmp);
548        if (run_program_and_log_output(tmp, 3)) {
549            fatal_error
550                ("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.");
551        }
552    }
553    make_hole_for_dir(bkpinfo->scratchdir);
554    make_hole_for_dir(bkpinfo->tmpdir);
555    if (bkpinfo->backup_media_type == iso)
556        make_hole_for_dir(bkpinfo->isodir);
557
558    run_program_and_log_output("uname -a", 5);
559    run_program_and_log_output("cat /etc/*issue*", 5);
560    sprintf(g_tmpfs_mountpt, "%s/tmpfs", bkpinfo->tmpdir);
561    sprintf(command, "mkdir -p %s", g_tmpfs_mountpt);
562    paranoid_system(command);
563    rdsiz_MB = PPCFG_RAMDISK_SIZE + g_tape_buffer_size_MB;
564#ifdef __FreeBSD__
565    strcpy(tmp,
566           call_program_and_get_last_line_of_output
567           ("vmstat | tail -1 | tr -s ' ' | cut -d' ' -f6"));
568    avm += atol(tmp);
569    strcpy(tmp,
570           call_program_and_get_last_line_of_output
571           ("swapinfo | grep -v Device | tr -s ' ' | cut -d' ' -f4 | tr '\n' '+' | sed 's/+$//' | bc"));
572    avm += atol(tmp);
573    sprintf(command, "mdmfs -s %d%c md9 %s", rdsiz_MB, 'm',
574            g_tmpfs_mountpt);
575#else
576    strcpy(tmp,
577           call_program_and_get_last_line_of_output
578           ("free | grep \":\" | tr -s ' ' '\t' | cut -f2 | head -n1"));
579    avm += atol(tmp);
580    sprintf(command, "mount /dev/shm -t tmpfs %s -o size=%d%c",
581            g_tmpfs_mountpt, rdsiz_MB, 'm');
582    run_program_and_log_output("cat /proc/cpuinfo", 5);
583    run_program_and_log_output
584        ("rpm -q newt newt-devel slang slang-devel ncurses ncurses-devel gcc",
585         5);
586#endif
587    if (avm / 1024 > rdsiz_MB * 3) {
588        if (run_program_and_log_output(command, 5)) {
589            g_tmpfs_mountpt[0] = '\0';
590            log_it("Failed to mount tmpfs");
591        } else {
592            log_it("Tmpfs mounted OK - %d MB", rdsiz_MB);
593        }
594    } else {
595        g_tmpfs_mountpt[0] = '\0';
596        log_it("It doesn't seem you have enough swap to use tmpfs. Fine.");
597    }
598
599    if (bkpinfo->use_lzo) {
600        strcpy(bkpinfo->zip_exe, "lzop");
601        strcpy(bkpinfo->zip_suffix, "lzo");
602    } else if (bkpinfo->compression_level != 0) {
603        strcpy(bkpinfo->zip_exe, "bzip2");
604        strcpy(bkpinfo->zip_suffix, "bz2");
605    } else {
606        bkpinfo->zip_exe[0] = bkpinfo->zip_suffix[0] = '\0';
607    }
608
609// DVD
610
611    if (bkpinfo->backup_media_type == dvd) {
612        extra_cdrom_params[0] = '\0';
613        mondo_mkisofs_sz[0] = '\0';
614        if (find_home_of_exe("growisofs")) {
615            strcpy(cdr_exe, "growisofs");
616        }                       // unlikely to be used
617        else {
618            fatal_error("Please install growisofs.");
619        }
620        if (bkpinfo->nonbootable_backup) {
621            strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_NONBOOT);
622        } else if
623#ifdef __FreeBSD__
624            (TRUE)
625#else
626            (bkpinfo->make_cd_use_lilo)
627#endif
628#ifdef __IA64__
629    {
630        strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_ELILO);
631    }
632#else
633    {
634        strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_LILO);
635    }
636#endif
637        else
638        {
639            strcat(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_SYSLINUX);
640        }
641        if (bkpinfo->manual_cd_tray) {
642            fatal_error("Manual CD tray + DVD not supported yet.");
643            // -m isn't supported by growisofs, BTW...
644        } else {
645            sprintf(bkpinfo->call_make_iso,
646                    "%s %s -Z %s . 2>> _ERR_",
647                    mondo_mkisofs_sz,
648                    extra_cdrom_params, bkpinfo->media_device);
649        }
650        if (getenv ("SUDO_COMMAND")) {
651            fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
652        }
653        log_msg(2, "call_make_iso (DVD res) is ... %s",
654                bkpinfo->call_make_iso);
655    }                           // end of DVD code
656
657// CD-R or CD-RW
658    if (bkpinfo->backup_media_type == cdrw
659        || bkpinfo->backup_media_type == cdr) {
660        extra_cdrom_params[0] = '\0';
661        if (!bkpinfo->manual_cd_tray) {
662            strcat(extra_cdrom_params, "-waiti ");
663        }
664        if (bkpinfo->backup_media_type == cdrw) {
665            strcat(extra_cdrom_params, "blank=fast ");
666        }
667        if (find_home_of_exe("cdrecord")) {
668            strcpy(cdr_exe, "cdrecord");
669        } else if (find_home_of_exe("dvdrecord")) {
670            strcpy(cdr_exe, "dvdrecord");
671        } else {
672            fatal_error("Please install either cdrecord or dvdrecord.");
673        }
674        if (bkpinfo->nonbootable_backup) {
675            strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_NONBOOT);
676        } else if
677#ifdef __FreeBSD__
678            (TRUE)
679#else
680            (bkpinfo->make_cd_use_lilo)
681#endif
682#ifdef __IA64__
683    {
684        strcat(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_ELILO);
685    }
686#else
687    {
688        strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_LILO);
689    }
690#endif
691        else
692        {
693            strcpy(mondo_mkisofs_sz, MONDO_MKISOFS_REGULAR_SYSLINUX);
694        }
695        if (bkpinfo->manual_cd_tray) {
696            if (bkpinfo->call_before_iso[0] == '\0') {
697            sprintf(bkpinfo->call_before_iso,
698                        "%s -o %s/temporary.iso . 2>> _ERR_",
699                        mondo_mkisofs_sz, bkpinfo->tmpdir);
700                } else {
701                strncpy(call_before_iso_user, bkpinfo->call_before_iso, MAX_STR_LEN);
702                sprintf (bkpinfo->call_before_iso,
703                                    "( %s -o %s/temporary.iso . 2>> _ERR_ ; %s )",
704                        mondo_mkisofs_sz, bkpinfo->tmpdir, call_before_iso_user);
705            }
706            log_it("bkpinfo->call_before_iso = %s", bkpinfo->call_before_iso);
707            sprintf(bkpinfo->call_make_iso,
708                    "%s %s -v %s fs=4m dev=%s speed=%d %s/temporary.iso",
709                    cdr_exe, (bkpinfo->please_dont_eject) ? " " : "-eject",
710                    extra_cdrom_params, bkpinfo->media_device,
711                    bkpinfo->cdrw_speed, bkpinfo->tmpdir);
712        } else {
713            sprintf(bkpinfo->call_make_iso,
714                    "%s . 2>> _ERR_ | %s %s %s fs=4m dev=%s speed=%d -",
715                    mondo_mkisofs_sz, cdr_exe,
716                    (bkpinfo->please_dont_eject) ? " " : "-eject",
717                    extra_cdrom_params, bkpinfo->media_device,
718                    bkpinfo->cdrw_speed);
719        }
720    }                           // end of CD code
721
722    /*
723       if (bkpinfo->backup_data && bkpinfo->backup_media_type == tape)
724       {
725       sprintf (tmp,
726       "dd if=/dev/zero of=%s bs=%ld count=32 2> /dev/null",
727       bkpinfo->media_device, bkpinfo->internal_tape_block_size);
728       if (system(tmp))
729       {
730       retval++;
731       fprintf (stderr,
732       "Cannot write to tape device. Is the tape set read-only?\n");
733       }
734       } // end of tape code
735     */
736
737
738    if (bkpinfo->backup_media_type == iso) {
739
740/* Patch by Conor Daly <conor.daly@met.ie>
741 * 23-june-2004
742 * Break up isodir into iso_mnt and iso_path
743 * These will be used along with iso-dev at restore time
744 * to locate the ISOs where ever they're mounted
745 */
746
747        log_it("isodir = %s", bkpinfo->isodir);
748        sprintf(command, "df -P %s | tail -n1 | cut -d' ' -f1",
749                bkpinfo->isodir);
750        log_it("command = %s", command);
751        log_it("res of it = %s",
752               call_program_and_get_last_line_of_output(command));
753        sprintf(iso_dev, "%s",
754                call_program_and_get_last_line_of_output(command));
755        sprintf(tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
756        write_one_liner_data_file(tmp,
757                                  call_program_and_get_last_line_of_output
758                                  (command));
759
760        sprintf(command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3",
761                iso_dev);
762        log_it("command = %s", command);
763        log_it("res of it = %s",
764               call_program_and_get_last_line_of_output(command));
765        sprintf(iso_mnt, "%s",
766                call_program_and_get_last_line_of_output(command));
767        sprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
768        write_one_liner_data_file(tmp,
769                                  call_program_and_get_last_line_of_output
770                                  (command));
771        log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
772        sprintf(iso_tmp, "%s", bkpinfo->isodir);
773        if (strlen(iso_tmp) < strlen(iso_mnt)) {
774            iso_path[0] = '\0';
775        } else {
776            sprintf(iso_path, "%s", iso_tmp + strlen(iso_mnt));
777        }
778        sprintf(tmp, "%s/ISODIR", bkpinfo->tmpdir);
779        write_one_liner_data_file(tmp, iso_path);
780        log_it("isodir: %s", iso_path);
781        sprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
782        write_one_liner_data_file(tmp, bkpinfo->prefix);
783        log_it("iso-prefix: %s",  bkpinfo->prefix);
784
785/* End patch */
786    }                           // end of iso code
787
788    if (bkpinfo->backup_media_type == nfs) {
789        strcpy(hostname, bkpinfo->nfs_mount);
790        colon = strchr(hostname, ':');
791        if (!colon) {
792            log_it("nfs mount doesn't have a colon in it");
793            retval++;
794        } else {
795            struct hostent *hent;
796
797            *colon = '\0';
798            hent = gethostbyname(hostname);
799            if (!hent) {
800                log_it("Can't resolve NFS mount (%s): %s", hostname,
801                       hstrerror(h_errno));
802                retval++;
803            } else {
804                strcpy(ip_address, inet_ntoa
805                       ((struct in_addr)
806                        *((struct in_addr *) hent->h_addr)));
807                strcat(ip_address, strchr(bkpinfo->nfs_mount, ':'));
808                strcpy(bkpinfo->nfs_mount, ip_address);
809            }
810        }
811        store_nfs_config(bkpinfo);
812    }
813
814    log_it("Finished processing incoming params");
815    if (retval) {
816        fprintf(stderr, "Type 'man mondoarchive' for help.\n");
817    }
818    sprintf(tmp, "%s", MONDO_TMPISOS);  /* added #define 22 apr 2002 */
819    if (does_file_exist(tmp)) {
820        unlink(tmp);
821    }
822    if (strlen(bkpinfo->tmpdir) < 2 || strlen(bkpinfo->scratchdir) < 2) {
823        log_it("tmpdir or scratchdir are blank/missing");
824        retval++;
825    }
826    if (bkpinfo->include_paths[0] == '\0') {
827        //      fatal_error ("Why no backup path?");
828        strcpy(bkpinfo->include_paths, "/");
829    }
830    chmod(bkpinfo->scratchdir, 0700);
831    chmod(bkpinfo->tmpdir, 0700);
832    g_backup_media_type = bkpinfo->backup_media_type;
833    paranoid_free(mtpt);
834    paranoid_free(extra_cdrom_params);
835    paranoid_free(mondo_mkisofs_sz);
836    paranoid_free(command);
837    paranoid_free(hostname);
838    paranoid_free(ip_address);
839    paranoid_free(cdr_exe);
840    paranoid_free(tmp);
841    paranoid_free(iso_dev);
842    paranoid_free(iso_mnt);
843    paranoid_free(iso_tmp);
844    paranoid_free(iso_path);
845    return (retval);
846}
847
848
849
850/**
851 * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
852 * Seeds the random-number generator, loads important modules, checks the sanity
853 * of the user's Linux distribution, and deletes logfile.
854 * @param bkpinfo The backup information structure. Will be initialized.
855 * @return number of errors (0 for success)
856 */
857int pre_param_configuration(struct s_bkpinfo *bkpinfo)
858{
859    int res = 0;
860
861    make_hole_for_dir(MNT_CDROM);
862    assert(bkpinfo != NULL);
863    srandom((unsigned long) (time(NULL)));
864    insmod_crucial_modules();
865    reset_bkpinfo(bkpinfo);     // also sets defaults ('/'=backup path, 3=compression level)
866    if (bkpinfo->disaster_recovery) {
867        if (!does_nonMS_partition_exist()) {
868            fatal_error
869                ("I am in disaster recovery mode\nPlease don't run mondoarchive.");
870        }
871    }
872
873    unlink(MONDO_TRACEFILE);
874    run_program_and_log_output("rm -Rf /tmp/changed.files*", FALSE);
875    if (find_and_store_mondoarchives_home(g_mondo_home)) {
876        fprintf(stderr,
877                "Cannot find Mondo's homedir. I think you have >1 'mondo' directory on your hard disk. Please delete the superfluous 'mondo' directories and try again\n");
878        res++;
879        return (res);
880    }
881    res += some_basic_system_sanity_checks();
882    if (res) {
883        log_it("Your distribution did not pass Mondo's sanity test.");
884    }
885    g_current_media_number = 1;
886    bkpinfo->postnuke_tarball[0] = bkpinfo->nfs_mount[0] = '\0';
887    return (res);
888}
889
890
891
892
893/**
894 * Reset all fields of the backup information structure to a sensible default.
895 * @param bkpinfo The @c bkpinfo to reset.
896 */
897void reset_bkpinfo(struct s_bkpinfo *bkpinfo)
898{
899    int i;
900
901    log_msg(1, "Hi");
902    assert(bkpinfo != NULL);
903    memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
904    bkpinfo->manual_cd_tray = FALSE;
905    bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
906    bkpinfo->media_device[0] = '\0';
907    for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
908        bkpinfo->media_size[i] = -1;
909    }
910    bkpinfo->boot_loader = '\0';
911    bkpinfo->boot_device[0] = '\0';
912    bkpinfo->zip_exe[0] = '\0';
913    bkpinfo->zip_suffix[0] = '\0';
914    bkpinfo->restore_path[0] = '\0';
915    bkpinfo->use_lzo = FALSE;
916    bkpinfo->do_not_compress_these[0] = '\0';
917    bkpinfo->verify_data = FALSE;
918    bkpinfo->backup_data = FALSE;
919    bkpinfo->restore_data = FALSE;
920    bkpinfo->disaster_recovery =
921        (am_I_in_disaster_recovery_mode()? TRUE : FALSE);
922    if (bkpinfo->disaster_recovery) {
923        strcpy(bkpinfo->isodir, "/");
924    } else {
925        strcpy(bkpinfo->isodir, "/var/cache/mondo/iso");
926    }
927    strcpy(bkpinfo->prefix, STD_PREFIX);
928
929    bkpinfo->scratchdir[0] = '\0';
930    bkpinfo->make_filelist = TRUE;  // unless -J supplied to mondoarchive
931    sprintf(bkpinfo->tmpdir, "/tmp/tmpfs/mondo.tmp.%d", (int) (random() % 32768));  // for mondorestore
932    bkpinfo->optimal_set_size = 0;
933    bkpinfo->backup_media_type = none;
934    strcpy(bkpinfo->include_paths, "/");
935    bkpinfo->exclude_paths[0] = '\0';
936    bkpinfo->call_before_iso[0] = '\0';
937    bkpinfo->call_make_iso[0] = '\0';
938    bkpinfo->call_burn_iso[0] = '\0';
939    bkpinfo->call_after_iso[0] = '\0';
940    bkpinfo->image_devs[0] = '\0';
941    bkpinfo->postnuke_tarball[0] = '\0';
942    bkpinfo->kernel_path[0] = '\0';
943    bkpinfo->nfs_mount[0] = '\0';
944    bkpinfo->nfs_remote_dir[0] = '\0';
945    bkpinfo->wipe_media_first = FALSE;
946    bkpinfo->differential = FALSE;
947    bkpinfo->cdrw_speed = 0;
948// patch by Herman Kuster 
949    bkpinfo->differential = 0;
950// patch end
951    bkpinfo->compression_level = 3;
952}
953
954
955
956
957/**
958 * Get the remaining free space (in MB) on @p partition.
959 * @param partition The partition to check free space on (either a device or a mountpoint).
960 * @return The free space on @p partition, in MB.
961 */
962long free_space_on_given_partition(char *partition)
963{
964    char command[MAX_STR_LEN], out_sz[MAX_STR_LEN];
965    long res;
966
967    assert_string_is_neither_NULL_nor_zerolength(partition);
968
969    sprintf(command, "df -m -P %s &> /dev/null", partition);
970    if (system(command)) {
971        return (-1);
972    }                           // partition does not exist
973    sprintf(command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
974            partition);
975    strcpy(out_sz, call_program_and_get_last_line_of_output(command));
976    if (strlen(out_sz) == 0) {
977        return (-1);
978    }                           // error within df, probably
979    res = atol(out_sz);
980    return (res);
981}
982
983
984
985/**
986 * Check the user's system for sanity. Checks performed:
987 * - make sure user has enough RAM (32mb required, 64mb recommended)
988 * - make sure user has enough free space in @c /
989 * - check kernel for ramdisk support
990 * - make sure afio, cdrecord, mkisofs, bzip2, awk, md5sum, strings, mindi, and buffer exist
991 * - make sure CD-ROM is unmounted
992 * - make sure /etc/modules.conf exists
993 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
994 *
995 * @return number of problems with the user's setup (0 for success)
996 */
997int some_basic_system_sanity_checks()
998{
999
1000    /*@ buffers ************ */
1001    char tmp[MAX_STR_LEN];
1002    //  char command[MAX_STR_LEN];
1003
1004    /*@ int's *************** */
1005    int retval = 0;
1006    long Lres;
1007
1008
1009    mvaddstr_and_log_it(g_currentY, 0,
1010                        "Checking sanity of your Linux distribution");
1011#ifndef __FreeBSD__
1012    if (system("which mkfs.vfat &> /dev/null")
1013        && !system("which mkfs.msdos &> /dev/null")) {
1014        log_it
1015            ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
1016        run_program_and_log_output
1017            ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
1018    }
1019    strcpy(tmp,
1020           call_program_and_get_last_line_of_output
1021           ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
1022    if (atol(tmp) < 35000) {
1023        retval++;
1024        log_to_screen(_("You must have at least 32MB of RAM to use Mondo."));
1025    }
1026    if (atol(tmp) < 66000) {
1027        log_to_screen
1028            (_("WARNING! You have very little RAM. Please upgrade to 64MB or more."));
1029    }
1030#endif
1031
1032    if ((Lres = free_space_on_given_partition("/var/cache/mondo")) == -1) /* {
1033        Lres = free_space_on_given_partition("/");
1034    }
1035    */
1036    log_it("Free space on given partition = %ld MB", Lres);
1037
1038    if (Lres < 50) {
1039        fatal_error("Your /var/cache/mondo partition has <50MB free. Please adjust your partition table to something saner."); 
1040    }
1041
1042    if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
1043        retval++;
1044        log_to_screen
1045            ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
1046        fatal_error
1047            ("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. :)");
1048    }
1049#ifndef __FreeBSD__
1050    if (run_program_and_log_output
1051        ("grep ramdisk /proc/devices", FALSE)) {
1052        if (!ask_me_yes_or_no
1053            (_("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?")))
1054        {
1055            //          retval++;
1056            log_to_screen
1057                (_("It looks as if your kernel lacks ramdisk and initrd support."));
1058            log_to_screen
1059                (_("I'll allow you to proceed but FYI, if I'm right, your kernel is broken."));
1060        }
1061    }
1062#endif
1063    retval += whine_if_not_found(MKE2FS_OR_NEWFS);
1064    retval += whine_if_not_found("mkisofs");
1065    if (system("which dvdrecord > /dev/null 2> /dev/null")) {
1066        retval += whine_if_not_found("cdrecord");
1067    }
1068    retval += whine_if_not_found("bzip2");
1069    retval += whine_if_not_found("awk");
1070    retval += whine_if_not_found("md5sum");
1071    retval += whine_if_not_found("strings");
1072    retval += whine_if_not_found("mindi");
1073    retval += whine_if_not_found("buffer");
1074
1075    // abort if Windows partition but no ms-sys and parted
1076    if (!run_program_and_log_output
1077        ("mount | grep -w vfat | grep -v /dev/fd | grep -v nexdisk", 0)
1078        ||
1079        !run_program_and_log_output
1080        ("mount | grep -w dos | grep -v /dev/fd | grep -v nexdisk", 0)) {
1081        log_to_screen(_("I think you have a Windows 9x partition."));
1082        retval += whine_if_not_found("parted");
1083#ifndef __IA64__
1084        /* IA64 always has one vfat partition for EFI even without Windows */
1085        // retval +=
1086        if (!find_home_of_exe("ms-sys")) {
1087            log_to_screen(_("Please install ms-sys just in case."));
1088        }
1089#endif
1090    }
1091
1092    if (!find_home_of_exe("cmp")) {
1093        if (!find_home_of_exe("true")) {
1094            whine_if_not_found("cmp");
1095        } else {
1096            log_to_screen
1097                (_("Your system lacks the 'cmp' binary. I'll create a dummy cmp for you."));
1098            if (run_program_and_log_output
1099                ("cp -f `which true` /usr/bin/cmp", 0)) {
1100                fatal_error("Failed to create dummy 'cmp' file.");
1101            }
1102        }
1103    }
1104    run_program_and_log_output
1105        ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
1106    strcpy(tmp,
1107           call_program_and_get_last_line_of_output
1108           ("mount | grep -E \"cdr(om|w)\""));
1109    if (strcmp("", tmp)) {
1110        if (strstr(tmp, "autofs")) {
1111            log_to_screen
1112                (_("Your CD-ROM is mounted via autofs. I therefore cannot tell"));
1113            log_to_screen
1114                (_("if a CD actually is inserted. If a CD is inserted, please"));
1115            log_to_screen(_("eject it. Thank you."));
1116            log_it
1117                ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
1118        } else
1119            if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
1120            retval++;
1121            fatal_error
1122                ("Your CD-ROM drive is mounted. Please unmount it.");
1123        }
1124    }
1125#ifndef __FreeBSD__
1126    if (!does_file_exist("/etc/modules.conf")) {
1127        if (does_file_exist("/etc/conf.modules")) {
1128            log_it("Linking /etc/modules.conf to /etc/conf.modules");
1129            run_program_and_log_output
1130                ("ln -sf /etc/conf.modules /etc/modules.conf", 5);
1131        } else if (does_file_exist("/etc/modprobe.d")) {
1132            log_it
1133                ("Directory /etc/modprobe.d found. mindi will use its contents.");
1134        } else if (does_file_exist("/etc/modprobe.conf")) {
1135            log_it("Linking /etc/modules.conf to /etc/modprobe.conf");
1136            run_program_and_log_output
1137                ("ln -sf /etc/modprobe.conf /etc/modules.conf", 5);
1138        } else {
1139            retval++;
1140            log_to_screen
1141                (_("Please find out what happened to /etc/modules.conf"));
1142        }
1143    }
1144#endif
1145
1146    run_program_and_log_output("cat /etc/fstab", 5);
1147#ifdef __FreeBSD__
1148    run_program_and_log_output("vinum printconfig", 5);
1149#else
1150    run_program_and_log_output("cat /etc/raidtab", 5);
1151#endif
1152
1153    if (run_program_and_log_output("mindi -V", 1)) {
1154        log_to_screen(_("Could not ascertain mindi's version number."));
1155        log_to_screen
1156            (_("You have not installed Mondo and/or Mindi properly."));
1157        log_to_screen(_("Please uninstall and reinstall them both."));
1158        fatal_error("Please reinstall Mondo and Mindi.");
1159    }
1160    if (run_program_and_log_output
1161        ("mindi --makemountlist /tmp/mountlist.txt.test", 5)) {
1162        log_to_screen
1163            (_("Mindi --makemountlist /tmp/mountlist.txt.test failed for some reason."));
1164        log_to_screen
1165            (_("Please run that command by hand and examine /var/log/mindi.log"));
1166        log_to_screen
1167            (_("for more information. Perhaps your /etc/fstab file is insane."));
1168        log_to_screen
1169            (_("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see."));
1170        retval++;
1171    }
1172
1173    if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
1174        && !does_file_exist("/etc/raidtab")) {
1175        log_to_screen
1176            (_("You have RAID partitions but no /etc/raidtab - creating one from /proc/mdstat"));
1177        create_raidtab_from_mdstat("/etc/raidtab", "/proc/mdstat");
1178    }
1179
1180    if (retval) {
1181        mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
1182    } else {
1183        mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
1184    }
1185    return (retval);
1186}
1187
1188/**
1189 * Retrieve the line containing @p label from the config file.
1190 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
1191 * @param label What to read from the file.
1192 * @param value Where to put it.
1193 * @return 0 for success, 1 for failure.
1194 */
1195int read_cfg_var(char *config_file, char *label, char *value)
1196{
1197    /*@ buffer ****************************************************** */
1198    char command[MAX_STR_LEN * 2];
1199    char tmp[MAX_STR_LEN];
1200
1201    /*@ end vars *************************************************** */
1202
1203    assert_string_is_neither_NULL_nor_zerolength(config_file);
1204    assert_string_is_neither_NULL_nor_zerolength(label);
1205    if (!does_file_exist(config_file)) {
1206        sprintf(tmp, "(read_cfg_var) Cannot find %s config file",
1207                config_file);
1208        log_to_screen(tmp);
1209        value[0] = '\0';
1210        return (1);
1211    } else if (strstr(value, "/dev/") && strstr(value, "t0")
1212               && !strcmp(label, "media-dev")) {
1213        log_msg(2, "FYI, I shan't read new value for %s - already got %s",
1214                label, value);
1215        return (0);
1216    } else {
1217        sprintf(command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
1218                label, config_file);
1219        strcpy(value, call_program_and_get_last_line_of_output(command));
1220        if (strlen(value) == 0) {
1221            return (1);
1222        } else {
1223            return (0);
1224        }
1225    }
1226}
1227
1228
1229
1230/**
1231 * Remount @c supermount if it was unmounted earlier.
1232 */
1233void remount_supermounts_if_necessary()
1234{
1235    if (g_remount_cdrom_at_end) {
1236        run_program_and_log_output("mount " MNT_CDROM, FALSE);
1237    }
1238    if (g_remount_floppy_at_end) {
1239        run_program_and_log_output("mount " MNT_FLOPPY, FALSE);
1240    }
1241}
1242
1243/**
1244 * Unmount @c supermount if it's mounted.
1245 */
1246void unmount_supermounts_if_necessary()
1247{
1248    if (run_program_and_log_output
1249        ("mount | grep cdrom | grep super", FALSE) == 0) {
1250        g_remount_cdrom_at_end = TRUE;
1251        run_program_and_log_output("umount " MNT_CDROM, FALSE);
1252    }
1253    if (run_program_and_log_output
1254        ("mount | grep floppy | grep super", FALSE) == 0) {
1255        g_remount_floppy_at_end = TRUE;
1256        run_program_and_log_output("umount " MNT_FLOPPY, FALSE);
1257    }
1258}
1259
1260/**
1261 * Whether we had to stop autofs (if so, restart it at end).
1262 */
1263bool g_autofs_stopped = FALSE;
1264
1265/**
1266 * Path to the autofs initscript ("" if none exists).
1267 */
1268char g_autofs_exe[MAX_STR_LEN];
1269
1270/**
1271 * Autofs initscript in Xandros Linux distribution.
1272 */
1273#define XANDROS_AUTOFS_FNAME "/etc/init.d/xandros-autofs"
1274
1275/**
1276 * Autofs initscript in most Linux distributions.
1277 */
1278#define STOCK_AUTOFS_FNAME "/etc/rc.d/init.d/autofs"
1279
1280/**
1281 * If autofs is mounted, stop it (restart at end).
1282 */
1283void stop_autofs_if_necessary()
1284{
1285    char tmp[MAX_STR_LEN];
1286
1287    g_autofs_exe[0] = '\0';
1288    if (does_file_exist(XANDROS_AUTOFS_FNAME)) {
1289        strcpy(g_autofs_exe, XANDROS_AUTOFS_FNAME);
1290    } else if (does_file_exist(STOCK_AUTOFS_FNAME)) {
1291        strcpy(g_autofs_exe, STOCK_AUTOFS_FNAME);
1292    }
1293
1294    if (!g_autofs_exe[0]) {
1295        log_msg(3, "No autofs detected.");
1296    } else {
1297        log_msg(3, "%s --- autofs detected", g_autofs_exe);
1298// FIXME -- only disable it if it's running ---  sprintf(tmp, "%s status", autofs_exe);
1299        sprintf(tmp, "%s stop", g_autofs_exe);
1300        if (run_program_and_log_output(tmp, 2)) {
1301            log_it("Failed to stop autofs - I assume it wasn't running");
1302        } else {
1303            g_autofs_stopped = TRUE;
1304            log_it("Stopped autofs OK");
1305        }
1306    }
1307}
1308
1309/**
1310 * If autofs was stopped earlier, restart it.
1311 */
1312void restart_autofs_if_necessary()
1313{
1314    char tmp[MAX_STR_LEN];
1315
1316    if (!g_autofs_stopped || !g_autofs_exe[0]) {
1317        log_msg(3, "No autofs detected.");
1318        return;
1319    }
1320    sprintf(tmp, "%s start", g_autofs_exe);
1321    if (run_program_and_log_output(tmp, 2)) {
1322        log_it("Failed to start autofs");
1323    } else {
1324        g_autofs_stopped = FALSE;
1325        log_it("Started autofs OK");
1326    }
1327}
1328
1329
1330/**
1331 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1332 */
1333void mount_boot_if_necessary()
1334{
1335    char tmp[MAX_STR_LEN];
1336    char command[MAX_STR_LEN];
1337
1338    log_msg(1, "Started sub");
1339    log_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
1340    g_boot_mountpt[0] = '\0';
1341    log_msg(4, "Done. Great. Seeting command to something");
1342    strcpy(command,
1343           "grep -v \":\" /etc/fstab | grep -vx \"#.*\" | grep -w \"/boot\" | tr -s ' ' '\t' | cut -f1 | head -n1");
1344    log_msg(4, "Cool. Command = '%s'", command);
1345    strcpy(tmp, call_program_and_get_last_line_of_output(command));
1346    log_msg(4, "tmp = '%s'", tmp);
1347    if (tmp[0]) {
1348        log_it("/boot is at %s according to /etc/fstab", tmp);
1349        if (strstr(tmp, "LABEL=")) {
1350            if (!run_program_and_log_output("mount /boot", 5)) {
1351                strcpy(g_boot_mountpt, "/boot");
1352                log_msg(1, "Mounted /boot");
1353            } else {
1354                log_it("...ignored cos it's a label :-)");
1355            }
1356        } else {
1357            sprintf(command, "mount | grep -w \"%s\"", tmp);
1358            log_msg(3, "command = %s", command);
1359            if (run_program_and_log_output(command, 5)) {
1360                strcpy(g_boot_mountpt, tmp);
1361                sprintf(tmp,
1362                        "%s (your /boot partition) is not mounted. I'll mount it before backing up",
1363                        g_boot_mountpt);
1364                log_it(tmp);
1365                sprintf(tmp, "mount %s", g_boot_mountpt);
1366                if (run_program_and_log_output(tmp, 5)) {
1367                    g_boot_mountpt[0] = '\0';
1368                    log_msg(1, "Plan B");
1369                    if (!run_program_and_log_output("mount /boot", 5)) {
1370                        strcpy(g_boot_mountpt, "/boot");
1371                        log_msg(1, "Plan B worked");
1372                    } else {
1373                        log_msg(1,
1374                                "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1375                    }
1376                }
1377            }
1378        }
1379    }
1380    log_msg(1, "Ended sub");
1381}
1382
1383
1384/**
1385 * If we mounted /boot earlier, unmount it.
1386 */
1387void unmount_boot_if_necessary()
1388{
1389    char tmp[MAX_STR_LEN];
1390
1391    log_msg(3, "starting");
1392    if (g_boot_mountpt[0]) {
1393        sprintf(tmp, "umount %s", g_boot_mountpt);
1394        if (run_program_and_log_output(tmp, 5)) {
1395            log_it("WARNING - unable to unmount /boot");
1396        }
1397    }
1398    log_msg(3, "leaving");
1399}
1400
1401
1402
1403/**
1404 * Write a line to a configuration file. Writes a line of the form,
1405 * @c label @c value.
1406 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1407 * @param label What to call this bit of data you're writing.
1408 * @param value The bit of data you're writing.
1409 * @return 0 for success, 1 for failure.
1410 */
1411int write_cfg_var(char *config_file, char *label, char *value)
1412{
1413    /*@ buffers ***************************************************** */
1414    char command[MAX_STR_LEN * 2];
1415    char tempfile[MAX_STR_LEN];
1416    char tmp[MAX_STR_LEN];
1417
1418
1419    /*@ end vars *************************************************** */
1420    assert_string_is_neither_NULL_nor_zerolength(config_file);
1421    assert_string_is_neither_NULL_nor_zerolength(label);
1422    assert(value != NULL);
1423    if (!does_file_exist(config_file)) {
1424        sprintf(tmp, "(write_cfg_file) Cannot find %s config file",
1425                config_file);
1426        log_to_screen(tmp);
1427        return (1);
1428    }
1429    strcpy(tempfile,
1430           call_program_and_get_last_line_of_output
1431           ("mktemp -q /tmp/mojo-jojo.blah.XXXXXX"));
1432    if (does_file_exist(config_file)) {
1433        sprintf(command, "grep -vx '%s .*' %s > %s",
1434                label, config_file, tempfile);
1435        paranoid_system(command);
1436    }
1437    sprintf(command, "echo \"%s %s\" >> %s", label, value, tempfile);
1438    paranoid_system(command);
1439    sprintf(command, "mv -f %s %s", tempfile, config_file);
1440    paranoid_system(command);
1441    unlink(tempfile);
1442    return (0);
1443}
1444
1445/**
1446 * If @p y, malloc @p x, else free @p x.
1447 * @bug This function seems orphaned. Please remove.
1448 */
1449#define do_alloc_or_free_depending(x,y) { if(y) {x=malloc(MAX_STR_LEN);} else {paranoid_free(x);} }
1450
1451/**
1452 * Allocate or free important globals, depending on @p mal.
1453 * @param mal If TRUE, malloc; if FALSE, free.
1454 */
1455void do_libmondo_global_strings_thing(int mal)
1456{
1457    if (mal) {
1458        iamhere("Malloc'ing globals");
1459        malloc_string(g_boot_mountpt);
1460        malloc_string(g_mondo_home);
1461        malloc_string(g_tmpfs_mountpt);
1462        malloc_string(g_erase_tmpdir_and_scratchdir);
1463        malloc_string(g_serial_string);
1464        malloc_string(g_magicdev_command);
1465    } else {
1466        iamhere("Freeing globals");
1467        paranoid_free(g_boot_mountpt);
1468        paranoid_free(g_mondo_home);
1469        paranoid_free(g_tmpfs_mountpt);
1470        paranoid_free(g_erase_tmpdir_and_scratchdir);
1471        paranoid_free(g_serial_string);
1472        paranoid_free(g_magicdev_command);
1473    }
1474}
1475
1476/**
1477 * Allocate important globals.
1478 * @see do_libmondo_global_strings_thing
1479 */
1480void malloc_libmondo_global_strings(void)
1481{
1482    do_libmondo_global_strings_thing(1);
1483}
1484
1485/**
1486 * Free important globals.
1487 * @see do_libmondo_global_strings_thing
1488 */
1489void free_libmondo_global_strings(void)
1490{
1491    do_libmondo_global_strings_thing(0);
1492}
1493
1494
1495
1496/**
1497 * Stop @c magicdev if it's running.
1498 * The command used to start it is saved in @p g_magicdev_command.
1499 */
1500void stop_magicdev_if_necessary()
1501{
1502    strcpy(g_magicdev_command,
1503           call_program_and_get_last_line_of_output
1504           ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
1505    if (g_magicdev_command[0]) {
1506        log_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1507        paranoid_system("killall magicdev");
1508    }
1509}
1510
1511
1512/**
1513 * Restart magicdev if it was stopped.
1514 */
1515void restart_magicdev_if_necessary()
1516{
1517    char *tmp;
1518
1519    malloc_string(tmp);
1520    if (g_magicdev_command && g_magicdev_command[0]) {
1521        sprintf(tmp, "%s &", g_magicdev_command);
1522        paranoid_system(tmp);
1523    }
1524    paranoid_free(tmp);
1525}
1526
1527/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.