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

Last change on this file since 1592 was 1592, checked in by Bruno Cornec, 17 years ago
  • use of conf file for mkisofs and cdrecord commands
  • unification of cdr,cdrw and dvd
  • may not compile, modifications will continue
  • Property svn:keywords set to Id
File size: 35.0 KB
Line 
1/* $Id: libmondo-tools.c 1592 2007-08-09 13:19:05Z 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 1592 2007-08-09 13:19:05Z 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->cdrw_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->mondo_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->mondo_iso_burning_cmd,
505 extra_cdrom_params,
506 mr_conf->mondo_iso_burning_opt,
507 bkpinfo->media_device,
508 bkpinfo->cdrw_speed,
509 bkpinfo->tmpdir);
510 } else {
511 if (strstr(mr_conf->mondo_iso_burning_cmd,"growisofs") == NULL) {
512 if (getenv ("SUDO_COMMAND")) {
513 mr_asprintf(&command, "strings `which growisofs` | grep -c SUDO_COMMAND");
514 if (!strcmp(call_program_and_get_last_line_of_output(command), "1")) {
515 fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
516 }
517 mr_free(command);
518 }
519 sprintf(bkpinfo->call_make_iso,
520 "%s %s %s %s -Z %s -speed=%d . 2>> %s",
521 mr_conf->mondo_iso_burning_cmd,
522 mondo_mkisofs_sz,
523 extra_cdrom_params,
524 mr_conf->mondo_iso_burning_opt,
525 bkpinfo->media_device,
526 bkpinfo->cdrw_speed,
527 MONDO_LOGFILE);
528 } else {
529 sprintf(bkpinfo->call_make_iso,
530 "%s %s . 2>> %s | %s %s %s dev=%s speed=%d -",
531 mr_conf->iso_creation_cmd,
532 mondo_mkisofs_sz,
533 MONDO_LOGFILE,
534 mr_conf->mondo_iso_burning_cmd,
535 extra_cdrom_params,
536 mr_conf->mondo_iso_burning_opt,
537 bkpinfo->media_device,
538 bkpinfo->cdrw_speed);
539 }
540 }
541 mr_free(mondo_mkisofs_sz);
542 mr_free(extra_cdrom_params);
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(bkpinfo);
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 chmod(bkpinfo->tmpdir, 0700);
638 g_backup_media_type = bkpinfo->backup_media_type;
639 g_backup_media_string = bkpinfo->backup_media_string;
640 return (retval);
641}
642
643
644/**
645 * Do some miscellaneous setup tasks to be performed before filling @c bkpinfo.
646 * Seeds the random-number generator, loads important modules, checks the sanity
647 * of the user's Linux distribution, and deletes logfile.
648 * @param bkpinfo The backup information structure. Will be initialized.
649 * @return number of errors (0 for success)
650 */
651int pre_param_configuration(struct s_bkpinfo *bkpinfo)
652{
653 int res = 0;
654
655 make_hole_for_dir(MNT_CDROM);
656 assert(bkpinfo != NULL);
657 srandom((unsigned long) (time(NULL)));
658 insmod_crucial_modules();
659 reset_bkpinfo(bkpinfo); // also sets defaults ('/'=backup path, 3=compression level)
660 if (bkpinfo->disaster_recovery) {
661 if (!does_nonMS_partition_exist()) {
662 fatal_error
663 ("I am in disaster recovery mode\nPlease don't run mondoarchive.");
664 }
665 }
666
667 run_program_and_log_output("rm -Rf /tmp/changed.files*", FALSE);
668 res += some_basic_system_sanity_checks();
669 if (res) {
670 log_it("Your distribution did not pass Mondo's sanity test.");
671 }
672 g_current_media_number = 1;
673 bkpinfo->postnuke_tarball[0] = bkpinfo->nfs_mount[0] = '\0';
674 return (res);
675}
676
677
678
679
680/**
681 * Reset all fields of the backup information structure to a sensible default.
682 * @param bkpinfo The @c bkpinfo to reset.
683 */
684void reset_bkpinfo(struct s_bkpinfo *bkpinfo)
685{
686 mr_msg(1, "Hi");
687
688 assert(bkpinfo != NULL);
689 /* BERLIOS : Useless ?? */
690 memset((void *) bkpinfo, 0, sizeof(struct s_bkpinfo));
691
692 bkpinfo->manual_cd_tray = FALSE;
693 bkpinfo->internal_tape_block_size = DEFAULT_INTERNAL_TAPE_BLOCK_SIZE;
694 bkpinfo->media_device[0] = '\0';
695 bkpinfo->media_size = 0L;
696 bkpinfo->boot_loader = '\0';
697 bkpinfo->boot_device[0] = '\0';
698 bkpinfo->zip_exe[0] = '\0';
699 bkpinfo->zip_suffix[0] = '\0';
700 bkpinfo->restore_path[0] = '\0';
701 bkpinfo->use_lzo = FALSE;
702 bkpinfo->use_gzip = FALSE;
703 bkpinfo->do_not_compress_these[0] = '\0';
704 bkpinfo->verify_data = FALSE;
705 bkpinfo->backup_data = FALSE;
706 bkpinfo->restore_data = FALSE;
707 bkpinfo->disaster_recovery =
708 (am_I_in_disaster_recovery_mode()? TRUE : FALSE);
709 if (bkpinfo->disaster_recovery) {
710 strcpy(bkpinfo->isodir, "/");
711 } else {
712 strcpy(bkpinfo->isodir, "/var/cache/mondo/iso");
713 }
714 strcpy(bkpinfo->prefix, STD_PREFIX);
715
716 bkpinfo->scratchdir[0] = '\0';
717 bkpinfo->make_filelist = TRUE; // unless -J supplied to mondoarchive
718 sprintf(bkpinfo->tmpdir, "/tmp/tmpfs/mondo.tmp.%d", (int) (random() % 32768)); // for mondorestore
719 bkpinfo->optimal_set_size = 0;
720 bkpinfo->backup_media_type = none;
721 bkpinfo->backup_media_string[0] = '\0';
722 strcpy(bkpinfo->include_paths, "/");
723 bkpinfo->exclude_paths[0] = '\0';
724 bkpinfo->call_before_iso[0] = '\0';
725 bkpinfo->call_make_iso[0] = '\0';
726 bkpinfo->call_after_iso[0] = '\0';
727 bkpinfo->image_devs[0] = '\0';
728 bkpinfo->postnuke_tarball[0] = '\0';
729 bkpinfo->kernel_path[0] = '\0';
730 bkpinfo->nfs_mount[0] = '\0';
731 bkpinfo->nfs_remote_dir[0] = '\0';
732 bkpinfo->wipe_media_first = FALSE;
733 bkpinfo->differential = FALSE;
734 bkpinfo->cdrw_speed = 0;
735// patch by Herman Kuster
736 bkpinfo->differential = 0;
737// patch end
738 bkpinfo->compression_level = 3;
739}
740
741
742/**
743 * Get the remaining free space (in MB) on @p partition.
744 * @param partition The partition to check free space on (either a device or a mountpoint).
745 * @return The free space on @p partition, in MB.
746 */
747long free_space_on_given_partition(char *partition)
748{
749 char *command = NULL;
750 char *out_sz = NULL;
751 long res = 0L;
752
753 assert_string_is_neither_NULL_nor_zerolength(partition);
754
755 mr_asprintf(&command, "df -m -P %s &> /dev/null", partition);
756 if (system(command)) {
757 return (-1);
758 } // partition does not exist
759 mr_free(command);
760
761 mr_asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
762 partition);
763 mr_asprintf(&out_sz, call_program_and_get_last_line_of_output(command));
764 mr_free(command);
765
766 if (strlen(out_sz) == 0) {
767 return (-1);
768 } // error within df, probably
769 res = atol(out_sz);
770 mr_free(out_sz);
771 return (res);
772}
773
774
775/**
776 * Check the user's system for sanity. Checks performed:
777 * - make sure user has enough RAM (32mb required, 64mb recommended)
778 * - make sure user has enough free space in @c /
779 * - check kernel for ramdisk support
780 * - make sure afio, cdrecord, bzip2, awk, md5sum, strings, mindi, and buffer exist
781 * - make sure CD-ROM is unmounted
782 * - make sure /etc/modules.conf exists
783 * - make sure user's mountlist is OK by running <tt>mindi --makemountlist</tt>
784 *
785 * @return number of problems with the user's setup (0 for success)
786 */
787int some_basic_system_sanity_checks()
788{
789
790 /*@ buffers ************ */
791 char *tmp = NULL;
792
793 /*@ int's *************** */
794 int retval = 0;
795 long Lres;
796
797
798 mvaddstr_and_log_it(g_currentY, 0,
799 "Checking sanity of your Linux distribution");
800#ifndef __FreeBSD__
801 if (system("which mkfs.vfat &> /dev/null")
802 && !system("which mkfs.msdos &> /dev/null")) {
803 log_it
804 ("OK, you've got mkfs.msdos but not mkfs.vfat; time for the fairy to wave her magic wand...");
805 run_program_and_log_output
806 ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
807 }
808 mr_asprintf(&tmp,
809 call_program_and_get_last_line_of_output
810 ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
811 if (atol(tmp) < 35000) {
812 retval++;
813 log_to_screen(_("You must have at least 32MB of RAM to use Mondo."));
814 }
815 if (atol(tmp) < 66000) {
816 log_to_screen
817 (_("WARNING! You have very little RAM. Please upgrade to 64MB or more."));
818 }
819 mr_free(tmp);
820#endif
821
822 Lres = free_space_on_given_partition(MONDO_CACHE);
823 log_it("Free space on given partition = %ld MB", Lres);
824
825 if (Lres < 50) {
826 fatal_error("Your "MONDO_CACHE" partition has <50MB free. Please adjust your partition table to something saner or make "MONDO_CACHE" a symbolic link");
827 }
828
829 if (system("which " MKE2FS_OR_NEWFS " > /dev/null 2> /dev/null")) {
830 retval++;
831 log_to_screen
832 ("Unable to find " MKE2FS_OR_NEWFS " in system path.");
833 fatal_error
834 ("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. :)");
835 }
836#ifndef __FreeBSD__
837 if (run_program_and_log_output
838 ("grep ramdisk /proc/devices", FALSE)) {
839 if (!ask_me_yes_or_no
840 (_("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?")))
841 {
842 // retval++;
843 log_to_screen
844 (_("It looks as if your kernel lacks ramdisk and initrd support."));
845 log_to_screen
846 (_("I'll allow you to proceed but FYI, if I'm right, your kernel is broken."));
847 }
848 }
849#endif
850 retval += whine_if_not_found(mr_conf->mr_iso_creation_cmd);
851 retval += whine_if_not_found(mr_conf->mondo_iso_burning_cmd);
852 retval += whine_if_not_found(MKE2FS_OR_NEWFS);
853 retval += whine_if_not_found("bzip2");
854 retval += whine_if_not_found("gzip");
855 retval += whine_if_not_found("awk");
856 retval += whine_if_not_found("md5sum");
857 retval += whine_if_not_found("strings");
858 retval += whine_if_not_found("mindi");
859 retval += whine_if_not_found("buffer");
860
861 // abort if Windows partition but no ms-sys and parted
862 if (!run_program_and_log_output
863 ("mount | grep -w vfat | grep -vE \"/dev/fd|nexdisk\"", 0)
864 ||
865 !run_program_and_log_output
866 ("mount | grep -w dos | grep -vE \"/dev/fd|nexdisk\"", 0)) {
867 log_to_screen(_("I think you have a Windows 9x partition."));
868 retval += whine_if_not_found("parted");
869#ifndef __IA64__
870 /* IA64 always has one vfat partition for EFI even without Windows */
871 // retval +=
872 if (!find_home_of_exe("ms-sys")) {
873 log_to_screen("Please install ms-sys just in case.");
874 }
875#endif
876 }
877
878 if (!find_home_of_exe("cmp")) {
879 if (!find_home_of_exe("true")) {
880 whine_if_not_found("cmp");
881 } else {
882 log_to_screen
883 ("Your system lacks the 'cmp' binary. I'll create a dummy cmp for you.");
884 if (run_program_and_log_output
885 ("cp -f `which true` /usr/bin/cmp", 0)) {
886 fatal_error("Failed to create dummy 'cmp' file.");
887 }
888 }
889 }
890 run_program_and_log_output
891 ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
892 mr_asprintf(&tmp,
893 call_program_and_get_last_line_of_output
894 ("mount | grep -E \"cdr(om|w)\""));
895 if (strcmp("", tmp)) {
896 if (strstr(tmp, "autofs")) {
897 log_to_screen
898 (_("Your CD-ROM is mounted via autofs. I therefore cannot tell"));
899 log_to_screen
900 (_("if a CD actually is inserted. If a CD is inserted, please"));
901 log_to_screen(_("eject it. Thank you."));
902 log_it
903 ("Ignoring autofs CD-ROM 'mount' since we hope nothing's in it.");
904 } else
905 if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
906 retval++;
907 fatal_error
908 ("Your CD-ROM drive is mounted. Please unmount it.");
909 }
910 }
911 mr_free(tmp);
912#ifndef __FreeBSD__
913 if (!does_file_exist("/etc/modules.conf")) {
914 if (does_file_exist("/etc/conf.modules")) {
915 log_it("Linking /etc/modules.conf to /etc/conf.modules");
916 run_program_and_log_output
917 ("ln -sf /etc/conf.modules /etc/modules.conf", 5);
918 } else if (does_file_exist("/etc/modprobe.d")) {
919 log_it
920 ("Directory /etc/modprobe.d found. mindi will use its contents.");
921 } else if (does_file_exist("/etc/modprobe.conf")) {
922 log_it("Linking /etc/modules.conf to /etc/modprobe.conf");
923 run_program_and_log_output
924 ("ln -sf /etc/modprobe.conf /etc/modules.conf", 5);
925 } else {
926 retval++;
927 log_to_screen
928 (_("Please find out what happened to /etc/modules.conf"));
929 }
930 }
931#endif
932
933 run_program_and_log_output("cat /etc/fstab", 5);
934#ifdef __FreeBSD__
935 run_program_and_log_output("vinum printconfig", 5);
936#else
937 run_program_and_log_output("cat /etc/raidtab", 5);
938#endif
939
940 if (run_program_and_log_output("mindi -V", 1)) {
941 log_to_screen(_("Could not ascertain mindi's version number."));
942 log_to_screen
943 (_("You have not installed Mondo and/or Mindi properly."));
944 log_to_screen(_("Please uninstall and reinstall them both."));
945 fatal_error("Please reinstall Mondo and Mindi.");
946 }
947 if (run_program_and_log_output
948 ("mindi --makemountlist /tmp/mountlist.txt.test", 5)) {
949 log_to_screen
950 (_("Mindi --makemountlist /tmp/mountlist.txt.test failed for some reason."));
951 log_to_screen
952 (_("Please run that command by hand and examine /var/log/mindi.log"));
953 log_to_screen
954 (_("for more information. Perhaps your /etc/fstab file is insane."));
955 log_to_screen
956 (_("Perhaps Mindi's MakeMountlist() subroutine has a bug. We'll see."));
957 retval++;
958 }
959
960 if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
961 && !does_file_exist("/etc/raidtab")) {
962 log_to_screen
963 (_("You have RAID partitions but no /etc/raidtab - creating one from "MDSTAT_FILE));
964 create_raidtab_from_mdstat("/etc/raidtab");
965 }
966
967 if (retval) {
968 mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
969 } else {
970 mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
971 }
972 return (retval);
973}
974
975/**
976 * Retrieve the line containing @p label from the config file.
977 * @param config_file The file to read from, usually @c /tmp/mondo-restore.cfg.
978 * @param label What to read from the file.
979 * @param value Where to put it.
980 * @return 0 for success, 1 for failure.
981 */
982int read_cfg_var(char *config_file, char *label, char *value)
983{
984 /*@ buffer ****************************************************** */
985 char *command = NULL;
986 char *tmp = NULL;
987
988 /*@ end vars *************************************************** */
989
990 assert_string_is_neither_NULL_nor_zerolength(config_file);
991 assert_string_is_neither_NULL_nor_zerolength(label);
992
993 if (!does_file_exist(config_file)) {
994 mr_asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
995 config_file);
996 log_to_screen(tmp);
997 mr_free(tmp);
998 value[0] = '\0';
999 return (1);
1000 /* } BERLIOS: Useless:
1001 else if (strstr(value, "/dev/") && strstr(value, "t0")
1002 && !strcmp(label, "media-dev")) {
1003 mr_msg(2, "FYI, I shan't read new value for %s - already got %s",
1004 label, value);
1005 return (0);
1006 */ } else {
1007 mr_asprintf(&command, "grep '%s .*' %s| cut -d'=' -f2,3,4,5",
1008 label, config_file);
1009 strcpy(value, call_program_and_get_last_line_of_output(command));
1010 mr_free(command);
1011 if (strlen(value) == 0) {
1012 return (1);
1013 } else {
1014 return (0);
1015 }
1016 }
1017}
1018
1019
1020/**
1021 * Remount @c supermount if it was unmounted earlier.
1022 */
1023void remount_supermounts_if_necessary()
1024{
1025 if (g_remount_cdrom_at_end) {
1026 run_program_and_log_output("mount " MNT_CDROM, FALSE);
1027 }
1028 if (g_remount_floppy_at_end) {
1029 run_program_and_log_output("mount " MNT_FLOPPY, FALSE);
1030 }
1031}
1032
1033/**
1034 * Unmount @c supermount if it's mounted.
1035 */
1036void unmount_supermounts_if_necessary()
1037{
1038 if (run_program_and_log_output
1039 ("mount | grep cdrom | grep super", FALSE) == 0) {
1040 g_remount_cdrom_at_end = TRUE;
1041 run_program_and_log_output("umount " MNT_CDROM, FALSE);
1042 }
1043 if (run_program_and_log_output
1044 ("mount | grep floppy | grep super", FALSE) == 0) {
1045 g_remount_floppy_at_end = TRUE;
1046 run_program_and_log_output("umount " MNT_FLOPPY, FALSE);
1047 }
1048}
1049
1050/**
1051 * If this is a distribution like Gentoo that doesn't keep /boot mounted, mount it.
1052 */
1053void mount_boot_if_necessary()
1054{
1055 char *tmp = NULL;
1056 char *tmp1 = NULL;
1057 char *command = NULL;
1058
1059 mr_msg(1, "Started sub");
1060 mr_msg(4, "About to set g_boot_mountpt[0] to '\\0'");
1061 g_boot_mountpt[0] = '\0';
1062 mr_msg(4, "Done. Great. Setting command to something");
1063 mr_asprintf(&command,
1064 "grep -v ':' /etc/fstab | grep -vE '^#.*$' | grep -E \"[ ]/boot[ ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
1065 mr_msg(4, "Cool. Command = '%s'", command);
1066 mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
1067 mr_free(command);
1068
1069 mr_msg(4, "tmp = '%s'", tmp);
1070 if (tmp[0]) {
1071 log_it("/boot is at %s according to /etc/fstab", tmp);
1072 if ((strstr(tmp, "LABEL=") || strstr(tmp,"UUID="))) {
1073 if (!run_program_and_log_output("mount /boot", 5)) {
1074 strcpy(g_boot_mountpt, "/boot");
1075 mr_msg(1, "Mounted /boot");
1076 } else {
1077 log_it("...ignored cos it's a label or uuid :-)");
1078 }
1079 } else {
1080 mr_asprintf(&command, "mount | grep -E '^%s'", tmp);
1081 mr_msg(3, "command = %s", command);
1082 if (run_program_and_log_output(command, 5)) {
1083 strcpy(g_boot_mountpt, tmp);
1084 mr_asprintf(&tmp1,
1085 "%s (your /boot partition) is not mounted. I'll mount it before backing up",
1086 g_boot_mountpt);
1087 log_it(tmp1);
1088 mr_free(tmp1);
1089
1090 mr_asprintf(&tmp1, "mount %s", g_boot_mountpt);
1091 if (run_program_and_log_output(tmp1, 5)) {
1092 g_boot_mountpt[0] = '\0';
1093 mr_msg(1, "Plan B");
1094 if (!run_program_and_log_output("mount /boot", 5)) {
1095 strcpy(g_boot_mountpt, "/boot");
1096 mr_msg(1, "Plan B worked");
1097 } else {
1098 mr_msg(1,
1099 "Plan B failed. Unable to mount /boot for backup purposes. This probably means /boot is mounted already, or doesn't have its own partition.");
1100 }
1101 }
1102 mr_free(tmp1);
1103 }
1104 mr_free(command);
1105 }
1106 }
1107 mr_free(tmp);
1108 mr_msg(1, "Ended sub");
1109}
1110
1111
1112/**
1113 * If we mounted /boot earlier, unmount it.
1114 */
1115void unmount_boot_if_necessary()
1116{
1117 char *tmp;
1118
1119 mr_msg(3, "starting");
1120 if (g_boot_mountpt[0]) {
1121 mr_asprintf(&tmp, "umount %s", g_boot_mountpt);
1122 if (run_program_and_log_output(tmp, 5)) {
1123 log_it("WARNING - unable to unmount /boot");
1124 }
1125 mr_free(tmp);
1126 }
1127 mr_msg(3, "leaving");
1128}
1129
1130
1131/**
1132 * Write a line to a configuration file. Writes a line of the form,
1133 * @c label @c value.
1134 * @param config_file The file to write to. Usually @c mondo-restore.cfg.
1135 * @param label What to call this bit of data you're writing.
1136 * @param value The bit of data you're writing.
1137 * @return 0 for success, 1 for failure.
1138 */
1139int write_cfg_var(char *config_file, char *label, char *value)
1140{
1141 /*@ buffers ***************************************************** */
1142 char *command = NULL;
1143 char *tempfile = NULL;
1144 char *tmp = NULL;
1145
1146
1147 /*@ end vars *************************************************** */
1148 assert_string_is_neither_NULL_nor_zerolength(config_file);
1149 assert_string_is_neither_NULL_nor_zerolength(label);
1150 assert(value != NULL);
1151
1152 if (!does_file_exist(config_file)) {
1153 mr_asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
1154 config_file);
1155 log_to_screen(tmp);
1156 mr_free(tmp);
1157 return (1);
1158 }
1159 mr_asprintf(&tempfile,
1160 call_program_and_get_last_line_of_output
1161 ("mktemp -q /tmp/mojo-jojo.blah.XXXXXX"));
1162 if (does_file_exist(config_file)) {
1163 mr_asprintf(&command, "grep -vE '^%s .*$' %s > %s",
1164 label, config_file, tempfile);
1165 paranoid_system(command);
1166 mr_free(command);
1167 }
1168 mr_asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
1169 paranoid_system(command);
1170 mr_free(command);
1171
1172 mr_asprintf(&command, "mv -f %s %s", tempfile, config_file);
1173 paranoid_system(command);
1174 mr_free(command);
1175 unlink(tempfile);
1176 mr_free(tempfile);
1177 return (0);
1178}
1179
1180
1181/**
1182 * Allocate or free important globals, depending on @p mal.
1183 * @param mal If TRUE, malloc; if FALSE, free.
1184 */
1185void do_libmondo_global_strings_thing(int mal)
1186{
1187 if (mal) {
1188 iamhere("Malloc'ing globals");
1189 malloc_string(g_boot_mountpt);
1190 malloc_string(g_tmpfs_mountpt);
1191 malloc_string(g_erase_tmpdir_and_scratchdir);
1192 malloc_string(g_serial_string);
1193 malloc_string(g_magicdev_command);
1194 } else {
1195 iamhere("Freeing globals");
1196 mr_free(g_boot_mountpt);
1197 mr_free(g_tmpfs_mountpt);
1198 mr_free(g_erase_tmpdir_and_scratchdir);
1199 mr_free(g_serial_string);
1200 mr_free(g_magicdev_command);
1201 }
1202}
1203
1204/**
1205 * Allocate important globals.
1206 * @see do_libmondo_global_strings_thing
1207 */
1208void malloc_libmondo_global_strings(void)
1209{
1210 do_libmondo_global_strings_thing(1);
1211}
1212
1213/**
1214 * Free important globals.
1215 * @see do_libmondo_global_strings_thing
1216 */
1217void free_libmondo_global_strings(void)
1218{
1219 do_libmondo_global_strings_thing(0);
1220}
1221
1222
1223
1224/**
1225 * Stop @c magicdev if it's running.
1226 * The command used to start it is saved in @p g_magicdev_command.
1227 */
1228void stop_magicdev_if_necessary()
1229{
1230 strcpy(g_magicdev_command,
1231 call_program_and_get_last_line_of_output
1232 ("ps ax | grep -w magicdev | grep -v grep | tr -s '\t' ' '| cut -d' ' -f6-99"));
1233 if (g_magicdev_command[0]) {
1234 mr_msg(1, "g_magicdev_command = '%s'", g_magicdev_command);
1235 paranoid_system("killall magicdev");
1236 }
1237}
1238
1239
1240/**
1241 * Restart magicdev if it was stopped.
1242 */
1243void restart_magicdev_if_necessary()
1244{
1245 char *tmp = NULL;
1246
1247 if (g_magicdev_command && g_magicdev_command[0]) {
1248 mr_asprintf(&tmp, "%s &", g_magicdev_command);
1249 paranoid_system(tmp);
1250 mr_free(tmp);
1251 }
1252}
1253
1254/* @} - end of utilityGroup */
Note: See TracBrowser for help on using the repository browser.