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

Last change on this file since 1628 was 1628, checked in by Bruno Cornec, 17 years ago

prefix is also now read from conf file

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