source: MondoRescue/branches/3.3/mondo/src/common/libmondo-tools.c@ 3882

Last change on this file since 3882 was 3882, checked in by Bruno Cornec, 4 months ago

More cleanup

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