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

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

svn merge -r 1902:1923 $SVN_M/branches/2.2.6

  • Property svn:keywords set to Id
File size: 35.0 KB
Line 
1/* $Id: libmondo-tools.c 1924 2008-04-17 23:41:41Z 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 1924 2008-04-17 23:41:41Z 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->restore_path[0] = '\0';
761 bkpinfo->do_not_compress_these[0] = '\0';
762 bkpinfo->verify_data = FALSE;
763 bkpinfo->backup_data = FALSE;
764 bkpinfo->restore_data = FALSE;
765 bkpinfo->disaster_recovery =
766 (am_I_in_disaster_recovery_mode()? TRUE : FALSE);
767 if (bkpinfo->disaster_recovery) {
768 strcpy(bkpinfo->isodir, "/");
769 } else {
770 strcpy(bkpinfo->isodir, "/var/cache/mondo/iso");
771 }
772 mr_asprintf(&tmp,mr_conf->prefix);
773 bkpinfo->prefix = tmp;
774
775 bkpinfo->make_filelist = TRUE; // unless -J supplied to mondoarchive
776 bkpinfo->optimal_set_size = 0;
777 bkpinfo->backup_media_type = none;
778 bkpinfo->backup_media_string[0] = '\0';
779 strcpy(bkpinfo->include_paths, "/");
780 bkpinfo->call_before_iso[0] = '\0';
781 bkpinfo->call_make_iso[0] = '\0';
782 bkpinfo->call_after_iso[0] = '\0';
783 bkpinfo->image_devs[0] = '\0';
784 bkpinfo->postnuke_tarball[0] = '\0';
785 bkpinfo->nfs_mount[0] = '\0';
786 bkpinfo->nfs_remote_dir[0] = '\0';
787 bkpinfo->wipe_media_first = FALSE;
788 bkpinfo->differential = FALSE;
789 bkpinfo->writer_speed = mr_conf->iso_burning_speed;
790}
791
792
793/**
794 * Get the remaining free space (in MB) on @p partition.
795 * @param partition The partition to check free space on (either a device or a mountpoint).
796 * @return The free space on @p partition, in MB.
797 */
798long free_space_on_given_partition(char *partition)
799{
800 char *command = NULL;
801 char *out_sz = NULL;
802 long res = 0L;
803
804 assert_string_is_neither_NULL_nor_zerolength(partition);
805
806 mr_asprintf(&command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
807 if (system(command)) {
808 return (-1);
809 } // partition does not exist
810 mr_free(command);
811
812 mr_asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
813 partition);
814 mr_asprintf(&out_sz, call_program_and_get_last_line_of_output(command));
815 mr_free(command);
816
817 if (strlen(out_sz) == 0) {
818 return (-1);
819 } // error within df, probably
820 res = atol(out_sz);
821 mr_free(out_sz);
822 return (res);
823}
824
825
826/**
827 * Check the user's system for sanity. Checks performed:
828 * - make sure user has enough RAM (32mb required, 64mb recommended)
829 * - make sure user has enough free space in @c /
830 * - check kernel for ramdisk support
831 * - make sure afio, cdrecord, bzip2, awk, md5sum, strings, mindi, and buffer exist
832 * - make sure CD-ROM is unmounted
833 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
834 *
835 * @return number of problems with the user's setup (0 for success)
836 */
837int some_basic_system_sanity_checks()
838{
839
840 /*@ buffers ************ */
841 char *tmp = NULL;
842
843 /*@ int's *************** */
844 int retval = 0;
845
846 mvaddstr_and_log_it(g_currentY, 0,
847 "Checking sanity of your Linux distribution");
848#ifndef __FreeBSD__
849 if (system("which mkfs.vfat 2> /dev/null 1> /dev/null")
850 && !system("which mkfs.msdos 2> /dev/null 1> /dev/null")) {
851 log_it
852 ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
853 run_program_and_log_output
854 ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
855 }
856 mr_asprintf(&tmp,
857 call_program_and_get_last_line_of_output
858 ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
859 if (atol(tmp) < 35000) {
860 retval++;
861 log_to_screen(_("You must have at least 32MB of RAM to use Mondo."));
862 }
863 if (atol(tmp) < 66000) {
864 log_to_screen
865 (_("WARNING! You have very little RAM. Please upgrade to 64MB or more."));
866 }
867 mr_free(tmp);
868#endif
869
870 Lres = free_space_on_given_partition(MONDO_CACHE);
871 log_it("Free space on given partition = %ld MB", Lres);
872
873 if (Lres < 50) {
874 fatal_error("Your "MONDO_CACHE" partition has <50MB free. Please adjust your partition table to something saner or make "MONDO_CACHE" a symbolic link");
875 }
876
877 if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
878 retval++;
879 log_to_screen
880 ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
881 fatal_error
882 ("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. :)");
883 }
884#ifndef __FreeBSD__
885 if (run_program_and_log_output
886 ("grep ramdisk /proc/devices", FALSE)) {
887 if (!ask_me_yes_or_no
888 (_("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?")))
889 {
890 // retval++;
891 log_to_screen
892 (_("It looks as if your kernel lacks ramdisk and initrd support."));
893 log_to_screen
894 (_("I'll allow you to proceed but FYI, if I'm right, your kernel is broken."));
895 }
896 }
897#endif
898 retval += whine_if_not_found(mr_conf->iso_creation_cmd);
899 retval += whine_if_not_found(mr_conf->iso_burning_cmd);
900 retval += whine_if_not_found(MKE2FS_OR_NEWFS);
901 retval += whine_if_not_found(mr_conf->compresstion_tool);
902 retval += whine_if_not_found("gzip");
903 retval += whine_if_not_found("awk");
904 retval += whine_if_not_found("md5sum");
905 retval += whine_if_not_found("strings");
906 retval += whine_if_not_found("mindi");
907 retval += whine_if_not_found("buffer");
908
909 // abort if Windows partition but no ms-sys and parted
910 if (!run_program_and_log_output("mount | grep -Ew 'vfat|fat|dos' | grep -vE \"/dev/fd|nexdisk\"", 0)) {
911 log_to_screen(_("I think you have a Windows 9x partition."));
912 retval += whine_if_not_found("parted");
913 }
914
915 if (!find_home_of_exe("cmp")) {
916 if (!find_home_of_exe("true")) {
917 whine_if_not_found("cmp");
918 } else {
919 log_to_screen
920 ("Your system lacks the 'cmp' binary. I'll create a dummy cmp for you.");
921 if (run_program_and_log_output
922 ("cp -f `which true` /usr/bin/cmp", 0)) {
923 fatal_error("Failed to create dummy 'cmp' file.");
924 }
925 }
926 }
927 run_program_and_log_output
928 ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
929 mr_asprintf(&tmp,
930 call_program_and_get_last_line_of_output
931 ("mount | grep -E \"cdr(om|w)\""));
932 if (strcmp("", tmp)) {
933 if (strstr(tmp, "autofs")) {
934 log_to_screen
935 (_("Your CD-ROM is mounted via autofs. I therefore cannot tell"));
936 log_to_screen
937 (_("if a CD actually is inserted. If a CD is inserted, please"));
938 log_to_screen(_("eject it. Thank you."));
939 log_it
940 ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
941 } else
942 if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
943 retval++;
944 fatal_error
945 ("Your CD-ROM drive is mounted. Please unmount it.");
946 }
947 }
948 mr_free(tmp);
949
950 run_program_and_log_output("cat /etc/fstab", 5);
951#ifdef __FreeBSD__
952 run_program_and_log_output("vinum printconfig", 5);
953#else
954 run_program_and_log_output("cat /etc/raidtab", 5);
955#endif
956
957 if (run_program_and_log_output("mindi -V", 1)) {
958 log_to_screen(_("Could not ascertain mindi's version number."));
959 log_to_screen
960 (_("You have not installed Mondo and/or Mindi properly."));
961 log_to_screen(_("Please uninstall and reinstall them both."));
962 fatal_error("Please reinstall Mondo and Mindi.");
963 }
964 mr_asprintf(&tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
965 if (run_program_and_log_output(tmp, 5)) {
966 log_to_screen(tmp);
967 log_to_screen
968 (_("failed for some reason."));
969 log_to_screen
970 (_("Please run that command by hand and examine /var/log/mindi.log"));
971 log_to_screen
972 (_("for more information. Perhaps your /etc/fstab file is insane."));
973 log_to_screen
974 (_("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see."));
975 retval++;
976 }
977 mr_free(tmp);
978
979 if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
980 && !does_file_exist("/etc/raidtab")) {
981 log_to_screen
982 (_("You have RAID partitions but no /etc/raidtab - creating one from "MDSTAT_FILE));
983 create_raidtab_from_mdstat("/etc/raidtab");
984 }
985
986 if (retval) {
987 mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
988 } else {
989 mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
990 }
991 return (retval);
992}
993
994/**
995 * Retrieve the line containing @p label from the config file.
996 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
997 * @param label What to read from the file.
998 * @param value Where to put it.
999 * @return 0 for success, 1 for failure.
1000 */
1001int read_cfg_var(char *config_file, char *label, char *value)
1002{
1003 /*@ buffer ****************************************************** */
1004 char *command = NULL;
1005 char *tmp = NULL;
1006
1007 /*@ end vars *************************************************** */
1008
1009 assert_string_is_neither_NULL_nor_zerolength(config_file);
1010 assert_string_is_neither_NULL_nor_zerolength(label);
1011
1012 if (!does_file_exist(config_file)) {
1013 mr_asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
1014 config_file);
1015 log_to_screen(tmp);
1016 mr_free(tmp);
1017 value[0] = '\0';
1018 return (1);
1019 /* } BERLIOS: Useless:
1020 else if (strstr(value, "/dev/") && strstr(value, "t0")
1021 && !strcmp(label, "media-dev")) {
1022 mr_msg(2, "FYI, I shan't read new value for %s - already got %s",
1023 label, value);
1024 return (0);
1025 */ } else {
1026 mr_asprintf(&command, "grep '%s .*' %s| cut -d'=' -f2,3,4,5",
1027 label, config_file);
1028 strcpy(value, call_program_and_get_last_line_of_output(command));
1029 mr_free(command);
1030 if (strlen(value) == 0) {
1031 return (1);
1032 } else {
1033 return (0);
1034 }
1035 }
1036}
1037
1038
1039/**
1040 * Remount @c supermount if it was unmounted earlier.
1041 */
1042void remount_supermounts_if_necessary()
1043{
1044 if (g_remount_cdrom_at_end) {
1045 run_program_and_log_output("mount " MNT_CDROM, FALSE);
1046 }
1047}
1048
1049/**
1050 * Unmount @c supermount if it's mounted.
1051 */
1052void unmount_supermounts_if_necessary()
1053{
1054 if (run_program_and_log_output
1055 ("mount | grep cdrom | grep super", FALSE) == 0) {
1056 g_remount_cdrom_at_end = TRUE;
1057 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1058 }
1059}
1060
1061/**
1062 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1063 */
1064void mount_boot_if_necessary()
1065{
1066 char *tmp = NULL;
1067 char *tmp1 = NULL;
1068 char *command = NULL;
1069
1070 mr_msg(1, "Started sub");
1071 mr_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
1072 g_boot_mountpt[0] = '\0';
1073 mr_msg(4, "Done. Great. Setting command to something");
1074 mr_asprintf(&command,
1075 "grep -v ':' /etc/fstab | grep -vE '^#.*$' | grep -E \"[ ]/boot[ ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
1076 mr_msg(4, "Cool. Command = '%s'", command);
1077 mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
1078 mr_free(command);
1079
1080 mr_msg(4, "tmp = '%s'", tmp);
1081 if (tmp[0]) {
1082 log_it("/boot is at %s according to /etc/fstab", tmp);
1083 mr_asprintf(&command, "mount | grep -Ew '/boot'");
1084 mr_asprintf(&tmp1, call_program_and_get_last_line_of_output(command));
1085 mr_free(command);
1086 if (!strcmp(tmp1,"")) {
1087 if ((strstr(tmp1, "LABEL=") || strstr(tmp1,"UUID="))) {
1088 if (!run_program_and_log_output("mount /boot", 5)) {
1089 strcpy(g_boot_mountpt, "/boot");
1090 log_msg(1, "Mounted /boot");
1091 }
1092 } else {
1093 log_it("...ignored cos it's a label or uuid :-)");
1094 }
1095 mr_free(tmp1);
1096 } else {
1097 mr_free(tmp1);
1098 mr_asprintf(&command, "mount | grep -E '^%s'", tmp);
1099 mr_msg(3, "command = %s", command);
1100 if (run_program_and_log_output(command, 5)) {
1101 strcpy(g_boot_mountpt, tmp);
1102 mr_asprintf(&tmp1,
1103 "%s (your /boot partition) is not mounted. I'll mount it before backing up",
1104 g_boot_mountpt);
1105 log_it(tmp1);
1106 mr_free(tmp1);
1107
1108 mr_asprintf(&tmp1, "mount %s", g_boot_mountpt);
1109 if (run_program_and_log_output(tmp1, 5)) {
1110 g_boot_mountpt[0] = '\0';
1111 mr_msg(1, "Plan B");
1112 if (!run_program_and_log_output("mount /boot", 5)) {
1113 strcpy(g_boot_mountpt, "/boot");
1114 mr_msg(1, "Plan B worked");
1115 } else {
1116 mr_msg(1,
1117 "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1118 }
1119 }
1120 }
1121 mr_free(tmp1);
1122 }
1123 mr_free(command);
1124 }
1125 }
1126 mr_free(tmp);
1127 mr_msg(1, "Ended sub");
1128}
1129
1130
1131/**
1132 * If we mounted /boot earlier, unmount it.
1133 */
1134void unmount_boot_if_necessary()
1135{
1136 char *tmp;
1137
1138 mr_msg(3, "starting");
1139 if (g_boot_mountpt[0]) {
1140 mr_asprintf(&tmp, "umount %s", g_boot_mountpt);
1141 if (run_program_and_log_output(tmp, 5)) {
1142 log_it("WARNING - unable to unmount /boot");
1143 }
1144 mr_free(tmp);
1145 }
1146 mr_msg(3, "leaving");
1147}
1148
1149
1150/**
1151 * Write a line to a configuration file. Writes a line of the form,
1152 * @c label @c value.
1153 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1154 * @param label What to call this bit of data you're writing.
1155 * @param value The bit of data you're writing.
1156 * @return 0 for success, 1 for failure.
1157 */
1158int write_cfg_var(char *config_file, char *label, char *value)
1159{
1160 /*@ buffers ***************************************************** */
1161 char *command = NULL;
1162 char *tempfile = NULL;
1163 char *tmp = NULL;
1164
1165
1166 /*@ end vars *************************************************** */
1167 assert_string_is_neither_NULL_nor_zerolength(config_file);
1168 assert_string_is_neither_NULL_nor_zerolength(label);
1169 assert(value != NULL);
1170
1171 if (!does_file_exist(config_file)) {
1172 mr_asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
1173 config_file);
1174 log_to_screen(tmp);
1175 mr_free(tmp);
1176 return (1);
1177 }
1178 mr_asprintf(&tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
1179 if (does_file_exist(config_file)) {
1180 mr_asprintf(&command, "grep -vE '^%s .*$' %s > %s",
1181 label, config_file, tempfile);
1182 paranoid_system(command);
1183 mr_free(command);
1184 }
1185 mr_asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
1186 paranoid_system(command);
1187 mr_free(command);
1188
1189 mr_asprintf(&command, "mv -f %s %s", tempfile, config_file);
1190 paranoid_system(command);
1191 mr_free(command);
1192 unlink(tempfile);
1193 mr_free(tempfile);
1194 return (0);
1195}
1196
1197
1198/**
1199 * Allocate or free important globals, depending on @p mal.
1200 * @param mal If TRUE, malloc; if FALSE, free.
1201 */
1202void do_libmondo_global_strings_thing(int mal)
1203{
1204 if (mal) {
1205 malloc_string(g_boot_mountpt);
1206 malloc_string(g_tmpfs_mountpt);
1207 malloc_string(g_serial_string);
1208 malloc_string(g_magicdev_command);
1209 } else {
1210 mr_free(g_boot_mountpt);
1211 mr_free(g_tmpfs_mountpt);
1212 mr_free(g_serial_string);
1213 mr_free(g_magicdev_command);
1214 }
1215}
1216
1217/**
1218 * Allocate important globals.
1219 * @see do_libmondo_global_strings_thing
1220 */
1221void malloc_libmondo_global_strings(void)
1222{
1223 do_libmondo_global_strings_thing(1);
1224}
1225
1226/**
1227 * Free important globals.
1228 * @see do_libmondo_global_strings_thing
1229 */
1230void free_libmondo_global_strings(void)
1231{
1232 do_libmondo_global_strings_thing(0);
1233}
1234
1235
1236
1237/**
1238 * Stop @c magicdev if it's running.
1239 * The command used to start it is saved in @p g_magicdev_command.
1240 */
1241void stop_magicdev_if_necessary()
1242{
1243 strcpy(g_magicdev_command,
1244 call_program_and_get_last_line_of_output
1245 ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
1246 if (g_magicdev_command[0]) {
1247 mr_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1248 paranoid_system("killall magicdev");
1249 }
1250}
1251
1252
1253/**
1254 * Restart magicdev if it was stopped.
1255 */
1256void restart_magicdev_if_necessary()
1257{
1258 char *tmp = NULL;
1259
1260 if (g_magicdev_command && g_magicdev_command[0]) {
1261 mr_asprintf(&tmp, "%s &", g_magicdev_command);
1262 paranoid_system(tmp);
1263 mr_free(tmp);
1264 }
1265}
1266
1267/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.