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

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