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

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

log_msg => mr_msg in trunk

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