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

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

Use of conf file entries (iso_burning_*, media_device, media_size)
and adapatation of the rest of the code to that (including bkpinfo)

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