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

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