source: MondoRescue/branches/2.2.2/mondo/src/common/libmondo-tools.c

Last change on this file was 1316, checked in by Bruno Cornec, 17 years ago

MONDO_LOGFILE is rather a char* exported by each main program (consolidation of those mecanisms in .h files with ps_*)

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