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

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

log_msg => mr_msg for common files

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