source: MondoRescue/branches/2.2.6/mondo/src/common/libmondo-tools.c@ 1919

Last change on this file since 1919 was 1919, checked in by Bruno Cornec, 16 years ago

busybox under /usr/lib shouldn't be put on the boot disk as it breaks the /usr/lib* build. Use only the /bin/busybox bin.

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