source: MondoRescue/trunk/mondo/mondo/common/libmondo-tools.c@ 49

Last change on this file since 49 was 49, checked in by bcornec, 19 years ago

some bugs corrected (size_t, & for getline, ...) + indent

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