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

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