source: MondoRescue/branches/2.04_berlios/mondo/mondo/common/libmondo-tools.c@ 99

Last change on this file since 99 was 99, checked in by bcornec, 18 years ago

Apply patch from Andree Leidenfrost <aleidenf_AT_bigpond.net.au>
(-r72:75 of trunk) for :
+ correcting NFS handling in interactive mode
+ growisofs and sudo pb

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