source: MondoRescue/trunk/mondo/src/common/libmondo-tools.c@ 1074

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

merge -r1042:1045 $SVN_M/branches/stable

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