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

Last change on this file since 1977 was 1977, checked in by Bruno Cornec, 16 years ago

svn merge -r 1938:1976 $SVN_M/branches/2.2.6

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